Class: Awful::AutoScaling
- Inherits:
-
Cli
show all
- Defined in:
- lib/awful/auto_scaling.rb
Constant Summary
collapse
- COLORS =
{
Pending: :yellow,
InService: :green,
Terminating: :red,
Healthy: :green,
Unhealthy: :red,
HEALTHY: :green,
UNHEALTHY: :red,
Successful: :green,
Failed: :red,
Cancelled: :red,
running: :green,
stopped: :yellow,
terminated: :red,
}
Instance Method Summary
collapse
-
#activities(name) ⇒ Object
-
#attach(name, *instance_ids) ⇒ Object
-
#create(file = nil) ⇒ Object
-
#delete(name) ⇒ Object
-
#detach(name, *instance_ids) ⇒ Object
-
#dump(name) ⇒ Object
-
#enter_standby(name, *instance_ids) ⇒ Object
-
#exit_standby(name, *instance_ids) ⇒ Object
-
#instances(*names) ⇒ Object
-
#ips(*names) ⇒ Object
-
#launch_configuration(*names) ⇒ Object
-
#ls(*names) ⇒ Object
-
#old_instances(*names) ⇒ Object
-
#processes ⇒ Object
-
#resume(name, *procs) ⇒ Object
-
#ssh(name, *args) ⇒ Object
-
#stop(name, num = 1) ⇒ Object
-
#suspend(name, *procs) ⇒ Object
-
#terminate(name, num = 1) ⇒ Object
-
#update(name, file = nil) ⇒ Object
-
#wait(*instance_ids) ⇒ Object
Methods inherited from Cli
#initialize, #ll, #version
Constructor Details
This class inherits a constructor from Awful::Cli
Instance Method Details
#activities(name) ⇒ Object
380
381
382
383
384
385
386
387
388
389
390
|
# File 'lib/awful/auto_scaling.rb', line 380
def activities(name)
autoscaling.describe_scaling_activities(auto_scaling_group_name: name).activities.output do |activities|
if options[:long]
print_table activities.map { |a| [color(a.status_code), a.description, a.start_time, a.end_time] }
elsif options[:cause]
print_table activities.map { |a| [color(a.status_code), a.description, a.start_time, a.end_time, "\n#{a.cause}\n\n"] }
else
puts activities.map(&:activity_id)
end
end
end
|
#attach(name, *instance_ids) ⇒ Object
303
304
305
|
# File 'lib/awful/auto_scaling.rb', line 303
def attach(name, *instance_ids)
autoscaling.attach_instances(auto_scaling_group_name: name, instance_ids: instance_ids)
end
|
#create(file = nil) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/awful/auto_scaling.rb', line 167
def create(file = nil)
opt = load_cfg(options, file)
whitelist = %i[auto_scaling_group_name launch_configuration_name instance_id min_size max_size desired_capacity default_cooldown availability_zones
load_balancer_names health_check_type health_check_grace_period placement_group vpc_zone_identifier termination_policies tags ]
opt = remove_empty_strings(opt)
opt = only_keys_matching(opt, whitelist)
opt[:tags] = opt.has_key?(:tags) ? opt[:tags].map { |tag| only_keys_matching(tag, %i[key value propagate_at_launch]) } : []
autoscaling.create_auto_scaling_group(opt)
end
|
#delete(name) ⇒ Object
78
79
80
81
82
|
# File 'lib/awful/auto_scaling.rb', line 78
def delete(name)
if yes? "Really delete auto-scaling group #{name}?", :yellow
autoscaling.delete_auto_scaling_group(auto_scaling_group_name: name)
end
end
|
#detach(name, *instance_ids) ⇒ Object
309
310
311
|
# File 'lib/awful/auto_scaling.rb', line 309
def detach(name, *instance_ids)
autoscaling.detach_instances(auto_scaling_group_name: name, instance_ids: instance_ids, should_decrement_desired_capacity: options[:decrement])
end
|
#dump(name) ⇒ Object
153
154
155
156
157
158
159
|
# File 'lib/awful/auto_scaling.rb', line 153
def dump(name)
all_matching_asgs(name).map(&:to_hash).output do |asgs|
asgs.each do |asg|
puts YAML.dump(stringify_keys(asg)) unless options[:quiet]
end
end
end
|
#enter_standby(name, *instance_ids) ⇒ Object
404
405
406
407
408
409
410
|
# File 'lib/awful/auto_scaling.rb', line 404
def enter_standby(name, *instance_ids)
autoscaling.enter_standby(
auto_scaling_group_name: name,
instance_ids: instance_ids,
should_decrement_desired_capacity: options[:decrement]
)
end
|
#exit_standby(name, *instance_ids) ⇒ Object
413
414
415
|
# File 'lib/awful/auto_scaling.rb', line 413
def exit_standby(name, *instance_ids)
autoscaling.exit_standby(auto_scaling_group_name: name, instance_ids: instance_ids)
end
|
#instances(*names) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/awful/auto_scaling.rb', line 87
def instances(*names)
instances = asg_instances(*names)
if options[:describe]
instances = autoscaling.describe_auto_scaling_instances(instance_ids: instances.map(&:instance_id)).auto_scaling_instances
end
instances.output do |list|
if options[:long]
print_table list.map { |i|
[
i.instance_id,
options[:describe] ? i.auto_scaling_group_name : nil,
i.availability_zone,
color(i.lifecycle_state),
color(i.health_status),
i.launch_configuration_name,
].compact
}
else
puts list.map(&:instance_id)
end
end
end
|
#ips(*names) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/awful/auto_scaling.rb', line 115
def ips(*names)
asg_instance_details(*names).output do |instances|
if options[:long]
print_table instances.map { |i|
[ i.public_ip_address, i.private_ip_address, i.instance_id, i.image_id, i.instance_type, i.placement.availability_zone, color(i.state.name), i.launch_time ]
}
else
puts instances.map(&:public_ip_address)
end
end
end
|
#launch_configuration(*names) ⇒ Object
315
316
317
318
319
320
321
322
323
324
325
326
327
|
# File 'lib/awful/auto_scaling.rb', line 315
def launch_configuration(*names)
autoscaling.describe_auto_scaling_groups(
auto_scaling_group_names: names
).map(&:auto_scaling_groups).flatten.each_with_object({}) do |asg, h|
h[asg.auto_scaling_group_name] = asg.launch_configuration_name
end.output do |hash|
if options[:long]
print_table hash
else
puts hash.values
end
end
end
|
#ls(*names) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/awful/auto_scaling.rb', line 57
def ls(*names)
autoscaling.describe_auto_scaling_groups(auto_scaling_group_names: names).auto_scaling_groups.output do |asgs|
if options[:long]
print_table asgs.map { |a|
[
tag_name(a, '-')[0,40],
a.auto_scaling_group_name[0,40],
a.launch_configuration_name[0,40],
"#{a.instances.length}/#{a.desired_capacity}",
"#{a.min_size}-#{a.max_size}",
a.availability_zones.map{ |az| az[-1,1] }.sort.join(','),
a.created_time
]
}
else
puts asgs.map(&:auto_scaling_group_name)
end
end
end
|
#old_instances(*names) ⇒ Object
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
# File 'lib/awful/auto_scaling.rb', line 336
def old_instances(*names)
asgs = autoscaling.describe_auto_scaling_groups(auto_scaling_group_names: names).map(&:auto_scaling_groups).flatten
olds = asgs.each_with_object({}) do |asg, hash|
outdated = asg.instances.select do |instance|
instance.launch_configuration_name != asg.launch_configuration_name
end
hash[asg.auto_scaling_group_name] = outdated unless outdated.empty?
end
if olds.empty?
elsif options[:detach]
autoscaling.detach_instances(auto_scaling_group_name: name, instance_ids: olds.values.flatten.map(&:instance_id), should_decrement_desired_capacity: options[:decrement])
elsif options[:deregister]
asgs.select do |asg|
olds.has_key?(asg.auto_scaling_group_name)
end.each do |asg|
instance_ids = olds[asg.auto_scaling_group_name].flatten.map(&:instance_id)
asg.load_balancer_names.each do |elb_name|
say "Deregistering #{instance_ids.join(',')} from ELB #{elb_name}", :yellow
elb.deregister_instances_from_load_balancer(load_balancer_name: elb_name, instances: instance_ids.map { |id| {instance_id: id} })
end
end
elsif options[:terminate]
olds.values.flatten.map do |instance|
autoscaling.terminate_instance_in_auto_scaling_group(instance_id: instance.instance_id, should_decrement_desired_capacity: options[:decrement] && true)
instance.instance_id
end.output { |ids| say("Terminated: #{ids.join(',')}", :yellow) }
elsif options[:groups]
puts olds.keys
elsif options[:long]
print_table olds.map { |asg, ins| ins.map { |i| [i.instance_id, asg, i.launch_configuration_name] }.flatten }
else
puts olds.values.flatten.map(&:instance_id)
end
olds
end
|
#processes ⇒ Object
271
272
273
274
275
|
# File 'lib/awful/auto_scaling.rb', line 271
def processes
autoscaling.describe_scaling_process_types.processes.map(&:process_name).sort.output do |procs|
puts procs
end
end
|
#resume(name, *procs) ⇒ Object
294
295
296
297
298
299
300
|
# File 'lib/awful/auto_scaling.rb', line 294
def resume(name, *procs)
if procs.empty?
autoscaling.resume_processes(auto_scaling_group_name: name)
else
autoscaling.resume_processes(auto_scaling_group_name: name, scaling_processes: procs)
end
end
|
#ssh(name, *args) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/awful/auto_scaling.rb', line 133
def ssh(name, *args)
instances = asg_instance_details(name)
if options[:instances]
instances.select! do |i|
options[:instances].map{ |pattern| i.instance_id.match(pattern) }.any?
end
end
ips = instances.map(&:public_ip_address)
num = options[:all] ? ips.count : options[:number].to_i
login_name = options[:login_name] ? "-l #{options[:login_name]}" : ''
ips.last(num).each do |ip|
puts ip if options[:verbose]
system "ssh #{login_name} #{ip} #{Array(args).join(' ')}"
end
end
|
#stop(name, num = 1) ⇒ Object
261
262
263
264
265
266
267
268
|
# File 'lib/awful/auto_scaling.rb', line 261
def stop(name, num = 1)
ins = options[:newest] ? newest(name) : oldest(name)
ins.first(num.to_i).map(&:instance_id).output do |ids|
if yes? "Really stop #{num} instances: #{ids.join(',')}?", :yellow
ec2.stop_instances(instance_ids: ids)
end
end
end
|
#suspend(name, *procs) ⇒ Object
279
280
281
282
283
284
285
286
287
288
289
290
291
|
# File 'lib/awful/auto_scaling.rb', line 279
def suspend(name, *procs)
if options[:list]
autoscaling.describe_auto_scaling_groups(
auto_scaling_group_names: Array(name)
).map(&:auto_scaling_groups).flatten.first.suspended_processes.output do |list|
print_table list.map{ |proc| [ proc.process_name, proc.suspension_reason] }
end
elsif procs.empty?
autoscaling.suspend_processes(auto_scaling_group_name: name)
else
autoscaling.suspend_processes(auto_scaling_group_name: name, scaling_processes: procs)
end
end
|
#terminate(name, num = 1) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/awful/auto_scaling.rb', line 240
def terminate(name, num = 1)
ins = options[:newest] ? newest(name) : oldest(name)
num = ins.length if options[:all]
if ins.empty?
say 'No instances to terminate.', :green
else
ids = ins.first(num.to_i).map(&:instance_id)
if yes? "Really terminate #{num} instances: #{ids.join(',')}?", :yellow
ids.each do |id|
puts "Terminating instance: #{id}"
autoscaling.terminate_instance_in_auto_scaling_group(instance_id: id, should_decrement_desired_capacity: options[:decrement] && true)
end
else
puts 'Nothing terminated'
end
end
end
|
#update(name, file = nil) ⇒ Object
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
|
# File 'lib/awful/auto_scaling.rb', line 186
def update(name, file = nil)
opt = load_cfg(options, file)
asgs = all_matching_asgs(name)
if asgs.length < 1
warn "no match for #{name}"
return
elsif asgs.length > 1
warn "ambiguous match for #{name}:", asgs.map(&:auto_scaling_group_name)
return
end
opt[:auto_scaling_group_name] = asgs.first.auto_scaling_group_name
opt = remove_empty_strings(opt)
whitelist = %i[auto_scaling_group_name launch_configuration_name min_size max_size desired_capacity default_cooldown availability_zones
health_check_type health_check_grace_period placement_group vpc_zone_identifier termination_policies ]
autoscaling.update_auto_scaling_group(only_keys_matching(opt, whitelist))
if opt[:tags]
tags = opt[:tags].map { |tag| tag.merge(resource_id: name, resource_type: 'auto-scaling-group') }
autoscaling.create_or_update_tags(tags: tags)
end
end
|
#wait(*instance_ids) ⇒ Object
395
396
397
398
399
400
|
# File 'lib/awful/auto_scaling.rb', line 395
def wait(*instance_ids)
until instance_lifecycle_state(*instance_ids).all?{ |s| s == options[:state] }
puts "waiting for #{instance_ids.count} instances to enter state #{options[:state]}" unless options[:quiet]
sleep options[:period]
end
end
|