Created
September 19, 2018 02:11
-
-
Save jochen42/e3011dbdebb33de8e215d5e3efc0dc70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Script for unique running an aws_ecs_task and wait until it is finished. | |
# | |
# ONLY TESTED FOR FARGATE_CLUSTERS | |
# | |
# REQUIREMENTS | |
# * aws cli | |
# * AWS-credential ENV's | |
# | |
# Example Usage: | |
# ./runtask_test.sh -r "eu-central-1" -c "vms-prod-app-frontend" -s "migration" -l "FARGATE" -t "vms-prod-frontend-api-router" -o '{"containerOverrides": [{"name": "vms-prod-frontend-api-router", "command": ["kong", "migrations", "up"]}]}' -n '{"awsvpcConfiguration": {"subnets": ["subnet-0dab18f06351fce1f"], "securityGroups": ["sg-0bd674652dbe13f31"], "assignPublicIp": "DISABLED"}}' | |
# | |
REGION="" | |
CLUSTER="" | |
STARTED_BY="" | |
LAUNCH_TYPE="" | |
TASK_DEFINITION="" | |
OVERRIDES="" | |
NETWORK_CONFIGURATION="" | |
### | |
# get arguments | |
while getopts ':r:c:s:l:t:o:n:' opt; do | |
echo "${opt}: ${OPTARG}" | |
case ${opt} in | |
r) | |
REGION=${OPTARG} | |
;; | |
c) | |
CLUSTER=${OPTARG} | |
;; | |
s) | |
STARTED_BY=${OPTARG} | |
;; | |
l) | |
LAUNCH_TYPE=${OPTARG} | |
;; | |
t) | |
TASK_DEFINITION=${OPTARG} | |
;; | |
o) | |
OVERRIDES=${OPTARG} | |
;; | |
n) | |
NETWORK_CONFIGURATION=${OPTARG} | |
;; | |
esac | |
done | |
#### | |
# check arguments | |
if [ -z "$REGION" ]; then | |
echo "-r <region> is required" | |
exit 1 | |
fi | |
if [ -z "$CLUSTER" ]; then | |
echo "-c <clustername> is required" | |
exit 1 | |
fi | |
if [ -z "$STARTED_BY" ]; then | |
STARTED_BY=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1) | |
fi | |
if [ -z "$LAUNCH_TYPE" ]; then | |
LAUNCH_TYPE="EC2" | |
fi | |
if [ -z "$TASK_DEFINITION" ]; then | |
echo "-t <task definition> is required" | |
exit 1 | |
fi | |
if [ -z "$OVERRIDES" ]; then | |
OVERRIDES="{}" | |
fi | |
if [ -z "$NETWORK_CONFIGURATION" ]; then | |
NETWORK_CONFIGURATION="{}" | |
fi | |
### | |
# GLOBALS | |
WAIT_TIMEOUT="1s" | |
START=$(date +%s) | |
checkRunningTasksStartedBy() | |
{ | |
RUNNING_TASK_COUNT=`aws ecs list-tasks \ | |
--region ${REGION} \ | |
--cluster ${CLUSTER} \ | |
--started-by ${STARTED_BY} \ | |
--output text | wc -l` | |
} | |
getStatusForTaskArn() | |
{ | |
TASK_ARN=$1 | |
STATUS=`aws ecs describe-tasks \ | |
--region ${REGION} \ | |
--cluster ${CLUSTER} \ | |
--task ${TASK_ARN} \ | |
--output text \ | |
--query "tasks[0].containers[0].lastStatus"` | |
} | |
getExitCodeForTaskArn() | |
{ | |
TASK_ARN=$1 | |
EXIT_CODE=`aws ecs describe-tasks \ | |
--region ${REGION} \ | |
--cluster ${CLUSTER} \ | |
--task ${TASK_ARN} \ | |
--query "tasks[0].containers[0].exitCode"` | |
} | |
runTaskStartedBy() | |
{ | |
TASK_ARN=`aws ecs run-task \ | |
--region ${REGION} \ | |
--cluster ${CLUSTER} \ | |
--started-by ${STARTED_BY} \ | |
--launch-type ${LAUNCH_TYPE} \ | |
--task-definition ${TASK_DEFINITION} \ | |
--overrides "${OVERRIDES}" \ | |
--network-configuration "${NETWORK_CONFIGURATION}" \ | |
--output text \ | |
--query 'tasks[0].taskArn'` | |
} | |
RUNNING_TASK_COUNT=1 | |
while [ ${RUNNING_TASK_COUNT} -ne 0 ]; do | |
checkRunningTasksStartedBy | |
NOW=$(date +%s) | |
if [ ${RUNNING_TASK_COUNT} -ne 0 ]; | |
then | |
echo "${RUNNING_TASK_COUNT} Migration-Tasks already running. Wait 5 seconds for exit. $(( $NOW - $START ))s elapsed." | |
sleep ${WAIT_TIMEOUT} | |
else | |
echo "No Migration-Tasks is running, starting it now. $(( $NOW - $START ))s elapsed." | |
break | |
fi | |
done | |
runTaskStartedBy | |
if [ $? -ne 0 ]; then | |
NOW=$(date +%s) | |
echo "Could not start task. Exit with error. $(( $NOW - $START ))s elapsed." | |
exit 1 | |
fi | |
echo "Task started: ${TASK_ARN}" | |
STATUS="RUNNING" | |
while [ "${STATUS}" != "STOPPED" ]; do | |
getStatusForTaskArn "${TASK_ARN}" | |
NOW=$(date +%s) | |
if [ "${STATUS}" != "STOPPED" ]; | |
then | |
echo "The Migration-Tasks is running. Wait 5 seconds for exit. $(( $NOW - $START ))s elapsed." | |
sleep ${WAIT_TIMEOUT} | |
else | |
NOW=$(date +%s) | |
echo "The Migration-Tasks has finished. Checking exit-code now. $(( $NOW - $START ))s elapsed." | |
getExitCodeForTaskArn "${TASK_ARN}" | |
NOW=$(date +%s) | |
if [ "${EXIT_CODE}" -ne "0" ]; | |
then | |
echo "Exit code is ${EXIT_CODE}. Exiting with error. $(( $NOW - $START ))s elapsed." | |
exit 1 | |
else | |
echo "Exit code is 0. Well done. $(( $NOW - $START ))s elapsed." | |
exit 0 | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment