-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.sh
executable file
·337 lines (316 loc) · 11.4 KB
/
build.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
#!/bin/bash
versions=$(
echo 'wheezy' && \
echo 'jessie' && \
echo 'stretch' && \
echo 'buster' && \
echo 'bullseye' && \
echo 'bookworm'
)
detectVersion () {
# detection the major version of the image
version=""
if [[ "${version}" == "" ]] && [[ -f hashes/$1 ]] && [[ "$2" == "" ]]; then
version=$(cat hashes/$1 | grep 'version' | sed -e 's/^version://')
fi
if [[ "${version}" == "" ]]; then
for possibleVersion in $versions; do
if [[ $1 == *"${possibleVersion}"* ]]; then
version="${possibleVersion}"
fi
done
fi
if [[ "${version}" == "" ]]; then
docker image pull --platform linux/amd64 node:$1 > /dev/null
version=$(
docker run --rm --platform linux/amd64 node:$1 cat /etc/os-release | \
grep 'VERSION=' | \
grep -oE '\(.*\)' | \
grep -oE '\w+' || \
echo ''
)
fi
if [[ "${version}" == "" ]]; then
docker image pull --platform linux/amd64 node:$1 > /dev/null
version=$(
docker run --rm --platform linux/amd64 node:$1 cat /etc/os-release | \
grep 'PRETTY_NAME=' | \
grep -oE '\w+/\w+"' | \
grep -oE '\w+/' | \
grep -oE '\w+' || \
echo ''
)
fi
if [[ "${version}" == "" ]]; then
version=$(cat hashes/$1 | grep 'version' | sed -e 's/^version://')
fi
echo $version
}
detectDockerfile () {
# detecting suitable docker file
dockerfile="docker/Dockerfile"
if [[ -f docker/$1/Dockerfile ]]; then
dockerfile="docker/$1/Dockerfile"
fi
echo $dockerfile;
}
if [[ ! -d ./buildx-data ]]; then
mkdir ./buildx-data
fi
if [[ ! -d ./buildx-data/index ]]; then
mkdir ./buildx-data/index
fi
if [[ -f .url ]]; then
URL=$(cat .url)
fi
tagsInclude=""
if [[ $URL == "" ]]; then
URL=https://registry.hub.docker.com/v2/repositories/library/node/tags
for version in $versions; do
if [[ $version != "wheezy" ]] && [[ $version != "jessie" ]]; then
tagsInclude=$(
echo "${tagsInclude}" && \
echo "${version}-slim"
)
fi
tagsInclude=$(
echo "${tagsInclude}" && \
echo "${version}"
)
if [[ $version == "jessie" ]]; then
tagsInclude=$(
echo "${tagsInclude}" && \
echo "dubnium-${version}-slim" && \
echo "dubnium-${version}"
)
fi
done
fi
while [[ $URL != "" ]]; do
echo $URL > .url
exitCode=1
while [[ $exitCode != 0 ]]; do
content=$(curl -sL $URL)
exitCode=$?
echo "$exitCode - $URL"
done
URL=$(
echo $content | jq -r .next
)
if [[ "${URL}" == "null" ]]; then
URL=""
fi
tags=$(
echo "${tagsInclude}" && \
echo $content | \
jq -r '.results[].name' | \
grep -v 'alpine' | \
grep -v 'onbuild'
)
tags=$(echo "${tags}" | sed -E '/^$/d')
tagsInclude=""
for tag in $tags; do
exitCode=1
while [[ $exitCode != 0 ]]; do
content=$(curl -sL https://registry.hub.docker.com/v2/repositories/library/node/tags/$tag)
exitCode=$?
echo "$exitCode - https://registry.hub.docker.com/v2/repositories/library/node/tags/$tag"
done
mediaType=$(
echo $content | jq -r .media_type
)
if [[ "${mediaType:0:48}" == "application/vnd.docker.distribution.manifest.v1+" ]]; then
continue;
fi
digestCurrent=$(
echo $content | \
jq -r '.digest, .images[].digest' | \
sed '/^null$/d' | \
sort | \
uniq
)
tagStatus=$(
echo $content | \
jq -r '.tag_status'
)
if [[ "${digestCurrent}" == "" ]] && [[ "${tagStatus}" == "inactive" ]]; then
continue;
fi
version=$(detectVersion $tag)
dockerfile=$(detectDockerfile $version)
if [[ "${version}" == "" ]]; then
echo "Cannot detect version of ${tag}"
exit 1
fi
md5=""
if [[ "$(which md5)" != "" ]]; then
md5=$(md5 -q $dockerfile)
fi
if [[ "$(which md5sum)" != "" ]]; then
md5=$(md5sum $dockerfile | grep -oE '^[^ ]+')
fi
if [[ "${md5}" == "" ]]; then
echo "Cannot calculate md5 sum for the template"
exit 1
fi
updateExist=$(curl --fail -s -o /dev/null "http://deb.debian.org/debian/dists/$version-updates/Release" && echo "yes" || echo "")
securityExist=$(curl --fail -s -o /dev/null "http://security.debian.org/debian-security/dists/$version-security/Release" && echo "yes" || echo "")
if [[ "${securityExist}" == "" ]]; then
securityExist=$(curl --fail -s -o /dev/null "http://security.debian.org/debian-security/dists/$version/updates/Release" && echo "yes" || echo "")
fi
digestCurrent=$(
echo "${digestCurrent}" && \
echo dockerfile:${md5} && \
echo version:${version}
)
platforms=$(
echo $content | \
jq -r '.images[] | (.os + "/" + .architecture + "/" + .variant)' | \
sed '/unknown/d' | \
sed 's/\/$//' | \
sed 's/^\//linux\//' | \
sort | \
uniq | \
tr '\n' ',' | \
sed -e 's/,$//'
)
if [[ "${platforms}" == "linux/" ]]; then
platforms="linux/amd64"
fi
if [[ "${platforms}" == "" ]]; then
platforms="linux/amd64"
fi
contentType=$(
echo $content | jq -r .content_type
)
digestOld=$(cat hashes/$tag 2> /dev/null)
buildArgs=""
if [[ "${digestOld}" != "" ]]; then
buildArgs=$(echo "${digestOld}" | grep 'buildx:' | sed -E 's/^buildx\:/--cache-from type=local,src=.\/buildx-data,digest=/g')
digestOld=$(echo "${digestOld}" | sed -E '/buildx\:/d')
fi
if [[ "${digestCurrent}" != "" ]]; then
currentBuildFile=""
if [[ "$(which md5)" != "" ]]; then
currentBuildFile=$(echo "${digestCurrent}" | grep -oE '^sha256:.*$' | md5)
fi
if [[ "$(which md5sum)" != "" ]]; then
currentBuildFile=$(echo "${digestCurrent}" | grep -oE '^sha256:.*$' | md5sum | grep -oE '^[^ ]+')
fi
if [[ "${currentBuildFile}" == "" ]]; then
echo "Cannot calculate md5 sum for the digestCurrent"
exit 1
fi
if [[ -f "./buildx-data/index/${currentBuildFile}" ]]; then
buildArgs=$(
echo "${buildArgs}" && \
echo "--cache-from type=local,src=./buildx-data,digest=$(cat ./buildx-data/index/${currentBuildFile})"
)
fi
fi
if [[ "${digestOld}" != "" ]]; then
currentBuildFile=""
if [[ "$(which md5)" != "" ]]; then
currentBuildFile=$(echo "${digestOld}" | grep -oE '^sha256:.*$' | md5)
fi
if [[ "$(which md5sum)" != "" ]]; then
currentBuildFile=$(echo "${digestOld}" | grep -oE '^sha256:.*$' | md5sum | grep -oE '^[^ ]+')
fi
if [[ "${currentBuildFile}" == "" ]]; then
echo "Cannot calculate md5 sum for the digestOld"
exit 1
fi
if [[ -f "./buildx-data/index/${currentBuildFile}" ]]; then
buildArgs=$(
echo "${buildArgs}" && \
echo "--cache-from type=local,src=./buildx-data,digest=$(cat ./buildx-data/index/${currentBuildFile})"
)
fi
fi
buildArgs=$(
echo "${buildArgs}" && \
echo "--cache-to type=local,dest=./buildx-data"
)
buildCommand="docker buildx build --builder puppeteer-node"
if [[ "${contentType}" != "image" ]]; then
buildCommand="docker --context=default buildx build" && \
buildArgs=""
fi
if [[ "${digestOld}" != "" ]] && [[ "$(echo "$digestOld" | grep version:)" == "" ]]; then
echo version:${version} >> hashes/$tag
git add hashes/$tag
git commit -m "chore($tag): version" hashes/$tag
digestOld=$(
echo "$digestOld" && \
echo version:${version}
)
fi
if [[ "$(echo "$digestCurrent" | sort)" != "$(echo "$digestOld" | sort)" ]] && [[ $digestCurrent != "" ]] || [[ -f hashes/$tag.error ]] || [[ -f "hashes/${tag}@error" ]]; then
version=$(detectVersion $tag true)
dockerfile=$(detectDockerfile $version)
echo Tag: $tag
echo Version: $version
echo Platforms: $platforms
echo Update Repo: $updateExist
echo Security Repo: $securityExist
echo Dockerfile: $dockerfile
echo Command: $buildCommand
echo Args: $buildArgs
echo "FROM node:${tag}" > Dockerfile && \
cat $dockerfile \
| sed -e "s/UPDATE_REPO=\"\"/UPDATE_REPO=\"${updateExist}\"/" \
| sed -e "s/SECURITY_REPO=\"\"/SECURITY_REPO=\"${securityExist}\"/" \
>> Dockerfile && \
$(
CONTAINERD_ENABLE_DEPRECATED_PULL_SCHEMA_1_IMAGE=1 ${buildCommand} ${buildArgs} \
--add-host archive.debian.org.lo:172.16.0.1 \
--add-host deb.debian.org.lo:172.16.0.1 \
--add-host http.debian.net:172.16.0.1 \
--add-host httpredir.debian.org.lo:172.16.0.1 \
--add-host security.debian.org.lo:172.16.0.1 \
--add-host snapshot.debian.org.lo:172.16.0.1 \
--platform $platforms \
--allow network.host \
--tag satantime/puppeteer-node:$tag --push .
) && \
digestCurrent=$(echo "${digestCurrent}" | sed -E '/version:/d' && echo "version:${version}") && \
buildArgs=$(cat ./buildx-data/index.json | jq -r '.manifests[].digest') && \
digestCurrent=$(echo "${digestCurrent}" | sed -E '/buildx:/d' && echo "buildx:${buildArgs}") && \
currentBuildFile="" && \
if [[ "$(which md5)" != "" ]]; then
currentBuildFile=$(echo "${digestCurrent}" | grep -oE '^sha256:.*$' | md5)
fi && \
if [[ "$(which md5sum)" != "" ]]; then
currentBuildFile=$(echo "${digestCurrent}" | grep -oE '^sha256:.*$' | md5sum | grep -oE '^[^ ]+')
fi && \
if [[ "${currentBuildFile}" == "" ]]; then
echo "Cannot calculate md5 sum for the template"
exit 1
fi && \
echo "${buildArgs}" > "./buildx-data/index/${currentBuildFile}" && \
rm Dockerfile
code="${?}"
if [[ -f "hashes/${tag}.error" ]]; then
rm "hashes/${tag}.error"
git rm -f "hashes/${tag}.error"
fi
if [[ -f "hashes/${tag}@error" ]]; then
rm "hashes/${tag}@error"
git rm -f "hashes/${tag}@error"
fi
if [[ "${code}" == "0" ]]; then
printf '%s\n' $digestCurrent > hashes/$tag
git add hashes/$tag
git commit -m "chore($tag): updated" "hashes/${tag}"
fi
if [[ "${code}" != "0" ]]; then
printf '%s\n' $digestCurrent > "hashes/${tag}@error"
git add "hashes/${tag}@error"
fi
sleep 10
fi
true;
done
true;
done
rm .url