Class: Stackup::Stack

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

Constant Summary collapse

SUCESS_STATES =
["CREATE_COMPLETE", "UPDATE_COMPLETE"]
FAILURE_STATES =
["CREATE_FAILED", "DELETE_FAILED", "UPDATE_ROLLBACK_FAILED", "ROLLBACK_FAILED", "ROLLBACK_COMPLETE","ROLLBACK_FAILED","UPDATE_ROLLBACK_COMPLETE","UPDATE_ROLLBACK_FAILED"]
END_STATES =
SUCESS_STATES + FAILURE_STATES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, template) ⇒ Stack

Returns a new instance of Stack.



10
11
12
13
14
15
16
# File 'lib/stackup/stack.rb', line 10

def initialize(name, template)
  @cf = Aws::CloudFormation::Client.new
  @stack = Aws::CloudFormation::Stack.new(name: name, client: cf)
  @monitor = Stackup::Monitor.new(@stack)
  @template = template
  @name = name
end

Instance Attribute Details

#cfObject (readonly)

Returns the value of attribute cf.



5
6
7
# File 'lib/stackup/stack.rb', line 5

def cf
  @cf
end

#monitorObject (readonly)

Returns the value of attribute monitor.



5
6
7
# File 'lib/stackup/stack.rb', line 5

def monitor
  @monitor
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/stackup/stack.rb', line 5

def name
  @name
end

#stackObject (readonly)

Returns the value of attribute stack.



5
6
7
# File 'lib/stackup/stack.rb', line 5

def stack
  @stack
end

#templateObject (readonly)

Returns the value of attribute template.



5
6
7
# File 'lib/stackup/stack.rb', line 5

def template
  @template
end

Instance Method Details

#createObject



18
19
20
21
22
23
24
25
26
# File 'lib/stackup/stack.rb', line 18

def create
  response = cf.create_stack({
    stack_name: name,
    template_body: template,
    disable_rollback: true
    })
  !response[:stack_id].nil?
  stack.wait_until(max_attempts: 1000, delay: 10) { |resource| display_events ; END_STATES.include?(resource.stack_status) }
end

#deployed?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/stackup/stack.rb', line 36

def deployed?
  !stack.stack_status.nil?
rescue Aws::CloudFormation::Errors::ValidationError => e
  false
end

#display_eventsObject



28
29
30
31
32
33
34
# File 'lib/stackup/stack.rb', line 28

def display_events
  monitor.new_events.each do |e|
    ts = e.timestamp.localtime.strftime("%H:%M:%S")
    fields = [e.logical_resource_id, e.resource_status, e.resource_status_reason]
    puts("[#{ts}] #{fields.compact.join(' - ')}")
  end
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/stackup/stack.rb', line 42

def valid?
  response = cf.validate_template(template)
  response[:code].nil?
end