From 56da44d804ba3a20eb40104b26671db9c361c38f Mon Sep 17 00:00:00 2001 From: Ed Hartwell Goose Date: Wed, 18 Feb 2015 19:19:01 +0000 Subject: [PATCH 1/9] Add the ability to deregister a instance by name --- lib/capify-ec2.rb | 15 +++++++++++++++ lib/capify-ec2/capistrano.rb | 11 +++++++++-- readme.md | 13 +++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/capify-ec2.rb b/lib/capify-ec2.rb index 1f7285e..cb1649a 100644 --- a/lib/capify-ec2.rb +++ b/lib/capify-ec2.rb @@ -306,6 +306,21 @@ def register_instance_in_elb(instance_name, load_balancer_name = '') end end + def deregister_instance_from_named_elb_by_dns(server_dns, load_balancer_name) + instance = get_instance_by_dns(server_dns) + load_balancer = get_load_balancer_by_name(load_balancer_name) + + if load_balancer + puts "[Capify-EC2] Removing instance from named ELB '#{load_balancer.id}'..." + + result = elb.deregister_instances_from_load_balancer(instance.id, load_balancer.id) + raise "Unable to remove instance from ELB '#{load_balancer.id}'..." unless result.status == 200 + + return load_balancer + end + false + end + def deregister_instance_from_elb_by_dns(server_dns) instance = get_instance_by_dns(server_dns) load_balancer = get_load_balancer_by_instance(instance.id) diff --git a/lib/capify-ec2/capistrano.rb b/lib/capify-ec2/capistrano.rb index b9ed26b..1c125f7 100644 --- a/lib/capify-ec2/capistrano.rb +++ b/lib/capify-ec2/capistrano.rb @@ -123,17 +123,24 @@ def capify_ec2 load_balancer_to_reregister = nil # Set to nil again here, to ensure it always starts off nil for every iteration. is_load_balanced = false + load_balancer_name = false server_roles.each do |a_role| role a_role, server_dns, all_options[a_role][server_dns] is_load_balanced = true if all_options[a_role][server_dns][:load_balanced] + + load_balancer_name = all_options[a_role][server_dns][:elb_name] if all_options[a_role][server_dns][:elb_name] end puts "[Capify-EC2]" puts "[Capify-EC2] (#{index+1} of #{all_servers.length}) Beginning deployment to #{instance_dns_with_name_tag(server_dns)} with #{server_roles.count > 1 ? 'roles' : 'role'} '#{server_roles.join(', ')}'...".bold - unless dry_run - load_balancer_to_reregister = capify_ec2.deregister_instance_from_elb_by_dns(server_dns) if is_load_balanced + if is_load_balanced && !dry_run + if load_balancer_name + load_balancer_to_reregister = capify_ec2.deregister_instance_from_named_elb_by_dns(server_dns, load_balancer_name) + else + load_balancer_to_reregister = capify_ec2.deregister_instance_from_elb_by_dns(server_dns) + end end # Call the standard 'cap deploy' task with our redefined role containing a single server. diff --git a/readme.md b/readme.md index b0326f3..b34a10a 100644 --- a/readme.md +++ b/readme.md @@ -550,7 +550,20 @@ If an instance has been tagged with multiple roles, this behaviour will apply if If an instance is not associated with any ELBs, then the behaviour will be skipped silently, even if `:load_balanced` is set to 'true'. +If an instance belongs to multiple ELBs, you can force which ELB is chosen by using the :elb_name parameter like this: +```ruby +ec2_roles :name => "web", + :variables => { + :healthcheck => { + :path => '/status', + :port => 80, + :result => 'OK' + } + :load_balanced => true, + :elb_name => "the_name_of_your_elb" + } +``` #### Viewing All Instances From a9c5b3421fe2fa363d1976db34ffcf20ff4d8329 Mon Sep 17 00:00:00 2001 From: Ed Hartwell Goose Date: Mon, 23 Feb 2015 17:04:59 +0000 Subject: [PATCH 2/9] Further extend the concept of named elb to include an array of named elbs --- lib/capify-ec2.rb | 28 ++++++++++++++++++---------- lib/capify-ec2/capistrano.rb | 18 ++++++++++-------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/capify-ec2.rb b/lib/capify-ec2.rb index cb1649a..5cb8cec 100644 --- a/lib/capify-ec2.rb +++ b/lib/capify-ec2.rb @@ -266,7 +266,6 @@ def get_load_balancer_by_name(load_balancer_name) lbs[load_balancer.id] = load_balancer end lbs[load_balancer_name] - end def deregister_instance_from_elb(instance_name) @@ -306,19 +305,24 @@ def register_instance_in_elb(instance_name, load_balancer_name = '') end end - def deregister_instance_from_named_elb_by_dns(server_dns, load_balancer_name) + def deregister_instance_from_named_elbs_by_dns(server_dns, load_balancer_names) instance = get_instance_by_dns(server_dns) - load_balancer = get_load_balancer_by_name(load_balancer_name) - if load_balancer - puts "[Capify-EC2] Removing instance from named ELB '#{load_balancer.id}'..." + lbs = [] + for load_balancer_name in load_balancer_names do + load_balancer = get_load_balancer_by_name(load_balancer_name) - result = elb.deregister_instances_from_load_balancer(instance.id, load_balancer.id) - raise "Unable to remove instance from ELB '#{load_balancer.id}'..." unless result.status == 200 + if load_balancer + puts "[Capify-EC2] Removing instance from named ELB '#{load_balancer.id}'..." - return load_balancer + result = elb.deregister_instances_from_load_balancer(instance.id, load_balancer.id) + raise "Unable to remove instance from ELB '#{load_balancer.id}'..." unless result.status == 200 + + lbs << load_balancer + end end - false + + lbs end def deregister_instance_from_elb_by_dns(server_dns) @@ -331,7 +335,11 @@ def deregister_instance_from_elb_by_dns(server_dns) result = elb.deregister_instances_from_load_balancer(instance.id, load_balancer.id) raise "Unable to remove instance from ELB '#{load_balancer.id}'..." unless result.status == 200 - return load_balancer + #TODO: The ability to remove an instance from multiple ELBs has been added, which returns an [] of elbs. + #I've taken a shortcut here to return an array for this (the single ELB case). However, the correct solution + #would be to extend the above method to remove an instance from multiple ELBs by DNS too. + #This may break things for some users however? Needs to be checked. + return [load_balancer] end false end diff --git a/lib/capify-ec2/capistrano.rb b/lib/capify-ec2/capistrano.rb index 1c125f7..67e21fe 100644 --- a/lib/capify-ec2/capistrano.rb +++ b/lib/capify-ec2/capistrano.rb @@ -121,25 +121,27 @@ def capify_ec2 roles.clear - load_balancer_to_reregister = nil # Set to nil again here, to ensure it always starts off nil for every iteration. + load_balancers_to_reregister = nil # Set to nil again here, to ensure it always starts off nil for every iteration. is_load_balanced = false - load_balancer_name = false + load_balancer_names = false server_roles.each do |a_role| role a_role, server_dns, all_options[a_role][server_dns] is_load_balanced = true if all_options[a_role][server_dns][:load_balanced] - load_balancer_name = all_options[a_role][server_dns][:elb_name] if all_options[a_role][server_dns][:elb_name] + if all_options[a_role][server_dns][:elb_names] + load_balancer_names = all_options[a_role][server_dns][:elb_names] + end end puts "[Capify-EC2]" puts "[Capify-EC2] (#{index+1} of #{all_servers.length}) Beginning deployment to #{instance_dns_with_name_tag(server_dns)} with #{server_roles.count > 1 ? 'roles' : 'role'} '#{server_roles.join(', ')}'...".bold if is_load_balanced && !dry_run - if load_balancer_name - load_balancer_to_reregister = capify_ec2.deregister_instance_from_named_elb_by_dns(server_dns, load_balancer_name) + if load_balancer_names + load_balancers_to_reregister = capify_ec2.deregister_instance_from_named_elbs_by_dns(server_dns, load_balancer_names) else - load_balancer_to_reregister = capify_ec2.deregister_instance_from_elb_by_dns(server_dns) + load_balancers_to_reregister = capify_ec2.deregister_instance_from_elb_by_dns(server_dns) end end @@ -175,7 +177,7 @@ def capify_ec2 end - if load_balancer_to_reregister + for load_balancer_to_reregister in load_balancers_to_reregister do reregistered = capify_ec2.reregister_instance_with_elb_by_dns(server_dns, load_balancer_to_reregister, 60) if reregistered puts "[Capify-EC2] Instance registration with ELB '#{load_balancer_to_reregister.id}' successful.".green.bold @@ -334,4 +336,4 @@ def pluralise(n, singular, plural=nil) "#{singular}s" end end -end +end \ No newline at end of file From dc96647c9988dc16a901620166016b2f3782fa4d Mon Sep 17 00:00:00 2001 From: Ed Hartwell Goose Date: Mon, 23 Feb 2015 18:22:54 +0000 Subject: [PATCH 3/9] Update readme with new changes --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index b34a10a..3c0f406 100644 --- a/readme.md +++ b/readme.md @@ -550,7 +550,7 @@ If an instance has been tagged with multiple roles, this behaviour will apply if If an instance is not associated with any ELBs, then the behaviour will be skipped silently, even if `:load_balanced` is set to 'true'. -If an instance belongs to multiple ELBs, you can force which ELB is chosen by using the :elb_name parameter like this: +If an instance belongs to multiple ELBs (e.g. a single instance pointed to by separate ELBs), you can force which ELBs are chosen by using the :elb_names parameter like this: ```ruby ec2_roles :name => "web", @@ -561,7 +561,7 @@ ec2_roles :name => "web", :result => 'OK' } :load_balanced => true, - :elb_name => "the_name_of_your_elb" + :elb_names => ["the_name_of_your_elb", "the_name_of_another_elb"] } ``` From 85f08563fdc6dfcdecf49c20db229c135f0ad7be Mon Sep 17 00:00:00 2001 From: Ed Hartwell Goose Date: Tue, 24 Feb 2015 09:43:10 +0000 Subject: [PATCH 4/9] Add a logging message --- lib/capify-ec2/capistrano.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/capify-ec2/capistrano.rb b/lib/capify-ec2/capistrano.rb index 67e21fe..4d5a2bf 100644 --- a/lib/capify-ec2/capistrano.rb +++ b/lib/capify-ec2/capistrano.rb @@ -178,6 +178,7 @@ def capify_ec2 end for load_balancer_to_reregister in load_balancers_to_reregister do + puts "[Capify-EC2] Starting registration of ELB '#{load_balancer_to_reregister.id}'" reregistered = capify_ec2.reregister_instance_with_elb_by_dns(server_dns, load_balancer_to_reregister, 60) if reregistered puts "[Capify-EC2] Instance registration with ELB '#{load_balancer_to_reregister.id}' successful.".green.bold From a7fa5e67716a6f9caa77476f31a4de0960484113 Mon Sep 17 00:00:00 2001 From: Ed Hartwell Goose Date: Tue, 24 Feb 2015 10:08:43 +0000 Subject: [PATCH 5/9] Use empty array instead of nil to hold load balancers to reregister --- lib/capify-ec2/capistrano.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/capify-ec2/capistrano.rb b/lib/capify-ec2/capistrano.rb index 4d5a2bf..6936488 100644 --- a/lib/capify-ec2/capistrano.rb +++ b/lib/capify-ec2/capistrano.rb @@ -121,7 +121,7 @@ def capify_ec2 roles.clear - load_balancers_to_reregister = nil # Set to nil again here, to ensure it always starts off nil for every iteration. + load_balancers_to_reregister = [] # Set to empty again here, to ensure it always starts off empty for every iteration. is_load_balanced = false load_balancer_names = false @@ -174,7 +174,6 @@ def capify_ec2 end end end - end for load_balancer_to_reregister in load_balancers_to_reregister do From cdfad6fa99868b56c65f52f70021f49a299d6548 Mon Sep 17 00:00:00 2001 From: Ed Hartwell Goose Date: Tue, 3 May 2016 18:11:09 +0100 Subject: [PATCH 6/9] Attempt to use threading to control elbs --- lib/capify-ec2.rb | 22 +++++++++++++++------- lib/capify-ec2/capistrano.rb | 25 +++++++++++++++++-------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/capify-ec2.rb b/lib/capify-ec2.rb index 5cb8cec..0dd63f9 100644 --- a/lib/capify-ec2.rb +++ b/lib/capify-ec2.rb @@ -309,17 +309,25 @@ def deregister_instance_from_named_elbs_by_dns(server_dns, load_balancer_names) instance = get_instance_by_dns(server_dns) lbs = [] + threads = [] + for load_balancer_name in load_balancer_names do - load_balancer = get_load_balancer_by_name(load_balancer_name) + threads << Thread.new({ + load_balancer = get_load_balancer_by_name(load_balancer_name) - if load_balancer - puts "[Capify-EC2] Removing instance from named ELB '#{load_balancer.id}'..." + if load_balancer + puts "[Capify-EC2] Removing instance from named ELB '#{load_balancer.id}'..." - result = elb.deregister_instances_from_load_balancer(instance.id, load_balancer.id) - raise "Unable to remove instance from ELB '#{load_balancer.id}'..." unless result.status == 200 + result = elb.deregister_instances_from_load_balancer(instance.id, load_balancer.id) + raise "Unable to remove instance from ELB '#{load_balancer.id}'..." unless result.status == 200 - lbs << load_balancer - end + lbs << load_balancer + end + }) + end + + for t in threads do + t.join end lbs diff --git a/lib/capify-ec2/capistrano.rb b/lib/capify-ec2/capistrano.rb index 6936488..906553b 100644 --- a/lib/capify-ec2/capistrano.rb +++ b/lib/capify-ec2/capistrano.rb @@ -176,15 +176,24 @@ def capify_ec2 end end + threads = [] + for load_balancer_to_reregister in load_balancers_to_reregister do - puts "[Capify-EC2] Starting registration of ELB '#{load_balancer_to_reregister.id}'" - reregistered = capify_ec2.reregister_instance_with_elb_by_dns(server_dns, load_balancer_to_reregister, 60) - if reregistered - puts "[Capify-EC2] Instance registration with ELB '#{load_balancer_to_reregister.id}' successful.".green.bold - else - puts "[Capify-EC2] Instance registration with ELB '#{load_balancer_to_reregister.id}' failed!".red.bold - raise CapifyEC2RollingDeployError.new("ELB registration timeout exceeded", server_dns) - end + threads << Thread.new({ + puts "[Capify-EC2] Starting registration of ELB '#{load_balancer_to_reregister.id}'" + + reregistered = capify_ec2.reregister_instance_with_elb_by_dns(server_dns, load_balancer_to_reregister, 60) + if reregistered + puts "[Capify-EC2] Instance registration with ELB '#{load_balancer_to_reregister.id}' successful.".green.bold + else + puts "[Capify-EC2] Instance registration with ELB '#{load_balancer_to_reregister.id}' failed!".red.bold + raise CapifyEC2RollingDeployError.new("ELB registration timeout exceeded", server_dns) + end + }) + end + + for t in threads do + t.join end puts "[Capify-EC2] Deployment successful to #{instance_dns_with_name_tag(server_dns)}.".green.bold From 1d671a7cf5f2a26084d0e26b08b64240065dd513 Mon Sep 17 00:00:00 2001 From: Ed Hartwell Goose Date: Wed, 4 May 2016 08:28:15 +0100 Subject: [PATCH 7/9] Fix bad threading code --- lib/capify-ec2.rb | 6 +++--- lib/capify-ec2/capistrano.rb | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/capify-ec2.rb b/lib/capify-ec2.rb index 0dd63f9..43cf635 100644 --- a/lib/capify-ec2.rb +++ b/lib/capify-ec2.rb @@ -312,8 +312,8 @@ def deregister_instance_from_named_elbs_by_dns(server_dns, load_balancer_names) threads = [] for load_balancer_name in load_balancer_names do - threads << Thread.new({ - load_balancer = get_load_balancer_by_name(load_balancer_name) + threads << Thread.new(load_balancer_name) do |lb| + load_balancer = get_load_balancer_by_name(lb) if load_balancer puts "[Capify-EC2] Removing instance from named ELB '#{load_balancer.id}'..." @@ -323,7 +323,7 @@ def deregister_instance_from_named_elbs_by_dns(server_dns, load_balancer_names) lbs << load_balancer end - }) + end end for t in threads do diff --git a/lib/capify-ec2/capistrano.rb b/lib/capify-ec2/capistrano.rb index 906553b..93609b7 100644 --- a/lib/capify-ec2/capistrano.rb +++ b/lib/capify-ec2/capistrano.rb @@ -179,17 +179,17 @@ def capify_ec2 threads = [] for load_balancer_to_reregister in load_balancers_to_reregister do - threads << Thread.new({ - puts "[Capify-EC2] Starting registration of ELB '#{load_balancer_to_reregister.id}'" + threads << Thread.new(load_balancer_to_reregister) do |lb| + puts "[Capify-EC2] Starting registration of ELB '#{lb.id}'" - reregistered = capify_ec2.reregister_instance_with_elb_by_dns(server_dns, load_balancer_to_reregister, 60) + reregistered = capify_ec2.reregister_instance_with_elb_by_dns(server_dns, lb, 60) if reregistered - puts "[Capify-EC2] Instance registration with ELB '#{load_balancer_to_reregister.id}' successful.".green.bold + puts "[Capify-EC2] Instance registration with ELB '#{lb.id}' successful.".green.bold else - puts "[Capify-EC2] Instance registration with ELB '#{load_balancer_to_reregister.id}' failed!".red.bold + puts "[Capify-EC2] Instance registration with ELB '#{lb.id}' failed!".red.bold raise CapifyEC2RollingDeployError.new("ELB registration timeout exceeded", server_dns) end - }) + end end for t in threads do From 2fa52b2d60e58e7c7575731792c22dfc2faddb35 Mon Sep 17 00:00:00 2001 From: Ed Hartwell Goose Date: Sun, 24 Feb 2019 18:59:06 +0000 Subject: [PATCH 8/9] Fix fog to specific version --- capify-ec2.gemspec | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/capify-ec2.gemspec b/capify-ec2.gemspec index ff04b05..c10fd8c 100644 --- a/capify-ec2.gemspec +++ b/capify-ec2.gemspec @@ -19,7 +19,8 @@ Gem::Specification.new do |s| s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] - s.add_dependency('fog', '>= 1.23.0') + s.add_dependency('fog', '=1.23.0') + s.add_dependency('aws-sdk', '~> 3') s.add_dependency('colored', '=1.2') s.add_dependency('capistrano', '~> 2.14') end From 36d254fa5d260c4af8e6e4aa51e1b76674cb3c04 Mon Sep 17 00:00:00 2001 From: Ed Hartwell Goose Date: Sun, 24 Feb 2019 19:39:55 +0000 Subject: [PATCH 9/9] Use net-ssh 3.0.2 --- capify-ec2.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/capify-ec2.gemspec b/capify-ec2.gemspec index c10fd8c..767a6ec 100644 --- a/capify-ec2.gemspec +++ b/capify-ec2.gemspec @@ -23,4 +23,5 @@ Gem::Specification.new do |s| s.add_dependency('aws-sdk', '~> 3') s.add_dependency('colored', '=1.2') s.add_dependency('capistrano', '~> 2.14') + s.add_dependency('net-ssh', '=3.0.2') end