lib/hygroscope/cli.rb in hygroscope-1.1.3 vs lib/hygroscope/cli.rb in hygroscope-1.1.4

- old
+ new

@@ -50,10 +50,34 @@ end def template_path File.join(hygro_path, 'template') end + + def say_paramset(name) + begin + p = Hygroscope::ParamSet.new(name) + rescue Hygroscope::ParamSetNotFoundError + say_fail("Paramset #{name} does not exist!") + end + + say "Parameters for '#{File.basename(Dir.pwd)}' paramset '#{p.name}':", :yellow + print_table p.parameters, indent: 2 + say "\nTo edit existing parameters, use the 'create' command with the --ask flag." + end + + def say_paramset_list + files = Dir.glob(File.join(hygro_path, 'paramsets', '*.{yml,yaml}')) + + say_fail("No saved paramsets for #{hygro_name}.") if files.empty? + + say "Saved paramsets for '#{hygro_name}':", :yellow + files.map do |f| + say ' ' + File.basename(f, File.extname(f)) + end + say "\nTo list parameters in a set, use the --name option." + end end desc 'prepare', 'Prepare to create or update a stack by generating the template, assembling parameters, and managing payload upload', hide: true def prepare # Generate the template @@ -152,19 +176,10 @@ p.set('HygroscopePayloadSignedUrl', payload.generate_url) if missing.include?('HygroscopePayloadSignedUrl') say_status('ok', 'Payload uploaded to:', :green) say_status('', "s3://#{payload.bucket}/#{payload.key}") end - # Set some additional parameters, if present - # HygroscopeAccountAzList - # HygroscopeAccountAzCount - #if missing.include?('HygroscopeAccountAzList') || - # misisng.include?('HygroscopeAccountAzCount') - # p.set('HygroscopeAccountAzList', azlist) if missing.include?('HygroscopeAccountAzList') - # p.set('HygroscopeAccountAzCount', azlist) if missing.include?('HygroscopeAccountAzCount') - #end - [t, p] end desc 'create', "Create a new stack.\nUse the --name option to launch more than one stack from the same template.\nCommand prompts for parameters unless --paramset is specified.\nUse --existing to set parameters from an existing stack's outputs." method_option :name, @@ -183,21 +198,35 @@ method_option :ask, aliases: '-a', type: :boolean, default: false, desc: 'Still prompt for parameters even when using a paramset' + method_option :tags, + aliases: '-t', + type: :array, + required: false, + desc: 'One or more parameters to apply as tags to all stack resources' def create check_path validate # Prepare task takes care of shared logic between "create" and "update" template, paramset = prepare s = Hygroscope::Stack.new(options[:name]) s.parameters = paramset.parameters s.template = template.compress - s.tags['X-Hygroscope-Template'] = File.basename(Dir.pwd) + + s.tags['X-Hygroscope-Template'] = hygro_name + options[:tags].each do |tag| + if paramset.get(tag) + s.tags[tag] = paramset.get(tag) + else + say_status('info', "Skipping tag #{tag} because it does not exist", :blue) + end + end + s.capabilities = ['CAPABILITY_IAM'] s.create! status @@ -247,16 +276,17 @@ type: :boolean, default: false, desc: 'Delete without asking for confirmation' def delete check_path - if options[:force] || yes?("Really delete stack #{options[:name]} [y/N]?") - say('Deleting stack!') - stack = Hygroscope::Stack.new(options[:name]) - stack.delete! - status - end + abort unless options[:force] || + yes?("Really delete stack #{options[:name]} [y/N]?") + + say('Deleting stack!') + stack = Hygroscope::Stack.new(options[:name]) + stack.delete! + status end desc 'status', 'View status of stack create/update/delete action.\nUse the --name option to change which stack is reported upon.' method_option :name, aliases: '-n', @@ -282,11 +312,11 @@ print_table header puts # Fancy acrobatics to fit output to terminal width. If the terminal # window is too small, fallback to something appropriate for ~80 chars - term_width = %x(stty size 2>/dev/null).split[1].to_i || %x(tput cols 2>/dev/null).to_i + term_width = `stty size 2>/dev/null`.split[1].to_i || `tput cols 2>/dev/null`.to_i type_width = term_width < 80 ? 30 : term_width - 50 output_width = term_width < 80 ? 54 : term_width - 31 # Header row puts set_color(sprintf(' %-28s %-*s %-18s ', 'Resource', type_width, 'Type', 'Status'), :white, :on_blue) @@ -319,11 +349,13 @@ rescue Interrupt abort end while true end - desc 'generate', "Generate and display JSON output from template files.\nTo validate that the template is well-formed use the 'validate' command." + desc 'generate', + "Generate and display JSON output from template files.\n" \ + "To validate that the template is well-formed use the 'validate' command." method_option :color, aliases: '-c', type: :boolean, default: true, desc: 'Colorize JSON output' @@ -339,25 +371,20 @@ end desc 'validate', "Generate JSON from template files and validate that it is well-formed.\nThis utilzies the CloudFormation API to validate the template but does not detect logical errors." def validate check_path + begin t = Hygroscope::Template.new(template_path) t.validate rescue Aws::CloudFormation::Errors::ValidationError => e - say_status('error', 'Validation error', :red) - print_wrapped e.message, indent: 2 - abort + say_fail("Validation error: #{e.message}") rescue Hygroscope::TemplateYamlParseError => e - say_status('error', 'YAML parsing error', :red) - puts e - abort + say_fail("YAML parsing error: #{e.message}") rescue => e - say_status('error', 'Unexpected error', :red) - print_wrapped e.message, indent: 2 - abort + say_fail(e.message) else say_status('ok', 'Template is valid', :green) end end @@ -366,28 +393,12 @@ aliases: '-n', required: false, desc: 'Name of a paramset' def paramset if options[:name] - begin - p = Hygroscope::ParamSet.new(options[:name]) - rescue Hygroscope::ParamSetNotFoundError - raise("Paramset #{options[:name]} does not exist!") - end - say "Parameters for '#{hygro_name}' paramset '#{p.name}':", :yellow - print_table p.parameters, indent: 2 - say "\nTo edit existing parameters, use the 'create' command with the --ask flag." + say_paramset(options[:name]) else - files = Dir.glob(File.join(hygro_path, 'paramsets', '*.{yml,yaml}')) - if files.empty? - say "No saved paramsets for '#{hygro_name}'.", :red - else - say "Saved paramsets for '#{hygro_name}':", :yellow - files.map do |f| - say ' ' + File.basename(f, File.extname(f)) - end - say "\nTo list parameters in a set, use the --name option." - end + say_paramset_list end end end end