forked from flaviostutz/docker-janitor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.sh
executable file
·58 lines (44 loc) · 1.63 KB
/
run.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
#!/bin/sh
FILTERS="--filter until=${UNUSED_TIME}"
if [ -n "$EXCLUDE_RESOURCES" ]; then
oldIFS="$IFS"
IFS=','
set -- $EXCLUDE_RESOURCES
for resource in "$@"; do
if [ -n "$resource" ]; then
FILTERS="$FILTERS --filter label!=${resource}"
fi
done
IFS="$oldIFS"
fi
FIRST_RUN=true
while [ : ]
do
HOUR_NOW=$(date +"%H")
if [ "$HOUR_NOW" -ge "$HOUR_OF_DAY_START" ] && [ "$HOUR_NOW" -le "$HOUR_OF_DAY_END" ]; then
if [ "$FIRST_RUN" == "true" ]; then
if [ "$RUN_ON_STARTUP" == "true" ]; then
echo "Running system prune on startup..."
echo "$(date) Calling docker system prune --all $VOLUMES --force $FILTERS"
docker system prune --all $VOLUMES --force $FILTERS
fi
RANDOM_SLEEP_TIME=$((RANDOM % TIME_BETWEEN_RUNS))
if [ "$SKIP_RANDOM_BACKOFF" == "false" ]; then
echo "Waiting for a random time (${RANDOM_SLEEP_TIME}s) to avoid cluster instances to start to perform the cleanup at the same time..."
sleep ${RANDOM_SLEEP_TIME}s
fi
FIRST_RUN=false
fi
echo "$(date) Calling 'docker system prune --all --force $FILTERS'..."
docker system prune --all --force $FILTERS
if [ "$PRUNE_VOLUMES" == "true" ]; then
echo "$(date) Calling 'docker volume prune --force'..."
docker volume prune --force
fi
echo "$(date) Done. Waiting for next run..."
sleep ${TIME_BETWEEN_RUNS}
else
echo "CURRENT HOUR ($HOUR_NOW) IS OUTSIDE WORKING WINDOW"
sleep 60
fi
done