|
| 1 | +# |
| 2 | +# Cookbook Name:: alb_support |
| 3 | +# Recipe:: detach_from_alb |
| 4 | +# |
| 5 | +# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 8 | +# may not use this file except in compliance with the License. A copy of |
| 9 | +# the License is located at |
| 10 | +# |
| 11 | +# http://aws.amazon.com/apache2.0/ |
| 12 | +# |
| 13 | +# or in the "license" file accompanying this file. This file is |
| 14 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 15 | +# ANY KIND, either express or implied. See the License for the specific |
| 16 | +# language governing permissions and limitations under the License. |
| 17 | +# |
| 18 | +chef_gem "aws-sdk-core" do |
| 19 | + version "~> 2.6" |
| 20 | + action :install |
| 21 | +end |
| 22 | + |
| 23 | +ruby_block "detach from ALB" do |
| 24 | + block do |
| 25 | + require "aws-sdk-core" |
| 26 | + |
| 27 | + raise "alb_helper block not specified in layer JSON" if node[:alb_helper].nil? |
| 28 | + raise "Target Group ARN not specified in layer JSON" if node[:alb_helper][:target_group_arn].nil? |
| 29 | + |
| 30 | + connection_draining_timeout = node[:alb_helper][:connection_draining_timeout] |
| 31 | + state_check_frequency = node[:alb_helper][:state_check_frequency] |
| 32 | + |
| 33 | + Chef::Log.info("connection_draining_timeout: #{connection_draining_timeout}") |
| 34 | + Chef::Log.info("state_check_frequency: #{state_check_frequency}") |
| 35 | + |
| 36 | + if ( Chef::VERSION.to_f >= 12.0 ) |
| 37 | + stack = search("aws_opsworks_stack").first |
| 38 | + instance = search("aws_opsworks_instance", "self:true").first |
| 39 | + |
| 40 | + stack_region = stack[:region] |
| 41 | + ec2_instance_id = instance[:ec2_instance_id] |
| 42 | + else |
| 43 | + stack_region = node["opsworks"]["instance"]["region"] |
| 44 | + ec2_instance_id = node["opsworks"]["instance"]["aws_instance_id"] |
| 45 | + end |
| 46 | + |
| 47 | + target_group_arn = node[:alb_helper][:target_group_arn] |
| 48 | + |
| 49 | + Chef::Log.info("Creating ELB client in region #{stack_region}") |
| 50 | + client = Aws::ElasticLoadBalancingV2::Client.new(region: stack_region) |
| 51 | + |
| 52 | + target_to_detach = { |
| 53 | + target_group_arn: target_group_arn, |
| 54 | + targets: [ { id: ec2_instance_id } ], |
| 55 | + } |
| 56 | + |
| 57 | + Chef::Log.info("Deregistering EC2 instance #{ec2_instance_id} from Target Group #{target_group_arn}") |
| 58 | + client.deregister_targets(target_to_detach) |
| 59 | + |
| 60 | + if connection_draining_timeout == 0 |
| 61 | + Chef::Log.info("connection_draining_timeout was set to 0 seconds. execution of shutdown recipes will not be delayed") |
| 62 | + else |
| 63 | + Chef::Log.info("delaying execution recipes until instance is drained from ALB or timeout of #{connection_draining_timeout} seconds elapses") |
| 64 | + start_time = Time.now |
| 65 | + loop do |
| 66 | + response = client.describe_target_health(target_to_detach) |
| 67 | + target_health_state = response[:target_health_descriptions].first[:target_health][:state] |
| 68 | + Chef::Log.info("state of instance in ALB: #{target_health_state}") |
| 69 | + seconds_elapsed = Time.now - start_time |
| 70 | + Chef::Log.info("#{seconds_elapsed} of a maximum #{connection_draining_timeout} seconds elapsed") |
| 71 | + Chef::Log.info("Sleeping #{ state_check_frequency} seconds") |
| 72 | + break if target_health_state == "unused" || seconds_elapsed > connection_draining_timeout |
| 73 | + sleep(state_check_frequency) |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + action :run |
| 78 | +end |
0 commit comments