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

Enable run_task command to wait until the executions ends #231

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 19 additions & 2 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ workflows:
aws-resource-name-prefix: ${AWS_RESOURCE_NAME_PREFIX_FARGATE}
terraform-config-dir: "tests/terraform_setup/fargate"
context: [CPE-OIDC]
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
- build-test-app:
name: fargate_build-test-app
docker-image-namespace: "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com"
Expand Down Expand Up @@ -574,11 +574,28 @@ workflows:
run_task_output: "run_task_output.json"
overrides: '{"containerOverrides":[{"name": "${INTERPOLATION_TEST}", "memory": 512}]}'
context: [CPE-OIDC]
- aws-ecs/run_task:
name: ec2_run_task_and_wait-test
auth:
- aws-cli/setup:
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
filters: *filters
requires:
- ec2_set-up-run_task-test
cluster: "${AWS_RESOURCE_NAME_PREFIX_EC2}-cluster"
task_definition: "${AWS_RESOURCE_NAME_PREFIX_EC2}-sleep360"
launch_type: "EC2"
awsvpc: false
overrides: '{"containerOverrides":[{"name": "${INTERPOLATION_TEST}", "memory": 512}]}'
context: [CPE-OIDC]
wait_task_stopped: true
exit_code_from: sleep
- tear-down-run_task-test:
name: ec2_tear-down-run_task-test
filters: *filters
requires:
- ec2_run_task-test
- ec2_run_task_and_wait-test
family_name: ${AWS_RESOURCE_NAME_PREFIX_EC2}-sleep360
context: [CPE-OIDC]
role_arn: "arn:aws:iam::122211685980:role/CPE_ECS_OIDC_TEST"
Expand Down Expand Up @@ -846,7 +863,7 @@ commands:
else
PORT="<< parameters.port >>"
fi

set -x
TARGET_GROUP_ARN=$(aws ecs describe-services --cluster << parameters.cluster >> --services << parameters.service_name >> $PROFILE | jq -r '.services[0].loadBalancers[0].targetGroupArn')
ELB_ARN=$(aws elbv2 describe-target-groups --target-group-arns $TARGET_GROUP_ARN $PROFILE | jq -r '.TargetGroups[0].LoadBalancerArns[0]')
Expand Down
10 changes: 10 additions & 0 deletions src/commands/run_task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ parameters:
description: AWS region to use. Defaults to AWS_DEFAULT_REGION environment variable.
type: string
default: $AWS_DEFAULT_REGION
wait_task_stopped:
description: Wait until the task execution ends. Doesn't work with `run_task_output`.
type: boolean
default: false
exit_code_from:
description: Name of the container which exit code will be returned if wait_task_stopped is true. Defaults to the first container.
type: string
default: ''
steps:
- run:
name: Run Task
Expand Down Expand Up @@ -155,3 +163,5 @@ steps:
ORB_STR_CD_CAPACITY_PROVIDER_STRATEGY: <<parameters.capacity_provider_strategy>>
ORB_STR_PROFILE_NAME: <<parameters.profile_name>>
ORB_STR_RUN_TASK_OUTPUT: <<parameters.run_task_output>>
ORB_BOOL_WAIT_TASK_STOPPED: <<parameters.wait_task_stopped>>
ORB_STR_EXIT_CODE_FROM: <<parameters.exit_code_from>>
27 changes: 27 additions & 0 deletions src/examples/run_task_and_wait.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
description: Start the execution of an ECS task and wait until the task is completed.
usage:
version: 2.1
orbs:
aws-ecs: circleci/[email protected]
aws-cli: circleci/[email protected]
jobs:
run_task:
docker:
- image: cimg/python:3.10
steps:
- aws-cli/setup:
profile_name: "OIDC-USER"
role_arn: "arn:aws:iam::123456789012:role/VALID_OIDC_ECS_ROLE"
- aws-ecs/run_task:
cluster: $CLUSTER_NAME
launch_type: ""
region: us-east-1
task_definition: $My_Task_Def
subnet_ids: '$SUBNET_ONE, $SUBNET_TWO'
security_group_ids: $SECURITY_GROUP_IDS_FETCHED\
wait_task_stopped: true
exit_code_from: app
workflows:
run_task:
jobs:
- run_task
10 changes: 10 additions & 0 deletions src/jobs/run_task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ parameters:
description: The executor to use for this job. By default, this will use the "default" executor provided by this orb.
type: executor
default: default
wait_task_stopped:
description: Wait until the task execution ends. Doesn't work with `run_task_output`.
type: boolean
default: false
exit_code_from:
description: Name of the container which exit code will be returned if wait_task_stopped is true. Defaults to the first container.
type: string
default: ''
executor: << parameters.executor >>
steps:
- steps: << parameters.auth >>
Expand All @@ -179,3 +187,5 @@ steps:
capacity_provider_strategy: << parameters.capacity_provider_strategy >>
profile_name: << parameters.profile_name >>
run_task_output: <<parameters.run_task_output>>
wait_task_stopped: <<parameters.wait_task_stopped>>
exit_code_from: <<parameters.exit_code_from>>
66 changes: 61 additions & 5 deletions src/scripts/run_task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ORB_STR_RUN_TASK_OUTPUT="$(circleci env subst "$ORB_STR_RUN_TASK_OUTPUT")"
ORB_STR_PROFILE_NAME="$(circleci env subst "$ORB_STR_PROFILE_NAME")"
ORB_STR_ASSIGN_PUB_IP="$(circleci env subst "$ORB_STR_ASSIGN_PUB_IP")"
ORB_AWS_REGION="$(circleci env subst "$ORB_AWS_REGION")"
ORB_STR_EXIT_CODE_FROM="$(circleci env subst "$ORB_STR_EXIT_CODE_FROM")"

