diff --git a/License2Deploy/rolling_deploy.py b/License2Deploy/rolling_deploy.py index 475db01..f90223d 100644 --- a/License2Deploy/rolling_deploy.py +++ b/License2Deploy/rolling_deploy.py @@ -23,7 +23,8 @@ def __init__(self, session=None, creation_wait=[10, 60], ready_wait=[10, 30], - health_wait=[10, 30]): + health_wait=[10, 30], + only_new_wait=[10, 30]): self.env = env self.session = session self.project = project.replace('-','') @@ -46,6 +47,7 @@ def __init__(self, self.creation_wait = creation_wait self.ready_wait = ready_wait self.health_wait = health_wait + self.only_new_wait = only_new_wait def get_ami_id_state(self, ami_id): try: @@ -201,19 +203,23 @@ def lb_healthcheck(self, new_ids): else: logging.info('ELB healthcheck OK') return True - - def confirm_lb_has_only_new_instances(self, wait_time=60): - ''' Confirm that only new instances with the current build tag are in the load balancer ''' - sleep(wait_time) # Allotting time for the instances to shut down + + def only_new_instances_check(self): instance_ids = self.conn_elb.describe_instance_health(self.load_balancer) for instance in instance_ids: build = self.conn_ec2.get_all_reservations(instance.instance_id)[0].instances[0].tags['BUILD'] if build != self.build_number: - logging.error("There is still an old instance in the ELB: {0}. Please investigate".format(instance)) - exit(self.exit_error_code) + raise Exception("There is still an old instance in the ELB: {0}.".format(instance)) logging.info("Deployed instances {0} to ELB: {1}".format(instance_ids, self.load_balancer)) return instance_ids + def confirm_lb_has_only_new_instances(self): + try: + return retry_call(self.only_new_instances_check, tries=self.only_new_wait[0], delay=self.only_new_wait[1], logger=logging) + except Exception as e: + logging.error("There are still old instances in the ELB. Please investigate.") + exit(self.exit_error_code) + def tag_ami(self, ami_id, env): ''' Tagging AMI with DEPLOYED tag ''' try: @@ -336,12 +342,13 @@ def get_args(): # pragma: no cover parser.add_argument('-C', '--creation-wait', action='store', dest='creation_wait', help='Wait time for ec2 instance creation', type=int, nargs=2, default=[10, 60]) parser.add_argument('-r', '--ready-wait', action='store', dest='ready_wait', help='Wait time for ec2 instance to be ready', type=int, nargs=2, default=[10, 30]) parser.add_argument('-H', '--health-wait', action='store', dest='health_wait', help='Wait time for ec2 instance health check', type=int, nargs=2, default=[10, 30]) + parser.add_argument('-o', '--only-new-wait', action='store', dest='only_new_wait', help='Wait time for old ec2 instances to terminate', type=int, nargs=2, default=[10, 30]) return parser.parse_args() def main(): # pragma: no cover args = get_args() SetLogging.setup_logging() - deployObj = RollingDeploy(args.env, args.project, args.build_number, args.ami_id, args.profile, args.config, args.stack_name, None, args.creation_wait, args.ready_wait, args.health_wait) + deployObj = RollingDeploy(args.env, args.project, args.build_number, args.ami_id, args.profile, args.config, args.stack_name, None, args.creation_wait, args.ready_wait, args.health_wait, args.only_new_wait) deployObj.deploy() if __name__ == "__main__": # pragma: no cover diff --git a/README.md b/README.md index a18ab5e..88c4725 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Usage ``` usage: rolling_deploy.py [-h] -e ENV -p PROJECT -b BUILD_NUM -a AMI_ID [-P PROFILE] [-c CONFIG] [-s STACK_NAME] - [-C CREATION_WAIT] [-r READY_WAIT] [-H HEALTH_WAIT] + [-C CREATION_WAIT] [-r READY_WAIT] [-H HEALTH_WAIT] [-o ONLY_NEW_WAIT] optional arguments: -h, --help show this help message and exit @@ -54,6 +54,10 @@ optional arguments: Time to wait for EC2 instances to be health checked (# of tries, interval of each try in seconds), default (10, 30) e.g. -H 10 30 + -o ONLY_NEW_WAIT, --only-new-wait ONLY_NEW_WAIT + Time to wait for old EC2 instances to terminate + (# of tries, interval of each try in seconds), default (10, 30) + e.g. -o 10 30 ``` Requirements ================== diff --git a/tests/rolling_deploy_test.py b/tests/rolling_deploy_test.py index 4571161..dfe0103 100644 --- a/tests/rolling_deploy_test.py +++ b/tests/rolling_deploy_test.py @@ -205,7 +205,7 @@ def test_wait_ami_availability(self): def test_confirm_lb_has_only_new_instances(self): instance_ids = self.setUpEC2()[1] self.rolling_deploy.load_balancer = self.rolling_deploy.get_lb() - self.assertEqual(len(instance_ids), len(self.rolling_deploy.confirm_lb_has_only_new_instances(1))) #Return All LB's with the proper build number + self.assertEqual(len(instance_ids), len(self.rolling_deploy.confirm_lb_has_only_new_instances())) #Return All LB's with the proper build number @mock_elb def test_get_lb(self):