Class: CloudFormationTool::CloudFormation::Stack
- Inherits:
-
Object
- Object
- CloudFormationTool::CloudFormation::Stack
show all
- Includes:
- CloudFormationTool, Storable, Enumerable
- Defined in:
- lib/cloud_formation_tool/cloud_formation/stack.rb
Constant Summary
VERSION
Instance Attribute Summary collapse
Instance Method Summary
collapse
#aws_config, #awsas, #awscdn, #awscf, #awscreds, #awsec2, #awsecs, #awss3, #cf_bucket_name, #find_profile, #profile, #s3_bucket_name
Methods included from Storable
#cached_object, #make_filename, #prefix, #upload
Constructor Details
#initialize(name) ⇒ Stack
Returns a new instance of Stack.
14
15
16
17
18
19
20
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 14
def initialize(name)
@name = name
@seenev = Set.new
@watch_timeouts = 0
@nested_stacks = Hash[]
@region = nil
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
12
13
14
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 12
def name
@name
end
|
Instance Method Details
#asgroups ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 167
def asgroups
output = []
resources do |res|
output << res if res.resource_type == 'AWS::AutoScaling::AutoScalingGroup'
end
output.collect do |res|
res.extend(CloudFormationTool)
res.instance_eval do
def group
Aws::AutoScaling::AutoScalingGroup.new(self.physical_resource_id, client: awsas).reload
end
end
res
end
end
|
#cdns ⇒ Object
221
222
223
224
225
226
227
228
229
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 221
def cdns
output = []
resources do |res|
output << res if res.resource_type == 'AWS::CloudFront::Distribution'
end
output.collect do |res|
res.extend(CloudFrontDistribution)
end
end
|
#check_nested_stack(ev) ⇒ Object
281
282
283
284
285
286
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 281
def check_nested_stack(ev)
return unless ev.resource_type == "AWS::CloudFormation::Stack" and
ev.logical_resource_id != self.name return if @nested_stacks.has_key? ev.logical_resource_id @nested_stacks[nested_stack_name(ev)] = ev.logical_resource_id
end
|
#create(template, params = {}) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 66
def create(template, params = {})
@template = CloudFormation.parse(template).to_yaml(params)
url = upload(make_filename('yaml'), @template, gzip: false)
return update(url, template, params) if exist?
log "Creating stack '#{name}' from '#{template}' params #{params.inspect}"
valid_check do
resp = awscf.create_stack({
stack_name: @name,
template_url: url,
capabilities: %w(CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND),
on_failure: "DO_NOTHING", parameters: params.collect do |k,v|
{
parameter_key: k.to_s,
parameter_value: v.to_s,
use_previous_value: false,
}
end
})
@stack_id = resp.stack_id
end
end
|
#delete ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 30
def delete
begin
awscf.delete_stack stack_name: @name
rescue Aws::Errors::MissingCredentialsError => e
raise CloudFormationTool::Errors::AuthError, e.message
end
end
|
#each ⇒ Object
298
299
300
301
302
303
304
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 298
def each
tracked_stacks.each do |name|
events_for(name) do |ev|
yield ev
end
end
end
|
#events_for(stack_name) ⇒ Object
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 306
def events_for(stack_name)
token = nil
sleep(if @_last_poll_time.nil?
0
else
diff = Time.now - @_last_poll_time
if diff < 1
diff
else
0
end
end)
begin
resp = awscf.describe_stack_events stack_name: stack_name, next_token: token
@watch_timeouts = 0
resp.stack_events.each do |ev|
yield ev
end
rescue Aws::CloudFormation::Errors::Throttling => e
sleep 1
retry
rescue Seahorse::Client::NetworkingError => e if (@watch_timeouts += 1) > 5
raise CloudFormationTool::Errors::AppError, "Too many timeouts!"
else
retry
end
rescue Aws::CloudFormation::Errors::ValidationError => e
if e.message =~ /does not exist/
if stack_name == self.name
raise CloudFormationTool::Errors::StackDoesNotExistError, "Stack does not exist"
end
else
raise e
end
end
end
|
#exist? ⇒ Boolean
38
39
40
41
42
43
44
45
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 38
def exist?
begin
awscf.describe_stacks stack_name: name
true
rescue Aws::CloudFormation::Errors::ValidationError => e
false
end
end
|
#fargate_services ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 183
def fargate_services
output = []
resources do |res|
output << res if res.resource_type == 'AWS::ECS::Service'
end
output.collect do |res|
res.extend(CloudFormationTool)
res.instance_eval do
def arn
physical_resource_id
end
def cluster_name
self.arn.split(/:/).last.split('/')[1]
end
def service_name
self.arn.split(/:/).last.split('/')[2]
end
def service
svc = awsecs.describe_services(cluster: cluster_name, services: [ service_name ], include: [ 'TAGS' ]).services.first
svc.instance_eval do
def client= ecsclient
@client = ecsclient
end
def client
@client
end
end
svc.client = awsecs
svc
end
def task_definition
awsecs.describe_task_definition(task_definition: service.task_definition).task_definition
end
end
res
end
end
|
261
262
263
264
265
266
267
268
269
270
271
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 261
def format_resource(res, columns)
columns.collect { |field|
(name,size) = field.split(":")
size ||= 1
(if name == 'logical_resource_id' and res.respond_to?(:stack_name) and res.stack_name != self.name
logical_nested_stack_name(res.stack_name) + "|"
else
''
end + res.send(name.to_sym)).ljust(size.to_i, ' ')
}
end
|
#is_final_event(ev) ⇒ Object
288
289
290
291
292
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 288
def is_final_event(ev)
ev.resource_type == "AWS::CloudFormation::Stack" and
ev.resource_status =~ /(_COMPLETE|_FAILED)$/ and
ev.logical_resource_id == self.name
end
|
#logical_nested_stack_name(phys_name) ⇒ Object
273
274
275
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 273
def logical_nested_stack_name(phys_name)
@nested_stacks[phys_name] || 'unknown'
end
|
#monitor(start_time = nil) ⇒ Object
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 235
def monitor(start_time = nil)
@nested_stacks = Hash[]
done = false
begin
until done
reverse_each do |ev|
next if @seenev.add?(ev.event_id).nil?
text = "#{ev.timestamp.strftime "%Y-%m-%d %H:%M:%S"}| " + format_resource(ev, %w(
resource_type:40
logical_resource_id:42
resource_status
)).join(" ")
text += " " + ev.resource_status_reason if ev.resource_status =~ /_FAILED/
if start_time.nil? or start_time < ev.timestamp
puts text
end
check_nested_stack(ev)
done = is_final_event(ev)
end
sleep 1
end
rescue CloudFormationTool::Errors::StackDoesNotExistError => e
puts "Stack #{name} does not exist"
end
end
|
#nested_stack_name(ev) ⇒ Object
277
278
279
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 277
def nested_stack_name(ev)
ev.physical_resource_id.split('/')[1]
end
|
#output ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 134
def output
begin
key_width = 0
resp = awscf.list_exports
resp.exports.select do |exp|
exp.exporting_stack_id == stack_id
end.each do |exp|
key_width = [ key_width, exp.name.length ].max
end.collect do |exp|
"%-#{key_width}s: %s" % [ exp.name, exp.value ]
end
rescue Aws::CloudFormation::Errors::ValidationError => e
raise CloudFormationTool::Errors::AppError, "Failed to get resources: #{e.message}"
end
end
|
#preview_changes(template, params = {}) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 89
def preview_changes(template, params = {})
@template = CloudFormation.parse(template).to_yaml(params)
url = upload(make_filename('yaml'), @template, gzip: false)
log "Previewing stack changes for '#{name}' from '#{template}' params #{params.inspect}"
valid_check do
resp = awscf.create_change_set({
stack_name: @name,
template_url: url,
capabilities: %w(CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND),
on_stack_failure: "DO_NOTHING", parameters: params.collect do |k,v|
{
parameter_key: k.to_s,
parameter_value: v.to_s,
use_previous_value: false,
}
end,
change_set_name: "preview-#{@name}-dryrun"
})
change_set_id = resp.id
loop do
resp = awscf.describe_change_set({change_set_name: change_set_id })
break unless %w( CREATE_PENDING CREATE_IN_PROGRESS ).include? resp.status
end
puts "Update preview for '#{name}':"
puts "Status: #{resp.execution_status} | #{resp.status} | #{resp.status_reason}"
puts "---"
puts "Resource Changes:"
resp.changes.each do |change|
puts format_resource(change.resource_change, %w(
resource_type:40
logical_resource_id:42
action
)).join(" ") + " => " + change.resource_change.details.collect { |d| "#{d.evaluation}:#{d.target}" }.join(", ")
end
awscf.delete_change_set({ change_set_name: change_set_id })
end
end
|
#region ⇒ Object
26
27
28
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 26
def region
@region || super
end
|
#region=(region) ⇒ Object
22
23
24
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 22
def region=(region)
$__region = @region = region
end
|
#resources ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 150
def resources
begin
awscf.list_stack_resources(stack_name: @name).each do |resp|
resp.stack_resource_summaries.each do |res|
yield res
if res.resource_type == 'AWS::CloudFormation::Stack'
Stack.new(res.physical_resource_id).resources do |nested_res|
yield nested_res
end
end
end
end
rescue Aws::CloudFormation::Errors::ValidationError => e
raise CloudFormationTool::Errors::AppError, "Failed to get resources: #{e.message}"
end
end
|
#see_events ⇒ Object
231
232
233
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 231
def see_events
each { |e| @seenev << e.event_id }
end
|
#stack_id ⇒ Object
130
131
132
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 130
def stack_id
@stack_id ||= awscf.describe_stacks(stack_name: @name).stacks.first.stack_id
end
|
#tracked_stacks ⇒ Object
294
295
296
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 294
def tracked_stacks
[ self.name ] + @nested_stacks.keys.compact
end
|
#update(url, filepath, params = {}) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/cloud_formation_tool/cloud_formation/stack.rb', line 47
def update(url, filepath, params = {})
log "Updating existing stack '#{name}' from '#{filepath}' params #{params.inspect}"
valid_check do
resp = awscf.update_stack({
stack_name: @name,
template_url: url,
capabilities: %w(CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND),
parameters: params.collect do |k,v|
{
parameter_key: k.to_s,
parameter_value: v.to_s,
use_previous_value: false,
}
end
})
resp.stack_id
end
end
|