-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathupgrade.sh
executable file
·119 lines (92 loc) · 3.13 KB
/
upgrade.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
set -eux
fetch_latest() {
# Fetch latest version from Github releases API
LATEST=$(curl -s "https://api.github.com/repos/moby/moby/releases?per_page=1" | jq -r '.[0].tag_name')
}
# Validate the version format
validate_version() {
# Original simplified RegEx:
# v\d+.\d+.\d+\-*(rc.\d|rc\d|beta.\d)*
# By analysing the last tags on github.com/moby/moby/tags
# of last 3 years (since 2021).
if [[ "$LATEST" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]|rc[0-9]|beta\.[0-9])?$ ]]; then
echo "$LATEST matches the regex."
else
echo "Version doesn't match known pattern."
exit 1
fi
}
check_yq() {
if ! command -v yq &>/dev/null; then
echo -e "yq is not installed."
echo -e "Please run:"
echo -e "\tsudo snap install yq"
exit 1
fi
}
check_new_version() {
if [[ "$CURRENT" == "$LATEST" ]]; then
echo -e "Docker snap is already updated\n"
exit 0
fi
}
main() {
check_yq
# Define the path to the YAML file
yaml_file="snap/snapcraft.yaml"
CURRENT=$(yq e '.parts.engine.source-tag' "$yaml_file")
fetch_latest
echo "Latest TAG: $LATEST"
validate_version
check_new_version
SNAP_VERSION=${LATEST#v}
echo -e "New snap version $SNAP_VERSION"
echo "The latest version of moby is: $LATEST"
# Fetch the Dockerfile
dockerfile=$(curl -s "https://raw.githubusercontent.com/moby/moby/refs/tags/$LATEST/Dockerfile")
# Declare variables and their corresponding regex patterns
declare -A variables=(
[GO_VERSION]='^ARG GO_VERSION='
[CONTAINERD_VERSION]='^ARG CONTAINERD_VERSION='
[RUNC_VERSION]='^ARG RUNC_VERSION='
[TINI_VERSION]='^ARG TINI_VERSION='
[DOCKERCLI_VERSION]='^ARG DOCKERCLI_VERSION='
[BUILDX_VERSION]='^ARG BUILDX_VERSION='
[COMPOSE_VERSION]='^ARG COMPOSE_VERSION='
)
# Extract versions using a loop
for var in "${!variables[@]}"; do
value=$(echo "$dockerfile" | awk -F= "/${variables[$var]}/ {print \$2}")
# Handle special cases: GO_VERSION and BUILDX_VERSION
if [[ "$var" == "GO_VERSION" ]]; then
# for GO_VERSION Extract major.minor
value=$(echo "$value" | awk -F. '{print $1 "." $2}')
elif [[ "$var" == "BUILDX_VERSION" && $value != v* ]]; then
value="v$value" # Prepend 'v' if missing
fi
declare "$var=$value"
echo "$var: ${!var}"
done
# Replace the `version:` field with the value of $SNAP_VERSION
yq -i ".version = \"$SNAP_VERSION\"" "$yaml_file"
# Replace fields in YAML using a loop
declare -A yaml_updates=(
[engine.source-tag]=$LATEST
[containerd.source-tag]=$CONTAINERD_VERSION
[runc.source-tag]=$RUNC_VERSION
[tini.source-tag]=$TINI_VERSION
[docker-cli.source-tag]=$DOCKERCLI_VERSION
[buildx.source-tag]=$BUILDX_VERSION
[compose-v2.source-tag]=$COMPOSE_VERSION
)
for part in "${!yaml_updates[@]}"; do
yq -i ".parts.${part} = \"${yaml_updates[$part]}\"" "$yaml_file"
done
# Replace `build-snaps` for `engine` with $GO_VERSION
yq -i '.parts.engine."build-snaps"[0] |= sub("[0-9]+\.[0-9]+", "'"$GO_VERSION"'")' "$yaml_file"
# Replace the remaining comments
sed -i "s/$CURRENT/$LATEST/g" "$yaml_file"
echo "YAML file updated successfully."
}
main