From a54ef14dfc5e5efb365bf0c4e7828dd8cf0213a7 Mon Sep 17 00:00:00 2001 From: Kaushik Sirineni Date: Thu, 10 Mar 2016 15:58:25 -0500 Subject: [PATCH] Add elbs instance state to status --- lib/moonshot/creds_helper.rb | 4 ++++ lib/moonshot/stack_asg_printer.rb | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/moonshot/creds_helper.rb b/lib/moonshot/creds_helper.rb index ba5fe839..6710262b 100644 --- a/lib/moonshot/creds_helper.rb +++ b/lib/moonshot/creds_helper.rb @@ -24,5 +24,9 @@ def as_client def s3_client Aws::S3::Client.new end + + def elb_client + Aws::ElasticLoadBalancing::Client.new + end end end diff --git a/lib/moonshot/stack_asg_printer.rb b/lib/moonshot/stack_asg_printer.rb index e0d8adf5..327ac078 100644 --- a/lib/moonshot/stack_asg_printer.rb +++ b/lib/moonshot/stack_asg_printer.rb @@ -100,8 +100,10 @@ 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| + elb_instance_state = elb_instance_health(asg_info, asg_instance.instance_id) row = instance_row(asg_instance, - ec2_info[asg_instance.instance_id]) + ec2_info[asg_instance.instance_id], + elb_instance_state) row << if current_lc == asg_instance.launch_configuration_name '(launch config up to date)'.green else @@ -110,17 +112,31 @@ def create_instance_table(asg_info) end end - def instance_row(asg_instance, ec2_instance) + def instance_row(asg_instance, ec2_instance, instance_elb_state) [ asg_instance.instance_id, # @todo What about ASGs with only private IPs? ec2_instance.public_ip_address, + '(ASG):', lifecycle_color(asg_instance.lifecycle_state), + '(ELB):', + lifecycle_color(instance_elb_state), health_color(asg_instance.health_status), uptime_format(ec2_instance.launch_time) ] end + def elb_instance_health(asg_info, instance_id) + asg_info.load_balancer_names.map do |load_balancer_name| + elb_client.describe_instance_health( + load_balancer_name: load_balancer_name, + instances: [{ + instance_id: instance_id + }] + ).instance_states.first.state + end.compact.first + end + 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)