Module: Aws::Cfn::Stacker::Options
- Includes:
- DLDInternet::Mixlib::CLI::Parsers
- Included in:
- ListParams
- Defined in:
- lib/aws/cfn/stacker/mixins/options.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.extended(includer) ⇒ Object
————————————————————————————————————–.
- .included(includer) ⇒ Object
Instance Method Summary collapse
-
#checkArgsSources(options) ⇒ Object
—————————————————————————————————————.
- #no_command_given?(argv = ARGV) ⇒ Boolean
-
#no_subcommand_given?(argv = ARGV) ⇒ Boolean
————————————————————————————————————– private ————————————————————————————————————–.
-
#parse_and_validate_options(options = nil, source = 'ARGV') ⇒ Object
—————————————————————————–.
-
#parse_options(args, source = nil) ⇒ Object
——————————————————————————–.
-
#parseActionSymbol(v) ⇒ Object
——————————————————————————–.
-
#parseINIFile(options = nil) ⇒ Object
——————————————————————————–.
-
#parsePrecedence(v) ⇒ Object
——————————————————————————–.
-
#prescreen_options(argv = ARGV) ⇒ Object
————————————————————————————————————–.
- #print_help_and_exit(exitcode = 1, fatal_message = nil) ⇒ Object
-
#setDefaultOptions(options) ⇒ Object
—————————————————————————–.
-
#setOrigins(options, source) ⇒ Object
—————————————————————————————————————.
-
#validate_options(options = nil) ⇒ Object
—————————————————————————–.
- #want_help? ⇒ Boolean
- #want_version? ⇒ Boolean
Class Method Details
.extended(includer) ⇒ Object
If this module is included we inject this payload into the including class.
286 287 288 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 286 def self.extended(includer) included includer end |
.included(includer) ⇒ Object
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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 289 def self.included(includer) includer.extend(::DLDInternet::Mixlib::CLI::Parsers::ClassMethods) includer.extend(ClassMethods) includer.class_eval do self. = {} <<-EOF -c CONFIG_FILE, --config-file CONFIG_FILE An ini file to use that contains one section per stack, with all the parameters for the stack enumerated -v, --verbose Increase verbosity, can be specified multiple times (currently just sets the debug level for AWS) --debug Increase debug, can be specified multiple times -r, --remove Delete the requested stack. WARNING: No second chances! -d, --delete Delete the requested stack. WARNING: No second chances! -l, --list-params List the parameters in the template, and show what values are supplied by your config file -t TEMPLATE, --template TEMPLATE Specify a different template to run. Note that specific outputs are expected, so results may vary. --template_url TEMPLATE_URL Specify the key of a template stored in an S3 bucket to run. This method assumes the template has already been uploaded. --use_s3 Use an S3 bucket to upload the template to before attempting stack run. This method assumes the template needs to be uploaded and may overwrite a key with the same name. -k KEYFILE, --keyfile KEYFILE The path to the SSH key file to use for SSH to hosts -b, --build Build configuration directory for use with Ansible. -i INITIAL_SETUP, --initial_setup INITIAL_SETUP An initial setup file for Ansible -o OUTPUT, --output OUTPUT Output folder for the Ansible build configuration -R ROLES_PATH, --roles_path ROLES_PATH Folder for the Ansible roles --ssh_user SSH_USER SSH User name --ssh_user_bastion SSH_USER_BASTION Bastion SSH User name -u, --update Update the configuration of an existing stack -C, --configure Run pre-defined ansible playbooks against the given stack. Implies -b -s, --status Print the current status of a given stack. -p, --progress Print a progress indicator during stack create/update/delete. EOF option :help, short: '-h', long: '--help', description: 'Show this message', show_options: true, exit: 1 # print the version. option :version, short: '-V', long: '--version', description: 'Show version', proc: Proc.new{ puts ::Aws::Cfn::Stacker::VERSION }, exit: 2 option :config_file_alt, short: '-c', long: '--config-file FILE', description: 'A config file to use that contains one section per stack, with all the parameters for the stack enumerated. INI, YAML and JSON formats supported.', proc: lambda{ |v| @options[:config_file] = v } option :config_file, short: '-c', long: '--config_file FILE', description: 'A config file to use that contains one section per stack, with all the parameters for the stack enumerated. INI, YAML and JSON formats supported.', default: 'config/config.ini' option :verbose, short: '-v', long: '--verbose', description: 'Increase verbosity, can be specified multiple times', proc: lambda {|v| index = $STKR.class.loglevels.index(@options[:log_level]) || $STKR.class.loglevels.index(:warn) index -= 1 if index > 0 @options[:log_level] = $STKR.class..loglevels[index] } option :log_level, long: '--debug', description: 'Set debug level logging. No effect if specified second time.', default: :debug option :actions, short: '-a', long: '--action ACTION', description: "Perform the requested action against the stack. (#{StackerApplication.allactions.to_s})", proc: lambda{|v| actions = $STKR.parseOptionString(v,',', 'parseActionSymbol') all = [StackerApplication.allactions, :all].flatten actions.each{ |act| unless all.include?(act.to_sym) raise ::OptionParser::InvalidOption.new("Invalid action: #{act.to_s}. Valid actions are: #{all.to_s}") end } actions }, default: [ :create ] option :build, short: '-b', long: '--build', description: 'Build configuration directory for use with Ansible.', proc: lambda { |v| @options[:action] = :build} option :remove, short: '-r', long: '--remove', description: 'Delete the requested stack. WARNING: No second chances!', proc: lambda { |v| @options[:action] = :delete } option :delete, short: '-d', long: '--delete', description: 'Delete the requested stack. WARNING: No second chances!', proc: lambda { |v| @options[:action] = :delete } option :update, short: '-u', long: '--update', description: 'Update the configuration of an existing stack', proc: lambda { |v| @options[:action] = :update} option :listparams, short: '-l', long: '--list-params', description: 'List the parameters in the template, and show what values are supplied by your config file', proc: lambda { |v| @options[:action] = :listparams} option :configure, short: '-C', long: '--configure', description: 'Run pre-defined ansible playbooks against the given stack. Implies -b', proc: lambda { |v| @options[:action] = :configure} option :status, short: '-s', long: '--status', description: 'Run pre-defined ansible playbooks against the given stack. Implies -b', proc: lambda { |v| @options[:action] = :status} option :template, short: '-t', long: '--template FILE', description: "Specify a template to run. Note that specific outputs are expected, so results may vary. Default #{StackerApplication.[:template_file]}", proc: lambda { |v| @options[:template_file] = v } option :template_file, long: '--template-file FILE', description: "Specify a template to run. Note that specific outputs are expected, so results may vary. Default #{StackerApplication.[:template_file]}", default: 'templates/mvc-vpc.json' option :template_file_alt, long: '--template_file FILE', description: "Specify a template to run. Note that specific outputs are expected, so results may vary. Default #{StackerApplication.[:template_file]}", proc: lambda { |v| @options[:template_file] = v } option :template_url_alt, long: '--template-url URL', description: 'Specify the URL of a template stored in an S3 bucket to run. This method assumes the template has already been uploaded.', proc: lambda { |v| @options[:template_url] = v } option :template_url, long: '--template_url URL', description: 'Specify the URL of a template stored in an S3 bucket to run. This method assumes the template has already been uploaded.' option :use_s3, long: '--use_s3', description: 'Use an S3 bucket to upload the template to before attempting stack run. This method assumes the template needs to be uploaded and may overwrite a key with the same name.' option :ssh_keyfile, short: '-k', long: '--keyfile FILE', description: 'The path to the SSH key file to use for SSH to hosts' option :ssh_user, long: '--ssh_user USER', description: 'SSH User name', default: 'ubuntu' option :ssh_user_bastion, long: '--ssh_user_bastion USER', description: 'Bastion SSH User name', default: 'ubuntu' option :initial_setup, short: '-i', long: '--initial_setup FILE', description: 'An initial setup file for Ansible', default: 'ansible/playbooks/initial_setup.yml' option :output_path, short: '-o', long: '--output PATH', description: 'Output folder for the Ansible build configuration' option :roles_path, short: '-r', long: '--roles_path PATH', description: 'Output folder for the Ansible build configuration', default: 'ansible/playbooks/roles' option :progress, short: '-p', long: '--progress', description: 'Print a progress indicator during stack create/update/delete.', default: false option :inifile, short: "-f", long: "--inifile FILE", description: "INI file with settings" option :log_file_alt, long: '--log-file PATH', description: 'Log destination file', proc: lambda { |v| @options[:log_file] = v } option :log_file, long: '--log_file PATH', description: 'Log destination file' option :log_level_alt, long: '--log-level LEVEL', description: 'Logging level', proc: lambda{|v| if StackerApplication.loglevels.include? v.to_sym v.to_sym else level = StackerApplication.loglevels.select{|l| l.to_s.match(%r(^#{v}))} unless level.size > 0 raise ::OptionParser::InvalidOption.new("Invalid log level: #{v}. Valid levels are #{StackerApplication.loglevels.ai}") end level[0].to_sym end } option :log_level, long: '--log_level LEVEL', description: 'Logging level', proc: lambda{|v| if StackerApplication.loglevels.include? v.to_sym v.to_sym else level = StackerApplication.loglevels.select{|l| l.to_s.match(%r(^#{v}))} unless level.size > 0 raise ::OptionParser::InvalidOption.new("Invalid log level: #{v}. Valid levels are #{StackerApplication.loglevels.ai}") end level[0].to_sym end } option :report_config_alt, long: '--report-config', description: 'Report Configuration', proc: lambda { |v| @options[:report_config] = true } option :report_config, long: '--report-config', description: 'Report Configuration' end # included # ------------------------------------------------------------------------------------------------------------ end |
Instance Method Details
#checkArgsSources(options) ⇒ Object
260 261 262 263 264 265 266 267 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 260 def checkArgsSources() if @origins missing = @origins.select{ |k,v| v.nil? }.map{ |k,v| k } raise StackerError.new("Missing origins: #{missing.ai}") if missing.size > 0 end end |
#no_command_given?(argv = ARGV) ⇒ Boolean
77 78 79 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 77 def no_command_given?(argv=ARGV) argv.empty? end |
#no_subcommand_given?(argv = ARGV) ⇒ Boolean
private
73 74 75 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 73 def no_subcommand_given?(argv=ARGV) argv[0] =~ /^--?[^a]/ end |
#parse_and_validate_options(options = nil, source = 'ARGV') ⇒ Object
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 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 205 def (=nil,source='ARGV') = @config unless setOrigins(,source) #options = parseOptions(options,source) unless @origins and @name_key_map # These are the essential default options which things like parseOptions depend on { :verbosity => @verbosity, :auto_purge => false, }.each{ |k,v| [k] = v unless [k] } setOrigins(,'hardcoded-default') @name_key_map = {} unless @name_key_map @options.each{ |name,args| @name_key_map[name] = {} unless @name_key_map[name] [:short,:long,:description].each{|key| @name_key_map[name][key] = args[key] if args[key] } } end begin parseINIFile() setDefaultOptions() # Check for all the necessary options () checkArgsSources() #findRootPath(options) rescue StackerError => e puts e..light_red puts "#{__FILE__}:#{__LINE__} reraising ... " raise e exit -1 rescue Exception => e puts e..light_red puts "#{__FILE__}:#{__LINE__} reraising ... " raise e exit -2 end end |
#parse_options(args, source = nil) ⇒ Object
Override Mixlib::CLI.parse_options and knowingly add extra paramater to track argument source
We do this by monkey patching this override method at include time instead of statically declaring it in which case the Mixlib::CLI.parse_options seems to “win” most of the time OR the interpreter complains.
21 22 23 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/aws/cfn/stacker/mixins/options.rb', line 21 def (args,source=nil) # noinspection RubySuperCallWithoutSuperclassInspection argv = super(args) (argv) @config = (@config,source ? source : "ARGV - #{File.basename __FILE__}::#{__LINE__}") if argv.size > 0 argv.each { |arg| action = parseOptionString(arg, ',', 'parseActionSymbol') if action @config[:actions] << action end } end actions = Hash[@config[:actions].flatten.map{ |v| [v, 1] }] @config[:actions] = actions.keys @config[:actions].each {|action| unless StackerApplication.actors[action] subcommand_class = StackerApplication.subcommand_class_from([action.to_s]) subcommand_class.load_deps instance = subcommand_class.new() StackerApplication.actors[action] = instance instance.configure_application end } argv end |
#parseActionSymbol(v) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 98 def parseActionSymbol(v) if v.to_sym == :all StackerApplication.allactions else s = v.gsub('-', '_').to_sym allactions = [StackerApplication.allactions, :all].flatten unless allactions.include?(s) allactions.each{ |p| s = p if p.match(%r/^#{s}/) } end s = StackerApplication.allactions if s == :all s end end |
#parseINIFile(options = nil) ⇒ Object
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 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 139 def parseINIFile(=nil) = @config unless if .key?(:inifile) logStep "Parse INI file - #{[:inifile]}" raise StackerError.new("Cannot find inifile (#{[:inifile]})") unless File.exist?([:inifile]) raise StackerError.new("Recursive call to inifile == '#{[:inifile]}'") if @inis.include?([:inifile]) ini = nil begin # puts "IniFile.load('#{options[:inifile]}')" ini = IniFile.load([:inifile]) @inis << [:inifile] ini['global'].each { |key, value| # puts "#{key}=#{value}" ENV[key]=value } argv=[] cli = ini['cli'] || [] cli.each{ |key,value| argv << key.gsub(%r/:[0-9]+$/, '').gsub(%r/^([^-])/, '--\1') argv << value } if argv.size > 0 (argv,"INI-#{[:inifile]}") end rescue => e puts e..light_red raise e end end end |
#parsePrecedence(v) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 115 def parsePrecedence(v) @prec_max += 1 match = v.match(%r/^(json|rb|yaml)$/i) unless match m = "ERROR: Invalid precedence argument: #{v}. Accept only from this set: [json,rb,yaml]" puts m raise Exception.new(m) end s = { v => @prec_max } match = v.match(%r/^(\S+):(\d+)$/) if match begin a = match[1] i = match[2].to_i s = { a => i } rescue => e puts "ERROR: Unable to match precedence #{v}" raise e end end s end |
#prescreen_options(argv = ARGV) ⇒ Object
Do a quick prescreening of the arguments and bail early for some obvious cases
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 56 def (argv=ARGV) # Checking ARGV validity *before* parse_options because parse_options # mangles ARGV in some situations if no_command_given? print_help_and_exit(1, @NO_COMMAND_GIVEN) elsif no_subcommand_given? if want_help? || want_version? print_help_and_exit else print_help_and_exit(2, @NO_COMMAND_GIVEN) end end end |
#print_help_and_exit(exitcode = 1, fatal_message = nil) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 89 def print_help_and_exit(exitcode=1, =nil) puts "FATAL: #{}" if puts self.opt_parser puts exit exitcode end |
#setDefaultOptions(options) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 172 def setDefaultOptions() @options.each{|name,args| if args[:default] [name] = args[:default] unless [name] end } setOrigins(,'default') end |
#setOrigins(options, source) ⇒ Object
252 253 254 255 256 257 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 252 def setOrigins(,source) @origins = {} unless @origins .each { |key, val| @origins[key] = source unless (@origins[key]) } end |
#validate_options(options = nil) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 182 def (=nil) = @config unless # Check for the necessary environment variables logStep ('Check ENVironment') env = ENV.to_hash missing = {} %w(AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY).each { |k| missing[k] = true unless ENV.has_key?(k) } if [:use_chef] %w(KNIFE_CHEF_SERVER_URL KNIFE_CLIENT_KEY KNIFE_CLIENT_NAME).each { |k| missing[k] = true unless ENV.has_key?(k) } end if missing.count() > 0 #@logger.error "Missing keys: #{missing.keys.ai}" raise StackerError.new("Missing environment variables: #{missing.keys}") end end |
#want_help? ⇒ Boolean
81 82 83 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 81 def want_help? ARGV[0] =~ /^(--help|-h)$/ end |
#want_version? ⇒ Boolean
85 86 87 |
# File 'lib/aws/cfn/stacker/mixins/options.rb', line 85 def want_version? ARGV[0] =~ /^(--version|-v)$/ end |