-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
helper-functions
243 lines (199 loc) · 7.56 KB
/
helper-functions
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
set -eo pipefail
BUILDER=""
DISTRO_REPO="${DISTRO_REPO:=openhab/openhab-distro}"
VERSIONS=""
get_released_versions_from_tags() {
git ls-remote --refs --tags "https://github.com/${DISTRO_REPO}.git" | grep -E '.+/tags/[0-9]+\.[0-9]+\.[0-9]+(\.(M|RC)[0-9]+)?$' | sed -E 's#.+/tags/(.+)#\1#g' || echo ""
}
get_snapshot_versions_from_poms() {
local branch_names="$(git ls-remote --refs --heads "https://github.com/${DISTRO_REPO}.git" | grep -E '.+/heads/(main)$' | sed -E 's#.+/heads/(.+)#\1#g')"
for branch_name in $branch_names; do
curl -sS "https://raw.githubusercontent.com/${DISTRO_REPO}/${branch_name}/pom.xml" | grep -E '^ <version>' | grep 'SNAPSHOT' | sed -E 's#.+<version>(.+)-SNAPSHOT</version>#\1-snapshot#g' || echo ""
done
}
get_versions() {
(get_released_versions_from_tags && get_snapshot_versions_from_poms) | sort --unique --version-sort
}
VERSIONS=$(get_versions)
# Supported base images
bases() {
echo "debian alpine"
}
docker_repo() {
echo "${DOCKER_REPO:=openhab/openhab}"
}
# Supported Docker platforms
platforms() {
local version="$1"
local base="$2"
if [ "$base" == "alpine" ]; then
# There is no linux/arm/v7 Alpine image for openHAB 3 (or newer) because the required openjdk package is unavailable for this architecture
echo "linux/amd64,linux/arm64"
else
echo "linux/amd64,linux/arm64,linux/arm/v7"
fi
}
tags() {
local version="$1"
local base="$2"
local tags=()
if [ "$base" == "debian" ]; then
tags+=("$(docker_repo):$version")
fi
tags+=("$(docker_repo):$version-$base")
if [ "$version" == "$(last_stable_version)" ]; then
if [ "$base" == "debian" ]; then
tags+=("$(docker_repo):latest")
fi
tags+=("$(docker_repo):latest-$base")
fi
milestone_maturity_version="$(last_milestone_version)"
if [ "$milestone_maturity_version" == "" ]; then
milestone_maturity_version="$(last_stable_version)"
fi
if [ "$version" == "$milestone_maturity_version" ]; then
if [ "$base" == "debian" ]; then
tags+=("$(docker_repo):milestone")
fi
tags+=("$(docker_repo):milestone-$base")
fi
if [ "$version" == "$(last_snapshot_version)" ]; then
if [ "$base" == "debian" ]; then
tags+=("$(docker_repo):snapshot")
fi
tags+=("$(docker_repo):snapshot-$base")
fi
echo $(IFS=' '; echo "${tags[*]}")
}
last_stable_version() {
grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' <<< $VERSIONS | tail -n 1
}
stable_versions() {
grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' <<< $VERSIONS
}
last_stable_minor_versions() {
local minor_versions=$(grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' <<< $VERSIONS | sed -E 's/^([0-9]+\.[0-9]+).+/\1/' | sort --unique --version-sort)
for minor_version in $minor_versions; do
stable_versions | grep -E "^$minor_version" | tail -n 1
done
}
snapshot_versions() {
grep -E '^[0-9]+\.[0-9]+\.[0-9]+-snapshot$' <<< $VERSIONS || echo ""
}
last_snapshot_version() {
grep -E '^[0-9]+\.[0-9]+\.[0-9]+-snapshot$' <<< $VERSIONS | tail -n 1 || echo ""
}
next_stable_version() {
sed 's/-snapshot//' <<< $(last_snapshot_version)
}
milestone_versions() {
grep -E "$(next_stable_version)\.(M|RC)[0-9]+$" <<< $VERSIONS | tail -n 3 || echo ""
}
last_milestone_version() {
grep -E "$(next_stable_version)\.(M|RC)[0-9]+$" <<< $VERSIONS | tail -n 1 || echo ""
}
generate_readme_versions() {
local text="$1"
local versions="$(echo "$2" | sort --reverse --version-sort | head -n 2)"
if [ "$versions" != "" ]; then
echo "* $text"
for version in $versions; do
case $version in
*-snapshot) echo " * \`$version\`";;
*) echo " * \`$version\` ([Release notes](https://github.com/openhab/openhab-distro/releases/tag/$version))";;
esac
done
fi
}
update_readme() {
local file=README.md
if [ "$(last_stable_version)" == "" ]; then
echo "Cannot update $file because the last stable version is unknown!"
exit 1
fi
local generate="false"
while IFS= read -r line
do
if [[ $line =~ ^\*\ \*\*Stable:\*\*.+$ ]]; then
generate="true"
else
if [ "$generate" == "true" ]; then
if [ "$line" == "" ]; then
generate="false"
generate_readme_versions \
'**Stable:** Thoroughly tested semi-annual official releases of openHAB. Use the stable version for your production environment if you do not need the latest enhancements and prefer a robust system.' \
"$(last_stable_minor_versions)"
generate_readme_versions \
'**Milestone:** Intermediary releases of the next openHAB version which are released about once a month. They include recently added features and bugfixes and are a good compromise between the current stable version and the bleeding-edge and potentially unstable snapshot version.' \
"$(milestone_versions)"
generate_readme_versions \
'**Snapshot:** Usually 1 or 2 days old and include the latest code. Use these for testing out very recent changes using the latest code. Be aware that some snapshots might be unstable so use these in production at your own risk!' \
"$(snapshot_versions)"
echo
fi
else
echo "$line"
fi
fi
done < $file > $file.new && mv $file.new $file
sed -i "s#version-[0-9]*\.[0-9]*\.[0-9]*-blue#version-$(last_stable_version)-blue#g" $file
sed -i "s#openhab/tags?name=[0-9]*\.[0-9]*\.[0-9]*#openhab/tags?name=$(last_stable_version)#g" $file
sed -i "s#openhab/openhab:[0-9]*\.[0-9]*\.[0-9]*#openhab/openhab:$(last_stable_version)#g" $file
sed -i "s#OPENHAB_VERSION=[0-9]*\.[0-9]*\.[0-9]*#OPENHAB_VERSION=$(last_stable_version)#g" $file
}
validate_readme_constraints() {
local count=$(wc -m <README.md)
if [ $count -gt 25000 ]; then
echo "README.md contains $count characters which exceeds the 25000 character limit of Docker Hub" >&2
exit 1
else
echo "README.md contains $count characters which is below the 25000 character limit of Docker Hub"
fi
}
update_dockerhub_readme() {
if [ "$(docker info 2>&1 | grep 'pushrm: Push Readme to container registry' | wc -l)" -eq "0" ]; then
# The pushrm plugin is available from https://github.com/christian-korneck/docker-pushrm
echo "Failed to update README for $(docker_repo) on Docker Hub (pushrm Docker CLI plugin not installed)" >&2
exit 1
fi
if docker pushrm $(docker_repo); then
echo "Successfully updated README for $(docker_repo) on Docker Hub"
else
echo "Failed to update README for $(docker_repo) on Docker Hub" >&2
exit 1
fi
}
prepare_builder() {
if [ "$BUILDER" == "" ]; then
docker run --privileged --rm tonistiigi/binfmt:qemu-v8.0.4 --install all &> /dev/null
(docker buildx inspect builder &> /dev/null && echo -e "\nReusing existing builder") || \
(docker buildx create --name builder --use &> /dev/null && echo -e "\nCreated builder")
BUILDER="builder"
fi
}
build() {
prepare_builder
local openhab_version="${1/SNAPSHOT/snapshot}"
local dist="$2"
local push="$3"
local java_version=""
case $openhab_version in
3.*) java_version="11";;
*) java_version="17";;
esac
local build_arg_options="--build-arg BUILD_DATE=$(date +"%Y-%m-%dT%H:%M:%SZ") --build-arg VCS_REF=$(git rev-parse HEAD) --build-arg JAVA_VERSION=$java_version --build-arg OPENHAB_VERSION=$openhab_version"
local tags=$(tags $openhab_version $dist)
local tag_options=${tags//$(docker_repo)/--tag $(docker_repo)}
local output_options="--output type=image,oci-mediatypes=false,push=$([ "$push" == "--push" ] && echo true || echo false)"
local build_options="$build_arg_options --platform $(platforms $openhab_version $dist) $tag_options $output_options --progress plain --provenance=false"
local dockerfile_path="./$dist"
local build_command="docker buildx build $build_options $dockerfile_path"
echo
echo "Building openHAB $openhab_version $dist Docker image"
echo
set -x
$build_command
{ set +x; } 2> /dev/null
echo
}