Class: Stackup::Stack

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

Overview

An abstraction of a CloudFormation stack.

Constant Summary collapse

ALMOST_DEAD_STATUSES =
%w(CREATE_FAILED ROLLBACK_COMPLETE)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, client = {}, options = {}) ⇒ Stack

Returns a new instance of Stack.



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

def initialize(name, client = {}, options = {})
  client = Aws::CloudFormation::Client.new(client) if client.is_a?(Hash)
  @name = name
  @cf_client = client
  options.each do |key, value|
    public_send("#{key}=", value)
  end
end

Instance Attribute Details

#cf_clientObject (readonly)

Returns the value of attribute cf_client.



21
22
23
# File 'lib/stackup/stack.rb', line 21

def cf_client
  @cf_client
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/stackup/stack.rb', line 21

def name
  @name
end

#watcherObject (readonly)

Returns the value of attribute watcher.



21
22
23
# File 'lib/stackup/stack.rb', line 21

def watcher
  @watcher
end

Instance Method Details

#cancel_updateSymbol

Cancel update in-progress.

Returns:

  • (Symbol)

    ‘:update_cancelled` if successful

Raises:



99
100
101
102
103
104
105
106
107
# File 'lib/stackup/stack.rb', line 99

def cancel_update
  status = modify_stack do
    cf_stack.cancel_update
  end
  fail StackUpdateError, "update cancel failed" unless status =~ /_COMPLETE$/
  :update_cancelled
rescue InvalidStateError
  nil
end

#create_or_update(options) ⇒ Symbol Also known as: up

Create or update the stack.

Parameters:

Returns:

  • (Symbol)

    ‘:created` or `:updated` if successful

Raises:



57
58
59
60
61
62
63
64
# File 'lib/stackup/stack.rb', line 57

def create_or_update(options)
  options = options.dup
  options[:capabilities] ||= ["CAPABILITY_IAM"]
  delete if ALMOST_DEAD_STATUSES.include?(status)
  update(options)
rescue NoSuchStack
  create(options)
end

#deleteSymbol Also known as: down

Delete the stack.

Parameters:

  • template (String)

    template JSON

  • parameters (Array<Hash>)

    template parameters

Returns:

  • (Symbol)

    ‘:deleted` if successful

Raises:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/stackup/stack.rb', line 77

def delete
  begin
    @stack_id = cf_stack.stack_id
  rescue NoSuchStack
    return nil
  end
  status = modify_stack do
    cf_stack.delete
  end
  fail StackUpdateError, "stack delete failed" unless status == "DELETE_COMPLETE"
  :deleted
ensure
  @stack_id = nil
end

#exists?boolean

Returns true iff the stack exists.

Returns:

  • (boolean)

    true iff the stack exists



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

def exists?
  status
  true
rescue NoSuchStack
  false
end

#on_event(event_handler = nil, &block) ⇒ Object

Register a handler for reporting of stack events

Parameters:

  • event_handler (Proc) (defaults to: nil)


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

def on_event(event_handler = nil, &block)
  event_handler ||= block
  fail ArgumentError, "no event_handler provided" if event_handler.nil?
  @event_handler = event_handler
end

#outputsHash<String, String>

Get stack outputs.

Returns:

  • (Hash<String, String>)

    mapping of logical resource-name to physical resource-name

Raises:



115
116
117
118
119
120
121
# File 'lib/stackup/stack.rb', line 115

def outputs
  {}.tap do |h|
    cf_stack.outputs.each do |output|
      h[output.output_key] = output.output_value
    end
  end
end

#statusString

Returns the current stack status.

Returns:

  • (String)

    the current stack status

Raises:



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

def status
  cf_stack.stack_status
end