2022-06-15 14:59:55 -07:00
|
|
|
name: Delete GCP resources
|
|
|
|
|
|
|
|
on:
|
2022-09-08 13:24:24 -07:00
|
|
|
# Run daily, when most devs aren't working
|
|
|
|
# 0700 UTC is after AEST working hours but before ET working hours
|
2022-06-15 14:59:55 -07:00
|
|
|
schedule:
|
2022-09-08 13:24:24 -07:00
|
|
|
- cron: "0 7 * * *"
|
2022-06-15 14:59:55 -07:00
|
|
|
workflow_dispatch:
|
|
|
|
|
2022-09-05 19:51:46 -07:00
|
|
|
env:
|
|
|
|
# Delete all resources created before $DELETE_AGE_DAYS days ago.
|
2022-09-08 13:24:24 -07:00
|
|
|
# We keep this short to reduce storage costs.
|
|
|
|
DELETE_AGE_DAYS: 2
|
2022-09-05 19:51:46 -07:00
|
|
|
# But keep the latest $KEEP_LATEST_IMAGE_COUNT images of each type.
|
2022-09-08 13:24:24 -07:00
|
|
|
# We keep this small to reduce storage costs.
|
|
|
|
KEEP_LATEST_IMAGE_COUNT: 2
|
2022-09-05 19:51:46 -07:00
|
|
|
|
2022-06-15 14:59:55 -07:00
|
|
|
jobs:
|
|
|
|
delete-resources:
|
|
|
|
name: Delete old GCP resources
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
permissions:
|
|
|
|
contents: 'read'
|
|
|
|
id-token: 'write'
|
|
|
|
steps:
|
2022-09-05 19:51:46 -07:00
|
|
|
- uses: actions/checkout@v3.0.2
|
|
|
|
with:
|
|
|
|
persist-credentials: false
|
|
|
|
|
2022-06-15 14:59:55 -07:00
|
|
|
# Setup gcloud CLI
|
|
|
|
- name: Authenticate to Google Cloud
|
|
|
|
id: auth
|
2022-09-01 16:48:28 -07:00
|
|
|
uses: google-github-actions/auth@v0.8.1
|
2022-06-15 14:59:55 -07:00
|
|
|
with:
|
2022-08-23 20:49:55 -07:00
|
|
|
retries: '3'
|
2022-06-15 14:59:55 -07:00
|
|
|
workload_identity_provider: 'projects/143793276228/locations/global/workloadIdentityPools/github-actions/providers/github-oidc'
|
|
|
|
service_account: 'github-service-account@zealous-zebra.iam.gserviceaccount.com'
|
|
|
|
token_format: 'access_token'
|
|
|
|
|
2022-09-05 19:51:46 -07:00
|
|
|
# Deletes all the instance templates older than $DELETE_AGE_DAYS days.
|
2022-06-15 14:59:55 -07:00
|
|
|
- name: Delete old instance templates
|
|
|
|
run: |
|
2022-09-05 19:51:46 -07:00
|
|
|
DELETE_BEFORE_DATE=$(date --date="$DELETE_AGE_DAYS days ago" '+%Y%m%d')
|
|
|
|
TEMPLATES=$(gcloud compute instance-templates list --sort-by=creationTimestamp --filter="name~-[0-9a-f]+$ AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME)')
|
2022-06-15 14:59:55 -07:00
|
|
|
|
|
|
|
for TEMPLATE in $TEMPLATES
|
|
|
|
do
|
2022-09-05 19:51:46 -07:00
|
|
|
gcloud compute instance-templates delete ${TEMPLATE} || continue
|
2022-06-15 14:59:55 -07:00
|
|
|
done
|
2022-09-05 15:13:39 -07:00
|
|
|
|
2022-09-05 19:51:46 -07:00
|
|
|
# Deletes all the disks older than $DELETE_AGE_DAYS days.
|
2022-09-05 15:13:39 -07:00
|
|
|
#
|
2022-09-05 19:51:46 -07:00
|
|
|
# Disks that are attached to an instance template can't be deleted, so it is safe to delete all disks here.
|
|
|
|
- name: Delete old disks
|
2022-09-05 15:13:39 -07:00
|
|
|
run: |
|
2022-09-05 19:51:46 -07:00
|
|
|
DELETE_BEFORE_DATE=$(date --date="$DELETE_AGE_DAYS days ago" '+%Y%m%d')
|
|
|
|
|
|
|
|
# Disks created by PR jobs, and other jobs that use a commit hash
|
|
|
|
COMMIT_DISKS=$(gcloud compute disks list --sort-by=creationTimestamp --filter="name~-[0-9a-f]+$ AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME)')
|
|
|
|
|
|
|
|
for DISK in $COMMIT_DISKS
|
2022-09-05 15:13:39 -07:00
|
|
|
do
|
2022-09-05 19:51:46 -07:00
|
|
|
gcloud compute disks delete --verbosity=info ${DISK} || continue
|
2022-09-05 15:13:39 -07:00
|
|
|
done
|
2022-09-05 19:51:46 -07:00
|
|
|
|
|
|
|
# Disks created by managed instance groups, and other jobs that start with "zebrad-"
|
|
|
|
ZEBRAD_DISKS=$(gcloud compute disks list --sort-by=creationTimestamp --filter="name~^zebrad- AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME)')
|
2022-09-05 15:13:39 -07:00
|
|
|
|
2022-09-05 19:51:46 -07:00
|
|
|
for DISK in $ZEBRAD_DISKS
|
2022-09-05 15:13:39 -07:00
|
|
|
do
|
2022-09-05 19:51:46 -07:00
|
|
|
gcloud compute disks delete --verbosity=info ${DISK} || continue
|
2022-09-05 15:13:39 -07:00
|
|
|
done
|
|
|
|
|
2022-09-05 19:51:46 -07:00
|
|
|
# Deletes cache images older than $DELETE_AGE_DAYS days.
|
|
|
|
#
|
2022-09-13 00:24:25 -07:00
|
|
|
# Keeps all images younger than $DELETE_AGE_DAYS.
|
|
|
|
# Also keeps $KEEP_LATEST_IMAGE_COUNT older images of each type:
|
2022-09-05 19:51:46 -07:00
|
|
|
# - zebrad checkpoint cache
|
|
|
|
# - zebrad tip cache
|
|
|
|
# - lightwalletd + zebrad tip cache
|
|
|
|
#
|
2022-09-13 00:24:25 -07:00
|
|
|
# TODO:
|
|
|
|
# - keep the latest $KEEP_LATEST_IMAGE_COUNT, if there are at least that many recent images, delete all the outdated images
|
|
|
|
# - when we add testnet to the workflows, keep the latest $KEEP_LATEST_IMAGE_COUNT testnet images,
|
|
|
|
# and the latest $KEEP_LATEST_IMAGE_COUNT mainnet images.
|
2022-09-05 19:51:46 -07:00
|
|
|
- name: Delete old cache disks
|
|
|
|
run: |
|
|
|
|
DELETE_BEFORE_DATE=$(date --date="$DELETE_AGE_DAYS days ago" '+%Y%m%d')
|
2022-09-05 15:13:39 -07:00
|
|
|
|
2022-09-05 19:51:46 -07:00
|
|
|
ZEBRAD_CHECKPOINT_IMAGES=$(gcloud compute images list --sort-by=~creationTimestamp --filter="name~^zebrad-cache-.*net-checkpoint AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME)')
|
|
|
|
KEPT_IMAGES=0
|
|
|
|
for IMAGE in $ZEBRAD_CHECKPOINT_IMAGES
|
|
|
|
do
|
|
|
|
if [[ "$KEPT_IMAGES" -lt "$KEEP_LATEST_IMAGE_COUNT" ]];
|
|
|
|
then
|
|
|
|
KEPT_IMAGES=$((KEPT_IMAGES+1))
|
|
|
|
echo "Keeping image $KEPT_IMAGES named $IMAGE"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
gcloud compute images delete ${IMAGE} || continue
|
|
|
|
done
|
|
|
|
|
|
|
|
ZEBRAD_TIP_IMAGES=$(gcloud compute images list --sort-by=~creationTimestamp --filter="name~^zebrad-cache-.*net-tip AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME)')
|
|
|
|
KEPT_IMAGES=0
|
|
|
|
for IMAGE in $ZEBRAD_TIP_IMAGES
|
|
|
|
do
|
|
|
|
if [[ "$KEPT_IMAGES" -lt "$KEEP_LATEST_IMAGE_COUNT" ]];
|
|
|
|
then
|
|
|
|
KEPT_IMAGES=$((KEPT_IMAGES+1))
|
|
|
|
echo "Keeping image $KEPT_IMAGES named $IMAGE"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
gcloud compute images delete ${IMAGE} || continue
|
|
|
|
done
|
|
|
|
|
|
|
|
LWD_TIP_IMAGES=$(gcloud compute images list --sort-by=~creationTimestamp --filter="name~^lwd-cache-.*net-tip AND creationTimestamp < $DELETE_BEFORE_DATE" --format='value(NAME)')
|
|
|
|
KEPT_IMAGES=0
|
|
|
|
for IMAGE in $LWD_TIP_IMAGES
|
2022-09-05 15:13:39 -07:00
|
|
|
do
|
2022-09-05 19:51:46 -07:00
|
|
|
if [[ "$KEPT_IMAGES" -lt "$KEEP_LATEST_IMAGE_COUNT" ]];
|
|
|
|
then
|
|
|
|
KEPT_IMAGES=$((KEPT_IMAGES+1))
|
|
|
|
echo "Keeping image $KEPT_IMAGES named $IMAGE"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
gcloud compute images delete ${IMAGE} || continue
|
2022-09-05 15:13:39 -07:00
|
|
|
done
|