-
Notifications
You must be signed in to change notification settings - Fork 66
/
cleanup.sh
executable file
·94 lines (83 loc) · 1.84 KB
/
cleanup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
SRC=$(realpath $(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd))
OUT=$SRC/out
IMAGE=docker.io/chromedp/headless-shell
CHANNELS=()
VERSIONS=()
MTIME=90
OPTIND=1
while getopts "o:i:c:v:m:" opt; do
case "$opt" in
o) OUT=$OPTARG ;;
i) IMAGE=$OPTARG ;;
c) CHANNELS+=($OPTARG) ;;
v) VERSIONS+=($OPTARG) ;;
m) MTIME=$OPTARG ;;
esac
done
set -e
if [ ${#CHANNELS[@]} -eq 0 ]; then
CHANNELS=(stable)
fi
if [ ${#VERSIONS[@]} -eq 0 ]; then
for CHANNEL in ${CHANNELS[@]}; do
VERSIONS+=($(verhist -platform win64 -channel "$CHANNEL" -latest))
done
fi
# join_by ',' ${A[@]} ${B[@]}
join_by() {
local d=${1-} f=${2-}
if shift 2; then
printf %s "$f" "${@/#/$d}"
fi
}
echo -e "KEEP: $(join_by ', ' latest ${CHANNELS[@]} ${VERSIONS[@]})"
# cleanup old directories and files
if [ -d $OUT ]; then
REGEX=".*($(join_by '|' ${VERSIONS[@]})).*"
(set -x;
find $OUT \
-mindepth 1 \
-maxdepth 1 \
-regextype posix-extended \
\( \
-type d \
-regex '.*/[0-9]+(\.[0-9]+){3}-(amd64|arm64)$' \
-or \
-type f \
-regex '.*/headless-shell-[0-9]+(\.[0-9]+){3}-(amd64|arm64)\.tar\.bz2$' \
\) \
-mtime $MTIME \
-not \
-regex "$REGEX" \
-exec echo REMOVING {} \; \
-exec rm -rf {} \;
)
fi
# remove containers
CONTAINERS=$(
podman container ls \
--filter=ancestor=$IMAGE \
--filter=status=exited \
--filter=status=created \
--quiet
)
if [ ! -z "$CONTAINERS" ]; then
(set -x;
podman container rm --force $CONTAINERS
)
fi
# remove images
IMAGES=$(
podman images \
--noheading \
--filter=reference=$IMAGE \
--filter=reference=localhost/$(basename $IMAGE) \
|grep -Ev "($(join_by '|' latest ${CHANNELS[@]} ${VERSIONS[@]}))" \
|awk '{print $3}'
)
if [ ! -z "$IMAGES" ]; then
(set -x;
podman rmi --force $IMAGES
)
fi