Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 38 additions & 24 deletions lib/automan/cloudformation/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,51 +50,59 @@ 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
cfn.validate_template( template_handle(template) )
template_val = { template_body: template_handle(template) }
cfn2.validate_template( template_val)
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]
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"
end
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?
cfn.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
cfn.stacks[name].status
cfn2resource.stacks[name].status
end

def stack_launch_complete?
Expand All @@ -118,25 +126,31 @@ 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}"
cfn.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? }
end
end

def update_stack(name, opts)
cfn.stacks[name].update( opts )
cfn2resource.stacks[name].update( opts )
end

def update
Expand All @@ -154,7 +168,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
Expand Down
16 changes: 16 additions & 0 deletions lib/automan/mixins/aws_caller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ def cfn
@cfn
end

attr_writer :cfn2
def cfn2
if @cfn2.nil?
@cfn2 = Aws::CloudFormation::Client.new
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?
Expand Down