Class: Awful::AutoScaling

Inherits:
Cli show all
Defined in:
lib/awful/auto_scaling.rb

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

Methods inherited from Cli

#initialize

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#activities(name) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
# File 'lib/awful/auto_scaling.rb', line 382

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



305
306
307
# File 'lib/awful/auto_scaling.rb', line 305

def attach(name, *instance_ids)
  autoscaling.attach_instances(auto_scaling_group_name: name, instance_ids: instance_ids)
end

#create(file = nil) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/awful/auto_scaling.rb', line 169

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)

  ## 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



80
81
82
83
84
# File 'lib/awful/auto_scaling.rb', line 80

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



311
312
313
# File 'lib/awful/auto_scaling.rb', line 311

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



155
156
157
158
159
160
161
# File 'lib/awful/auto_scaling.rb', line 155

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



406
407
408
409
410
411
412
# File 'lib/awful/auto_scaling.rb', line 406

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



415
416
417
# File 'lib/awful/auto_scaling.rb', line 415

def exit_standby(name, *instance_ids)
  autoscaling.exit_standby(auto_scaling_group_name: name, instance_ids: instance_ids)
end

#instances(*names) ⇒ 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
# File 'lib/awful/auto_scaling.rb', line 89

def instances(*names)
  instances = asg_instances(*names)

  ## make extra call to get asg name as part of object
  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



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/awful/auto_scaling.rb', line 117

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



317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/awful/auto_scaling.rb', line 317

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/awful/auto_scaling.rb', line 59

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



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
376
377
# File 'lib/awful/auto_scaling.rb', line 338

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 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

#processesObject



273
274
275
276
277
# File 'lib/awful/auto_scaling.rb', line 273

def processes
  autoscaling.describe_scaling_process_types.processes.map(&:process_name).sort.output do |procs|
    puts procs
  end
end

#resume(name, *procs) ⇒ Object



296
297
298
299
300
301
302
# File 'lib/awful/auto_scaling.rb', line 296

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/awful/auto_scaling.rb', line 135

def ssh(name, *args)
  instances = asg_instance_details(name)

  ## filter by list of instance id patterns
  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
   = options[:login_name] ? "-l #{options[:login_name]}" : ''
  ips.last(num).each do |ip|
    puts ip if options[:verbose]
    system "ssh #{} #{ip} #{Array(args).join(' ')}"
  end
end

#stop(name, num = 1) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/awful/auto_scaling.rb', line 263

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



281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/awful/auto_scaling.rb', line 281

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



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/awful/auto_scaling.rb', line 242

def terminate(name, num = 1)
  ins = options[:newest] ? newest(name) : oldest(name)
  num = ins.length if options[: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: options[:decrement] && true)
      end
    else
      puts 'Nothing terminated'
    end
  end
end

#update(name, file = nil) ⇒ Object



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
# File 'lib/awful/auto_scaling.rb', line 188

def update(name, file = nil)
  opt = load_cfg(options, 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]
    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



397
398
399
400
401
402
# File 'lib/awful/auto_scaling.rb', line 397

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