Skip to content

Commit 410411c

Browse files
authored
[#180098892] Single and only tg per service approach (#1)
1 parent ecf1f75 commit 410411c

6 files changed

Lines changed: 66 additions & 38 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.gem
2+
.rubocop-https*

.rubocop-shared.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
inherit_from:
2+
- https://raw.githubusercontent.com/simplepractice/rubocop-simplepractice/v0.83.0-1/.rubocop-shared.yml

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
inherit_from:
2+
- ./.rubocop-shared.yml
3+
AllCops:
4+
TargetRubyVersion: 2.7

bin/blue_green_switch

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
require "aws-sdk-elasticloadbalancingv2"
44

55
REGION = "us-west-2".freeze
6-
lb_name = ARGV[0]
7-
lb_port = 443
8-
domain = ARGV[1]
9-
tg_prefix = ARGV[2]
10-
color = ARGV[3]
6+
tg_name = ARGV[0]
7+
color = ARGV[1]
8+
color_port = ARGV[2]
9+
10+
if color_port.nil?
11+
fail "Cannot detect current color_port. Color_port not passed in cli?"
12+
elsif ["blue", "green"].include?(color)
13+
puts "Switching to #{color}"
14+
else
15+
fail "Cannot detect current color. Wrong color passed in cli?"
16+
end
1117

1218
class AWSClient
1319
class << self
@@ -17,30 +23,42 @@ class AWSClient
1723
end
1824
end
1925

26+
tg = AWSClient.elbv2.describe_target_groups.target_groups.find { |x| x.target_group_name == tg_name }
27+
targets = AWSClient.elbv2.describe_target_health(target_group_arn: tg.target_group_arn)
28+
.target_health_descriptions.map(&:target)
2029

21-
alb = AWSClient.elbv2.describe_load_balancers.load_balancers.find { |x| x.load_balancer_name == lb_name }
22-
listener = AWSClient.elbv2.describe_listeners(load_balancer_arn: alb.load_balancer_arn).listeners.find { |x| x.port == lb_port }
23-
rule = AWSClient.elbv2.describe_rules(listener_arn: listener.listener_arn).rules.find { |x| x.conditions.first.values.include? domain }
24-
tg_green = AWSClient.elbv2.describe_target_groups.target_groups.find { |x| x.target_group_name == "#{tg_prefix}-green" }
25-
tg_blue = AWSClient.elbv2.describe_target_groups.target_groups.find { |x| x.target_group_name == "#{tg_prefix}-blue" }
26-
27-
if color == "green"
28-
puts "Switching to green"
29-
tg_color = tg_green.target_group_arn
30-
elsif color == "blue"
31-
puts "Switching to blue"
32-
tg_color = tg_blue.target_group_arn
33-
else
34-
fail "Cannot detect current color. Color not passed in cli?"
30+
new_targets = []
31+
targets.map(&:id).each do |i|
32+
new_targets <<
33+
{
34+
id: i,
35+
port: color_port
36+
}
3537
end
3638

37-
targets = AWSClient.elbv2.describe_target_health(target_group_arn: tg_color)
38-
.target_health_descriptions.map(&:target)
39+
AWSClient.elbv2.register_targets({
40+
target_group_arn: tg.target_group_arn,
41+
targets: new_targets
42+
})
3943

4044
AWSClient.elbv2.wait_until(
4145
:target_in_service,
42-
target_group_arn: tg_color,
46+
target_group_arn: tg.target_group_arn,
47+
targets: new_targets
48+
)
49+
50+
puts "\n#{color.capitalize} target in service"
51+
puts "De-registering an old one"
52+
53+
AWSClient.elbv2.deregister_targets({
54+
target_group_arn: tg.target_group_arn,
55+
targets: targets
56+
})
57+
58+
AWSClient.elbv2.wait_until(
59+
:target_deregistered,
60+
target_group_arn: tg.target_group_arn,
4361
targets: targets
4462
)
4563

46-
AWSClient.elbv2.modify_rule(rule_arn: rule.rule_arn, actions: [{ type: "forward", target_group_arn: tg_color }])
64+
puts "\nBlue-green switch completed"

bin/detect_inactive_color

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

4-
lb_name = ARGV[0]
5-
lb_port = 443
6-
domain = ARGV[1]
4+
tg_name = ARGV[0]
5+
blue_port = ARGV[1]
6+
green_port = ARGV[2]
77

88
client = Aws::ElasticLoadBalancingV2::Client.new
9-
alb = client.describe_load_balancers.load_balancers.find {|x| x.load_balancer_name==lb_name}
10-
listener = client.describe_listeners(load_balancer_arn: alb.load_balancer_arn).listeners.find {|x| x.port == lb_port}
11-
rule = client.describe_rules(listener_arn: listener.listener_arn).rules.find {|x| x.conditions.first.values.include? domain}
9+
tg = client.describe_target_groups.target_groups.find { |x| x.target_group_name == tg_name }
10+
targets = client.describe_target_health(target_group_arn: tg.target_group_arn)
11+
.target_health_descriptions.map(&:target)
12+
target_ports = targets.map(&:port).uniq
1213

13-
if rule.actions.first.target_group_arn.include? 'blue'
14-
puts 'green'
15-
elsif rule.actions.first.target_group_arn.include? 'green'
16-
puts 'blue'
14+
if target_ports.count > 1
15+
fail "Both ports are active"
16+
elsif target_ports.first == blue_port.to_i
17+
puts "green"
18+
elsif target_ports.first == green_port.to_i
19+
puts "blue"
1720
else
18-
fail 'Cannot detect current color'
21+
fail "Cannot detect current color"
1922
end

deploy-tools.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Gem::Specification.new do |s|
22
s.name = 'deploy-tools'
3-
s.version = '0.0.5'
4-
s.date = '2021-03-17'
3+
s.version = '0.1.0'
4+
s.date = '2022-01-27'
55
s.summary = "Deploy tools"
66
s.description = "A set of script used for deployment"
7-
s.authors = ["Tony Nyurkin"]
7+
s.authors = ["Tony Nyurkin", "Serhii Voronoi"]
88
s.email = 'tony@simplepractice.com'
99
s.files = `git ls-files `.split("\n")
1010
s.bindir = "bin"

0 commit comments

Comments
 (0)