Module: RubyAemAws::HealthyResourceVerifier

Overview

Mixin for checking health of a component via ELB ‘healthy’ count vs ASG desired_capacity. Add this to a component to make it capable of determining its own health.

Instance Method Summary collapse

Instance Method Details

#health_state_asgObject

Provides detail of the state of the instances comprising the component.

  • no_asg: AutoScalingGroup could not be located (by StackPrefix and Component tags).

  • misconfigured: AutoScalingGroup.desired_capacity is less than 1.

  • recovering: ELB running instance count is less than AutoScalingGroup.desired_capacity.

  • scaling: ELB running instance count is more than AutoScalingGroup.desired_capacity.

  • ready: ELB running instance count is equal to AutoScalingGroup.desired_capacity.

Returns:

  • one of:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_aem_aws/mixins/healthy_resource_verifier.rb', line 79

def health_state_asg
  asg = find_auto_scaling_group(asg_client)
  return :no_asg if asg.nil?

  # Debug:
  # unless asg.nil?
  #   puts("ASG: #{asg} #{asg.auto_scaling_group_name} (#{asg.desired_capacity})")
  #   asg.instances.each do |i|
  #     puts("  Instance #{i.instance_id}: #{i.health_status}")
  #   end
  # end
  instances_inservice = 0
  desired_capacity = asg.desired_capacity

  get_all_instances.each do |i|
    next if i.nil? || i.state.code != Constants::INSTANCE_STATE_CODE_RUNNING
    return false if i.state.name != Constants::INSTANCE_STATE_HEALTHY

    instances_inservice += 1
  end

  return :misconfigured if desired_capacity < 1
  return :recovering if instances_inservice < desired_capacity
  return :scaling if instances_inservice > desired_capacity

  :ready
end

#health_state_elbObject

Provides detail of the state of the instances comprising the component.

  • no_asg: AutoScalingGroup could not be located (by StackPrefix and Component tags).

  • no_elb: ElasticLoadBalancer could not be located (by StackPrefix and aws:cloudformation:logical-id tags).

  • misconfigured: AutoScalingGroup.desired_capacity is less than 1.

  • recovering: ELB running instance count is less than AutoScalingGroup.desired_capacity.

  • scaling: ELB running instance count is more than AutoScalingGroup.desired_capacity.

  • ready: ELB running instance count is equal to AutoScalingGroup.desired_capacity.

Returns:

  • one of:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby_aem_aws/mixins/healthy_resource_verifier.rb', line 41

def health_state_elb
  asg = find_auto_scaling_group(asg_client)
  return :no_asg if asg.nil?

  # Debug:
  # unless asg.nil?
  #   puts("ASG: #{asg} #{asg.auto_scaling_group_name} (#{asg.desired_capacity})")
  #   asg.instances.each do |i|
  #     puts("  Instance #{i.instance_id}: #{i.health_status}")
  #   end
  # end

  elb = find_elb(elb_client)
  return :no_elb if elb.nil?

  elb_instance_state = get_instances_elb_state(elb_client, elb)
  return :no_elb_targets if elb_instance_state.nil?

  elb_running_instances = 0
  elb_instance_state.each do |i|
    elb_running_instances += 1 if i.target_health.state == RubyAemAws::Constants::ELB_INSTANCE_INSERVICE
  end

  desired_capacity = asg.desired_capacity
  return :misconfigured if desired_capacity < 1
  return :recovering if elb_running_instances < desired_capacity
  return :scaling if elb_running_instances > desired_capacity

  :ready
end

#healthy_asg?Boolean

Aggregate health_states considered healthy.

Returns:

  • (Boolean)

    health_state_asg is ready or scaling.



29
30
31
# File 'lib/ruby_aem_aws/mixins/healthy_resource_verifier.rb', line 29

def healthy_asg?
  %i[ready scaling].include? health_state_asg
end

#healthy_elb?Boolean

Aggregate health_states considered healthy.

Returns:

  • (Boolean)

    health_state_elb is ready or scaling.



23
24
25
# File 'lib/ruby_aem_aws/mixins/healthy_resource_verifier.rb', line 23

def healthy_elb?
  %i[ready scaling].include? health_state_elb
end

#wait_until_healthy_asgObject



115
116
117
118
119
120
# File 'lib/ruby_aem_aws/mixins/healthy_resource_verifier.rb', line 115

def wait_until_healthy_asg
  raise ASGMisconfiguration if health_state_asg.eql?(:misconfigured)

  sleep 60 while health_state_asg.eql?(:recovering) || health_state_asg.eql?(:scaling)
  return true if health_state_asg.eql?(:ready)
end

#wait_until_healthy_elbObject

Returns true, if all instances within the ELB are inService.

Returns:

  • true, if all instances within the ELB are inService

Raises:



108
109
110
111
112
113
# File 'lib/ruby_aem_aws/mixins/healthy_resource_verifier.rb', line 108

def wait_until_healthy_elb
  raise ELBMisconfiguration if health_state_elb.eql?(:misconfigured)

  sleep 60 while health_state_elb.eql?(:recovering) || health_state_elb.eql?(:scaling)
  return true if health_state_elb.eql?(:ready)
end