Class: MCollective::Application::Tasks
Instance Attribute Summary
#options
Instance Method Summary
collapse
[], []=, #application_cli_arguments, #application_description, #application_failure, application_options, #application_options, #application_parse_options, #application_usage, #clioptions, #configuration, description, #disconnect, exclude_argument_sections, external, #external_help, external_help, #external_main, #halt, #halt_code, #help, intialize_application_options, option, #rpcclient, usage, #validate_cli_options, #validate_option
Methods included from RPC
const_missing, discovered, #empty_filter?, #printrpc, #printrpcstats, #rpcclient, #rpcoptions, stats
Instance Method Details
#bolt_tasks ⇒ Object
375
376
377
|
# File 'lib/mcollective/application/tasks.rb', line 375
def bolt_tasks
@_bolt_tasks ||= rpcclient("bolt_tasks")
end
|
#choria ⇒ Object
396
397
398
399
400
|
# File 'lib/mcollective/application/tasks.rb', line 396
def choria
Util.loadclass("MCollective::Util::Choria")
@_choria ||= Util::Choria.new
end
|
#cli ⇒ Object
406
407
408
409
410
411
412
413
414
|
# File 'lib/mcollective/application/tasks.rb', line 406
def cli
format = configuration[:__json_format] ? :json : :default
if options
@_cli ||= tasks_support.cli(format, options[:verbose])
else
tasks_support.cli(format, false)
end
end
|
#download_files(task, files) ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
# File 'lib/mcollective/application/tasks.rb', line 222
def download_files(task, files)
bolt_tasks.batch_size = 50
bolt_tasks.batch_sleep_time = 1
failed = false
downloads = []
cnt = bolt_tasks.discover.size
idx = 0
bolt_tasks.download(:environment => "production", :task => task, :files => files.to_json) do |_, s|
unless configuration[:__json_format]
print(cli.twirl("Downloading and verifying %d file(s) from the Puppet Server to all nodes:" % [files.size], cnt, idx + 1))
puts if cnt == idx + 1
end
idx += 1
downloads << s
end
downloads.select {|d| d[:statuscode] > 0}.each_with_index do |download, i|
failed = true
puts if i == 0
puts(" %s: %s" % [Util.colorize(:red, "Could not download files onto %s" % download[:sender]), download[:statusmsg]])
end
unless bolt_tasks.stats.noresponsefrom.empty?
puts
puts bolt_tasks.stats.no_response_report
failed = true
end
if failed
puts
abort("Could not download the task %s onto all nodes" % task)
end
end
|
384
385
386
387
388
389
390
|
# File 'lib/mcollective/application/tasks.rb', line 384
def
idx = ARGV.index("--environment")
return "production" unless idx
ARGV[idx + 1]
end
|
#list_command ⇒ Object
358
359
360
|
# File 'lib/mcollective/application/tasks.rb', line 358
def list_command
cli.show_task_list("production", configuration[:__detail])
end
|
#list_options ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/mcollective/application/tasks.rb', line 31
def list_options
self.class.option :__detail,
:arguments => ["--detail"],
:description => "Show command descriptions",
:default => false,
:type => :boolean
end
|
#main ⇒ Object
416
417
418
419
420
421
422
|
# File 'lib/mcollective/application/tasks.rb', line 416
def main
if valid_commands.include?(configuration[:__command])
send("%s_command" % configuration[:__command])
else
show_task_help(configuration[:__command])
end
end
|
#post_option_parser(configuration) ⇒ Object
27
28
29
|
# File 'lib/mcollective/application/tasks.rb', line 27
def post_option_parser(configuration)
configuration[:__command] = ARGV.shift || "list"
end
|
#request_and_report(action, arguments, taskid = nil) ⇒ Object
rubocop:disable Metrics/MethodLength
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
# File 'lib/mcollective/application/tasks.rb', line 280
def request_and_report(action, arguments, taskid=nil) task_not_known_nodes = 0
wrapper_failure = 0
completed_nodes = 0
running_nodes = 0
runtime = 0.0
success_nodes = 0
fail_nodes = 0
progress = configuration[:__summary] || configuration[:__batch_size] ? RPC::Progress.new : nil
cnt = 0
expected = bolt_tasks.discover.size
task_names = []
callers = []
if configuration[:__batch_size]
bolt_tasks.batch_size = configuration[:__batch_size]
bolt_tasks.batch_sleep_time = configuration[:__batch_sleep]
end
say
bolt_tasks.send(action, arguments) do |_, reply|
status = reply[:data]
if reply[:statuscode] == 3
fail_nodes += 1
task_not_known_nodes += 1
elsif [-1, 0].include?(status[:exitcode])
status[:completed] ? completed_nodes += 1 : running_nodes += 1
runtime += status[:runtime]
reply[:statuscode] == 0 ? success_nodes += 1 : fail_nodes += 1
elsif reply[:statuscode] == 5
wrapper_failure += 1
fail_nodes += 1
else
fail_nodes += 1
end
task_names << status[:task] if status[:task]
callers << status[:callerid] if status[:callerid]
if progress
print(progress.twirl(cnt + 1, expected))
say if cnt + 1 == expected
else
cli.print_result(reply)
end
cnt += 1
end
taskid ||= bolt_tasks.stats.requestid
callers.compact!
callers.uniq!
task_names.compact!
task_names.uniq!
say
cli.print_task_summary(
taskid,
task_names,
callers,
completed_nodes,
running_nodes,
task_not_known_nodes,
wrapper_failure,
success_nodes,
fail_nodes,
runtime,
bolt_tasks.stats
)
ensure
reset_client!
end
|
#reset_client! ⇒ Object
379
380
381
382
|
# File 'lib/mcollective/application/tasks.rb', line 379
def reset_client!
bolt_tasks.batch_size = 0
bolt_tasks.progress = options[:verbose]
end
|
#run ⇒ Object
362
363
364
365
366
367
368
369
|
# File 'lib/mcollective/application/tasks.rb', line 362
def run
command = ARGV[0]
command = "list" unless valid_commands.include?(command)
send("%s_options" % command)
super
end
|
#run_command ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/mcollective/application/tasks.rb', line 164
def run_command
task = ARGV.shift
input = cli.task_input(configuration)
say("Retrieving task metadata for task %s from the Puppet Server" % task)
begin
meta = cli.task_metadata(task, "production")
rescue
abort($!.to_s)
end
cli.transform_hash_strings(meta, input)
cli.validate_task_input(task, meta, input)
say("Attempting to download and run task %s on %d nodes" % [Util.colorize(:bold, task), bolt_tasks.discover.size])
say
download_files(task, meta["files"])
request = {
:task => task,
:files => meta["files"].to_json
}
request[:run_as] = configuration[:__run_as] if configuration[:__run_as]
request[:input] = input.to_json if input
if configuration[:__background]
puts("Starting task %s in the background" % [Util.colorize(:bold, task)])
if configuration[:__batch_size]
bolt_tasks.batch_size = configuration[:__batch_size]
bolt_tasks.batch_sleep_time = configuration[:__batch_sleep]
bolt_tasks.progress = true
end
printrpc bolt_tasks.run_no_wait(request)
printrpcstats
if bolt_tasks.stats.okcount > 0
puts
puts("Request detailed status for the task using 'mco tasks status %s'" % [Util.colorize(:bold, bolt_tasks.stats.requestid)])
end
else
say("Running task %s and waiting up to %s seconds for it to complete" % [
Util.colorize(:bold, task),
Util.colorize(:bold, bolt_tasks.ddl.meta[:timeout])
])
request_and_report(:run_and_wait, request)
end
ensure
reset_client!
end
|
#run_options ⇒ Object
rubocop:disable Metrics/MethodLength
71
72
73
74
75
76
77
78
79
80
81
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/mcollective/application/tasks.rb', line 71
def run_options application_options[:usage].clear
self.class.usage <<-USAGE
mco tasks run <TASK NAME> [OPTIONS]
Runs a task in the background and wait up to 50 seconds for it to complete.
Task inputs are handled using --argument=value for basic String, Numeric and Boolean
types, others can be passed using --input
Input can also be read from a file using "--input @file.json" or "--input @file.yaml".
For complex data types like Hashes, Arrays or Variants you have to supply input
as YAML or JSON.
Once a task is run the task ID will be displayed which can later be used with
the "mco tasks status" command to extract results.
Examples:
Run myapp::upgrade task in the background and wait for it to complete:
mco tasks run myapp::upgrade --version 1.0.0
Run myapp::upgrade task in the background and return immediately:
mco tasks run myapp::upgrade --version 1.0.0 --background
Supply complex data input to the task:
Should input be given on both the CLI arguments and a file
the CLI arguments will override the file
mco tasks run myapp::upgrade --input @input.json
mco tasks run myapp::upgrade --input @input.yaml
mco tasks run myapp::upgrade --version 1.0.0 --input \\
'{"source": {
"url": "http://repo/archive-1.0.0.tgz",
"hash": "68b329da9893e34099c7d8ad5cb9c940"}}'
USAGE
task = ARGV[1]
abort("Please specify a task to run") unless task
cli.create_task_options(task, "production", self)
self.class.option :__summary,
:arguments => ["--summary"],
:description => "Only show a overall summary of the task",
:default => false,
:type => :boolean
self.class.option :__background,
:arguments => ["--background"],
:description => "Do not wait for the task to complete",
:default => false,
:type => :boolean
self.class.option :__json_input,
:arguments => ["--input INPUT"],
:description => "JSON input to pass to the task",
:required => false,
:type => String
self.class.option :__batch_size,
:arguments => ["--batch SIZE"],
:description => "Run tasks on nodes in batches",
:required => false,
:type => String
self.class.option :__batch_sleep,
:arguments => ["--batch-sleep SECONDS"],
:description => "Time to sleep between invocations of batches of nodes",
:required => false,
:default => 1,
:type => Integer
self.class.option :__run_as,
:arguments => ["--run-as USERNAME"],
:description => "Run task as user USERNAME",
:required => false,
:default => nil,
:type => String
end
|
#say(msg = "") ⇒ Object
160
161
162
|
# File 'lib/mcollective/application/tasks.rb', line 160
def say(msg="")
puts(msg) unless configuration[:__json_format]
end
|
#show_task_help(task) ⇒ Object
371
372
373
|
# File 'lib/mcollective/application/tasks.rb', line 371
def show_task_help(task)
cli.show_task_help(task, "production")
end
|
#status_command ⇒ Object
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/mcollective/application/tasks.rb', line 260
def status_command
taskid = ARGV.shift
abort("Please specify a task id to display") unless taskid
if configuration[:__metadata]
say("Requesting task metadata for request %s" % Util.colorize(:bold, taskid)) unless options[:verbose]
bolt_tasks.task_status(:task_id => taskid).each do |status|
cli.print_result_metadata(status)
end
cli.print_rpc_stats(bolt_tasks.stats)
else
say("Requesting task status for request %s, showing failures only pass --verbose for all output" % Util.colorize(:bold, taskid)) unless options[:verbose]
request_and_report(:task_status, {:task_id => taskid}, taskid)
end
end
|
#status_options ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/mcollective/application/tasks.rb', line 39
def status_options
application_options[:usage].clear
self.class.usage <<-USAGE
mco tasks status <REQUEST> [FLAGS]
Retrieves the status for a task you previously requested. It can be running or completed.
By default only failed exuecutions are shown, use --verbose to see them all.
USAGE
self.class.option :__summary,
:arguments => ["--summary"],
:description => "Only show a overall summary of the task",
:default => false,
:type => :boolean
self.class.option :__metadata,
:arguments => ["--metadata"],
:description => "Only show task metadata for each node",
:default => false,
:type => :boolean
self.class.option :__json_format,
:arguments => ["--json"],
:description => "Display results in JSON format",
:default => false,
:type => :boolean
end
|
#tasks_support ⇒ Object
402
403
404
|
# File 'lib/mcollective/application/tasks.rb', line 402
def tasks_support
@_tasks_support ||= choria.tasks_support
end
|
#valid_commands ⇒ Object
392
393
394
|
# File 'lib/mcollective/application/tasks.rb', line 392
def valid_commands
methods.grep(/_command$/).map {|c| c.to_s.gsub("_command", "")}
end
|