Skip to content

Commit d56f786

Browse files
authored
Merge pull request #54 from galaxyproject/prune-snapshots
Add a script for pruning snapshots on release managers and optionally schedule with cron
2 parents 58939da + be0a138 commit d56f786

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ cvmfs_repositories:
8989
- KEY=val
9090
```
9191
92+
For Stratum 0 / Release Managers, you can automatically prune older snapshots using the `prune_snapshots_time`, a hash
93+
having keys that correspond to the [cron module
94+
options](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/cron_module.html). If
95+
`prune_snapshots_time` is unset, then snapshots are not automatically pruned.
96+
97+
```
98+
cvmfs_repositories:
99+
- repository: repo.example.org
100+
owner: user1
101+
prune_snapshots_count: 20
102+
prune_snapshots_time:
103+
special_time: daily
104+
```
105+
106+
The per-repository `prune_snapshots_count` option defaults to the value of `cvmfs_stratum0_prune_snapshots_count` in
107+
[defaults/main.yml][defaults] if unset.
108+
92109
## Server variables
93110
94111
variable | type | description

defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ cvmfs_stratum1_cache_mem: 128 # MB
4141
cvmfs_stratum1_snapshot_time:
4242
special_time: hourly
4343

44+
# Number of snapshots to keep. Per the documentation, the recommended count is no more than 50.
45+
cvmfs_stratum0_prune_snapshots_count: 50
46+
4447
# Whether the client or server should be upgraded or just installed if missing
4548
cvmfs_upgrade_client: false
4649
cvmfs_upgrade_server: false

files/cvmfs_prune_snapshots.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Prune named snapshots on CVMFS release managers by age and count
4+
#
5+
set -euo pipefail
6+
7+
COUNT=50
8+
VERBOSE=false
9+
DRYRUN=false
10+
MUTEX="${HOME}/.updaterepo.lock"
11+
MUTEX_ACQUIRED=false
12+
13+
function help() {
14+
cat <<EOF
15+
usage: $0 [options] REPOSITORY
16+
options:
17+
-h this help message
18+
-v verbose ouptut
19+
-c COUNT number of snapshots to keep (default: ${COUNT})
20+
-n no changes (dry run, only show what would be done)
21+
EOF
22+
}
23+
24+
function trap_handler() {
25+
$MUTEX_ACQUIRED && { rmdir "$MUTEX"; }
26+
return 0
27+
}
28+
trap "trap_handler" SIGTERM SIGINT ERR EXIT
29+
30+
while getopts ":c:hnv" opt; do
31+
case "$opt" in
32+
c)
33+
COUNT="$OPTARG"
34+
;;
35+
h)
36+
help
37+
exit 0
38+
;;
39+
n)
40+
DRYRUN=true
41+
;;
42+
v)
43+
VERBOSE=true
44+
;;
45+
*)
46+
echo "ERROR: Unknown option: ${opt}. See '-h' for help."
47+
exit 1
48+
;;
49+
esac
50+
done
51+
shift $((OPTIND-1))
52+
53+
if [ -z "${1:-}" ]; then
54+
echo "ERROR: repository argument is required. See '-h' for help."
55+
exit 1
56+
fi
57+
58+
REPO="$1"
59+
60+
echo "Keeping newest ${COUNT} named snapshots in repository: ${REPO}"
61+
62+
if ! $DRYRUN; then
63+
mkdir "$MUTEX"
64+
MUTEX_ACQUIRED=true
65+
fi
66+
67+
tag_args=($(cvmfs_server tag -lx "$REPO" | sort -nk 5 | head -n -"$COUNT" | awk '{print $1}' | sed 's/^/-r /'))
68+
69+
if [ "${#tag_args[@]}" -eq 0 ]; then
70+
echo "no tags to remove"
71+
exit 0
72+
fi
73+
74+
if $DRYRUN; then
75+
echo cvmfs_server tag -f "${tag_args[@]}" "$REPO"
76+
else
77+
cvmfs_server tag -f "${tag_args[@]}" "$REPO"
78+
fi

tasks/stratum0.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,27 @@
9090
- name: Include garbage collection tasks
9191
ansible.builtin.include_tasks: gc.yml
9292
when: cvmfs_gc_enabled
93+
94+
- name: Install cvmfs_prune_snapshots
95+
copy:
96+
src: cvmfs_prune_snapshots.sh
97+
dest: /usr/local/bin/cvmfs_prune_snapshots
98+
mode: 0755
99+
100+
- name: Schedule snapshot pruning
101+
ansible.builtin.cron:
102+
name: cvmfs_prune_snapshots_{{ item.repository }}
103+
cron_file: ansible_cvmfs_stratum0_prune_snapshots
104+
user: "{{ item.owner | default('root') }}"
105+
job: >-
106+
output=$(/usr/local/bin/cvmfs_prune_snapshots
107+
-c {{ item.prune_snapshots_count | default(cvmfs_stratum0_prune_snapshots_count) }}
108+
'{{ item.repository }}' 2>&1) || echo "$output"
109+
hour: "{{ item.prune_snapshots_time.hour | default(omit) }}"
110+
minute: "{{ item.prune_snapshots_time.minute | default(omit) }}"
111+
day: "{{ item.prune_snapshots_time.day | default(omit) }}"
112+
month: "{{ item.prune_snapshots_time.month | default(omit) }}"
113+
weekday: "{{ item.prune_snapshots_time.weekday | default(omit) }}"
114+
special_time: "{{ item.prune_snapshots_time.special_time | default(omit) }}"
115+
loop: "{{ cvmfs_repositories }}"
116+
when: item.prune_snapshots_time is defined

0 commit comments

Comments
 (0)