lib/cfer.rb in cfer-0.2.0 vs lib/cfer.rb in cfer-0.3.0

- old
+ new

@@ -18,26 +18,34 @@ end module Core end + %w{ + DB + ASG + }.each do |acronym| + ActiveSupport::Inflector.inflections.acronym acronym + end + # The Cfer logger LOGGER = Logger.new(STDERR) LOGGER.level = Logger::INFO - LOGGER.formatter = proc { |severity, datetime, progname, msg| - msg = case severity - when 'FATAL' - Rainbow(msg).red.bright - when 'ERROR' - Rainbow(msg).red - when 'WARN' - Rainbow(msg).yellow - when 'DEBUG' - Rainbow(msg).black.bright - else - msg - end + LOGGER.formatter = proc { |severity, _datetime, _progname, msg| + msg = + case severity + when 'FATAL' + Rainbow(msg).red.bright + when 'ERROR' + Rainbow(msg).red + when 'WARN' + Rainbow(msg).yellow + when 'DEBUG' + Rainbow(msg).black.bright + else + msg + end "#{msg}\n" } class << self @@ -45,50 +53,61 @@ def converge!(stack_name, options = {}) config(options) tmpl = options[:template] || "#{stack_name}.rb" cfn = options[:aws_options] || {} - cfn_stack = Cfer::Cfn::Client.new(cfn.merge(stack_name: stack_name)) - stack = Cfer::stack_from_file(tmpl, options.merge(client: cfn_stack)) + cfn_stack = options[:cfer_client] || Cfer::Cfn::Client.new(cfn.merge(stack_name: stack_name)) + stack = options[:cfer_stack] || Cfer::stack_from_file(tmpl, options.merge(client: cfn_stack)) begin cfn_stack.converge(stack, options) if options[:follow] tail! stack_name, options end rescue Aws::CloudFormation::Errors::ValidationError => e Cfer::LOGGER.info "CFN validation error: #{e.message}" end - describe! stack_name, options + describe! stack_name, options unless options[:follow] end def describe!(stack_name, options = {}) config(options) cfn = options[:aws_options] || {} - cfn_stack = Cfer::Cfn::Client.new(cfn.merge(stack_name: stack_name)).fetch_stack + cfn_stack = options[:cfer_client] || Cfer::Cfn::Client.new(cfn.merge(stack_name: stack_name)) + cfn_stack = cfn_stack.fetch_stack Cfer::LOGGER.debug "Describe stack: #{cfn_stack}" - case options[:output_format] || 'table' + case options[:output_format] when 'json' puts render_json(cfn_stack, options) - when 'table' + when 'table', nil puts "Status: #{cfn_stack[:stack_status]}" puts "Description: #{cfn_stack[:description]}" if cfn_stack[:description] puts "" - parameters = (cfn_stack[:parameters] || []).map { |param| {:Type => "Parameter", :Key => param[:parameter_key], :Value => param[:parameter_value]} } - outputs = (cfn_stack[:outputs] || []).map { |output| {:Type => "Output", :Key => output[:output_key], :Value => output[:output_value]} } + def tablify(list, type) + list ||= [] + list.map { |param| + { + :Type => type.to_s.titleize, + :Key => param[:"#{type}_key"], + :Value => param[:"#{type}_value"] + } + } + end + parameters = tablify(cfn_stack[:parameters] || [], 'parameter') + outputs = tablify(cfn_stack[:outputs] || [], 'output') tp parameters + outputs, :Type, :Key, {:Value => {:width => 80}} else raise Cfer::Util::CferError, "Invalid output format #{options[:output_format]}." end end def tail!(stack_name, options = {}, &block) config(options) cfn = options[:aws_options] || {} - cfn_client = Cfer::Cfn::Client.new(cfn.merge(stack_name: stack_name)) + cfn_client = options[:cfer_client] || Cfer::Cfn::Client.new(cfn.merge(stack_name: stack_name)) if block cfn_client.tail(options, &block) else cfn_client.tail(options) do |event| Cfer::LOGGER.info "%s %-30s %-40s %-20s %s" % [event.timestamp, color_map(event.resource_status), event.resource_type, event.logical_resource_id, event.resource_status_reason] @@ -141,10 +160,10 @@ Aws.config.update credentials: Aws::SharedCredentials.new(profile_name: options[:profile]) if options[:profile] end def render_json(obj, options = {}) if options[:pretty_print] - puts JSON.pretty_generate(obj) + puts JSON.pretty_generate(obj, options) else puts obj.to_json end end