Class: Stackup::Stack
- Inherits:
-
Object
- Object
- Stackup::Stack
- Includes:
- ErrorHandling
- 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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#cancel_update ⇒ Symbol
Cancel update in-progress.
-
#create_or_update(options) ⇒ Symbol
(also: #up)
Create or update the stack.
-
#delete ⇒ Symbol
(also: #down)
Delete the stack.
-
#exists? ⇒ boolean
True iff the stack exists.
-
#initialize(name, client = {}, options = {}) ⇒ Stack
constructor
A new instance of Stack.
-
#on_event(event_handler = nil, &block) ⇒ Object
Register a handler for reporting of stack events.
-
#outputs ⇒ Hash<String, String>
Get stack outputs.
-
#parameters ⇒ Hash
Get the current parameters.
-
#resources ⇒ Hash<String, String>
Get stack outputs.
-
#status ⇒ String
The current stack status.
-
#template ⇒ Hash
Get the current template.
-
#wait ⇒ String
Wait until stack reaches a stable state.
- #watch(zero = true) {|watcher| ... } ⇒ Object
Methods included from ErrorHandling
Constructor Details
#initialize(name, client = {}, options = {}) ⇒ Stack
Returns a new instance of Stack.
13 14 15 16 17 18 19 20 |
# File 'lib/stackup/stack.rb', line 13 def initialize(name, client = {}, = {}) client = Aws::CloudFormation::Client.new(client) if client.is_a?(Hash) @name = name @cf_client = client .each do |key, value| public_send("#{key}=", value) end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
22 23 24 |
# File 'lib/stackup/stack.rb', line 22 def name @name end |
Instance Method Details
#cancel_update ⇒ Symbol
Cancel update in-progress.
153 154 155 156 157 158 159 160 161 |
# File 'lib/stackup/stack.rb', line 153 def cancel_update status = modify_stack do cf_stack.cancel_update end fail StackUpdateError, "update cancel failed" unless status =~ /_COMPLETE$/ status rescue InvalidStateError nil end |
#create_or_update(options) ⇒ Symbol Also known as: up
Create or update the stack.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/stackup/stack.rb', line 97 def create_or_update() = .dup if (template_data = .delete(:template)) [:template_body] = MultiJson.dump(template_data) end if (parameters = [:parameters]) [:parameters] = normalise_parameters(parameters) end if (policy_data = .delete(:stack_policy)) [:stack_policy_body] = MultiJson.dump(policy_data) end if (policy_data = .delete(:stack_policy_during_update)) [:stack_policy_during_update_body] = MultiJson.dump(policy_data) end [:capabilities] ||= ["CAPABILITY_IAM"] delete if ALMOST_DEAD_STATUSES.include?(status) update() rescue NoSuchStack create() end |
#delete ⇒ Symbol Also known as: down
Delete the stack.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/stackup/stack.rb', line 129 def delete begin @stack_id = handling_validation_error do cf_stack.stack_id end rescue NoSuchStack return nil end status = modify_stack do cf_stack.delete end fail StackUpdateError, "stack delete failed" unless status == "DELETE_COMPLETE" status ensure @stack_id = nil end |
#exists? ⇒ boolean
Returns true iff the stack exists.
46 47 48 49 50 51 |
# File 'lib/stackup/stack.rb', line 46 def exists? status true rescue NoSuchStack false end |
#on_event(event_handler = nil, &block) ⇒ Object
Register a handler for reporting of stack events.
27 28 29 30 31 |
# File 'lib/stackup/stack.rb', line 27 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 |
#outputs ⇒ Hash<String, String>
Get stack outputs.
205 206 207 208 209 210 211 212 213 |
# File 'lib/stackup/stack.rb', line 205 def outputs handling_validation_error do {}.tap do |h| cf_stack.outputs.each do |o| h[o.output_key] = o.output_value end end end end |
#parameters ⇒ Hash
Get the current parameters.
190 191 192 193 194 195 196 197 198 |
# File 'lib/stackup/stack.rb', line 190 def parameters handling_validation_error do {}.tap do |h| cf_stack.parameters.each do |p| h[p.parameter_key] = p.parameter_value end end end end |
#resources ⇒ Hash<String, String>
Get stack outputs.
221 222 223 224 225 226 227 228 229 |
# File 'lib/stackup/stack.rb', line 221 def resources handling_validation_error do {}.tap do |h| cf_stack.resource_summaries.each do |r| h[r.logical_resource_id] = r.physical_resource_id end end end end |
#status ⇒ String
Returns the current stack status.
38 39 40 41 42 |
# File 'lib/stackup/stack.rb', line 38 def status handling_validation_error do cf_stack.stack_status end end |
#template ⇒ Hash
Get the current template.
178 179 180 181 182 183 |
# File 'lib/stackup/stack.rb', line 178 def template handling_validation_error do template_json = cf_client.get_template(:stack_name => name).template_body MultiJson.load(template_json) end end |
#wait ⇒ String
Wait until stack reaches a stable state
167 168 169 170 171 |
# File 'lib/stackup/stack.rb', line 167 def wait modify_stack do # nothing end end |
#watch(zero = true) {|watcher| ... } ⇒ Object
231 232 233 234 235 |
# File 'lib/stackup/stack.rb', line 231 def watch(zero = true) watcher = Stackup::StackWatcher.new(cf_stack) watcher.zero if zero yield watcher end |