-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathupdate-kustomizations.sh
More file actions
executable file
·78 lines (69 loc) · 2.55 KB
/
Copy pathupdate-kustomizations.sh
File metadata and controls
executable file
·78 lines (69 loc) · 2.55 KB
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
#!/usr/bin/env bash
# This script will go through all kustomization files, looking for image tag
# changes such as the following:
# images:
# - name: ghcr.io/alexgustafsson/cupdate
# newTag: 0.24.2
#
# For all such images, the script will perform a lookup towards Cupdate's API to
# find the latest image and update the manifest in-place if the user whishes to
# do so.
# CUPDATE_API must be set to the URL where your Cupdate instance is reachable
CUPDATE_API="https://cupdate.home.local/api/v1/images"
# MANIFEST_FILE_NAME is the name of kustomization files. All files by this name
# in current directory will be checked
MANIFEST_FILE_NAME="kustomization.yaml"
function confirm() {
while true; do
echo -n "$1 [Y/n] "
read -rsn 1 result </dev/tty
echo
case "$result" in
'y' | 'Y' | '')
return 0
;;
'n' | 'N')
return 1
;;
*)
continue
;;
esac
done
}
# For each kustomization file found in the current directory
while read -r kustomization; do
# For each image reference tag replacement found in the kustomization, such as
# images:
# - name: ghcr.io/alexgustafsson/cupdate
# newTag: 0.24.2
while read -r reference; do
if [[ -z "$reference" ]]; then
continue
fi
# Use the Cupdate API to find the latest reference
latestReference="$(curl --silent --get "$CUPDATE_API?query=$reference" | jq -rc .images[0].latestReference 2>/dev/null | sed 's/@sha256:.*//')"
if [[ ! $! -eq 0 ]] || [[ -z "$latestReference" ]] || [[ "$latestReference" = "$reference" ]] || [[ "$latestReference" = "null" ]]; then
continue
fi
# Print a header and ask for confirmation
echo "$kustomization"
echo -e "\e[31m$reference\e[0m -> \e[32m$latestReference\e[0m"
confirm "Update?"
# If update is confirmed
if [[ $? -eq 0 ]]; then
# Parse reference
name=${reference%%:*}
version=${reference#*:}
latestVersion=${latestReference#*:}
# Create a diff using yq as yq doesn't support keeping whitespace or
# comments around
diff="$(diff -u --ignore-blank-lines -L "$kustomization" -L "$kustomization" <(grep -v '^$' "$kustomization") <(yq ".images[] |= select(.name == \"$name\" and .newTag == \"$version\").newTag = \"$latestVersion\"" "$kustomization"))"
# Print and apply the diff
echo "$diff"
echo "$diff" | patch --silent --unified --posix --fuzz 3 "$kustomization" -i -
fi
# Separate runs by a newline
echo
done <<<"$(yq '.images[] | select((.name|type == "!!str") and (.newTag|type == "!!str")) | "\(.name):\(.newTag)"' "$kustomization" | grep -v '^:$')"
done <<<"$(find . -type f -name "$MANIFEST_FILE_NAME")"