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, parameters) ⇒ Object



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

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

#deleteObject



44
45
46
47
48
49
# File 'lib/stackup/stack.rb', line 44

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, parameters = []) ⇒ Object



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

def deploy(template, parameters = [])
  if deployed?
    update(template, parameters)
  else
    create(template, parameters)
  end
rescue Aws::CloudFormation::Errors::ValidationError => e
  puts e.message
end

#deployed?Boolean

Returns:

  • (Boolean)


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

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

#display_eventsObject



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

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

#outputsObject



51
52
53
# File 'lib/stackup/stack.rb', line 51

def outputs
  puts stack.outputs.flat_map { |output| "#{output.output_key} - #{output.output_value}" }
end

#update(template, parameters) ⇒ Object



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

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

#valid?(template) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/stackup/stack.rb', line 69

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