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_COMPLETE", "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) ⇒ Stack

Returns a new instance of Stack.



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

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

Instance Attribute Details

#cfObject (readonly)

Returns the value of attribute cf.



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

def cf
  @cf
end

#monitorObject (readonly)

Returns the value of attribute monitor.



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

def monitor
  @monitor
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#stackObject (readonly)

Returns the value of attribute stack.



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

def stack
  @stack
end

Instance Method Details

#create(template) ⇒ Object



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

def create(template)
  response = cf.create_stack(:stack_name => name,
                             :template_body => template,
                             :disable_rollback => true)
  wait_till_end
  !response[:stack_id].nil?
end

#deleteObject



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

def delete
  response = cf.delete_stack(:stack_name => name)
  stack.wait_until(:max_attempts => 1000, :delay => 10) { |resource| display_events; END_STATES.include?(resource.stack_status) }
rescue Aws::CloudFormation::Errors::ValidationError
  puts "Stack does not exist."
end

#deploy(template) ⇒ Object



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

def deploy(template)
  if deployed?
    update(template)
  else
    create(template)
  end
end

#deployed?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/stackup/stack.rb', line 56

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

#display_eventsObject



48
49
50
51
52
53
54
# File 'lib/stackup/stack.rb', line 48

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

#update(template) ⇒ Object



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

def update(template)
  return false unless deployed?
  response = cf.update_stack(:stack_name => name, :template_body => template)
  wait_till_end
  !response[:stack_id].nil?
end

#valid?(template) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/stackup/stack.rb', line 62

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