if [[ "$ORB_STR_OVERRIDES" == *"\${"* ]]; then
ORB_STR_OVERRIDES="$(echo "${ORB_STR_OVERRIDES}" | circleci env subst)"
Expand Down Expand Up @@ -77,7 +78,6 @@ if [ -n "$ORB_STR_CD_CAPACITY_PROVIDER_STRATEGY" ]; then
# shellcheck disable=SC2086
set -- "$@" --capacity-provider-strategy $ORB_STR_CD_CAPACITY_PROVIDER_STRATEGY
fi

if [ -n "$ORB_VAL_LAUNCH_TYPE" ]; then
if [ -n "$ORB_STR_CD_CAPACITY_PROVIDER_STRATEGY" ]; then
echo "Error: "
Expand All @@ -88,7 +88,10 @@ if [ -n "$ORB_VAL_LAUNCH_TYPE" ]; then
set -- "$@" --launch-type "$ORB_VAL_LAUNCH_TYPE"
fi
fi

if [ "$ORB_BOOL_WAIT_TASK_STOPPED" == "1" ]; then
echo "Setting --query to export taskArn"
set -- "$@" --query 'tasks[].taskArn' --output text
fi

echo "Setting --count"
set -- "$@" --count "$ORB_INT_COUNT"
Expand All @@ -97,13 +100,66 @@ set -- "$@" --task-definition "$ORB_STR_TASK_DEF"
echo "Setting --cluster"
set -- "$@" --cluster "$ORB_STR_CLUSTER_NAME"


if [ -n "${ORB_STR_RUN_TASK_OUTPUT}" ]; then
if [ "$ORB_BOOL_WAIT_TASK_STOPPED" == "1" ]; then
echo "Exporting the run_task_output parameter is not compatible with wait_task_stopped parameter."
exit 1
fi

set -x
aws ecs run-task --profile "${ORB_STR_PROFILE_NAME}" --region "${ORB_AWS_REGION}" "$@" | tee "${ORB_STR_RUN_TASK_OUTPUT}"
set +x
else
set -x
aws ecs run-task --profile "${ORB_STR_PROFILE_NAME}" --region "${ORB_AWS_REGION}" "$@"
set -x
ORB_STR_TASK_ARN=$(aws ecs run-task --profile "${ORB_STR_PROFILE_NAME}" --region "${ORB_AWS_REGION}" "$@")
set +x
fi

if [ "$ORB_BOOL_WAIT_TASK_STOPPED" == "1" ]; then
if [ -z "${ORB_STR_TASK_ARN}" ]; then
exit 1
fi

echo "Waiting for ECS task $ORB_STR_TASK_ARN to stop..."

ORB_STR_WAIT_EXIT_CODE=$(aws ecs wait tasks-stopped \
--profile "${ORB_STR_PROFILE_NAME}" \
--region "${ORB_AWS_REGION}" \
--cluster "${ORB_STR_CLUSTER_NAME}" \
--tasks "${ORB_STR_TASK_ARN}"; \
echo $?
)

if [[ "${ORB_STR_WAIT_EXIT_CODE}" -ne 0 ]]; then
echo "Stopped waiting for the task execution to end. Please check the status of $ORB_STR_TASK_ARN on the AWS ECS console."
exit "${ORB_STR_WAIT_EXIT_CODE}"
fi

# Get exit code
if [ -n "$ORB_STR_EXIT_CODE_FROM" ]; then
ORB_STR_TASK_EXIT_CODE=$(aws ecs describe-tasks \
--profile "${ORB_STR_PROFILE_NAME}" \
--region "${ORB_AWS_REGION}" \
--cluster "${ORB_STR_CLUSTER_NAME}" \
--tasks "${ORB_STR_TASK_ARN}" \
--query "tasks[0].containers[?name=='$ORB_STR_EXIT_CODE_FROM'].exitCode" \
--output text)
else
# Assume the first container
ORB_STR_TASK_EXIT_CODE=$(aws ecs describe-tasks \
--profile "${ORB_STR_PROFILE_NAME}" \
--region "${ORB_AWS_REGION}" \
--cluster "${ORB_STR_CLUSTER_NAME}" \
--tasks "${ORB_STR_TASK_ARN}" \
--query "tasks[0].containers[0].exitCode" \
--output text)
fi

if [ "${ORB_STR_TASK_EXIT_CODE:-1}" == "0" ]; then
echo "The task execution ended successfully."
else
echo "The task execution ended with an error, please check the status and logs of $ORB_STR_TASK_ARN on the AWS ECS console."
fi

exit "${ORB_STR_TASK_EXIT_CODE:-1}"
fi