Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ecs_* - add waiters #1209

Merged
merged 6 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/1209-ecs_service-add-waiters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- ecs_service - add ``wait`` parameter and waiter for deleting services (https://github.com/ansible-collections/community.aws/pull/1209).
- ecs_task - add ``wait`` parameter and waiter for running and stopping tasks (https://github.com/ansible-collections/community.aws/pull/1209).
24 changes: 24 additions & 0 deletions plugins/modules/ecs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@
required: false
choices: ["DAEMON", "REPLICA"]
type: str
wait:
description:
- Whether or not to wait for the service to be inactive.
- Waits only when I(state) is C(absent).
type: bool
default: false
version_added: 3.4.0
markuman marked this conversation as resolved.
Show resolved Hide resolved
tremble marked this conversation as resolved.
Show resolved Hide resolved
extends_documentation_fragment:
- amazon.aws.aws
- amazon.aws.ec2
Expand Down Expand Up @@ -728,6 +735,7 @@ def main():
force_new_deployment=dict(required=False, default=False, type='bool'),
force_deletion=dict(required=False, default=False, type='bool'),
deployment_configuration=dict(required=False, default={}, type='dict'),
wait=dict(required=False, default=False, type='bool'),
placement_constraints=dict(
required=False,
default=[],
Expand Down Expand Up @@ -912,8 +920,24 @@ def main():
module.params['cluster'],
module.params['force_deletion'],
)

# Wait for service to be INACTIVE prior to exiting
if module.params['wait']:
waiter = service_mgr.ecs.get_waiter('services_inactive')
try:
waiter.wait(
services=[module.params['name']],
cluster=module.params['cluster'],
WaiterConfig={
'Delay': module.params['delay'],
'MaxAttempts': module.params['repeat']
}
)
except botocore.exceptions.WaiterError as e:
module.fail_json_aws(e, 'Timeout waiting for service removal')
except botocore.exceptions.ClientError as e:
module.fail_json_aws(e, msg="Couldn't delete service")

results['changed'] = True

elif module.params['state'] == 'deleting':
Expand Down
42 changes: 40 additions & 2 deletions plugins/modules/ecs_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
- Tags that will be added to ecs tasks on start and run
required: false
aliases: ['resource_tags']
wait:
description:
- Whether or not to wait for the desired state.
type: bool
default: false
version_added: 3.4.0
tremble marked this conversation as resolved.
Show resolved Hide resolved
extends_documentation_fragment:
- amazon.aws.aws
- amazon.aws.ec2
Expand Down Expand Up @@ -351,7 +357,8 @@ def main():
started_by=dict(required=False, type='str'), # R S
network_configuration=dict(required=False, type='dict'),
launch_type=dict(required=False, choices=['EC2', 'FARGATE']),
tags=dict(required=False, type='dict', aliases=['resource_tags'])
tags=dict(required=False, type='dict', aliases=['resource_tags']),
wait=dict(required=False, default=False, type='bool'),
)

module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True,
Expand Down Expand Up @@ -393,7 +400,9 @@ def main():
results['task'] = existing
else:
if not module.check_mode:
results['task'] = service_mgr.run_task(

# run_task returns a list of tasks created
tasks = service_mgr.run_task(
module.params['cluster'],
module.params['task_definition'],
module.params['overrides'],
Expand All @@ -402,6 +411,21 @@ def main():
module.params['launch_type'],
module.params['tags'],
)

# Wait for task(s) to be running prior to exiting
if module.params['wait']:

waiter = service_mgr.ecs.get_waiter('tasks_running')
try:
waiter.wait(
tasks=[task['taskArn'] for task in tasks],
cluster=module.params['cluster'],
)
except botocore.exceptions.WaiterError as e:
module.fail_json_aws(e, 'Timeout waiting for tasks to run')

results['task'] = tasks

results['changed'] = True

elif module.params['operation'] == 'start':
Expand All @@ -418,6 +442,7 @@ def main():
module.params['started_by'],
module.params['tags'],
)

results['changed'] = True

elif module.params['operation'] == 'stop':
Expand All @@ -431,6 +456,19 @@ def main():
module.params['cluster'],
module.params['task']
)

# Wait for task to be stopped prior to exiting
if module.params['wait']:

waiter = service_mgr.ecs.get_waiter('tasks_stopped')
try:
waiter.wait(
tasks=[module.params['task']],
cluster=module.params['cluster'],
)
except botocore.exceptions.WaiterError as e:
module.fail_json_aws(e, 'Timeout waiting for task to stop')

results['changed'] = True

module.exit_json(**results)
Expand Down
Loading