Class: Bolt::Outputter::Human
Constant Summary
collapse
- COLORS =
{
red: "31",
green: "32",
yellow: "33",
cyan: "36"
}.freeze
Instance Method Summary
collapse
-
#colorize(color, string) ⇒ Object
-
#duration_to_string(duration) ⇒ Object
-
#enabled? ⇒ Boolean
-
#fatal_error(err) ⇒ Object
-
#format_log(log) ⇒ Object
-
#handle_event(event) ⇒ Object
-
#initialize(color, verbose, trace, stream = $stdout) ⇒ Human
constructor
-
#plan_logging? ⇒ Boolean
-
#print_action_error(error) ⇒ Object
-
#print_action_step(step) ⇒ Object
-
#print_apply_result(apply_result, elapsed_time) ⇒ Object
-
#print_error(message) ⇒ Object
-
#print_groups(groups) ⇒ Object
-
#print_guide(guide, _topic) ⇒ Object
-
#print_head ⇒ Object
-
#print_message(message) ⇒ Object
-
#print_module_list(module_list) ⇒ Object
-
#print_plan_finish(event) ⇒ Object
-
#print_plan_info(plan) ⇒ Object
-
#print_plan_result(plan_result) ⇒ Object
-
#print_plan_start(event) ⇒ Object
-
#print_plans(plans, modulepath) ⇒ Object
-
#print_prompt(prompt) ⇒ Object
-
#print_prompt_error(message) ⇒ Object
-
#print_puppetfile_result(success, puppetfile, moduledir) ⇒ Object
-
#print_result(result) ⇒ Object
-
#print_result_set(result_set) ⇒ Object
-
#print_start(target) ⇒ Object
-
#print_step_finish(description:, result:, duration:, **_kwargs) ⇒ Object
-
#print_step_start(description:, targets:, **_kwargs) ⇒ Object
-
#print_summary(results, elapsed_time = nil) ⇒ Object
-
#print_table(results, padding_left = 0, padding_right = 3) ⇒ Object
-
#print_target_info(targets) ⇒ Object
-
#print_targets(target_list, inventoryfile) ⇒ Object
-
#print_task_info(task) ⇒ Object
-
#print_tasks(tasks, modulepath) ⇒ Object
-
#print_topics(topics) ⇒ Object
-
#remove_trail(string) ⇒ Object
-
#wrap(string, width = 80) ⇒ Object
for_format, #format_apply_result, #format_message, #indent, #print_message_event, #stringify
Constructor Details
#initialize(color, verbose, trace, stream = $stdout) ⇒ Human
Returns a new instance of Human.
17
18
19
20
21
22
23
|
# File 'lib/bolt/outputter/human.rb', line 17
def initialize(color, verbose, trace, stream = $stdout)
super
@plan_depth = 0
@disable_depth = 0
end
|
Instance Method Details
#colorize(color, string) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/bolt/outputter/human.rb', line 25
def colorize(color, string)
if @color && @stream.isatty
"\033[#{COLORS[color]}m#{string}\033[0m"
else
string
end
end
|
#duration_to_string(duration) ⇒ Object
457
458
459
460
461
462
463
464
465
466
467
468
469
|
# File 'lib/bolt/outputter/human.rb', line 457
def duration_to_string(duration)
hrs = (duration / 3600).floor
mins = ((duration % 3600) / 60).floor
secs = (duration % 60)
if hrs > 0
"#{hrs} hr, #{mins} min, #{secs.round} sec"
elsif mins > 0
"#{mins} min, #{secs.round} sec"
else
"#{secs.round(2)} sec"
end
end
|
#enabled? ⇒ Boolean
69
70
71
|
# File 'lib/bolt/outputter/human.rb', line 69
def enabled?
@disable_depth == 0
end
|
#fatal_error(err) ⇒ Object
405
406
407
408
409
410
411
412
413
414
415
416
|
# File 'lib/bolt/outputter/human.rb', line 405
def fatal_error(err)
@stream.puts(colorize(:red, err.message))
if err.is_a? Bolt::RunFailure
@stream.puts ::JSON.pretty_generate(err.result_set)
end
if @trace && err.backtrace
err.backtrace.each do |line|
@stream.puts(colorize(:red, "\t#{line}"))
end
end
end
|
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/bolt/outputter/human.rb', line 127
def format_log(log)
color = case log['level']
when 'warn'
:yellow
when 'err'
:red
end
source = "#{log['source']}: " if log['source']
message = "#{log['level'].capitalize}: #{source}#{log['message']}"
message = colorize(color, message) if color
message
end
|
#handle_event(event) ⇒ Object
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
|
# File 'lib/bolt/outputter/human.rb', line 41
def handle_event(event)
case event[:type]
when :enable_default_output
@disable_depth -= 1
when :disable_default_output
@disable_depth += 1
when :message
print_message_event(event)
end
if enabled?
case event[:type]
when :node_start
print_start(event[:target]) if @verbose
when :node_result
print_result(event[:result]) if @verbose
when :step_start
print_step_start(**event) if plan_logging?
when :step_finish
print_step_finish(**event) if plan_logging?
when :plan_start
print_plan_start(event)
when :plan_finish
print_plan_finish(event)
end
end
end
|
#plan_logging? ⇒ Boolean
73
74
75
|
# File 'lib/bolt/outputter/human.rb', line 73
def plan_logging?
@plan_depth > 0
end
|
#print_action_error(error) ⇒ Object
444
445
446
447
448
449
450
451
452
453
454
455
|
# File 'lib/bolt/outputter/human.rb', line 444
def print_action_error(error)
first, *remaining = error.lines
first = colorize(:red, indent(2, "→ #{wrap(first, 76)}"))
wrapped = remaining.map { |l| wrap(l) }
to_print = wrapped.map { |line| colorize(:red, indent(4, line)) }
step = [first, *to_print, "\n"].join
@stream.puts(step)
end
|
#print_action_step(step) ⇒ Object
434
435
436
437
438
439
440
441
442
|
# File 'lib/bolt/outputter/human.rb', line 434
def print_action_step(step)
first, *remaining = wrap(step, 76).lines
first = indent(2, "→ #{first}")
remaining = remaining.map { |line| indent(4, line) }
step = [first, *remaining, "\n"].join
@stream.puts(step)
end
|
#print_apply_result(apply_result, elapsed_time) ⇒ Object
373
374
375
|
# File 'lib/bolt/outputter/human.rb', line 373
def print_apply_result(apply_result, elapsed_time)
print_summary(apply_result, elapsed_time)
end
|
#print_error(message) ⇒ Object
422
423
424
|
# File 'lib/bolt/outputter/human.rb', line 422
def print_error(message)
@stream.puts(colorize(:red, message))
end
|
#print_groups(groups) ⇒ Object
366
367
368
369
370
|
# File 'lib/bolt/outputter/human.rb', line 366
def print_groups(groups)
count = "#{groups.count} group#{'s' unless groups.count == 1}"
@stream.puts groups.join("\n")
@stream.puts colorize(:green, count)
end
|
#print_guide(guide, _topic) ⇒ Object
302
303
304
|
# File 'lib/bolt/outputter/human.rb', line 302
def print_guide(guide, _topic)
@stream.puts(guide)
end
|
#print_head ⇒ Object
15
|
# File 'lib/bolt/outputter/human.rb', line 15
def print_head; end
|
#print_message(message) ⇒ Object
418
419
420
|
# File 'lib/bolt/outputter/human.rb', line 418
def print_message(message)
@stream.puts(message)
end
|
#print_module_list(module_list) ⇒ Object
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
|
# File 'lib/bolt/outputter/human.rb', line 306
def print_module_list(module_list)
module_list.each do |path, modules|
if (mod = modules.find { |m| m[:internal_module_group] })
@stream.puts(colorize(:cyan, mod[:internal_module_group]))
else
@stream.puts(colorize(:cyan, path))
end
if modules.empty?
@stream.puts('(no modules installed)')
else
module_info = modules.map do |m|
version = if m[:version].nil?
m[:internal_module_group].nil? ? '(no metadata)' : '(built-in)'
else
m[:version]
end
[m[:name], version]
end
print_table(module_info, 2, 1)
end
@stream.write("\n")
end
end
|
#print_plan_finish(event) ⇒ Object
166
167
168
169
170
171
|
# File 'lib/bolt/outputter/human.rb', line 166
def print_plan_finish(event)
@plan_depth -= 1
plan = event[:plan]
duration = event[:duration]
@stream.puts(colorize(:green, "Finished: plan #{plan} in #{duration_to_string(duration)}"))
end
|
#print_plan_info(plan) ⇒ Object
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
# File 'lib/bolt/outputter/human.rb', line 260
def print_plan_info(plan)
pretty_params = +""
plan_info = +""
usage = +"bolt plan run #{plan['name']}"
plan['parameters'].each do |name, p|
pretty_params << "- #{name}: #{p['type']}\n"
pretty_params << " Default: #{p['default_value']}\n" unless p['default_value'].nil?
pretty_params << " #{p['description']}\n" if p['description']
usage << (p.include?('default_value') ? " [#{name}=<value>]" : " #{name}=<value>")
end
plan_info << "\n#{plan['name']}"
plan_info << " - #{plan['description']}" if plan['description']
plan_info << "\n\n"
plan_info << "USAGE:\n#{usage}\n\n"
plan_info << "PARAMETERS:\n#{pretty_params}\n" unless plan['parameters'].empty?
plan_info << "MODULE:\n"
path = plan['module']
plan_info << if path.start_with?(Bolt::Config::Modulepath::MODULES_PATH)
"built-in module"
else
path
end
@stream.puts(plan_info)
end
|
#print_plan_result(plan_result) ⇒ Object
378
379
380
381
382
383
384
385
386
387
388
389
390
|
# File 'lib/bolt/outputter/human.rb', line 378
def print_plan_result(plan_result)
value = plan_result.value
case value
when nil
@stream.puts("Plan completed successfully with no result")
when Bolt::ApplyFailure, Bolt::RunFailure
print_result_set(value.result_set)
when Bolt::ResultSet
print_result_set(value)
else
@stream.puts(::JSON.pretty_generate(plan_result, quirks_mode: true))
end
end
|
#print_plan_start(event) ⇒ Object
156
157
158
159
160
161
162
163
164
|
# File 'lib/bolt/outputter/human.rb', line 156
def print_plan_start(event)
@plan_depth += 1
if event[:plan]
@stream.puts(colorize(:green, "Starting: plan #{event[:plan]}"))
end
end
|
#print_plans(plans, modulepath) ⇒ Object
289
290
291
292
293
294
|
# File 'lib/bolt/outputter/human.rb', line 289
def print_plans(plans, modulepath)
print_table(plans)
print_message("\nMODULEPATH:\n#{modulepath.join(File::PATH_SEPARATOR)}\n"\
"\nUse `bolt plan show <plan-name>` to view "\
"details and parameters for a specific plan.")
end
|
#print_prompt(prompt) ⇒ Object
426
427
428
|
# File 'lib/bolt/outputter/human.rb', line 426
def print_prompt(prompt)
@stream.print(colorize(:cyan, indent(4, prompt)))
end
|
#print_prompt_error(message) ⇒ Object
430
431
432
|
# File 'lib/bolt/outputter/human.rb', line 430
def print_prompt_error(message)
@stream.puts(colorize(:red, indent(4, message)))
end
|
#print_puppetfile_result(success, puppetfile, moduledir) ⇒ Object
397
398
399
400
401
402
403
|
# File 'lib/bolt/outputter/human.rb', line 397
def print_puppetfile_result(success, puppetfile, moduledir)
if success
@stream.puts("Successfully synced modules from #{puppetfile} to #{moduledir}")
else
@stream.puts(colorize(:red, "Failed to sync modules from #{puppetfile} to #{moduledir}"))
end
end
|
#print_result(result) ⇒ Object
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
|
# File 'lib/bolt/outputter/human.rb', line 81
def print_result(result)
if result.success?
@stream.puts(colorize(:green, "Finished on #{result.target.safe_name}:"))
else
@stream.puts(colorize(:red, "Failed on #{result.target.safe_name}:"))
end
if result.error_hash
@stream.puts(colorize(:red, remove_trail(indent(2, result.error_hash['msg']))))
end
if result.is_a?(Bolt::ApplyResult) && @verbose
result.resource_logs.each do |log|
next if %w[info debug].include?(log['level'])
message = format_log(log)
@stream.puts(indent(2, message))
end
end
if result.value.empty? || (result.value.keys == ['_output'] && !result.message?)
@stream.puts(indent(2, "#{result.action.capitalize} completed successfully with no result"))
else
if result.message?
@stream.puts(remove_trail(indent(2, result.message)))
end
if result.generic_value.keys == %w[stdout stderr exit_code]
safe_value = result.safe_value
unless safe_value['stdout'].strip.empty?
@stream.puts(indent(2, "STDOUT:"))
@stream.puts(indent(4, safe_value['stdout']))
end
unless safe_value['stderr'].strip.empty?
@stream.puts(indent(2, "STDERR:"))
@stream.puts(indent(4, safe_value['stderr']))
end
elsif result.generic_value.any?
@stream.puts(indent(2, ::JSON.pretty_generate(result.generic_value)))
end
end
end
|
#print_result_set(result_set) ⇒ Object
392
393
394
395
|
# File 'lib/bolt/outputter/human.rb', line 392
def print_result_set(result_set)
result_set.each { |result| print_result(result) }
print_summary(result_set)
end
|
#print_start(target) ⇒ Object
77
78
79
|
# File 'lib/bolt/outputter/human.rb', line 77
def print_start(target)
@stream.puts(colorize(:green, "Started on #{target.safe_name}..."))
end
|
#print_step_finish(description:, result:, duration:, **_kwargs) ⇒ Object
149
150
151
152
153
154
|
# File 'lib/bolt/outputter/human.rb', line 149
def print_step_finish(description:, result:, duration:, **_kwargs)
failures = result.error_set.length
plural = failures == 1 ? '' : 's'
message = "Finished: #{description} with #{failures} failure#{plural} in #{duration.round(2)} sec"
@stream.puts(colorize(:green, message))
end
|
#print_step_start(description:, targets:, **_kwargs) ⇒ Object
140
141
142
143
144
145
146
147
|
# File 'lib/bolt/outputter/human.rb', line 140
def print_step_start(description:, targets:, **_kwargs)
target_str = if targets.length > 5
"#{targets.count} targets"
else
targets.map(&:safe_name).join(', ')
end
@stream.puts(colorize(:green, "Starting: #{description} on #{target_str}"))
end
|
#print_summary(results, elapsed_time = nil) ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/bolt/outputter/human.rb', line 173
def print_summary(results, elapsed_time = nil)
ok_set = results.ok_set
unless ok_set.empty?
@stream.puts format('Successful on %<size>d target%<plural>s: %<names>s',
size: ok_set.size,
plural: ok_set.size == 1 ? '' : 's',
names: ok_set.targets.map(&:safe_name).join(','))
end
error_set = results.error_set
unless error_set.empty?
@stream.puts colorize(:red,
format('Failed on %<size>d target%<plural>s: %<names>s',
size: error_set.size,
plural: error_set.size == 1 ? '' : 's',
names: error_set.targets.map(&:safe_name).join(',')))
end
total_msg = format('Ran on %<size>d target%<plural>s',
size: results.size,
plural: results.size == 1 ? '' : 's')
total_msg << " in #{duration_to_string(elapsed_time)}" unless elapsed_time.nil?
@stream.puts total_msg
end
|
#print_table(results, padding_left = 0, padding_right = 3) ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/bolt/outputter/human.rb', line 198
def print_table(results, padding_left = 0, padding_right = 3)
require 'terminal-table'
@stream.puts Terminal::Table.new(
rows: results,
style: {
border_x: '',
border_y: '',
border_i: '',
padding_left: padding_left,
padding_right: padding_right,
border_top: false,
border_bottom: false
}
)
end
|
#print_target_info(targets) ⇒ Object
358
359
360
361
362
363
364
|
# File 'lib/bolt/outputter/human.rb', line 358
def print_target_info(targets)
@stream.puts ::JSON.pretty_generate(
"targets": targets.map(&:detail)
)
count = "#{targets.count} target#{'s' unless targets.count == 1}"
@stream.puts colorize(:green, count)
end
|
#print_targets(target_list, inventoryfile) ⇒ Object
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/bolt/outputter/human.rb', line 334
def print_targets(target_list, inventoryfile)
adhoc = colorize(:yellow, "(Not found in inventory file)")
targets = []
targets += target_list[:inventory].map { |target| [target.name, nil] }
targets += target_list[:adhoc].map { |target| [target.name, adhoc] }
if targets.any?
print_table(targets, 0, 2)
@stream.puts
end
@stream.puts "INVENTORY FILE:"
if inventoryfile.exist?
@stream.puts inventoryfile
else
@stream.puts wrap("Tried to load inventory from #{inventoryfile}, but the file does not exist")
end
@stream.puts "\nTARGET COUNT:"
@stream.puts "#{targets.count} total, #{target_list[:inventory].count} from inventory, "\
"#{target_list[:adhoc].count} adhoc"
end
|
#print_task_info(task) ⇒ Object
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
|
# File 'lib/bolt/outputter/human.rb', line 224
def print_task_info(task)
pretty_params = +""
task_info = +""
usage = +"bolt task run --targets <node-name> #{task.name}"
task.parameters&.each do |k, v|
pretty_params << "- #{k}: #{v['type'] || 'Any'}\n"
pretty_params << " Default: #{v['default'].inspect}\n" if v.key?('default')
pretty_params << " #{v['description']}\n" if v['description']
usage << if v['type'].is_a?(Puppet::Pops::Types::POptionalType)
" [#{k}=<value>]"
else
" #{k}=<value>"
end
end
usage << " [--noop]" if task.supports_noop
task_info << "\n#{task.name}"
task_info << " - #{task.description}" if task.description
task_info << "\n\n"
task_info << "USAGE:\n#{usage}\n\n"
task_info << "PARAMETERS:\n#{pretty_params}\n" unless pretty_params.empty?
task_info << "MODULE:\n"
path = task.files.first['path'].chomp("/tasks/#{task.files.first['name']}")
task_info << if path.start_with?(Bolt::Config::Modulepath::MODULES_PATH)
"built-in module"
else
path
end
@stream.puts(task_info)
end
|
#print_tasks(tasks, modulepath) ⇒ Object
216
217
218
219
220
221
|
# File 'lib/bolt/outputter/human.rb', line 216
def print_tasks(tasks, modulepath)
print_table(tasks)
print_message("\nMODULEPATH:\n#{modulepath.join(File::PATH_SEPARATOR)}\n"\
"\nUse `bolt task show <task-name>` to view "\
"details and parameters for a specific task.")
end
|
#print_topics(topics) ⇒ Object
296
297
298
299
300
|
# File 'lib/bolt/outputter/human.rb', line 296
def print_topics(topics)
print_message("Available topics are:")
print_message(topics.join("\n"))
print_message("\nUse `bolt guide <topic>` to view a specific guide.")
end
|
#remove_trail(string) ⇒ Object
33
34
35
|
# File 'lib/bolt/outputter/human.rb', line 33
def remove_trail(string)
string.sub(/\s\z/, '')
end
|
#wrap(string, width = 80) ⇒ Object
37
38
39
|
# File 'lib/bolt/outputter/human.rb', line 37
def wrap(string, width = 80)
string.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n")
end
|