Class: Bolt::CLI
- Inherits:
-
Object
- Object
- Bolt::CLI
- Defined in:
- lib/bolt/cli.rb
Constant Summary collapse
- COMMANDS =
{ 'command' => %w[run], 'script' => %w[run], 'task' => %w[show run], 'plan' => %w[show run], 'file' => %w[upload] }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #bundled_content ⇒ Object
- #execute(options) ⇒ Object
- #file_stat(path) ⇒ Object
- #handle_parser_errors ⇒ Object
-
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
- #outputter ⇒ Object
- #parse ⇒ Object
- #puppetdb_client ⇒ Object
- #query_puppetdb_nodes(query) ⇒ Object
- #validate(options) ⇒ Object
- #validate_file(type, path) ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
32 33 34 |
# File 'lib/bolt/cli.rb', line 32 def config @config end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
32 33 34 |
# File 'lib/bolt/cli.rb', line 32 def @options end |
Instance Method Details
#bundled_content ⇒ Object
357 358 359 360 361 362 363 364 365 366 |
# File 'lib/bolt/cli.rb', line 357 def bundled_content default_content = Bolt::PAL.new(Bolt::Config.new) plans = default_content.list_plans.each_with_object([]) do |iter, col| col << iter&.first end tasks = default_content.list_tasks.each_with_object([]) do |iter, col| col << iter&.first end plans.concat tasks end |
#execute(options) ⇒ Object
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 221 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 259 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 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 |
# File 'lib/bolt/cli.rb', line 194 def execute() = nil handler = Signal.trap :INT do |signo| @logger.info( "Exiting after receiving SIG#{Signal.signame(signo)} signal.#{ ? ' ' + : ''}" ) exit! end @analytics = Bolt::Analytics.build_client screen = "#{[:mode]}_#{[:action]}" # submit a different screen for `bolt task show` and `bolt task show foo` if [:action] == 'show' && [:object] screen += '_object' end @analytics.screen_view(screen, output_format: config[:format], target_nodes: .fetch(:targets, []).count, inventory_nodes: inventory.node_names.count, inventory_groups: inventory.group_names.count) if [:mode] == 'plan' || [:mode] == 'task' pal = Bolt::PAL.new(config) end if [:action] == 'show' if [:mode] == 'task' if [:object] outputter.print_task_info(pal.get_task_info([:object])) else outputter.print_table(pal.list_tasks) outputter.("\nUse `bolt task show <task-name>` to view "\ "details and parameters for a specific task.") end elsif [:mode] == 'plan' if [:object] outputter.print_plan_info(pal.get_plan_info([:object])) else outputter.print_table(pal.list_plans) outputter.("\nUse `bolt plan show <plan-name>` to view "\ "details and parameters for a specific plan.") end end return 0 end = 'There may be processes left executing on some nodes.' if [:task_options] && ![:params_parsed] && pal [:task_options] = pal.parse_params([:mode], [:object], [:task_options]) end if [:mode] == 'plan' unless [:nodes].empty? if [:task_options]['nodes'] raise Bolt::CLIError, "A plan's 'nodes' parameter may be specified using the --nodes option, but in that " \ "case it must not be specified as a separate nodes=<value> parameter nor included " \ "in the JSON data passed in the --params option" end [:task_options]['nodes'] = [:nodes].join(',') end params = [:noop] ? [:task_options].merge("_noop" => true) : [:task_options] plan_context = { plan_name: [:object], params: params } plan_context[:description] = [:description] if [:description] executor = Bolt::Executor.new(config, @analytics, [:noop], bundled_content: bundled_content) executor.start_plan(plan_context) result = pal.run_plan([:object], [:task_options], executor, inventory, puppetdb_client) # If a non-bolt exeception bubbles up the plan won't get finished executor.finish_plan(result) outputter.print_plan_result(result) code = result.ok? ? 0 : 1 else executor = Bolt::Executor.new(config, @analytics, [:noop], bundled_content: bundled_content) targets = [:targets] results = nil outputter.print_head elapsed_time = Benchmark.realtime do executor_opts = {} executor_opts['_description'] = [:description] if .key?(:description) results = case [:mode] when 'command' executor.run_command(targets, [:object], executor_opts) do |event| outputter.print_event(event) end when 'script' script = [:object] validate_file('script', script) executor.run_script( targets, script, [:leftovers], executor_opts ) do |event| outputter.print_event(event) end when 'task' pal.run_task([:object], targets, [:task_options], executor, inventory, [:description]) do |event| outputter.print_event(event) end when 'file' src = [:object] dest = [:leftovers].first if dest.nil? raise Bolt::CLIError, "A destination path must be specified" end validate_file('source file', src) executor.file_upload(targets, src, dest, executor_opts) do |event| outputter.print_event(event) end end end outputter.print_summary(results, elapsed_time) code = results.ok ? 0 : 2 end code rescue Bolt::Error => e outputter.fatal_error(e) raise e ensure # restore original signal handler Signal.trap :INT, handler if handler @analytics&.finish end |
#file_stat(path) ⇒ Object
349 350 351 |
# File 'lib/bolt/cli.rb', line 349 def file_stat(path) File.stat(path) end |
#handle_parser_errors ⇒ Object
172 173 174 175 176 177 178 179 180 |
# File 'lib/bolt/cli.rb', line 172 def handle_parser_errors yield rescue OptionParser::MissingArgument => e raise Bolt::CLIError, "Option '#{e.args.first}' needs a parameter" rescue OptionParser::InvalidArgument => e raise Bolt::CLIError, "Invalid parameter specified for option '#{e.args.first}': #{e.args[1]}" rescue OptionParser::InvalidOption, OptionParser::AmbiguousOption => e raise Bolt::CLIError, "Unknown argument '#{e.args.first}'" end |
#outputter ⇒ Object
353 354 355 |
# File 'lib/bolt/cli.rb', line 353 def outputter @outputter ||= Bolt::Outputter.for_format(config[:format], config[:color], config[:trace]) end |
#parse ⇒ Object
66 67 68 69 70 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 |
# File 'lib/bolt/cli.rb', line 66 def parse parser = BoltOptionParser.new() # This part aims to handle both `bolt <mode> --help` and `bolt help <mode>`. remaining = handle_parser_errors { parser.permute(@argv) } unless @argv.empty? if @argv.empty? || help?(parser, remaining) puts parser.help raise Bolt::CLIExit end config.update() config.validate Bolt::Logger.configure(config) # This section handles parsing non-flag options which are # mode specific rather then part of the config [:action] = remaining.shift [:object] = remaining.shift , remaining = remaining.partition { |s| s =~ /.+=/ } if [:task_options] unless .empty? raise Bolt::CLIError, "Parameters must be specified through either the --params " \ "option or param=value pairs, not both" end [:params_parsed] = true else [:params_parsed] = false [:task_options] = Hash[.map { |a| a.split('=', 2) }] end [:leftovers] = remaining validate() # After validation, initialize inventory and targets. Errors here are better to catch early. unless [:action] == 'show' if [:query] if [:nodes].any? raise Bolt::CLIError, "Only one of '--nodes' or '--query' may be specified" end nodes = query_puppetdb_nodes([:query]) [:targets] = inventory.get_targets(nodes) [:nodes] = nodes if [:mode] == 'plan' else [:targets] = inventory.get_targets([:nodes]) end end rescue Bolt::Error => e warn e. raise e end |
#puppetdb_client ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/bolt/cli.rb', line 182 def puppetdb_client return @puppetdb_client if @puppetdb_client @puppetdb_client = Bolt::Util::OnAccess.new do puppetdb_config = Bolt::PuppetDB::Config.new(nil, config.puppetdb) Bolt::PuppetDB::Client.from_config(puppetdb_config) end end |
#query_puppetdb_nodes(query) ⇒ Object
190 191 192 |
# File 'lib/bolt/cli.rb', line 190 def query_puppetdb_nodes(query) puppetdb_client.query_certnames(query) end |
#validate(options) ⇒ Object
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 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/bolt/cli.rb', line 122 def validate() unless COMMANDS.include?([:mode]) raise Bolt::CLIError, "Expected subcommand '#{[:mode]}' to be one of " \ "#{COMMANDS.keys.join(', ')}" end if [:action].nil? raise Bolt::CLIError, "Expected an action of the form 'bolt #{[:mode]} <action>'" end actions = COMMANDS[[:mode]] unless actions.include?([:action]) raise Bolt::CLIError, "Expected action '#{[:action]}' to be one of " \ "#{actions.join(', ')}" end if [:mode] != 'file' && [:mode] != 'script' && ![:leftovers].empty? raise Bolt::CLIError, "Unknown argument(s) #{[:leftovers].join(', ')}" end if %w[task plan].include?([:mode]) && [:action] == 'run' if [:object].nil? raise Bolt::CLIError, "Must specify a #{[:mode]} to run" end # This may mean that we parsed a parameter as the object unless [:object] =~ /\A([a-z][a-z0-9_]*)?(::[a-z][a-z0-9_]*)*\Z/ raise Bolt::CLIError, "Invalid #{[:mode]} '#{[:object]}'" end end if [:mode] != 'plan' && [:action] != 'show' if [:nodes].empty? && [:query].nil? raise Bolt::CLIError, "Targets must be specified with '--nodes' or '--query'" elsif [:nodes].any? && [:query] raise Bolt::CLIError, "Only one of '--nodes' or '--query' may be specified" end end if [:noop] && ([:mode] != 'task' || [:action] != 'run') raise Bolt::CLIError, "Option '--noop' may only be specified when running a task" end end |
#validate_file(type, path) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/bolt/cli.rb', line 333 def validate_file(type, path) if path.nil? raise Bolt::CLIError, "A #{type} must be specified" end stat = file_stat(path) if !stat.readable? raise Bolt::FileError.new("The #{type} '#{path}' is unreadable", path) elsif !stat.file? raise Bolt::FileError.new("The #{type} '#{path}' is not a file", path) end rescue Errno::ENOENT raise Bolt::FileError.new("The #{type} '#{path}' does not exist", path) end |