Last active
September 4, 2019 10:09
-
-
Save nirdosh17/72f7ed77412002aaa38548831fc688b5 to your computer and use it in GitHub Desktop.
Shows ECR image tags which currently deployed to ECS clusters
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/bash | |
# -------------------- Usage --------------------- | |
# Opt 1. Download the script locally and run: | |
# ❯❯❯ curl -s https://gist.githubusercontent.com/nirdosh17/72f7ed77412002aaa38548831fc688b5/raw -o ~/cluster_info.sh && chmod +x ~/cluster_info.sh && ./cluster_info.sh | |
# Opt 2. If you don't want to download the script locally, run from URL: | |
# ❯❯❯ bash <(curl -s https://gist.githubusercontent.com/nirdosh17/72f7ed77412002aaa38548831fc688b5/raw) | |
# You can also pass the ENV LABEL: | |
# ❯❯❯ ENV_LABEL=nirdosh bash <(curl -s https://gist.githubusercontent.com/nirdosh17/72f7ed77412002aaa38548831fc688b5/raw) | |
# Installing deps.. | |
command -v tty-table >/dev/null 2>&1 || { echo >&2 "Installing node package 'tty-table'..."; npm install tty-table -g ; } | |
# in latest linux distributions jq is already present: https://stedolan.github.io/jq/download/ | |
command -v jq >/dev/null 2>&1 || { echo >&2 "Installing 'JQ' ..."; brew install jq ; } | |
echo | |
echo "Scanning clusters in environment '$ENV_LABEL'..." | |
clusters=$(aws ecs list-clusters --profile $AWS_PROFILE --region $AWS_REGION --query clusterArns | jq -r .[] | grep $ENV_LABEL) | |
[ -f /tmp/clusters.csv ] && rm /tmp/clusters.csv | |
echo "Subsystem, Service,Service Version" >> /tmp/clusters.csv | |
for cluster_arn in $clusters | |
do | |
echo " Scanning cluster ${cluster_arn:43}..." | |
services=$(aws ecs list-services --cluster $cluster_arn --profile $AWS_PROFILE --region $AWS_REGION --query serviceArns | jq -r .[]) | |
for service_arn in $services | |
do | |
task_arns=$(aws ecs list-tasks --cluster $cluster_arn --service-name $service_arn --profile $AWS_PROFILE --region $AWS_REGION --query taskArns | jq -r .[]) | |
for task_arn in $task_arns | |
do | |
td_arns=$(aws ecs describe-tasks --cluster $cluster_arn --tasks $task_arn --profile $AWS_PROFILE --region $AWS_REGION --query tasks | jq -r ".[].taskDefinitionArn") | |
for td_arn in $td_arns | |
do | |
td=$(aws ecs describe-task-definition --task-definition $td_arn --profile $AWS_PROFILE --region $AWS_REGION) | |
img=$(echo $td | jq -r ".taskDefinition.containerDefinitions | map(.image)[]") | |
# service name, service version | |
service_version=$(echo "${img:45}" | tr ":" "\n" | sed -n '2p') | |
long_name=$(echo "${service_arn:43}") | |
subsystem=$(echo $long_name | tr "-" "\n" | sed -n 2p) | |
service=$(echo ${long_name//$ENV_LABEL-$subsystem-}) | |
echo "$subsystem,$service,$service_version" >> /tmp/clusters.csv | |
done | |
done | |
done | |
echo " Done!" | |
done | |
cat /tmp/clusters.csv | tty-table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment