33require "aws-sdk-elasticloadbalancingv2"
44
55REGION = "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
1218class AWSClient
1319 class << self
@@ -17,30 +23,42 @@ class AWSClient
1723 end
1824end
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+ }
3537end
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
4044AWSClient . 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 " \n Blue-green switch completed"
0 commit comments