Class: Awful::AutoScaling
Constant Summary collapse
- COLORS =
{ ## lifecycle states Pending: :yellow, InService: :green, Terminating: :red, ## health statuses Healthy: :green, Unhealthy: :red, ## same for asg instances describe HEALTHY: :green, UNHEALTHY: :red, ## activity status Successful: :green, Failed: :red, Cancelled: :red, ## instance state 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(name) ⇒ 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
Constructor Details
This class inherits a constructor from Awful::Cli
Instance Method Details
#activities(name) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/awful/auto_scaling.rb', line 364 def activities(name) autoscaling.describe_scaling_activities(auto_scaling_group_name: name).activities.output do |activities| if [:long] print_table activities.map { |a| [color(a.status_code), a.description, a.start_time, a.end_time] } elsif [: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
287 288 289 |
# File 'lib/awful/auto_scaling.rb', line 287 def attach(name, *instance_ids) autoscaling.attach_instances(auto_scaling_group_name: name, instance_ids: instance_ids) end |
#create(file = nil) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/awful/auto_scaling.rb', line 151 def create(file = nil) opt = load_cfg(, 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) ## scrub aws-provided keys from tags 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
69 70 71 72 73 |
# File 'lib/awful/auto_scaling.rb', line 69 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
293 294 295 |
# File 'lib/awful/auto_scaling.rb', line 293 def detach(name, *instance_ids) autoscaling.detach_instances(auto_scaling_group_name: name, instance_ids: instance_ids, should_decrement_desired_capacity: [:decrement]) end |
#dump(name) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/awful/auto_scaling.rb', line 137 def dump(name) all_matching_asgs(name).map(&:to_hash).output do |asgs| asgs.each do |asg| puts YAML.dump(stringify_keys(asg)) unless [:quiet] end end end |
#enter_standby(name, *instance_ids) ⇒ Object
388 389 390 391 392 393 394 |
# File 'lib/awful/auto_scaling.rb', line 388 def enter_standby(name, *instance_ids) autoscaling.enter_standby( auto_scaling_group_name: name, instance_ids: instance_ids, should_decrement_desired_capacity: [:decrement] ) end |
#exit_standby(name, *instance_ids) ⇒ Object
397 398 399 |
# File 'lib/awful/auto_scaling.rb', line 397 def exit_standby(name, *instance_ids) autoscaling.exit_standby(auto_scaling_group_name: name, instance_ids: instance_ids) end |
#instances(*names) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/awful/auto_scaling.rb', line 78 def instances(*names) instances = autoscaling.describe_auto_scaling_groups(auto_scaling_group_names: names).auto_scaling_groups.map(&:instances).flatten ## make extra call to get asg name as part of object if [:describe] instances = autoscaling.describe_auto_scaling_instances(instance_ids: instances.map(&:instance_id)).auto_scaling_instances end instances.output do |list| if [:long] print_table list.map { |i| [ i.instance_id, [: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(name) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/awful/auto_scaling.rb', line 106 def ips(name) ## get instance IDs for matching ASGs ids = all_matching_asgs(name).map(&:instances).flatten.map(&:instance_id) ## get instance details for these IDs ec2.describe_instances(instance_ids: ids).map(&:reservations).flatten.map(&:instances).flatten.sort_by(&:launch_time).output do |instances| if [: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
299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/awful/auto_scaling.rb', line 299 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 [:long] print_table hash else puts hash.values end end end |
#ls(*names) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/awful/auto_scaling.rb', line 48 def ls(*names) autoscaling.describe_auto_scaling_groups(auto_scaling_group_names: names).auto_scaling_groups.output do |asgs| if [: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
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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/awful/auto_scaling.rb', line 320 def old_instances(*names) asgs = autoscaling.describe_auto_scaling_groups(auto_scaling_group_names: names).map(&:auto_scaling_groups).flatten ## get hash of old instances by ASG name 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? # noop elsif [:detach] autoscaling.detach_instances(auto_scaling_group_name: name, instance_ids: olds.values.flatten.map(&:instance_id), should_decrement_desired_capacity: [:decrement]) elsif [: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 [:terminate] olds.values.flatten.map do |instance| autoscaling.terminate_instance_in_auto_scaling_group(instance_id: instance.instance_id, should_decrement_desired_capacity: [:decrement] && true) instance.instance_id end.output { |ids| say("Terminated: #{ids.join(',')}", :yellow) } elsif [:groups] puts olds.keys elsif [: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
255 256 257 258 259 |
# File 'lib/awful/auto_scaling.rb', line 255 def processes autoscaling.describe_scaling_process_types.processes.map(&:process_name).sort.output do |procs| puts procs end end |
#resume(name, *procs) ⇒ Object
278 279 280 281 282 283 284 |
# File 'lib/awful/auto_scaling.rb', line 278 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
127 128 129 130 131 132 133 134 |
# File 'lib/awful/auto_scaling.rb', line 127 def ssh(name, *args) ips = ips(name).map(&:public_ip_address) num = [:all] ? ips.count : [:number].to_i login_name = [:login_name] ? "-l #{[:login_name]}" : '' ips.last(num).each do |ip| system "ssh #{login_name} #{ip} #{Array(args).join(' ')}" end end |
#stop(name, num = 1) ⇒ Object
245 246 247 248 249 250 251 252 |
# File 'lib/awful/auto_scaling.rb', line 245 def stop(name, num = 1) ins = [: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
263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/awful/auto_scaling.rb', line 263 def suspend(name, *procs) if [: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
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/awful/auto_scaling.rb', line 224 def terminate(name, num = 1) ins = [:newest] ? newest(name) : oldest(name) num = ins.length if [:all] # all instances if requested 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: [:decrement] && true) end else puts 'Nothing terminated' end end end |
#update(name, file = nil) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/awful/auto_scaling.rb', line 170 def update(name, file = nil) opt = load_cfg(, file) ## allow matching by group name or name tag, but ensure we get only one 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 ## cleanup the group options opt[:auto_scaling_group_name] = asgs.first.auto_scaling_group_name opt = remove_empty_strings(opt) ## update the group 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)) ## update any tags if opt[:tags] = opt[:tags].map { |tag| tag.merge(resource_id: name, resource_type: 'auto-scaling-group') } autoscaling.(tags: ) end end |
#wait(*instance_ids) ⇒ Object
379 380 381 382 383 384 |
# File 'lib/awful/auto_scaling.rb', line 379 def wait(*instance_ids) until instance_lifecycle_state(*instance_ids).all?{ |s| s == [:state] } puts "waiting for #{instance_ids.count} instances to enter state #{[:state]}" unless [:quiet] sleep [:period] end end |