Class: Stax::Cmd::Asg

Inherits:
SubCommand show all
Defined in:
lib/stax/mixin/asg.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 SubCommand

#info, stax_info, stax_info_tasks

Instance Method Details

#lsObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stax/mixin/asg.rb', line 44

def ls
  print_table Aws::Asg.describe(stack_asgs.map(&:physical_resource_id)).map { |a|
    [
      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
    ]
  }
end

#oldObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/stax/mixin/asg.rb', line 87

def old
  Aws::Asg.describe(stack_asgs.map(&:physical_resource_id)).map do |a|
    Aws::Asg.instances(a.auto_scaling_group_name).select do |i|
      i.launch_configuration_name != a.launch_configuration_name
    end
  end.flatten.tap do |list|
    print_table list.map { |i| [i.instance_id, i.auto_scaling_group_name, i.launch_configuration_name] }
    if options[:terminate]
      list.each do |i|
        yes?("Terminate #{i.instance_id}?", :yellow) && Aws::Asg.terminate(i.instance_id, options[:decrement])
      end
    end
  end
end

#scaleObject



106
107
108
109
110
111
112
113
# File 'lib/stax/mixin/asg.rb', line 106

def scale
  opt = options.slice(*%w[desired_capacity min_size max_size])
  fail_task('No change requested') if opt.empty?
  stack_asgs.each do |a|
    debug("Scaling to #{opt} for #{a.logical_resource_id} #{a.physical_resource_id}")
    Aws::Asg.update(a.physical_resource_id, opt)
  end
end

#statusObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/stax/mixin/asg.rb', line 58

def status
  stack_asgs.each do |asg|
    debug("ASG status for #{asg.physical_resource_id}")
    print_table Aws::Asg.instances(asg.physical_resource_id).map { |i|
      [
        i.instance_id,
        i.availability_zone,
        color(i.lifecycle_state, COLORS),
        color(i.health_status, COLORS),
        i.launch_configuration_name,
      ]
    }
  end
end

#terminate(*ids) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/stax/mixin/asg.rb', line 75

def terminate(*ids)
   instances = Aws::Asg.instances(stack_asgs.map(&:physical_resource_id))
   instances.select do |i|
     ids.any? { |id| i.instance_id.match(id) }
   end.each do |i|
     yes?("Terminate #{i.instance_id}?", :yellow) && Aws::Asg.terminate(i.instance_id, options[:decrement])
   end
end