Created
March 30, 2016 14:43
-
-
Save madeddie/0c52a4ef437af811983992f9ff936fc1 to your computer and use it in GitHub Desktop.
"script" to clean up Ecs services created for closed PR's
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 | |
# retrieve list of closed github PRs, compare to running Ecs services, stop and delete services and deregister task-definitions | |
# settings | |
github_token='YOURGITHUBTOKEN' # github token with access to organization pull-requests | |
github_repo='SOMEORG/SOMEREPO' # org/repo from which to retrieve pullrequest status | |
cluster_arn='SOME_ECS_CLUSTER' # full arn or shortname of Ecs cluster | |
# retrieve list of closed pull-requests | |
closed_pulls="^($(curl -s -G -d access_token="$github_token" https://api.github.com/repos/"$github_repo"/pulls?state=closed | jq .[].number | sort -n | paste -sd'|' -))$" | |
# match PR number in service-arns with id number of closed pull-requests (service name is of form "some-app-pr123") | |
# TODO: when more projects/repos will use this system, PR numbers might overlap, need fix | |
closed_services=$(for i in $(aws ecs list-services --cluster "$cluster_arn" --output text | awk '{print $2}'); do prnum=$(echo "$i" | egrep -o 'pr\d{1,3}' | sed 's/pr//'); if [[ "$prnum" =~ $closed_pulls ]]; then echo "$i"; fi; done) | |
# retrieve list of task-arns in use by services to be removed | |
closed_tasks=$(for service in $closed_services; do aws ecs describe-services --cluster "$cluster_arn" --services "$service" --query 'services[].taskDefinition' --output text; done) | |
# make service ready for delete | |
for i in $closed_services; do echo $i; aws ecs update-service --cluster "$cluster_arn" --service $i --desired-count 0; done | |
# delete service | |
for i in $closed_services; do echo $i; aws ecs delete-service --cluster "$cluster_arn" --service $i; done | |
# delete associated tasks (just the revision latest in use) | |
# TODO: delete all revisions | |
for i in $closed_tasks; do echo $i; aws ecs deregister-task-definition --task-definition $i; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment