Class: Stackup::Stack

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

Defined Under Namespace

Classes: UpdateError

Constant Summary collapse

SUCESS_STATES =
["CREATE_COMPLETE", "DELETE_COMPLETE", "UPDATE_COMPLETE"]
FAILURE_STATES =
["CREATE_FAILED", "DELETE_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
26
# File 'lib/stackup/stack.rb', line 18

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

#deleteObject



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

def delete
  return false unless deployed?
  cf.delete_stack(:stack_name => name)
  status = wait_for_events
  fail UpdateError, "stack delete failed" unless status == "DELETE_COMPLETE"
  true
rescue Aws::CloudFormation::Errors::ValidationError
  puts "Stack does not exist."
end

#deploy(template, parameters = []) ⇒ Object



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

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)


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

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

#outputsObject



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

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

#update(template, parameters) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stackup/stack.rb', line 28

def update(template, parameters)
  return false unless deployed?
  if stack.stack_status == "CREATE_FAILED"
    puts "Stack is in CREATE_FAILED state so must be manually deleted before it can be updated"
    return false
  end
  if stack.stack_status == "ROLLBACK_COMPLETE"
    deleted = delete
    return false if !deleted
  end
  response = cf.update_stack(:stack_name => name, :template_body => template, :parameters => parameters, :capabilities => ["CAPABILITY_IAM"])
  wait_for_events
  !response[:stack_id].nil?
end

#valid?(template) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/stackup/stack.rb', line 73

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