summaryrefslogtreecommitdiff
path: root/contrib/imgprune/entrypoint.sh
blob: a4b77523b5fbd1bcecdbea40f79f536729d3002f (plain)
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
#!/bin/bash

set -e

source /usr/local/bin/lib_entrypoint.sh

req_env_var GCPJSON GCPNAME GCPPROJECT IMGNAMES

gcloud_init

# For safety's sake + limit nr background processes
PRUNE_LIMIT=10
THEFUTURE=$(date --date='+1 hour' +%s)
TOO_OLD='90 days ago'
THRESHOLD=$(date --date="$TOO_OLD" +%s)
# Format Ref: https://cloud.google.com/sdk/gcloud/reference/topic/formats
FORMAT='value[quote](name,selfLink,creationTimestamp,labels)'
PROJRE="/v1/projects/$GCPPROJECT/global/"
BASE_IMAGE_RE='cloud-base'
RECENTLY=$(date --date='30 days ago' --iso-8601=date)
EXCLUDE="$IMGNAMES $IMAGE_BUILDER_CACHE_IMAGE_NAME" # whitespace separated values
# Filter Ref: https://cloud.google.com/sdk/gcloud/reference/topic/filters
FILTER="selfLink~$PROJRE AND creationTimestamp<$RECENTLY AND NOT name=($EXCLUDE)"
TODELETE=$(mktemp -p '' todelete.XXXXXX)

echo "Searching images for pruning candidates older than $TOO_OLD ($THRESHOLD):"
$GCLOUD compute images list --format="$FORMAT" --filter="$FILTER" | \
    while read name selfLink creationTimestamp labels
    do
        created_ymd=$(date --date=$creationTimestamp --iso-8601=date)
        last_used=$(egrep --only-matching --max-count=1 'last-used=[[:digit:]]+' <<< $labels || true)
        markmsgpfx="Marking $name (created $created_ymd) for deletion"
        if [[ -z "$last_used" ]]
        then # image pre-dates addition of tracking labels
            echo "$markmsgpfx: Missing 'last-used' metadata, labels: '$labels'"
            echo "$name" >> $TODELETE
            continue
        fi

        last_used_timestamp=$(date --date=@$(cut -d= -f2 <<< $last_used || true) +%s || true)
        last_used_ymd=$(date --date=@$last_used_timestamp --iso-8601=date)
        if [[ -z "$last_used_timestamp" ]] || [[ "$last_used_timestamp" -ge "$THEFUTURE" ]]
        then
            echo "$markmsgpfx: Missing or invalid last-used timestamp: '$last_used_timestamp'"
            echo "$name" >> $TODELETE
            continue
        fi

        if [[ "$last_used_timestamp" -le "$THRESHOLD" ]]
        then
            echo "$markmsgpfx: Used over $TOO_OLD on $last_used_ymd"
            echo "$name" >> $TODELETE
            continue
        fi

        echo "NOT $markmsgpfx: last used on $last_used_ymd)"
    done

echo "Pruning up to $PRUNE_LIMIT images that were marked for deletion:"
for image_name in $(tail -$PRUNE_LIMIT $TODELETE | sort --random-sort)
do
    # This can take quite some time (minutes), run in parallel disconnected from terminal
    echo "TODO: Would have: $GCLOUD compute images delete $image_name &"
    sleep "$[1+RANDOM/1000]s" &  # Simlate background operation
done

wait || true  # Nothing to delete: No background jobs