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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/cloudformation_converger.rb', line 26
def converge(stack_name:,
stack_path:,
bindings: nil,
strip_extras: false)
cloudformation_client = Aws::CloudFormation::Client.new
validate_template_response = cloudformation_client.validate_template(template_body: IO.read(stack_path))
legal_parameters = validate_template_response.parameters.map { | parameter| parameter.parameter_key }
parameters = []
unless bindings.nil?
parameters = convert_hash_to_parameters bindings, legal_parameters,
end
resource = Aws::CloudFormation::Resource.new(client: cloudformation_client)
if resource.stacks.find {|stack| stack.name == stack_name }
stack = resource.stack(stack_name)
begin
stack.update(template_body: IO.read(stack_path),
capabilities: %w(CAPABILITY_IAM),
parameters: parameters)
rescue Exception => error
if error.to_s =~ /No updates are to be performed/
STDERR.puts 'no updates necessary'
return stack_outputs_hash(stack) if stack.stack_status =~ /COMPLETE/
else
raise error
end
end
else
stack = resource.create_stack(stack_name: stack_name,
template_body: IO.read(stack_path),
capabilities: %w(CAPABILITY_IAM),
parameters: parameters)
end
stack.wait_until(max_attempts:360, delay:15) do |stack|
stack.stack_status =~ /COMPLETE/ or stack.stack_status =~ /FAILED/
end
if stack.stack_status =~ /FAILED/ or stack.stack_status =~ /ROLLBACK_COMPLETE/
raise "#{stack_name} failed to converge: #{stack.stack_status}"
end
stack_outputs_hash(stack)
end
|