Skip to content

Instantly share code, notes, and snippets.

@tamakiii
Last active November 2, 2023 15:52
Show Gist options
  • Save tamakiii/c7c11f5e6c5b8feb6893698447bc3913 to your computer and use it in GitHub Desktop.
Save tamakiii/c7c11f5e6c5b8feb6893698447bc3913 to your computer and use it in GitHub Desktop.
[WIP] ECS register task definition script (update images with specific :tag)
#!/usr/bin/env bash
export VERBOSE=false
export DRYRUN=false
export HELP=false
export AWS_REGION=
export AWS_PROFILE=
export AWS_DEBUG=
# declare -x DESIRED_COUNT=false
function usage() {
cat <<EOM
usage: script.sh [options] <command> [parameters]
AVAILABLE COMMANDS
script.sh register-task <family> [--tag tag]
EOM
exit 3
}
function aws_ecs() {
aws ecs --profile $AWS_PROFILE --region $AWS_REGION $AWS_DEBUG $@
}
function get_latest_task_definition() {
declare TASK_DEF_ARNS=$(aws_ecs "list-task-definitions --family-prefix $1 --sort DESC")
declare TASK_DEF_ARN=$(echo $TASK_DEF_ARNS | jq -r '.taskDefinitionArns[0]')
aws_ecs "describe-task-definition --task-definition $TASK_DEF_ARN"
}
function replace_images_with_tag() {
declare ORIGINAL=$(cat -)
declare RESULT=$ORIGINAL
declare TAG="$1"
declare -a NAMES="${@:2}"
for name in ${NAMES[@]}; do
declare image=$(echo $ORIGINAL | jq -r ".[] | select(.name == \"$name\") .image")
if [ "$image" == "" ]; then
echo "Image \"$name\" not found in container definition"
exit 201
fi
declare new_image=$(echo $image | sed -e "s/^\([^:]\+\)\(.*\)$/\1:$TAG/")
RESULT=$(echo $RESULT | jq "(.[] | select(.name == \"$name\") | .image) |= \"$new_image\"")
done
echo $RESULT
}
function register_task_command() {
declare -i ARGC=0
declare -a ARGV=()
declare -x TAG=latest
while [[ $# -gt 0 ]]
do
case "$1" in
--tag)
TAG="$2"
shift
;;
*)
((++ARGC))
ARGV=("${ARGV[@]}" "$1")
;;
esac
shift
done
if [ $ARGC -lt 2 -o $HELP == true ]; then
usage
exit 200
fi
declare FAMILY="${ARGV[0]}" && ARGV=("${ARGV[@]:1}")
declare -a NAMES=${ARGV[@]}
declare TASK_DEF=$(get_latest_task_definition $FAMILY)
declare CONTAINER_DEF=$(echo "$TASK_DEF" | jq '.taskDefinition.containerDefinitions')
declare NEW_CONTAINER_DEF=$(echo $CONTAINER_DEF | replace_images_with_tag $TAG $NAMES)
if [ $DRYRUN == true ]; then
echo $NEW_CONTAINER_DEF | jq '.'
exit 0
fi
aws_ecs "register-task-definition --family $FAMILY --container-definitions $(echo $NEW_CONTAINER_DEF | jq -c '.')"
}
function deploy() {
echo "deploying"
while [[ $# -gt 0 ]]
do
case "$1" in
-d|--desired-count)
DESIRED_COUNT="$2"
shift
;;
*)
((++ARGC))
ARGV=("${ARGV[@]}" "$1")
;;
esac
shift
done
echo ${ARGV[@]:0};
echo $DESIRED_COUNT;
}
function revert() {
echo "reverting"
REVERT_COUNT=1
while [[ $# -gt 0 ]]
do
case "$1" in
--count)
REVERT_COUNT="$2"
shift
;;
*)
((++ARGC))
ARGV=("${ARGV[@]}" "$1")
;;
esac
shift
done
echo ${ARGV[@]:0};
echo $DESIRED_COUNT;
}
function main() {
declare -i ARGC=0
declare -a ARGV=()
declare COMMAND
if [ $# -gt 0 ]; then
COMMAND="$1" && shift
fi
case "$COMMAND" in
register-task)
register_task_command $@
;;
deploy)
deploy $@
;;
revert)
revert $@
;;
test)
test_command $@
;;
*)
usage
exit 101
;;
esac
}
if [ "$BASH_SOURCE" == "$0" ]; then
declare -i ARGC=0
declare -a ARGV=()
set -o errexit
set -o pipefail
set -u
set -e
while [[ $# -gt 0 ]]
do
case "$1" in
--profile)
AWS_PROFILE="$2"
shift
;;
--region)
AWS_REGION="$2"
shift
;;
--verbose)
VERBOSE=true
AWS_DEBUG="--debug"
;;
--dry-run)
DRYRUN=true
;;
--help)
HELP=true
;;
*)
((++ARGC))
ARGV=("${ARGV[@]}" "$1")
;;
esac
shift
done
if [ $VERBOSE == true ]; then
set -x
fi
main ${ARGV[@]:0}
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment