Skip to content

Commit 34a90ba

Browse files
Merge pull request #11 from significa/delete-all-volumes
Add DELETE_ALL_VOLUMES to trigger backup
2 parents 84d2771 + 50a6e61 commit 34a90ba

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

.github/workflows/trigger-backup.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,21 @@ on:
3131
description: >-
3232
Makes the action crash if when the backup finishes there are still volumes in the app.
3333
required: false
34+
default: true
3435
type: boolean
36+
delete-al-volumes:
37+
description: >-
38+
Deletes all volumes in the backup worker after the backup has completed.
39+
required: false
40+
default: true
41+
type: boolean
42+
action-ref:
43+
description: >-
44+
The `ref` to clone the trigger backup script from. Allows to override GitHub's context
45+
`github.action_ref` when it does not behave properly.
46+
required: false
47+
default: ${{ github.action_ref }}
48+
type: string
3549

3650
secrets:
3751
FLY_API_TOKEN:
@@ -48,6 +62,7 @@ jobs:
4862
with:
4963
repository: significa/fly-pg-dump-to-s3
5064
path: ./fly-pg-dump-to-s3
65+
ref: ${{ inputs.action-ref }}
5166

5267
- uses: superfly/flyctl-actions/setup-flyctl@master
5368

@@ -61,3 +76,4 @@ jobs:
6176
FLY_VOLUME_SIZE: ${{ inputs.volume-size }}
6277
DOCKER_IMAGE: ${{ inputs.docker-image }}
6378
ERROR_ON_DANGLING_VOLUMES: ${{ inputs.error-on-dangling-volumes }}
79+
DELETE_ALL_VOLUMES: ${{ inputs.delete-al-volumes }}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ requirements in a simple way.
8989
- `ERROR_ON_DANGLING_VOLUMES`: After the backup completes, checks if there are any volumes still
9090
available, and crashes if so. This might be useful to alert that there are dangling volumes
9191
(that you might want to be paying for). Defaults to `true`.
92+
- `DELETE_ALL_VOLUMES`: True to delete all volumes in the backup worker instead of the one used
93+
in the machine. Fly has been very inconsistent with what volume does the machine start.
94+
This solves the problem but prevents having multiple backup workers running in the same app.
95+
Default to `true`.
9296

9397
OPTION B: Call the reusable GitHub Actions workflow found in
9498
`.github/workflows/trigger-backup.yaml`. Example workflow definition:

trigger-backup.sh

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ FLY_MACHINE_SIZE=${FLY_MACHINE_SIZE:-shared-cpu-4x}
1111
FLY_VOLUME_SIZE=${FLY_VOLUME_SIZE:-3}
1212
DEFAULT_DOCKER_IMAGE="ghcr.io/significa/fly-pg-dump-to-s3:3"
1313
DOCKER_IMAGE=${DOCKER_IMAGE:-$DEFAULT_DOCKER_IMAGE}
14-
ERROR_ON_DANGLING_VOLUMES=${ERROR_ON_DANGLING_VOLUMES-true}
14+
ERROR_ON_DANGLING_VOLUMES=${ERROR_ON_DANGLING_VOLUMES:-true}
15+
DELETE_ALL_VOLUMES=${DELETE_ALL_VOLUMES:-true}
1516
VOLUME_NAME=${VOLUME_NAME:-temp_data}
1617
MAX_RETRIES=6
1718

1819
# Fly has been proucing very inconsistent results, where the state is not propagated.
1920
# TODO: Sleeping between operations is not ideal.
20-
SLEEP_TIME_SECONDS=${SLEEP_TIME_SECONDS:-10}
21+
SLEEP_TIME_SECONDS=${SLEEP_TIME_SECONDS:-15}
2122

2223
if [[ -z "$FLY_APP" || -z "$FLY_API_TOKEN" ]]; then
2324
>&2 echo "Env vars FLY_APP and FLY_API_TOKEN must not be empty"
@@ -61,14 +62,23 @@ sleep "$SLEEP_TIME_SECONDS"
6162

6263
volume_id=vol_nylzrem87pd4qmk
6364

64-
echo "Deleting volume '$volume_id'"
65+
delete_volume_or_volumes () {
66+
if "$DELETE_ALL_VOLUMES" ; then
67+
echo "Deleting all volumes in app '$FLY_APP'"
68+
flyctl volumes list --json -a "$FLY_APP" | jq -r '.[].id' | xargs -n1 flyctl volumes delete --yes
69+
else
70+
echo "Deleting volume '$volume_id'"
71+
flyctl volumes delete --yes "$volume_id"
72+
fi
73+
}
74+
6575
attempt_num=0
6676
set +e
67-
while ! flyctl volumes delete --yes "$volume_id"; do
77+
while ! delete_volume_or_volumes; do
6878
attempt_num=$(( attempt_num + 1 ))
6979

7080
if [ $attempt_num -ge $MAX_RETRIES ]; then
71-
echo "Exceeded max retries ($MAX_RETRIES) deleting volume '$volume_id', exiting"
81+
echo "Exceeded max retries ($MAX_RETRIES) deleting volume(s), exiting"
7282
exit 1
7383
fi
7484

0 commit comments

Comments
 (0)