From 6716640e8d8e6ddd878558eae5237bf7b54b189d Mon Sep 17 00:00:00 2001 From: Zhang Date: Fri, 21 Jul 2017 16:24:05 -0500 Subject: [PATCH] DEVOPS-9370: use old capacity value when scaling down --- License2Deploy/rolling_deploy.py | 32 ++++++++++++-------------------- tests/rolling_deploy_test.py | 8 +------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/License2Deploy/rolling_deploy.py b/License2Deploy/rolling_deploy.py index c991001..9ba34e9 100644 --- a/License2Deploy/rolling_deploy.py +++ b/License2Deploy/rolling_deploy.py @@ -52,6 +52,7 @@ def __init__(self, self.health_wait = health_wait self.only_new_wait = only_new_wait self.existing_instance_ids = [] + self.old_desired_capacity = 2 self.new_desired_capacity = None def get_ami_id_state(self, ami_id): @@ -123,24 +124,19 @@ def get_lb(self): def calculate_autoscale_desired_instance_count(self, group_name, desired_state): ''' Search via specific autoscale group name to return modified desired instance count ''' try: - cur_count = int(self.get_group_info(group_name)[0].desired_capacity) if desired_state == 'increase': - new_count = self.double_autoscale_instance_count(cur_count) + self.old_desired_capacity = int(self.get_group_info(group_name)[0].desired_capacity) + self.new_desired_capacity = self.old_desired_capacity * 2 + logging.info("Current desired count was changed from {0} to {1}".format(self.old_desired_capacity, self.new_desired_capacity)) elif desired_state == 'decrease': - new_count = self.decrease_autoscale_instance_count(cur_count) - logging.info("Current desired count was changed from {0} to {1}".format(cur_count, new_count)) - return new_count + logging.info("Current desired count was changed from {0} to {1}".format(self.new_desired_capacity, self.old_desired_capacity)) + self.new_desired_capacity = self.old_desired_capacity + else: + raise Exception("Please make sure the desired_state is set to either increase or decrease") + return self.new_desired_capacity except Exception as e: - logging.error("Please make sure the desired_state is set to either increase or decrease: {0}".format(e)) + logging.error(e) exit(self.exit_error_code) - - def double_autoscale_instance_count(self, count): - ''' Multiply current count by 2 ''' - return count * 2 - - def decrease_autoscale_instance_count(self, count): - ''' Divide current count in half ''' - return count / 2 def set_autoscale_instance_desired_count(self, new_count, group_name): ''' Increase desired count by double ''' @@ -191,16 +187,13 @@ def get_instance_ids_by_requested_build_tag(self, id_list, build): and 'BUILD' in inst.tags and inst.tags['BUILD'] == str(build)] - if len(new_instances) < self.get_new_instances_count(): + if len(new_instances) < self.old_desired_capacity: raise Exception('Not all new instances with build number "{0}" are in the group'.format(self.build_number)) else: ip_dict = self.get_instance_ip_addrs(new_instances) logging.info("New Instance List with IP Addresses: {0}".format(ip_dict)) return new_instances - def get_new_instances_count(self): - return self.new_desired_capacity / 2 - def wait_for_new_instances(self, instance_ids, retry=10, wait_time=30): ''' Monitor new instances that come up and wait until they are ready ''' for instance in instance_ids: @@ -363,8 +356,7 @@ def deploy(self): # pragma: no cover if not self.force_redeploy and self.is_redeploy(): self.stop_deploy('You are attempting to redeploy the same build. Please pass the force_redeploy flag if a redeploy is desired') self.disable_project_cloudwatch_alarms() - self.new_desired_capacity = self.calculate_autoscale_desired_instance_count(group_name, 'increase') - self.set_autoscale_instance_desired_count(self.new_desired_capacity, group_name) + self.set_autoscale_instance_desired_count(self.calculate_autoscale_desired_instance_count(group_name, 'increase'), group_name) self.launch_new_instances(group_name) self.set_autoscale_instance_desired_count(self.calculate_autoscale_desired_instance_count(group_name, 'decrease'), group_name) self.confirm_lb_has_only_new_instances() diff --git a/tests/rolling_deploy_test.py b/tests/rolling_deploy_test.py index e144195..5e025ad 100644 --- a/tests/rolling_deploy_test.py +++ b/tests/rolling_deploy_test.py @@ -276,7 +276,7 @@ def test_calculate_autoscale_desired_instance_count(self): increase = self.rolling_deploy.calculate_autoscale_desired_instance_count(self.GMS_AUTOSCALING_GROUP_STG, 'increase') decrease = self.rolling_deploy.calculate_autoscale_desired_instance_count(self.GMS_AUTOSCALING_GROUP_STG, 'decrease') self.assertEqual(increase, 4) - self.assertEqual(decrease, 1) + self.assertEqual(decrease, 2) @mock_autoscaling_deprecated def test_calculate_autoscale_desired_instance_count_failure(self): @@ -384,12 +384,6 @@ def test_wait_for_new_instances_failure(self): def test_set_autoscale_instance_desired_count_failure(self): self.assertRaises(SystemExit, lambda: self.rolling_deploy.set_autoscale_instance_desired_count(4, self.GMS_AUTOSCALING_GROUP_STG)) - def test_double_autoscale_instance_count(self): - self.assertEqual(self.rolling_deploy.double_autoscale_instance_count(2), 4) - - def test_decrease_autoscale_instance_count(self): - self.assertEqual(self.rolling_deploy.decrease_autoscale_instance_count(4), 2) - def main(): unittest.main()