lib/choria/colt/cli.rb in choria-colt-0.5.1 vs lib/choria/colt/cli.rb in choria-colt-0.6.0
- old
+ new
@@ -7,42 +7,45 @@
module Choria
class Colt
class CLI < Thor
class Tasks < Thor
+ def self.define_targets_and_filters_options
+ option :targets,
+ aliases: ['--target', '-t'],
+ desc: 'Identifies the targets of the command.'
+ option :targets_with_classes,
+ aliases: ['--targets-with-class', '-C'],
+ desc: 'Select the targets which have the specified Puppet classes.'
+ end
+
class_option :log_level,
desc: 'Set log level for CLI',
default: 'info'
# BOLT: desc 'run <task name> [parameters] {--targets TARGETS | --query QUERY | --rerun FILTER} [options]', 'Run a Bolt task'
desc 'run <task name> [parameters] --targets TARGETS [options]', 'Run a Bolt task'
long_desc <<~DESC
Run a task on the specified targets.
Parameters take the form parameter=value.
DESC
- option :targets,
- aliases: ['--target', '-t'],
- desc: 'Identifies the targets of the command.'
- option :targets_with_classes,
- aliases: ['--targets-with-class', '-C'],
- desc: 'Select the targets which have the specified Puppet classes.'
- def run(*args) # rubocop:disable Metrics/AbcSize
- input = extract_task_parameters_from_args(args)
- targets = extract_targets_from_options
-
+ define_targets_and_filters_options
+ option :environment,
+ aliases: ['-E'],
+ desc: 'Puppet environment to grab tasks from',
+ default: 'production'
+ def run(*args)
raise Thor::Error, 'Task name is required' if args.empty?
+
+ input = extract_task_parameters_from_args(args)
raise Thor::Error, "Too many arguments: #{args}" unless args.count == 1
- raise Thor::Error, 'Flag --targets or --targets-with-class is required' if options['targets'].nil? && options['targets_with_classes'].nil?
-
task_name = args.shift
+ targets, targets_with_classes = extract_targets_and_filters_from_options
- logger.debug "Targets: #{targets}"
-
- targets_with_classes = options['targets_with_classes']&.split(',')
-
- results = colt.run_bolt_task task_name, input: input, targets: targets, targets_with_classes: targets_with_classes do |result|
+ environment = options['environment']
+ results = colt.run_bolt_task task_name, input: input, targets: targets, targets_with_classes: targets_with_classes, environment: environment do |result|
$stdout.puts formatter.process_result(result)
end
File.write 'last_run.json', JSON.pretty_generate(results)
rescue Choria::Orchestrator::Error
@@ -82,12 +85,15 @@
long_desc <<~DESC
Show results from a previously ran task.
A task ID is required to request Choria services and retrieve results.
DESC
+ define_targets_and_filters_options
def status(task_id)
- results = colt.wait_bolt_task task_id do |result|
+ targets, targets_with_classes = extract_targets_and_filters_from_options
+
+ results = colt.wait_bolt_task(task_id, targets: targets, targets_with_classes: targets_with_classes) do |result|
$stdout.puts formatter.process_result(result)
end
File.write 'last_run.json', JSON.pretty_generate(results)
rescue Choria::Orchestrator::Error
@@ -128,11 +134,20 @@
[key, value]
end.to_h
end
+ def extract_targets_and_filters_from_options
+ raise Thor::Error, 'Flag --targets or --targets-with-class is required' if options['targets'].nil? && options['targets_with_classes'].nil?
+
+ targets = extract_targets_from_options
+ targets_with_classes = options['targets_with_classes']&.split(',')
+
+ [targets, targets_with_classes]
+ end
+
def extract_targets_from_options
- return nil if options['targets'] == 'all'
+ return nil if options['targets'] == ':all'
targets = options['targets']&.split(',')
targets&.map do |t|
t = File.read(t.sub(/^@/, '')).lines.map(&:strip) if t.start_with? '@'
t