Class: MCollective::Util::TasksSupport::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/util/tasks_support/cli.rb,
lib/mcollective/util/tasks_support/json_formatter.rb,
lib/mcollective/util/tasks_support/default_formatter.rb

Defined Under Namespace

Classes: DefaultFormatter, JSONFormatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(support, format, verbose) ⇒ CLI

Returns a new instance of CLI.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mcollective/util/tasks_support/cli.rb', line 7

def initialize(support, format, verbose)
  @support = support

  if format == :json
    require_relative "json_formatter"
    @output = CLI::JSONFormatter.new(self, verbose)
  else
    require_relative "default_formatter"
    @output = CLI::DefaultFormatter.new(self, verbose)
  end
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/mcollective/util/tasks_support/cli.rb', line 5

def output
  @output
end

Instance Method Details

#create_task_options(task, environment, application) ⇒ Object

Adds CLI options for all defined input

Parameters:

  • meta (Hash)

    the task metadata

  • application (Application)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/mcollective/util/tasks_support/cli.rb', line 184

def create_task_options(task, environment, application)
  meta = (task, environment)

  return if meta["metadata"]["parameters"].nil? || meta["metadata"]["parameters"].empty?

  meta["metadata"]["parameters"].sort_by {|n, _| n}.each do |name, details|
    type, array, _ = @support.puppet_type_to_ruby(details["type"])

    properties = {
      :description => "%s (%s)" % [details["description"], details["type"]],
      :arguments => ["--%s %s" % [name.downcase, name.upcase]],
      :type => type
    }

    properties[:type] = :array if array

    if type == Hash
      properties[:arguments] = ["--%s JSON" % name.downcase]
      properties[:type] = String
    end

    properties[:arguments] = ["--[no-]%s" % name.downcase] if type == :boolean

    application.class.option(name.intern, properties)
  end
end

Prints an individual result

Parameters:



123
124
125
# File 'lib/mcollective/util/tasks_support/cli.rb', line 123

def print_result(*args)
  @output.print_result(*args)
end

Prints metadata for an individual result

Parameters:

  • status (RPC::Reply)

    the individual task status to print



133
134
135
# File 'lib/mcollective/util/tasks_support/cli.rb', line 133

def (*args)
  @output.(*args)
end

Prints the RPC stats for a request

Parameters:



128
129
130
# File 'lib/mcollective/util/tasks_support/cli.rb', line 128

def print_rpc_stats(*args)
  @output.print_rpc_stats(*args)
end

Prints the summary of a task over a number of machines

Parameters:

  • taskid (String)

    the task id

  • names (Array<String>)

    list of unique task names seen in the estate

  • callers (Array<String>)

    list of unique callers seen in the estate

  • completed (Integer)

    nodes that completed the task - success or fails

  • running (Integer)

    nodes still running the task

  • task_not_known (Integer)

    nodes where the task is unknown

  • wrapper_failure (Integer)

    nodes where the wrapper executable failed to launch

  • success (Integer)

    nodes that completed the task succesfully

  • fails (Integer)

    nodes that did not complete the task succesfully

  • runtime (Float)

    total run time across all nodes

  • rpcstats (RPC::Stats)

    the stats from the RPC client that fetched the statusses



118
119
120
# File 'lib/mcollective/util/tasks_support/cli.rb', line 118

def print_task_summary(*args)
  @output.print_task_summary(*args)
end

#show_task_help(task, environment, out = $stdout) ⇒ Object

Creates help for a task

Parameters:

  • task (String)

    task name

  • environment (String)

    environment to feetch task from

  • out (IO) (defaults to: $stdout)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mcollective/util/tasks_support/cli.rb', line 82

def show_task_help(task, environment, out=$stdout)
  out.puts("Retrieving task metadata for task %s from the Puppet Server" % task)

  begin
    meta = (task, environment)
  rescue
    abort($!.to_s)
  end

  out.puts

  out.puts("%s - %s" % [Util.colorize(:bold, task), meta["metadata"]["description"]])
  out.puts

  if meta["metadata"]["parameters"].nil? || meta["metadata"]["parameters"].empty?
    out.puts("The task takes no parameters or have none defined")
    out.puts
  else
    out.puts("Task Parameters:")

    meta["metadata"]["parameters"].sort_by {|n, _| n}.each do |name, details|
      out.puts("  %-30s %s (%s)" % [name, details["description"], details["type"]])
    end
  end

  out.puts
  out.puts "Task Files:"
  meta["files"].each do |file|
    out.puts("  %-30s %s bytes" % [file["filename"], file["size_bytes"]])
  end

  out.puts
  out.puts("Use 'mco tasks run %s' to run this task" % [task])
end

#show_task_list(environment, detail, out = $stdout) ⇒ Object

Shows the task list

