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
- DEFAULT_WAIT_POLL_INTERVAL =
seconds
5- ALMOST_DEAD_STATUSES =
%w[CREATE_FAILED ROLLBACK_COMPLETE].freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#wait ⇒ String
Wait until stack reaches a stable state.
-
#wait_poll_interval ⇒ Object
Returns the value of attribute wait_poll_interval.
Instance Method Summary collapse
-
#cancel_update ⇒ String
Cancel update in-progress.
-
#change_set(name) ⇒ ChangeSet
An abstraction of a CloudFormation change-set.
-
#change_set_summaries ⇒ Array<ChangeSetSummary>
List change-sets.
-
#create_or_update(options) ⇒ String
(also: #up)
Create or update the stack.
-
#delete ⇒ String
(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.
-
#tags ⇒ Hash
Get the current tags.
-
#template ⇒ Hash
Get the current template.
-
#template_body ⇒ String
Get the current template body.
- #wait? ⇒ Boolean
- #watch(zero = true) {|watcher| ... } ⇒ Object
Methods included from ErrorHandling
Constructor Details
#initialize(name, client = {}, options = {}) ⇒ Stack
Returns a new instance of Stack.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/stackup/stack.rb', line 20 def initialize(name, client = {}, = {}) client = Aws::CloudFormation::Client.new(client) if client.is_a?(Hash) @name = name @cf_client = client @wait = true .each do |key, value| public_send("#{key}=", value) end @wait_poll_interval ||= DEFAULT_WAIT_POLL_INTERVAL end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
31 32 33 |
# File 'lib/stackup/stack.rb', line 31 def name @name end |
#wait ⇒ String
Wait until stack reaches a stable state
184 185 186 187 188 |
# File 'lib/stackup/stack.rb', line 184 def wait modify_stack(/./, "failed to stabilize") do # nothing end end |
#wait_poll_interval ⇒ Object
Returns the value of attribute wait_poll_interval.
32 33 34 |
# File 'lib/stackup/stack.rb', line 32 def wait_poll_interval @wait_poll_interval end |
Instance Method Details
#cancel_update ⇒ String
Cancel update in-progress.
172 173 174 175 176 177 178 |
# File 'lib/stackup/stack.rb', line 172 def cancel_update modify_stack(/_COMPLETE$/, "update cancel failed") do cf_stack.cancel_update end rescue InvalidStateError nil end |
#change_set(name) ⇒ ChangeSet
An abstraction of a CloudFormation change-set.
263 264 265 |
# File 'lib/stackup/stack.rb', line 263 def change_set(name) ChangeSet.new(name, self) end |
#change_set_summaries ⇒ Array<ChangeSetSummary>
List change-sets.
252 253 254 255 256 |
# File 'lib/stackup/stack.rb', line 252 def change_set_summaries handling_cf_errors do cf_client.list_change_sets(:stack_name => name).summaries end end |
#create_or_update(options) ⇒ String Also known as: up
Create or update the stack.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/stackup/stack.rb', line 117 def create_or_update() = .dup if (template_data = .delete(:template)) [:template_body] = MultiJson.dump(template_data) end if (parameters = [:parameters]) [:parameters] = Parameters.new(parameters).to_a end if ( = [:tags]) [:tags] = () 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_NAMED_IAM"] delete if ALMOST_DEAD_STATUSES.include?(status) update() rescue NoSuchStack create() end |
#delete ⇒ String Also known as: down
Delete the stack.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/stackup/stack.rb', line 150 def delete begin @stack_id = handling_cf_errors do cf_stack.stack_id end rescue NoSuchStack return nil end modify_stack("DELETE_COMPLETE", "stack delete failed") do cf_stack.delete end ensure @stack_id = nil end |
#exists? ⇒ boolean
Returns true iff the stack exists.
63 64 65 66 67 68 |
# File 'lib/stackup/stack.rb', line 63 def exists? status true rescue NoSuchStack false end |
#on_event(event_handler = nil, &block) ⇒ Object
Register a handler for reporting of stack events.
43 44 45 46 47 48 |
# File 'lib/stackup/stack.rb', line 43 def on_event(event_handler = nil, &block) event_handler ||= block raise ArgumentError, "no event_handler provided" if event_handler.nil? @event_handler = event_handler end |
#outputs ⇒ Hash<String, String>
Get stack outputs.
233 234 235 |
# File 'lib/stackup/stack.rb', line 233 def outputs extract_hash(:outputs, :output_key, :output_value) end |
#parameters ⇒ Hash
Get the current parameters.
215 216 217 |
# File 'lib/stackup/stack.rb', line 215 def parameters extract_hash(:parameters, :parameter_key, :parameter_value) end |
#resources ⇒ Hash<String, String>
Get stack outputs.
243 244 245 |
# File 'lib/stackup/stack.rb', line 243 def resources extract_hash(:resource_summaries, :logical_resource_id, :physical_resource_id) end |
#status ⇒ String
Returns the current stack status.
55 56 57 58 59 |
# File 'lib/stackup/stack.rb', line 55 def status handling_cf_errors do cf_stack.stack_status end end |
#tags ⇒ Hash
Get the current tags.
224 225 226 |
# File 'lib/stackup/stack.rb', line 224 def extract_hash(:tags, :key, :value) end |
#template ⇒ Hash
Get the current template.
206 207 208 |
# File 'lib/stackup/stack.rb', line 206 def template Stackup::YAML.load(template_body) end |
#template_body ⇒ String
Get the current template body.
195 196 197 198 199 |
# File 'lib/stackup/stack.rb', line 195 def template_body handling_cf_errors do cf_client.get_template(:stack_name => name).template_body end end |
#wait? ⇒ Boolean
36 37 38 |
# File 'lib/stackup/stack.rb', line 36 def wait? @wait end |
#watch(zero = true) {|watcher| ... } ⇒ Object
267 268 269 270 271 |
# File 'lib/stackup/stack.rb', line 267 def watch(zero = true) watcher = Stackup::StackWatcher.new(cf_stack) watcher.zero if zero yield watcher end |