Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cleanup AWS images) ensure staging images older than 7 days are removed #590

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Jenkinsfile_k8s
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ pipeline {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh './cleanup/aws.sh'
sh './cleanup/aws_images.sh'
sh './cleanup/aws_images.sh 1 dev'
sh './cleanup/aws_images.sh 7 staging'
sh './cleanup/aws_snapshots.sh'
}
}
Expand Down
45 changes: 20 additions & 25 deletions cleanup/aws_images.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#!/bin/bash

# Check input parameteres
if ! [[ "${1:-1}" =~ ^[0-9]+$ ]]; then
echo "ERROR: argument $1 is not a number" >&2
exit 1
fi
timeshift_day="${1}"
build_type="${2:-dev}"

set -eu -o pipefail

run_aws_ec2_deletion_command() {
run_aws_ec2_command() {
# Check the DRYRUN environment variable
if [ "${DRYRUN:-true}" = "false" ] || [ "${DRYRUN:-true}" = "no" ]
then
Expand All @@ -25,42 +33,29 @@ do
done

## When is last month exactly?
lastmonthdate=""
timeshift_month="1"
if date -v-${timeshift_month}m > /dev/null 2>&1; then
creation_date_threshold=""
timeshift_day="1"
if date -v-${timeshift_day}d > /dev/null 2>&1; then
# BSD systems (Mac OS X)
lastmonthdate="$(date -v-${timeshift_month}m +%Y-%m-%d)"
creation_date_threshold="$(date -v-${timeshift_day}d +%Y-%m-%d)"
else
# GNU systems (Linux)
lastmonthdate="$(date --date="-${timeshift_month} months" +%Y-%m-%d)"
creation_date_threshold="$(date --date="-${timeshift_day} days" +%Y-%m-%d)"
fi

## Check for aws API reachibility (is it configured?)
aws sts get-caller-identity >/dev/null || \
{ echo "[ERROR] Unable to request the AWS API: the command 'sts get-caller-identity' failed. Please check your AWS credentials"; exit 1; }

## STEP 1
## Remove images older than <timeshift_month> month(s) from dev
INSTANCE_IDS="$(aws ec2 describe-images --owners self --filters 'Name=tag:build_type,Values=dev' \
--query 'Images[?CreationDate<=`'"${lastmonthdate}"'`][].ImageId' | jq -r '.[]' | xargs)"
## Remove images older than $1 day(s) of build_type $2
ami_ids="$(aws ec2 describe-images --owners self --filters "Name=tag:build_type,Values=${build_type}" \
--query 'Images[?CreationDate<=`'"${creation_date_threshold}"'`][].ImageId' | jq -r '.[]')"

if [ -n "${INSTANCE_IDS}" ]
if [ -n "${ami_ids}" ]
then
#shellcheck disable=SC2086
#has to run on each instance
cpt="0"
for theimageid in ${INSTANCE_IDS}
do
run_aws_ec2_deletion_command deregister-image --image-id ${theimageid}
((cpt=cpt+1))
done

if [ "${DRYRUN:-true}" = "false" ] || [ "${DRYRUN:-true}" = "no" ]
then
echo "== $cpt images have been deregistered"
else
echo "==DRYRUN $cpt images would have been deregistered"
fi
export -f run_aws_ec2_command
echo "${ami_ids}" | parallel --halt-on-error never --no-run-if-empty run_aws_ec2_command deregister-image --image-id {} :::
else
echo "== No dangling images found to delete."
fi
Expand Down