schutzbot: Clean up non-default storage accounts
This commit is contained in:
parent
1a29f084a1
commit
149d53c1e4
1 changed files with 32 additions and 2 deletions
|
|
@ -24,6 +24,7 @@ RESOURCE_LIST=$(az resource list -g "$AZURE_RESOURCE_GROUP")
|
|||
RESOURCE_COUNT=$( echo "$RESOURCE_LIST" | jq .[].name | wc -l)
|
||||
|
||||
# filter out resources older than X hours
|
||||
HOURS_BACK="${HOURS_BACK:-6}"
|
||||
DELETE_TIME=$(date -d "- $HOURS_BACK hours" +%s)
|
||||
OLD_RESOURCE_LIST_NAMES=()
|
||||
for i in $(seq 0 $(("$RESOURCE_COUNT"-1))); do
|
||||
|
|
@ -37,8 +38,7 @@ done
|
|||
|
||||
#Exit early if no there are no resources to delete
|
||||
if [ ${#OLD_RESOURCE_LIST_NAMES[@]} == 0 ]; then
|
||||
echo "Nothing to delete."
|
||||
exit 0
|
||||
echo "Nothing to delete in the standard storage account."
|
||||
fi
|
||||
|
||||
# Keep only unique resource names
|
||||
|
|
@ -53,4 +53,34 @@ for i in $(seq 0 $(("$TO_DELETE_COUNT"-1))); do
|
|||
TEST_ID=${RESOURCE_TO_DELETE_LIST[$i]} /usr/libexec/osbuild-composer-test/cloud-cleaner
|
||||
done
|
||||
|
||||
# Explicitly check the other storage accounts (mostly the api test one)
|
||||
STORAGE_ACCOUNT_LIST=$(az resource list -g "$AZURE_RESOURCE_GROUP" --resource-type Microsoft.Storage/storageAccounts)
|
||||
STORAGE_ACCOUNT_COUNT=$(echo "$STORAGE_ACCOUNT_LIST" | jq .[].name | wc -l)
|
||||
DELETE_TIME=$(date -d "- $HOURS_BACK hours" +%s)
|
||||
for i in $(seq 0 $(("$STORAGE_ACCOUNT_COUNT"-1))); do
|
||||
STORAGE_ACCOUNT_NAME=$(echo "$STORAGE_ACCOUNT_LIST" | jq .["$i"].name | tr -d '"')
|
||||
if [ "$AZURE_STORAGE_ACCOUNT" = "$STORAGE_ACCOUNT_NAME" ]; then
|
||||
echo "Not checking default storage account $AZURE_STORAGE_ACCOUNT in other storage account script."
|
||||
continue
|
||||
fi
|
||||
|
||||
echo "Checking storage account $STORAGE_ACCOUNT_NAME for old blobs."
|
||||
CONTAINER_LIST=$(az storage container list --account-name "$STORAGE_ACCOUNT_NAME")
|
||||
CONTAINER_COUNT=$(echo "$CONTAINER_LIST" | jq .[].name | wc -l)
|
||||
for i2 in $(seq 0 $(("$CONTAINER_COUNT"-1))); do
|
||||
CONTAINER_NAME=$(echo "$CONTAINER_LIST" | jq .["$i2"].name | tr -d '"')
|
||||
BLOB_LIST=$(az storage blob list --account-name "$STORAGE_ACCOUNT_NAME" --container-name "$CONTAINER_NAME")
|
||||
BLOB_COUNT=$(echo "$BLOB_LIST" | jq .[].name | wc -l)
|
||||
for i3 in $(seq 0 $(("$BLOB_COUNT"-1))); do
|
||||
BLOB_NAME=$(echo "$BLOB_LIST" | jq .["$i3"].name | tr -d '"')
|
||||
BLOB_TIME=$(echo "$BLOB_LIST" | jq .["$i3"].properties.lastModified | tr -d '"')
|
||||
BLOB_TIME_SECONDS=$(date -d "$BLOB_TIME" +%s)
|
||||
if [[ "$BLOB_TIME_SECONDS" -lt "$DELETE_TIME" ]]; then
|
||||
echo "Deleting blob $BLOB_NAME in $STORAGE_ACCOUNT_NAME's $CONTAINER_NAME container."
|
||||
az storage blob delete --only-show-errors --account-name "$STORAGE_ACCOUNT_NAME" --container-name "$CONTAINER_NAME" -n "$BLOB_NAME"
|
||||
fi
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
echo "Azure cleanup complete!"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue