|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +trap 'echo "Ctrl+C caught, exiting..."; exit 130' INT |
| 5 | + |
| 6 | +function exec_tag() { |
| 7 | + local image=$1 |
| 8 | + local tag_prefix=$2 |
| 9 | + local -n hashes=$3 |
| 10 | + local count=0 |
| 11 | + local total="${#hashes[@]}" |
| 12 | + for h in "${hashes[@]}"; do |
| 13 | + local sha256="${h#sha256:}" |
| 14 | + local img_path="${image}@${h}" |
| 15 | + local tag="${tag_prefix}${sha256}" |
| 16 | + echo -n "tagging $img_path with $tag " |
| 17 | + if [[ ${dry_run} == false ]]; then |
| 18 | + gcrane tag "$img_path" "$tag" && echo "SUCCESS $((++count))/$total" || { echo "FAILED"; exit 1; } |
| 19 | + else |
| 20 | + echo "DRY_RUN" |
| 21 | + fi |
| 22 | + done |
| 23 | +} |
| 24 | + |
| 25 | +function tag_update() { |
| 26 | + local tag_prefix="update-available-" |
| 27 | + local image="$1" |
| 28 | + |
| 29 | + local NOW |
| 30 | + local images_json |
| 31 | + |
| 32 | + NOW=$(date +%s000) |
| 33 | + images_json=$(gcrane ls "$image" --json) |
| 34 | + |
| 35 | + # get all hashes for images that have no tags (or just the commit hash tag) |
| 36 | + # jq behavior for all(test(pattern)) matches the empty list [] |
| 37 | + # or are older than 2 days (we don't want to accidentally mess with any |
| 38 | + # ongoing builds) |
| 39 | + readarray -t targets < <(echo "$images_json" | jq -er --arg now "$NOW" ' |
| 40 | + .manifest | to_entries | sort_by(.value.timeUploadedMs|tonumber) | .[] | select( |
| 41 | + (.value.tag // [] | all(test(".*-[a-f0-9]{40}$|^[a-f0-9]{40}$"))) |
| 42 | + and |
| 43 | + (($now | tonumber) - (.value.timeUploadedMs | tonumber) > 172800000) |
| 44 | + ) | .key |
| 45 | + '); |
| 46 | + |
| 47 | + echo "tagging ${#targets[@]} images of $image" |
| 48 | + |
| 49 | + exec_tag "$image" "$tag_prefix" targets |
| 50 | +} |
| 51 | + |
| 52 | +function tag_deprecate() { |
| 53 | + local targets |
| 54 | + local tag_prefix="deprecated-public-image-" |
| 55 | + local image=$1 |
| 56 | + |
| 57 | + local images_json |
| 58 | + images_json=$(gcrane ls "$image" --json) |
| 59 | + |
| 60 | + # get all hashes for all images don't have the deprecated tag |
| 61 | + readarray -t targets < <(echo "$images_json" | jq -er ' |
| 62 | + .manifest | to_entries | sort_by(.value.timeUploadedMs|tonumber) | .[] | select( |
| 63 | + .value.tag // [] | all(test("deprecated-public-image-[a-f0-9]{64}$") | not) |
| 64 | + ) | .key |
| 65 | + '); |
| 66 | + |
| 67 | + echo "tagging ${#targets[@]} images of $image" |
| 68 | + echo "disabled for now, edit out comment" |
| 69 | + # exec_tag "$image" "$tag_prefix" targets |
| 70 | +} |
| 71 | + |
| 72 | +dry_run=${DRY_RUN:-true} |
| 73 | +if [[ "${dry_run}" != "true" && "${dry_run}" != "false" ]]; then |
| 74 | + echo "Error: DRY_RUN needs to have value (true|false)." |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +function tag() { |
| 79 | + local tag_mode=$1 |
| 80 | + local image=$2 |
| 81 | + case "$tag_mode" in |
| 82 | + update) |
| 83 | + tag_update "$image" |
| 84 | + ;; |
| 85 | + deprecate) |
| 86 | + tag_deprecate "$image" |
| 87 | + ;; |
| 88 | + *) |
| 89 | + echo "wtf?" |
| 90 | + exit 1 |
| 91 | + esac |
| 92 | +} |
| 93 | + |
| 94 | +function usage() { |
| 95 | + echo "USAGE: $1 [deprecate|update] <image>" >&2 |
| 96 | + exit 1 |
| 97 | +} |
| 98 | + |
| 99 | +case "${1:-"~~nocmd"}" in |
| 100 | + update | deprecate) |
| 101 | + if [[ "$#" -ne 2 ]]; then |
| 102 | + usage "$0" |
| 103 | + fi |
| 104 | + tag "$@" |
| 105 | + ;; |
| 106 | + ~~nocmd | *) |
| 107 | + usage "$0" |
| 108 | + ;; |
| 109 | +esac |
0 commit comments