forked from nodejs/docker-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.sh
executable file
·363 lines (306 loc) · 7.53 KB
/
functions.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env bash
#
# Utlity functions
# Don't change this file unless needed
# The GitHub Action for automating new builds rely on this file
info() {
printf "%s\\n" "$@"
}
fatal() {
printf "**********\\n"
printf "Fatal Error: %s\\n" "$@"
printf "**********\\n"
exit 1
}
# Get system architecture
#
# This is used to get the target architecture for docker image.
# For crossing building, we need a way to specify the target
# architecutre manually.
function get_arch() {
local arch
case $(uname -m) in
x86_64)
arch="amd64"
;;
ppc64le)
arch="ppc64le"
;;
s390x)
arch="s390x"
;;
aarch64)
arch="arm64"
;;
armv7l)
arch="arm32v7"
;;
*)
echo "$0 does not support architecture ${arch} ... aborting"
exit 1
;;
esac
echo "${arch}"
}
# Get corresponding variants based on the architecture.
# All supported variants of each supported architecture are listed in a
# file - 'architectures'. Its format is:
# <architecture 1> <supported variant 1 >,<supported variant 2>...
# <architecture 2> <supported variant 1 >,<supported variant 2>...
function get_variants() {
local dir
dir=${1:-.}
shift
local arch
local availablevariants
local variantsfilter
local variants=()
arch=$(get_arch)
variantsfilter=("$@")
IFS=' ' read -ra availablevariants <<< "$(grep "^${arch}" "${dir}/architectures" | sed -E 's/'"${arch}"'[[:space:]]*//' | sed -E 's/,/ /g')"
if [ ${#variantsfilter[@]} -gt 0 ]; then
for variant1 in "${availablevariants[@]}"; do
for variant2 in "${variantsfilter[@]}"; do
if [ "${variant1}" = "${variant2}" ]; then
variants+=("${variant1}")
fi
done
done
if [ ${#variants[@]} -gt 0 ]; then
echo "${variants[@]}"
fi
else
echo "${availablevariants[@]}"
fi
}
# Get supported architectures for a specific version and variant
#
# Get default supported architectures from 'architectures'. Then go to the version folder
# to see if there is a local architectures file. The local architectures will override the
# default architectures. This will give us some benefits:
# - a specific version may or may not support some architectures
# - if there is no specialization for a version, just don't provide local architectures
function get_supported_arches() {
local version
local variant
local arches
local lines
local line
version="$1"
shift
variant="$1"
shift
# Get default supported arches
lines=$(grep "${variant}" "$(dirname "${version}")"/architectures 2> /dev/null | cut -d' ' -f1)
# Get version specific supported architectures if there is specialized information
if [ -a "${version}"/architectures ]; then
lines=$(grep "${variant}" "${version}"/architectures 2> /dev/null | cut -d' ' -f1)
fi
while IFS='' read -r line; do
arches+=("${line}")
done <<< "${lines}"
echo "${arches[@]}"
}
# Get configuration values from the config file
#
# The configuration entries are simple key/value pairs which are whitespace separated.
function get_config() {
local dir
dir=${1:-.}
shift
local name
name=${1}
shift
local value
value=$(grep "^${name}" "${dir}/config" | sed -E 's/'"${name}"'[[:space:]]*//')
echo "${value}"
}
# Get available versions for a given path
#
# The result is a list of valid versions.
function get_versions() {
local versions=()
local dirs=()
local default_variant
default_variant=$(get_config "./" "default_variant")
IFS=' ' read -ra dirs <<< "$(echo "./"*/)"
for dir in "${dirs[@]}"; do
if [ -a "${dir}/Dockerfile" ] || [ -a "${dir}/${default_variant}/Dockerfile" ]; then
versions+=("${dir#./}")
fi
done
if [ ${#versions[@]} -gt 0 ]; then
echo "${versions[@]%/}"
fi
}
function is_alpine() {
local variant
variant=${1}
shift
if [ "${variant}" = "${variant#alpine}" ]; then
return 1
fi
}
function is_debian() {
local variant
variant=$1
shift
IFS=' ' read -ra debianVersions <<< "$(get_config "./" "debian_versions")"
for d in "${debianVersions[@]}"; do
if [ "${d}" = "${variant}" ]; then
return 0
fi
done
return 1
}
function is_debian_slim() {
local variant
variant=$1
shift
IFS=' ' read -ra debianVersions <<< "$(get_config "./" "debian_versions")"
for d in "${debianVersions[@]}"; do
if [ "${d}-slim" = "${variant}" ]; then
return 0
fi
done
return 1
}
function get_fork_name() {
local version
version=$1
shift
IFS='/' read -ra versionparts <<< "${version}"
if [ ${#versionparts[@]} -gt 1 ]; then
echo "${versionparts[0]}"
fi
}
function get_full_tag() {
local variant
local tag
local full_tag
variant="$1"
shift
tag="$1"
shift
if [ -z "${variant}" ]; then
full_tag="${tag}"
elif [ "${variant}" = "default" ]; then
full_tag="${tag}"
else
full_tag="${tag}-${variant}"
fi
echo "${full_tag}"
}
function get_full_version() {
local version
version=$1
shift
local default_dockerfile
if [ -f "${version}/${default_variant}/Dockerfile" ]; then
default_dockerfile="${version}/${default_variant}/Dockerfile"
else
default_dockerfile="${version}/Dockerfile"
fi
grep -m1 'ENV NODE_VERSION ' "${default_dockerfile}" | cut -d' ' -f3
}
function get_major_minor_version() {
local version
version=$1
shift
local fullversion
fullversion=$(get_full_version "${version}")
echo "$(echo "${fullversion}" | cut -d'.' -f1).$(echo "${fullversion}" | cut -d'.' -f2)"
}
function get_path() {
local version
local variant
local path
version="$1"
shift
variant="$1"
shift
if [ -z "${variant}" ]; then
path="${version}/${variant}"
elif [ "${variant}" = "default" ]; then
path="${version}"
else
path="${version}/${variant}"
fi
echo "${path}"
}
function get_tag() {
local version
version=$1
shift
local versiontype
versiontype=${1:-full}
shift
local tagversion
if [ "${versiontype}" = full ]; then
tagversion=$(get_full_version "${version}")
elif [ "${versiontype}" = majorminor ]; then
tagversion=$(get_major_minor_version "${version}")
fi
local tagparts
IFS=' ' read -ra tagparts <<< "$(get_fork_name "${version}") ${tagversion}"
IFS='-'
echo "${tagparts[*]}"
unset IFS
}
function sort_versions() {
local versions=("$@")
local sorted
local lines
local line
IFS=$'\n'
lines="${versions[*]}"
unset IFS
while IFS='' read -r line; do
sorted+=("${line}")
done <<< "$(echo "${lines}" | grep "^[0-9]" | sort -r)"
while IFS='' read -r line; do
sorted+=("${line}")
done <<< "$(echo "${lines}" | grep -v "^[0-9]" | sort -r)"
echo "${sorted[@]}"
}
function commit_range() {
local commit_id_end=${1}
shift
local commit_id_start=${1}
if [ -z "${commit_id_start}" ]; then
if [ -z "${commit_id_end}" ]; then
echo "HEAD~1..HEAD"
elif [[ "${commit_id_end}" =~ .. ]]; then
echo "${commit_id_end}"
else
echo "${commit_id_end}~1..${commit_id_end}"
fi
else
echo "${commit_id_end}..${commit_id_start}"
fi
}
function images_updated() {
local commit_range
local versions
local images_changed
commit_range="$(commit_range "$@")"
IFS=' ' read -ra versions <<< "$(
IFS=','
get_versions
)"
images_changed=$(git diff --name-only "${commit_range}" "${versions[@]}")
if [ -z "${images_changed}" ]; then
return 1
fi
return 0
}
function tests_updated() {
local commit_range
local test_changed
commit_range="$(commit_range "$@")"
test_changed=$(git diff --name-only "${commit_range}" test*)
if [ -z "${test_changed}" ]; then
return 1
fi
return 0
}