Change-Id: If4e8a826d76819817e111cc7477a135f0c871db9
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
|
||||
name: cleanup-expired
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: "17 * * * *"
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
cleanup-expired:
|
||||
runs-on:
|
||||
- toolzoo-host
|
||||
env:
|
||||
AWS_REGION: us-east-1
|
||||
APP_STACK_PREFIX: temp-stack
|
||||
TTL_GRACE_SECONDS: "600"
|
||||
steps:
|
||||
- name: Cleanup expired app stacks
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
now_epoch="$(date -u +%s)"
|
||||
prefix="${APP_STACK_PREFIX}-"
|
||||
stacks="$(aws cloudformation list-stacks \
|
||||
--stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE UPDATE_ROLLBACK_COMPLETE \
|
||||
--query "StackSummaries[?starts_with(StackName, \`${prefix}\`)].StackName" \
|
||||
--output text)"
|
||||
|
||||
for stack in $stacks; do
|
||||
expires_at="$(aws cloudformation describe-stacks --stack-name "$stack" --query "Stacks[0].Tags[?Key=='ExpiresAt'].Value | [0]" --output text)"
|
||||
owner_raw="$(aws cloudformation describe-stacks --stack-name "$stack" --query "Stacks[0].Tags[?Key=='Owner'].Value | [0]" --output text)"
|
||||
repo_raw="$(aws cloudformation describe-stacks --stack-name "$stack" --query "Stacks[0].Tags[?Key=='Repository'].Value | [0]" --output text)"
|
||||
|
||||
if [[ "$expires_at" == "None" || -z "$expires_at" ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
expires_epoch="$(date -u -d "$expires_at" +%s 2>/dev/null || echo 0)"
|
||||
if [[ "$expires_epoch" -eq 0 ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
if (( now_epoch >= expires_epoch + TTL_GRACE_SECONDS )); then
|
||||
aws cloudformation delete-stack --stack-name "$stack"
|
||||
aws cloudformation wait stack-delete-complete --stack-name "$stack"
|
||||
|
||||
if [[ "$owner_raw" != "None" && "$repo_raw" != "None" && -n "$owner_raw" && -n "$repo_raw" ]]; then
|
||||
repo_slug="$(printf '%s' "$repo_raw" | tr '[:upper:]' '[:lower:]')"
|
||||
ecr_repo="temp/${repo_slug}"
|
||||
if aws ecr describe-repositories --repository-names "$ecr_repo" >/dev/null 2>&1; then
|
||||
aws ecr delete-repository --repository-name "$ecr_repo" --force
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user