Created
September 2, 2016 17:15
-
-
Save discort/8bebc76a443d1c1cab6f6951c4a931f7 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
# more bash-friendly output for jq | |
JQ="jq --raw-output" | |
configure_aws_cli(){ | |
aws --version | |
aws configure set default.region us-east-1 | |
aws configure set default.output json | |
} | |
deploy_cluster() { | |
family="console-sample-app-static" | |
make_task_def | |
register_definition | |
if [[ $(aws ecs update-service --cluster default --service sample-webapp --task-definition $revision | \ | |
$JQ '.service.taskDefinition') != $revision ]]; then | |
echo "Error updating service." | |
return 1 | |
fi | |
# wait for older revisions to disappear | |
# not really necessary, but nice for demos | |
for attempt in {1..30}; do | |
if stale=$(aws ecs describe-services --cluster default --services sample-webapp); then | |
echo "Waiting for stale deployments:" | |
echo "$stale" | |
sleep 5 | |
else | |
echo "Deployed!" | |
return 0 | |
fi | |
done | |
echo "Service update took too long." | |
return 1 | |
} | |
make_task_def(){ | |
task_template='[ | |
{ | |
"name": "console-sample-app-static", | |
"image": "%s.dkr.ecr.us-east-1.amazonaws.com/cashflow:latest", | |
"essential": true, | |
"memory": 200, | |
"cpu": 10, | |
"portMappings": [ | |
{ | |
"containerPort": 8000, | |
"hostPort": 80 | |
} | |
] | |
} | |
]' | |
task_def=$(printf "$task_template" $AWS_ACCOUNT_ID) | |
} | |
push_ecr_image(){ | |
eval $(aws ecr get-login --region us-east-1) | |
docker push $AWS_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/cashflow:latest | |
} | |
register_definition() { | |
if revision=$(aws ecs register-task-definition --container-definitions "$task_def" --family $family | $JQ '.taskDefinition.taskDefinitionArn'); then | |
echo "Revision: $revision" | |
else | |
echo "Failed to register task definition" | |
return 1 | |
fi | |
} | |
configure_aws_cli | |
push_ecr_image | |
deploy_cluster |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment