From d9818a7691bb6396255c7da42c54d5c3148d8022 Mon Sep 17 00:00:00 2001 From: Raghu Udiyar Date: Sun, 7 Jun 2020 18:12:49 +0530 Subject: [PATCH] Add validation to ensure ecs service deployed successfully This expands the existing delay and repeat parameters to also check if a service is deployed successfully. This is done by checking the running count is equal to the desired count for the active deployment. When delay is set to 0, it'll disable this check. --- plugins/modules/ecs_service.py | 51 +++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/plugins/modules/ecs_service.py b/plugins/modules/ecs_service.py index 7761d3d3a26..fee67c7acc1 100644 --- a/plugins/modules/ecs_service.py +++ b/plugins/modules/ecs_service.py @@ -72,13 +72,14 @@ type: str delay: description: - - The time to wait before checking that the service is available. + - The time to wait before checking that the service is available or + removed. Set to 0 to disable check. required: false default: 10 type: int repeat: description: - - The number of times to check that the service is available. + - The number of times to check that the service is available or removed required: false default: 10 type: int @@ -794,7 +795,30 @@ def main(): except botocore.exceptions.ClientError as e: module.fail_json_aws(e, msg="Couldn't create service") - results['service'] = response + delay = module.params['delay'] + repeat = module.params['repeat'] + existing = None + if delay != 0: + time.sleep(delay) + while repeat != 0: + existing = service_mgr.describe_service(module.params['cluster'], module.params['name']) + primary_deployment = [d for d in + existing['deployments'] + if d['status'] == 'PRIMARY'] + if (primary_deployment and + primary_deployment[0]['desiredCount'] == primary_deployment[0]['runningCount']): + break + + repeat -= 1 + if repeat == 0: + module.fail_json( + service=existing, + msg="Couldn't verify successful deployment, tried with delay %s, %s times" % + (delay, module.params['repeat']) + ) + else: + time.sleep(delay) + results['service'] = existing or response results['changed'] = True @@ -828,17 +852,18 @@ def main(): # return info about the cluster deleted delay = module.params['delay'] repeat = module.params['repeat'] - time.sleep(delay) - for i in range(repeat): - existing = service_mgr.describe_service(module.params['cluster'], module.params['name']) - status = existing['status'] - if status == "INACTIVE": - results['changed'] = True - break + if delay != 0: time.sleep(delay) - if i is repeat - 1: - module.fail_json(msg="Service still not deleted after " + str(repeat) + " tries of " + str(delay) + " seconds each.") - return + for i in range(repeat): + existing = service_mgr.describe_service(module.params['cluster'], module.params['name']) + status = existing['status'] + if status == "INACTIVE": + results['changed'] = True + break + time.sleep(delay) + if i is repeat - 1: + module.fail_json(msg="Service still not deleted after " + str(repeat) + " tries of " + str(delay) + " seconds each.") + return module.exit_json(**results)