class Moonshot::StackASGPrinter

Display information about the AutoScaling Groups, associated ELBs, and managed instances to the user.

Public Class Methods

new(stack, table) click to toggle source
# File lib/moonshot/stack_asg_printer.rb, line 11
def initialize(stack, table)
  @stack = stack
  @table = table
end

Public Instance Methods

print() click to toggle source

Private Instance Methods

add_asg_info(table, asg_info) click to toggle source
# File lib/moonshot/stack_asg_printer.rb, line 82
def add_asg_info(table, asg_info)
  name = asg_info.auto_scaling_group_name.blue
  table.add_line "Name: #{name}"

  hc = asg_info.health_check_type.blue
  gp = (asg_info.health_check_grace_period.to_s << 's').blue
  table.add_line "Using #{hc} health checks, with a #{gp} health check grace period." # rubocop:disable LineLength

  dc = asg_info.desired_capacity.to_s.blue
  min = asg_info.min_size.to_s.blue
  max = asg_info.max_size.to_s.blue
  table.add_line "Desired Capacity is #{dc} (Min: #{min}, Max: #{max})."

  lbs = asg_info.load_balancer_names
  table.add_line "Has #{lbs.count.to_s.blue} Load Balancer(s): #{lbs.map(&:blue).join(', ')}" # rubocop:disable LineLength
end
add_recent_activity_leaf(table, asg_name) click to toggle source
# File lib/moonshot/stack_asg_printer.rb, line 130
def add_recent_activity_leaf(table, asg_name)
  recent = table.add_leaf('Recent Activity')
  resp = as_client.describe_scaling_activities(
    auto_scaling_group_name: asg_name).activities

  rows = resp.sort_by(&:start_time).reverse.first(10).map do |activity|
    row_for_activity(activity)
  end

  recent.add_table(rows)
end
asgs() click to toggle source
# File lib/moonshot/stack_asg_printer.rb, line 38
def asgs
  @stack.resources_of_type('AWS::AutoScaling::AutoScalingGroup')
end
create_instance_table(asg_info) click to toggle source
# File lib/moonshot/stack_asg_printer.rb, line 99
def create_instance_table(asg_info)
  current_lc = asg_info.launch_configuration_name
  ec2_info = get_addl_info(asg_info.instances.map(&:instance_id))
  asg_info.instances.map do |asg_instance|
    row = instance_row(asg_instance,
                       ec2_info[asg_instance.instance_id])
    row << if current_lc == asg_instance.launch_configuration_name
             '(launch config up to date)'.green
           else
             '(launch config out of date)'.red
           end
  end
end
get_addl_info(instance_ids) click to toggle source

Get additional information about instances not returned by the ASG API.

# File lib/moonshot/stack_asg_printer.rb, line 72
def get_addl_info(instance_ids)
  resp = ec2_client.describe_instances(instance_ids: instance_ids)

  data = {}
  resp.reservations.map(&:instances).flatten.each do |instance|
    data[instance.instance_id] = instance
  end
  data
end
health_color(health) click to toggle source
# File lib/moonshot/stack_asg_printer.rb, line 62
def health_color(health)
  case health
  when 'Healthy'
    health.green
  else
    health.red
  end
end
instance_row(asg_instance, ec2_instance) click to toggle source
# File lib/moonshot/stack_asg_printer.rb, line 113
def instance_row(asg_instance, ec2_instance)
  [
    asg_instance.instance_id,
    # @todo What about ASGs with only private IPs?
    ec2_instance.public_ip_address,
    lifecycle_color(asg_instance.lifecycle_state),
    health_color(asg_instance.health_status),
    uptime_format(ec2_instance.launch_time)
  ]
end
lifecycle_color(lifecycle) click to toggle source
# File lib/moonshot/stack_asg_printer.rb, line 53
def lifecycle_color(lifecycle)
  case lifecycle
  when 'InService'
    lifecycle.green
  else
    lifecycle.red
  end
end
row_for_activity(activity) click to toggle source
# File lib/moonshot/stack_asg_printer.rb, line 142
def row_for_activity(activity)
  [
    activity.start_time.to_s.light_black,
    activity.description,
    status_with_color(activity.status_code),
    activity.progress.to_s << '%'
  ]
end
status_with_color(status) click to toggle source
# File lib/moonshot/stack_asg_printer.rb, line 42
def status_with_color(status)
  case status
  when 'Successful'
    status.green
  when 'Failed'
    status.red
  else
    status.yellow
  end
end
uptime_format(launch_time) click to toggle source
# File lib/moonshot/stack_asg_printer.rb, line 124
def uptime_format(launch_time)
  # %td is "total days", instead of counting up again to weeks.
  Duration.new(Time.now.to_i - launch_time.to_i)
          .format('%tdd %hh %mm %ss')
end