|
1 | 1 | #!/usr/bin/env ruby |
2 | | -require 'aws-sdk-elasticloadbalancingv2' |
3 | 2 |
|
| 3 | +require "aws-sdk-elasticloadbalancingv2" |
| 4 | + |
| 5 | +REGION = "us-west-2".freeze |
4 | 6 | lb_name = ARGV[0] |
5 | 7 | lb_port = 443 |
6 | 8 | domain = ARGV[1] |
7 | 9 | tg_prefix = ARGV[2] |
8 | 10 |
|
| 11 | +class AWSClient |
| 12 | + class << self |
| 13 | + def elbv2 |
| 14 | + @elbv2 ||= Aws::ElasticLoadBalancingV2::Client.new(region: REGION) |
| 15 | + end |
| 16 | + end |
| 17 | +end |
| 18 | + |
9 | 19 |
|
10 | | -client = Aws::ElasticLoadBalancingV2::Client.new |
11 | | -alb = client.describe_load_balancers.load_balancers.find {|x| x.load_balancer_name==lb_name} |
12 | | -listener = client.describe_listeners(load_balancer_arn: alb.load_balancer_arn).listeners.find {|x| x.port == lb_port} |
13 | | -rule = client.describe_rules(listener_arn: listener.listener_arn).rules.find {|x| x.conditions.first.values.include? domain} |
14 | | -tg_green = client.describe_target_groups.target_groups.find {|x| x.target_group_name == "#{tg_prefix}-green"} |
15 | | -tg_blue = client.describe_target_groups.target_groups.find {|x| x.target_group_name == "#{tg_prefix}-blue"} |
| 20 | +alb = AWSClient.elbv2.describe_load_balancers.load_balancers.find { |x| x.load_balancer_name == lb_name } |
| 21 | +listener = AWSClient.elbv2.describe_listeners(load_balancer_arn: alb.load_balancer_arn).listeners.find { |x| x.port == lb_port } |
| 22 | +rule = AWSClient.elbv2.describe_rules(listener_arn: listener.listener_arn).rules.find { |x| x.conditions.first.values.include? domain } |
| 23 | +tg_green = AWSClient.elbv2.describe_target_groups.target_groups.find { |x| x.target_group_name == "#{tg_prefix}-green" } |
| 24 | +tg_blue = AWSClient.elbv2.describe_target_groups.target_groups.find { |x| x.target_group_name == "#{tg_prefix}-blue" } |
16 | 25 |
|
17 | | -if rule.actions.first.target_group_arn.include? 'blue' |
18 | | - puts 'Detected blue tg, switching to green' |
19 | | - tg_color = tg_green |
20 | | -elsif rule.actions.first.target_group_arn.include? 'green' |
21 | | - puts 'Detected green tg, switching to blue' |
22 | | - tg_color = tg_blue |
| 26 | +if rule.actions.first.target_group_arn.include? "blue" |
| 27 | + puts "Detected blue tg, switching to green" |
| 28 | + tg_color = tg_green.target_group_arn |
| 29 | +elsif rule.actions.first.target_group_arn.include? "green" |
| 30 | + puts "Detected green tg, switching to blue" |
| 31 | + tg_color = tg_blue.target_group_arn |
23 | 32 | else |
24 | | - fail 'Cannot detect current color' |
| 33 | + fail "Cannot detect current color" |
25 | 34 | end |
26 | 35 |
|
27 | | -client.modify_rule(rule_arn: rule.rule_arn, actions: [{ type: 'forward', target_group_arn: tg_color.target_group_arn }]) |
| 36 | +targets = AWSClient.elbv2.describe_target_health(target_group_arn: tg_color) |
| 37 | + .target_health_descriptions.map(&:target) |
| 38 | + |
| 39 | +AWSClient.elbv2.wait_until( |
| 40 | + :target_in_service, |
| 41 | + target_group_arn: tg_color, |
| 42 | + targets: targets |
| 43 | +) |
| 44 | + |
| 45 | +client.modify_rule(rule_arn: rule.rule_arn, actions: [{ type: "forward", target_group_arn: tg_color }]) |
0 commit comments