Skip to content

Commit 04626a2

Browse files
author
Tony Nyurkin
committed
rework
1 parent e86bf62 commit 04626a2

1 file changed

Lines changed: 33 additions & 15 deletions

File tree

bin/blue_green_switch

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
11
#!/usr/bin/env ruby
2-
require 'aws-sdk-elasticloadbalancingv2'
32

3+
require "aws-sdk-elasticloadbalancingv2"
4+
5+
REGION = "us-west-2".freeze
46
lb_name = ARGV[0]
57
lb_port = 443
68
domain = ARGV[1]
79
tg_prefix = ARGV[2]
810

11+
class AWSClient
12+
class << self
13+
def elbv2
14+
@elbv2 ||= Aws::ElasticLoadBalancingV2::Client.new(region: REGION)
15+
end
16+
end
17+
end
18+
919

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" }
1625

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
2332
else
24-
fail 'Cannot detect current color'
33+
fail "Cannot detect current color"
2534
end
2635

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

Comments
 (0)