From 4b024c1ecc016f45bd222c0d05410b32f4be527f Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 29 Jan 2024 15:45:18 +0100 Subject: [PATCH] wrote bash script + started writing action --- .../actions/e2e_cleanup_timeframe/action.yml | 26 ++++++ .../e2e_cleanup_timeframe/e2e-cleanup.sh | 79 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 .github/actions/e2e_cleanup_timeframe/action.yml create mode 100755 .github/actions/e2e_cleanup_timeframe/e2e-cleanup.sh diff --git a/.github/actions/e2e_cleanup_timeframe/action.yml b/.github/actions/e2e_cleanup_timeframe/action.yml new file mode 100644 index 000000000..e3aac7123 --- /dev/null +++ b/.github/actions/e2e_cleanup_timeframe/action.yml @@ -0,0 +1,26 @@ +name: E2E cleanup over timeframe +description: Clean up old terraform resources of E2E tests + +inputs: + ghToken: + description: 'The github token that is used with the github CLI.' + required: true + encryptionSecret: + description: 'The secret to use for decrypting the artifacts.' + required: true + +runs: + using: "composite" + steps: + - name: Install unzip + uses: ./.github/actions/setup_bazel_nix + with: + nixTools: | + unzip + - name: Run cleanup + run: ./.github/actions/e2e_cleanup_timeframe/e2e-cleanup.sh ${{ inputs.encryptionSecret }} + shell: bash + env: + GH_TOKEN: ${{ inputs.ghToken }} + + diff --git a/.github/actions/e2e_cleanup_timeframe/e2e-cleanup.sh b/.github/actions/e2e_cleanup_timeframe/e2e-cleanup.sh new file mode 100755 index 000000000..559b6189e --- /dev/null +++ b/.github/actions/e2e_cleanup_timeframe/e2e-cleanup.sh @@ -0,0 +1,79 @@ +#!/bin/env bash + +function get_e2e_test_ids_on_date { + ids="$(gh run list --created "$1" --json createdAt,workflowName,databaseId --jq '.[] | select(.workflowName | contains("e2e")) | .databaseId' -R edgelesssys/constellation)" + echo $ids +} + +function download_tfstate_artifact { + gh run download "$1" -p "terraform-state-*" -R edgelesssys/constellation &>/dev/null +} + +function delete_resources { + cd $1/constellation-terraform + terraform destroy -auto-approve &>/dev/null + cd ../../ + echo delete $1 +} + +function delete_iam_config { + cd $1/constellation-iam-terraform + terraform destroy -auto-approve &>/dev/null + cd ../../ + echo delete iam $1 +} + +if [[ -z $1 ]]; then + echo "No password for artifact decryption provided!" + echo "Usage: ./e2e-cleanup.sh " + exit 1 +fi + +artifact_pwd=$1 + +shopt -s nullglob + +start_date=$(date "+%Y-%m-%d") +end_date=$(date --date "-10 day" "+%Y-%m-%d") +dates_to_clean=() + +while [[ "$end_date" != "$start_date" ]]; do + dates_to_clean+=($end_date) + end_date=$(date --date "$end_date +1 day" "+%Y-%m-%d") +done + +echo "[*] retrieving run IDs for cleanup" +database_ids=() +for d in ${dates_to_clean[*]}; do + echo " retrieving run IDs from $d" + database_ids+=($(get_e2e_test_ids_on_date $d)) +done + +echo "[*] downloading terraform state artifacts" +for id in ${database_ids[*]}; do + echo " downloading from workflow $id" + download_tfstate_artifact $id +done + +echo "[*] extracting artifacts" +for artifact in ./terraform-state-*.zip; do + echo " extracting $artifact" + + mkdir ${artifact%.*} + + unzip "$artifact" + unzip artifact.zip -d ${artifact%.*} -P "$artifact_pwd" + + rm "$artifact" + rm artifact.zip +done + +echo "[*] deleting resources" +for directory in ./terraform-state-*; do + echo " deleting resources in $directory" + delete_resources $directory + echo " deleting IAM configuration in $directory" + delete_iam_config $directory +done + +exit 0