From b297a442dc43e6bfecaf3341d1cdf5a7a2d4f7f0 Mon Sep 17 00:00:00 2001 From: DhanashreeDorage Date: Thu, 11 Aug 2016 11:19:17 -0400 Subject: [PATCH 1/3] Cloud formation changes for AWS SDk v2 dyknow/dyknowme#5634 --- lib/automan/cloudformation/launcher.rb | 12 ++++++------ lib/automan/mixins/aws_caller.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/automan/cloudformation/launcher.rb b/lib/automan/cloudformation/launcher.rb index dbb18f6..f1c0955 100644 --- a/lib/automan/cloudformation/launcher.rb +++ b/lib/automan/cloudformation/launcher.rb @@ -57,7 +57,7 @@ def template_handle(template_path) end def parse_template_parameters - cfn.validate_template( template_handle(template) ) + cfn2.validate_template( template_handle(template) ) end def validate_parameters @@ -90,11 +90,11 @@ def validate_parameters end def stack_exists? - cfn.stacks[name].exists? + cfn2.stacks[name].exists? end def stack_status - cfn.stacks[name].status + cfn2.stacks[name].status end def stack_launch_complete? @@ -127,7 +127,7 @@ def launch opts[:disable_rollback] = disable_rollback logger.info "launching stack #{name}" - cfn.stacks.create name, template_handle(template), opts + cfn2.stacks.create name, template_handle(template), opts if wait_for_completion logger.info "waiting for stack #{name} to launch" @@ -136,7 +136,7 @@ def launch end def update_stack(name, opts) - cfn.stacks[name].update( opts ) + cfn2.stacks[name].update( opts ) end def update @@ -154,7 +154,7 @@ def update # it raises a ValidationError begin update_stack(name, opts) - rescue AWS::CloudFormation::Errors::ValidationError => e + rescue Aws::CloudFormation::Errors::ValidationError => e if e.message != "No updates are to be performed." raise e else diff --git a/lib/automan/mixins/aws_caller.rb b/lib/automan/mixins/aws_caller.rb index d5e2960..6b64aee 100644 --- a/lib/automan/mixins/aws_caller.rb +++ b/lib/automan/mixins/aws_caller.rb @@ -101,6 +101,14 @@ def cfn @cfn end + attr_writer :cfn2 + def cfn2 + if @cfn2.nil? + @cfn2 = Aws::CloudFormation::Client.new + end + @cfn2 + end + attr_writer :as def as if @as.nil? From 742493ce4c469a4290eff4f912214382656789b8 Mon Sep 17 00:00:00 2001 From: DhanashreeDorage Date: Thu, 11 Aug 2016 11:59:30 -0400 Subject: [PATCH 2/3] Added puts to debug --- lib/automan/cloudformation/launcher.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/automan/cloudformation/launcher.rb b/lib/automan/cloudformation/launcher.rb index f1c0955..1b6f413 100644 --- a/lib/automan/cloudformation/launcher.rb +++ b/lib/automan/cloudformation/launcher.rb @@ -57,15 +57,18 @@ def template_handle(template_path) end def parse_template_parameters + puts "Entered into Parse_parametrs" cfn2.validate_template( template_handle(template) ) end def validate_parameters + puts "Entered into validate parameters" if parameters.nil? raise MissingParametersError, "there are no parameters!" end template_params_hash = parse_template_parameters + puts "Hello" if template_params_hash.has_key? :code mesg = template_params_hash[:message] From 4b7cac4336cbc1649502e6ecf1aca5fd508cff3d Mon Sep 17 00:00:00 2001 From: DhanashreeDorage Date: Fri, 19 Aug 2016 09:52:29 -0400 Subject: [PATCH 3/3] fix up --- lib/automan/cloudformation/launcher.rb | 59 +++++++++++++++----------- lib/automan/mixins/aws_caller.rb | 10 ++++- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/lib/automan/cloudformation/launcher.rb b/lib/automan/cloudformation/launcher.rb index 1b6f413..3060e11 100644 --- a/lib/automan/cloudformation/launcher.rb +++ b/lib/automan/cloudformation/launcher.rb @@ -50,15 +50,15 @@ def read_manifest def template_handle(template_path) if looks_like_s3_path? template_path bucket, key = parse_s3_path template_path - return s3.buckets[bucket].objects[key] + return s3.buckets[bucket].objects[key].read else return File.read(template_path) end end def parse_template_parameters - puts "Entered into Parse_parametrs" - cfn2.validate_template( template_handle(template) ) + template_val = { template_body: template_handle(template) } + cfn2.validate_template( template_val) end def validate_parameters @@ -70,14 +70,14 @@ def validate_parameters template_params_hash = parse_template_parameters puts "Hello" - if template_params_hash.has_key? :code - mesg = template_params_hash[:message] - raise BadTemplateError, "template did not validate: #{mesg}" - end +# if template_params_hash.has_key? :code +# mesg = template_params_hash[:message] +# raise BadTemplateError, "template did not validate: #{mesg}" +# end # make sure any template parameters w/o a default are specified - template_params = template_params_hash[:parameters] - required_params = template_params.select { |x| !x.has_key? :default_value} + template_params = template_params_hash.parameters + required_params = template_params #.select { |x| !x.has_key? :default_value} required_params.each do |rp| unless parameters.has_key? rp[:parameter_key] raise MissingParametersError, "required parameter #{rp[:parameter_key]} was not specified" @@ -85,19 +85,24 @@ def validate_parameters end # add iam capabilities if needed - if template_params_hash.has_key?(:capabilities) && - template_params_hash[:capabilities].include?('CAPABILITY_IAM') + if template_params_hash.capabilities.include?('CAPABILITY_IAM') self.enable_iam = true end end def stack_exists? - cfn2.stacks[name].exists? + #cfn2.stacks[name]_exists? + begin + cfn2resource.stack(name).stack_id + true + rescue Aws::CloudFormation::Errors::ValidationError + false + end end def stack_status - cfn2.stacks[name].status + cfn2resource.stacks[name].status end def stack_launch_complete? @@ -121,17 +126,23 @@ def stack_update_complete? false end end + + def convert_parameters(parameters) + parameters.map { |k,v| { parameter_key: k, parameter_value: v } } + end - def launch - opts = { - parameters: parameters - } - opts[:capabilities] = ['CAPABILITY_IAM'] if enable_iam - opts[:disable_rollback] = disable_rollback - - logger.info "launching stack #{name}" - cfn2.stacks.create name, template_handle(template), opts - + def launch() + + logger.info "launching stack #{name}" + + cfn2resource.create_stack({ + stack_name: name, + parameters: convert_parameters(parameters), + disable_rollback: false, + capabilities: ["CAPABILITY_IAM"], + template_body: template_handle(template) + }) +# cfn2resource.stacks.new(name, template_val, opts if wait_for_completion logger.info "waiting for stack #{name} to launch" wait_until { stack_launch_complete? } @@ -139,7 +150,7 @@ def launch end def update_stack(name, opts) - cfn2.stacks[name].update( opts ) + cfn2resource.stacks[name].update( opts ) end def update diff --git a/lib/automan/mixins/aws_caller.rb b/lib/automan/mixins/aws_caller.rb index 6b64aee..bb5a48e 100644 --- a/lib/automan/mixins/aws_caller.rb +++ b/lib/automan/mixins/aws_caller.rb @@ -108,7 +108,15 @@ def cfn2 end @cfn2 end - + + attr_writer :cfn2resource + def cfn2resource + if @cfn2resource.nil? + @cfn2resource = Aws::CloudFormation::Resource.new + end + @cfn2resource + end + attr_writer :as def as if @as.nil?