Skip to content

Commit

Permalink
Add validation to ensure ecs service deployed successfully
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
raags committed Jun 7, 2020
1 parent 3982590 commit d9818a7
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions plugins/modules/ecs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit d9818a7

Please sign in to comment.