diff --git a/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb b/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb index 4658080..f3fb192 100755 --- a/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb +++ b/lib/aws-missing-tools/aws-ha-release/aws-ha-release.rb @@ -12,6 +12,7 @@ class AwsHaRelease end @max_size_change = 0 + @inservice_polling_time = 10 end def execute! @@ -23,6 +24,30 @@ class AwsHaRelease end @group.update(desired_capacity: @group.desired_capacity + 1) + + @group.ec2_instances.each do |instance| + until all_instances_inservice?(@group.load_balancers) + sleep @inservice_polling_time + end + + deregister_instance instance, @group.load_balancers + sleep opts[:elb_timeout] + instance.terminate false + end + + @group.update(desired_capacity: @group.desired_capacity - 1) + + if @max_size_change > 0 + @group.update(max_size: @group.max_size - @max_size_change) + end + + @group.resume_all_processes + end + + def deregister_instance(instance, load_balancers) + load_balancers.each do |load_balancer| + load_balancer.instances.deregister instance + end end def instances_inservice?(load_balancer) diff --git a/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb b/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb index 8f9b0b1..3511827 100644 --- a/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb +++ b/spec/aws-missing-tools/aws-ha-release/aws-ha-release_spec.rb @@ -13,7 +13,6 @@ describe 'aws-ha-release' do let(:as) { AWS::FakeAutoScaling.new } before do - AWS.stub(:config) AWS::AutoScaling.stub(:new).and_return(as) end @@ -101,4 +100,30 @@ describe 'aws-ha-release' do end end end + + describe '#deregister_instance' do + before do + @group = as.groups.create opts[:as_group_name] + @aws_ha_release = AwsHaRelease.new(opts) + end + + it 'deregisters an instance across all load balancers' do + instance_one = AWS::FakeEC2::Instance.new + instance_two = AWS::FakeEC2::Instance.new + + elb_one = AWS::FakeELB::LoadBalancer.new 'test_load_balancer_01' + elb_two = AWS::FakeELB::LoadBalancer.new 'test_load_balancer_02' + + elb_one.instances.register instance_one + elb_one.instances.register instance_two + + elb_two.instances.register instance_one + elb_two.instances.register instance_two + + @aws_ha_release.deregister_instance instance_one, [elb_one, elb_two] + + expect(elb_one.instances).not_to include instance_one + expect(elb_two.instances).not_to include instance_one + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8f90512..573217f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,4 +5,8 @@ Dir['spec/support/**/*.rb'].each { |f| require File.expand_path(f) } RSpec.configure do |config| config.order = 'random' + + config.before do + AWS.stub! + end end diff --git a/spec/support/fake_auto_scaling.rb b/spec/support/fake_auto_scaling.rb index 460b772..7d8bc55 100644 --- a/spec/support/fake_auto_scaling.rb +++ b/spec/support/fake_auto_scaling.rb @@ -42,6 +42,10 @@ module AWS self.instance_variable_set "@#{key}", value end end + + def ec2_instances + [] + end end end end diff --git a/spec/support/fake_elb.rb b/spec/support/fake_elb.rb index b581fa9..6713c2c 100644 --- a/spec/support/fake_elb.rb +++ b/spec/support/fake_elb.rb @@ -12,10 +12,20 @@ module AWS end end - class InstanceCollection + class InstanceCollection < Array def initialize end + def register(*instances) + self.concat instances + end + + def deregister(*instances) + instances.each do |i| + self.delete i + end + end + def health @health ||= [ {