wrote bash script + started writing action

This commit is contained in:
miampf 2024-01-29 15:45:18 +01:00
parent 680d3318af
commit 4b024c1ecc
No known key found for this signature in database
GPG Key ID: EF039364B5B6886C
2 changed files with 105 additions and 0 deletions

View File

@ -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 }}

View File

@ -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 <ARTIFACT_DECRYPT_PASSWD>"
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