Class: OpsworksWrapper::ELB
- Inherits:
-
Object
- Object
- OpsworksWrapper::ELB
- Defined in:
- lib/opsworks_wrapper.rb
Instance Method Summary collapse
-
#_wait_for_connection_draining ⇒ Object
Determines if elb has connection draining enabled and waits for the timeout period or (20s) default.
-
#_wait_for_instance_health_check(instance) ⇒ Boolean
Waits on instance to be in service according to the ELB.
-
#add_instance(instance) ⇒ Boolean
Adds instance to ELB and waits for instance health check to pass.
- #attributes ⇒ Object
- #client ⇒ Object
- #health_check ⇒ Object
-
#initialize(name) ⇒ ELB
constructor
A new instance of ELB.
-
#is_instance_healthy(instance) ⇒ Boolean
Checks whether an instance attached to ELB is healthy.
- #name ⇒ Object
-
#remove_instance(instance) ⇒ Object
Removes instance from ELB and waits for connection draining.
Constructor Details
#initialize(name) ⇒ ELB
Returns a new instance of ELB.
224 225 226 |
# File 'lib/opsworks_wrapper.rb', line 224 def initialize(name) @name = name end |
Instance Method Details
#_wait_for_connection_draining ⇒ Object
Determines if elb has connection draining enabled and waits for the timeout period or (20s) default
246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/opsworks_wrapper.rb', line 246 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
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/opsworks_wrapper.rb', line 261 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
299 300 301 302 303 304 305 |
# File 'lib/opsworks_wrapper.rb', line 299 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 ⇒ Object
236 237 238 |
# File 'lib/opsworks_wrapper.rb', line 236 def attributes @attributes ||= client.describe_load_balancer_attributes(load_balancer_name: name) end |
#client ⇒ Object
232 233 234 |
# File 'lib/opsworks_wrapper.rb', line 232 def client @client ||= Aws::ElasticLoadBalancing::Client.new end |
#health_check ⇒ Object
240 241 242 243 |
# File 'lib/opsworks_wrapper.rb', line 240 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
310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/opsworks_wrapper.rb', line 310 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 ⇒ Object
228 229 230 |
# File 'lib/opsworks_wrapper.rb', line 228 def name @name end |
#remove_instance(instance) ⇒ Object
Removes instance from ELB and waits for connection draining
288 289 290 291 292 293 294 |
# File 'lib/opsworks_wrapper.rb', line 288 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 |