class OpsworksWrapper::ELB

Public Class Methods

new(name) click to toggle source
# File lib/opsworks_wrapper.rb, line 222
def initialize(name)
  @name = name
end

Public Instance Methods

_wait_for_connection_draining() click to toggle source

Determines if elb has connection draining enabled and waits for the timeout period or (20s) default

# File lib/opsworks_wrapper.rb, line 244
def _wait_for_connection_draining
  connection_draining = attributes.load_balancer_attributes.connection_draining
  if connection_draining.enabled
    timeout = connection_draining.timeout
    puts "Connection Draining Enabled - sleeping for #{timeout}".light_black
    sleep(timeout)
  else
    puts "Connection Draining Disabled - sleeping for 20 seconds".light_black
    sleep(20)
  end
end
_wait_for_instance_health_check(instance) click to toggle source

Waits on instance to be in service according to the ELB @param [Object] instance @return [Boolean]

# File lib/opsworks_wrapper.rb, line 259
def _wait_for_instance_health_check(instance)
  health_threshold = health_check.healthy_threshold
  interval = health_check.interval

  # wait a little longer than the defined threshold to account for application launch time
  timeout = ((health_threshold + 2) * interval)

  begin
    client.wait_until(:instance_in_service, {load_balancer_name: name,
                                             instances: [{instance_id: instance.ec2_instance_id}]}) do |w|
      w.before_attempt do |attempt|
        puts "Attempt #{attempt} to check health status for #{instance.hostname}".light_black
      end
      w.interval = 10
      w.max_attempts = timeout / w.interval
    end
    puts "Instance #{instance.hostname} is now InService".green
    true
  rescue Aws::Waiters::Errors::WaiterFailed => e
    puts "Instance #{instance.hostname} failed to move to InService, #{e.message}".red
    false
  end

end
add_instance(instance) click to toggle source

Adds instance to ELB and waits for instance health check to pass @param [Object] instance - object with ec2_instance_id and hostname @return [Boolean]

# File lib/opsworks_wrapper.rb, line 297
def add_instance(instance)
  register_response = client.register_instances_with_load_balancer(load_balancer_name: name,
                                                                   instances: [{instance_id: instance.ec2_instance_id}])
  remaining_instance_count = register_response.instances.size
  puts "Added #{instance.hostname} to ELB #{name}. Attached instances: #{remaining_instance_count}".light_blue
  _wait_for_instance_health_check(instance)
end
attributes() click to toggle source
# File lib/opsworks_wrapper.rb, line 234
def attributes
  @attributes ||= client.describe_load_balancer_attributes(load_balancer_name: name)
end
client() click to toggle source
# File lib/opsworks_wrapper.rb, line 230
def client
  @client ||= Aws::ElasticLoadBalancing::Client.new
end
health_check() click to toggle source
# File lib/opsworks_wrapper.rb, line 238
def health_check
  elb = client.describe_load_balancers(load_balancer_names: [name]).load_balancer_descriptions.first
  @health_check ||= elb.health_check
end
is_instance_healthy(instance) click to toggle source

Checks whether an instance attached to ELB is healthy @param [Object] instance - object with ec2_instance_id and hostname @return [Boolean]

# File lib/opsworks_wrapper.rb, line 308
def is_instance_healthy(instance)
  instance_health = client.describe_instance_health(load_balancer_name: name,
                                                    instances: [{instance_id: instance.ec2_instance_id}])
  state_info = instance_health.instance_states.first
  status_detail = ''
  if state_info.state != 'InService'
    status_detail = "#{state_info.reason_code} - #{state_info.description}."
  end
  puts "Instance state is #{state_info.state} #{status_detail}"
  state_info.state == 'InService'
end
name() click to toggle source
# File lib/opsworks_wrapper.rb, line 226
def name
  @name
end
remove_instance(instance) click to toggle source

Removes instance from ELB and waits for connection draining @param [Object] instance - object with ec2_instance_id and hostname

# File lib/opsworks_wrapper.rb, line 286
def remove_instance(instance)
  deregister_response = client.deregister_instances_from_load_balancer(load_balancer_name: name,
                                                                       instances: [{instance_id: instance.ec2_instance_id}])
  remaining_instance_count = deregister_response.instances.size
  puts "Removed #{instance.hostname} from ELB #{name}. Remaining instances: #{remaining_instance_count}".light_blue
  _wait_for_connection_draining
end