Class: CloudFormationConverger

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudformation_converger.rb

Instance Method Summary collapse

Instance Method Details

#converge(stack_name:, stack_path:, bindings: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/cloudformation_converger.rb', line 6

def converge(stack_name:,
             stack_path:,
             bindings: nil)

  parameters = []
  unless bindings.nil?
    parameters = convert_hash_to_parameters bindings
  end

  cloudformation_client = Aws::CloudFormation::Client.new
  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/
        puts 'no updates necessary'
      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:100, 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.inject({}) do |hash, output|
    hash[output.output_key] = output.output_value
    hash
  end
end