Class: OpsworksWrapper::ELB

Inherits:
Object
  • Object
show all
Defined in:
lib/opsworks_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ELB



222
223
224
# File 'lib/opsworks_wrapper.rb', line 222

def initialize(name)
  @name = name
end

Instance Method Details

#_wait_for_connection_drainingObject

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



244
245
246
247
248
249
250
251
252
253
254
# 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) ⇒ Boolean

Waits on instance to be in service according to the ELB



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# 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) ⇒ Boolean

Adds instance to ELB and waits for instance health check to pass



297
298
299
300
301
302
303
# 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

#attributesObject



234
235
236
# File 'lib/opsworks_wrapper.rb', line 234

def attributes
  @attributes ||= client.describe_load_balancer_attributes(load_balancer_name: name)
end

#clientObject



230
231
232
# File 'lib/opsworks_wrapper.rb', line 230

def client
  @client ||= Aws::ElasticLoadBalancing::Client.new
end

#health_checkObject



238
239
240
241
# 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) ⇒ Boolean

Checks whether an instance attached to ELB is healthy



308
309
310
311
312
313
314
315
316
317
318
# 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

#nameObject



226
227
228
# File 'lib/opsworks_wrapper.rb', line 226

def name
  @name
end

#remove_instance(instance) ⇒ Object

Removes instance from ELB and waits for connection draining



286
287
288
289
290
291
292
# 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