Parameters:

  • environment (String)

    the environment to query

  • detail (Boolean)

    show task descriptions

  • out (IO) (defaults to: $stdout)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mcollective/util/tasks_support/cli.rb', line 24

def show_task_list(environment, detail, out=$stdout)
  out.puts "Known tasks in the %s environment" % environment
  out.puts

  known_tasks = task_list(environment, detail, out)

  out.print("\r")

  padding = known_tasks.keys.map(&:size).max + 2

  if detail
    known_tasks.each do |name, description|
      out.puts "  %-#{padding}s %s" % [name, description]
    end

    out.puts
    out.puts("Use mco task <TASK> to see task help")
  else
    known_tasks.keys.in_groups_of(3) do |tasks|
      out.puts "  %s" % tasks.compact.map {|t| "%-#{padding}s" % t }.join(" ")
    end

    out.puts
    out.puts("Use mco task <TASK> to see task help")
    out.puts("Pass option --detail to see task descriptions")
  end
end

#task_input(configuration) ⇒ Hash

Parses the given CLI input string and creates results based on it

Parameters:

  • configuration (Hash)

    the mcollective Application configuration

Returns:

  • (Hash)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mcollective/util/tasks_support/cli.rb', line 141

def task_input(configuration)
  result = {}

  input = configuration[:__json_input]

  if input
    if input.start_with?("@")
      input.sub!("@", "")
      result = JSON.parse(File.read(input)) if input.end_with?("json")
      result = YAML.safe_load(File.read(input)) if input.end_with?("yaml")
    else
      result = JSON.parse(input)
    end
  end

  configuration.each do |item, value|
    next if item.to_s.start_with?("__")

    result[item.to_s] = value
  end

  result
end

#task_list(environment, detail, out = $stdout) ⇒ Object

Retrieves the task list

Parameters:

  • environment (String)

    the environment to query

  • detail (Boolean)

    show task descriptions



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mcollective/util/tasks_support/cli.rb', line 56

def task_list(environment, detail, out=$stdout)
  tasks = {}

  out.print("Retrieving tasks....")

  known_tasks = @support.tasks(environment)

  known_tasks.each_with_index do |task, idx|
    description = nil
    if detail
      out.print(twirl("Retrieving tasks....", known_tasks.size, idx))
      meta = (task["name"], environment)
      description = meta["metadata"]["description"]
    end

    tasks[task["name"]] = description || ""
  end

  tasks
end

#task_metadata(task, environment) ⇒ Object

Retrieves task metadata from Puppet Server and cache it

Parameters:

  • task (String)

    the task to fetch

  • environment (String)

    Puppet environment



235
236
237
238
239
240
241
# File 'lib/mcollective/util/tasks_support/cli.rb', line 235

def (task, environment)
  @_metadata ||= {}

  return @_metadata[task] if @_metadata[task]

  @_metadata[task] = @support.(task, environment)
end

#transform_hash_strings(meta, input) ⇒ Object

Transform hash inputs given as json strings to hashes

Parameters:

  • meta (Hash)

    task metadata

  • input (Hash)

    proposed input to pass to the task



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/mcollective/util/tasks_support/cli.rb', line 215

def transform_hash_strings(meta, input)
  return unless meta["metadata"]["parameters"]

  meta["metadata"]["parameters"].each do |name, details|
    type, _, _ = @support.puppet_type_to_ruby(details["type"])

    next unless type == Hash && input[name].is_a?(String)

    begin
      input[name] = JSON.parse(input[name])
    rescue
      raise("Could not parse input '%s' as JSON data: %s: %s" % [name, $!.class, $!.message])
    end
  end
end

#twirl(msg, max, current) ⇒ Object

Draw a compact CLI progress indicator

Parameters:

  • msg (String)

    the message to prefix

  • max (Integer)

    the biggest number to expect

  • current (Integer)

    the current number



248
249
250
251
252
253
254
255
256
# File 'lib/mcollective/util/tasks_support/cli.rb', line 248

def twirl(msg, max, current)
  charset = ["", "", "", ""]
  index = current % charset.size
  char = charset[index]
  char = Util.colorize(:green, "") if max == current

  format = "\r%s %s  %#{@max.to_s.size}d / %d"
  format % [msg, char, current, max]
end

#validate_task_input(task, meta, input) ⇒ Boolean

Validates the inputs provided on the CLI would be acceptable to the task

Parameters:

  • task (String)

    task name

  • meta (Hash)

    task metadata

  • input (Hash)

    proposed input to pass to the task

Returns:

  • (Boolean)


172
173
174
175
176
177
178
# File 'lib/mcollective/util/tasks_support/cli.rb', line 172

def validate_task_input(task, meta, input)
  ok, reason = @support.validate_task_inputs(input, meta)

  return true if ok

  abort(reason)
end