From 8885c14fbe49183e7439633a4d5d3a0a2f9f679c Mon Sep 17 00:00:00 2001 From: Anuj Biyani Date: Mon, 10 Jun 2013 14:38:02 -0700 Subject: [PATCH] allow multiple new instances to be brought up simultaneously --- .../aws-ha-release/aws-ha-release.rb | 34 +++++++++++-------- .../aws-ha-release/aws-ha-release_spec.rb | 25 ++++++++++++++ 2 files changed, 44 insertions(+), 15 deletions(-) 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 1f6a028..7b608ee 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 @@ -1,4 +1,3 @@ -require 'timeout' require 'optparse' module AwsMissingTools @@ -31,7 +30,8 @@ module AwsMissingTools region: 'us-east-1', elb_timeout: 60, inservice_time_allowed: 300, - min_inservice_time: 30 + min_inservice_time: 30, + num_simultaneous_instances: 1 } OptionParser.new('Usage: aws-ha-release.rb -a [options]', 50) do |opts| @@ -62,6 +62,10 @@ module AwsMissingTools opts.on('-s', '--aws_secret_key AWS_SECRET_KEY', 'AWS Secret Key') do |v| options[:aws_secret_key] = v end + + opts.on('-n', '--num-simultaneous-instances NUM', 'Number of instances to simultaneously bring up per iteration') do |v| + options[:num_simultaneous_instances] = v.to_i + end end.parse!(arguments) raise OptionParser::MissingArgument, 'You must specify the AutoScaling Group Name: aws-ha-release.rb -a ' if options[:as_group_name].nil? @@ -86,15 +90,15 @@ module AwsMissingTools @group.suspend_processes PROCESSES_TO_SUSPEND if @group.max_size == @group.desired_capacity - puts "#{@group.name} has a max-size of #{@group.max_size}. In order to recycle instances max-size will be temporarily increased by 1." - @group.update(max_size: @group.max_size + 1) - @max_size_change = 1 + puts "#{@group.name} has a max-size of #{@group.max_size}. In order to recycle instances max-size will be temporarily increased by #{@opts[:num_simultaneous_instances]}." + @group.update(max_size: @group.max_size + @opts[:num_simultaneous_instances]) + @max_size_change = @opts[:num_simultaneous_instances] end - @group.update(desired_capacity: @group.desired_capacity + 1) + @group.update(desired_capacity: @group.desired_capacity + @opts[:num_simultaneous_instances]) puts "The list of instances in Auto Scaling Group #{@group.name} that will be terminated is:\n#{@group.auto_scaling_instances.map{ |i| i.ec2_instance.id }.to_ary}" - @group.auto_scaling_instances.each do |instance| + @group.auto_scaling_instances.each_slice(@opts[:num_simultaneous_instances]) do |instances| time_taken = 0 until all_instances_inservice_for_time_period?(@group.load_balancers, INSERVICE_POLLING_TIME) @@ -105,7 +109,7 @@ module AwsMissingTools puts "The following settings were changed and will not be changed back by this script:\n" puts "AutoScaling processes #{PROCESSES_TO_SUSPEND} were suspended." - puts "The desired capacity was changed from #{@group.desired_capacity - 1} to #{@group.desired_capacity}." + puts "The desired capacity was changed from #{@group.desired_capacity - @opts[:num_simultaneous_instances]} to #{@group.desired_capacity}." if @max_size_change > 0 puts "The maximum size was changed from #{@group.max_size - @max_size_change} to #{@group.max_size}" @@ -118,19 +122,19 @@ module AwsMissingTools end end - puts "\nThe new instance was found to be healthy; one old instance will now be removed from the load balancers." - deregister_instance instance.ec2_instance, @group.load_balancers + puts "\nThe new instance(s) was/were found to be healthy; #{@opts[:num_simultaneous_instances]} old instance(s) will now be removed from the load balancers." + instances.each { |instance| deregister_instance(instance.ec2_instance, @group.load_balancers) } puts "Sleeping for the ELB Timeout period of #{@opts[:elb_timeout]}" sleep @opts[:elb_timeout] - puts "\nInstance #{instance.id} will now be terminated. By terminating this instance, the actual capacity will be decreased to 1 under desired-capacity." - instance.terminate false + puts "\nInstance(s) #{instances.map{ |i| i.ec2_instance.id }.join(', ')} will now be terminated. By terminating this/these instance(s), the actual capacity will be decreased to #{@opts[:num_simultaneous_instances]} under desired-capacity." + instances.each { |instance| instance.terminate false } end - puts "\n#{@group.name} had its desired-capacity increased temporarily by 1 to a desired-capacity of #{@group.desired_capacity}." - puts "The desired-capacity of #{@group.name} will now be returned to its original desired-capacity of #{@group.desired_capacity - 1}." - @group.update(desired_capacity: @group.desired_capacity - 1) + puts "\n#{@group.name} had its desired-capacity increased temporarily by #{@opts[:num_simultaneous_instances]} to a desired-capacity of #{@group.desired_capacity}." + puts "The desired-capacity of #{@group.name} will now be returned to its original desired-capacity of #{@group.desired_capacity - @opts[:num_simultaneous_instances]}." + @group.update(desired_capacity: @group.desired_capacity - @opts[:num_simultaneous_instances]) if @max_size_change > 0 puts "\n#{@group.name} had its max_size increased temporarily by #{@max_size_change} to a max_size of #{@group.max_size}." 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 233e283..a404ae4 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 @@ -45,6 +45,7 @@ describe 'aws-ha-release' do expect(options[:aws_access_key]).not_to be_nil expect(options[:aws_secret_key]).not_to be_nil expect(options[:min_inservice_time]).not_to be_nil + expect(options[:num_simultaneous_instances]).not_to be_nil end context 'optional params' do @@ -80,6 +81,12 @@ describe 'aws-ha-release' do expect(AwsMissingTools::AwsHaRelease.parse_options(options)[:min_inservice_time]).to eq 30 end end + + it 'number of instances to simultaneously bring up' do + [%w(-a test_group -n 2), %w(-a test_group --num-simultaneous-instances 2)].each do |options| + expect(AwsMissingTools::AwsHaRelease.parse_options(options)[:num_simultaneous_instances]).to eq 2 + end + end end end @@ -116,6 +123,24 @@ describe 'aws-ha-release' do @aws_ha_release.group.should_receive(:update).with(desired_capacity: 1).ordered.and_call_original @aws_ha_release.execute! end + + it 'cycles more than one instance at a time if specified' do + @group.update(max_size: 2, desired_capacity: 2) + + aws_ha_release = AwsMissingTools::AwsHaRelease.new(%w(-a test_group --num-simultaneous-instances 2 -o testaccesskey -s testsecretkey -r test_region -i 1 -t 0 -m 5)) + aws_ha_release.stub!(:all_instances_inservice_for_time_period?).and_return(true) + + aws_ha_release.group.should_receive(:update).with(max_size: 4).ordered.and_call_original + aws_ha_release.group.should_receive(:update).with(desired_capacity: 4).ordered.and_call_original + + aws_ha_release.should_receive(:deregister_instance).twice.ordered.and_call_original + @group.auto_scaling_instances[0].should_receive(:terminate).ordered.and_call_original + @group.auto_scaling_instances[1].should_receive(:terminate).ordered.and_call_original + + aws_ha_release.group.should_receive(:update).with(desired_capacity: 2).ordered.and_call_original + aws_ha_release.group.should_receive(:update).with(max_size: 2).ordered.and_call_original + aws_ha_release.execute! + end end describe 'determining if instances are in service' do