forked from apache/couchdb-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·322 lines (288 loc) · 7.84 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
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# This shell script makes it easier to build multi-platform
# architecture Docker containers on an x86_64 host.
#
# For more reading:
# https://github.com/multiarch/qemu-user-static
# https://lobradov.github.io/Building-docker-multiarch-images/
# https://github.com/jessfraz/irssi/blob/master/.travis.yml
# https://engineering.docker.com/2019/04/multi-arch-images/
# https://github.com/docker/buildx
set -e
PROMPT="Are you sure (y/n)? "
QEMU="YES"
PLATFORMS="amd64 arm64v8 ppc64le"
BUILDX_PLATFORMS="linux/amd64,linux/arm64/v8,linux/ppc64le"
prompt() {
if [ -z "${PROMPT}" ]
then
return
fi
if [ "$1" ]
then
echo "$1"
fi
read -p "${PROMPT}"
if [[ $REPLY =~ ^[Yy]$ ]]
then
return
else
exit 0
fi
}
update_qemu() {
# necessary locally after every reboot, not sure why....update related maybe?
# basically harmless to run everytime, except for elevated privs necessary.
# disable with -n flag
docker rmi multiarch/qemu-user-static >/dev/null 2>&1 || true
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
docker rmi multiarch/qemu-user-static
}
clean() {
echo $#
if [ $# -eq 0 ]
then
regex="*"
ADD_PROMPT="This will remove *ALL* local apache/couchdb Docker images!"
elif [ $# -eq 1 ]
then
regex=$1
ADD_PROMPT="This will remove *ALL* apache/couchdb images matching regex '${1}' !"
else
usage
fi
prompt "${ADD_PROMPT}"
docker images --filter=reference="apache/couchdb:${regex}" | tr -s ' ' | cut -d ' ' -f 2 | while read tag
do
if [ ${tag} ] && [ ${tag} = "TAG" ]
then
continue
fi
docker rmi apache/couchdb:$tag
done
}
# Builds a specific version
build() {
VERSION=$1
ARCH=${2:-amd64}
FROMIMG="$(awk '$1 == toupper("FROM") { print $2 }' $VERSION/Dockerfile)"
CURRARCH=$(docker run --rm -t ${FROMIMG} uname -m)
if [ ${CURRARCH} != ${ARCH} ]
then
docker rmi ${FROMIMG}
docker pull "${ARCH}/${FROMIMG}"
docker tag "${ARCH}/${FROMIMG}" "${FROMIMG}"
fi
docker build -t apache/couchdb:${ARCH}-${VERSION} ${VERSION}
echo "CouchDB ${VERSION} for ${ARCH} built as apache/couchdb:${ARCH}-${VERSION}."
}
# Builds all platforms for a specific version, local only
# We can't do this with docker buildx, see https://github.com/docker/buildx/issues/166#issuecomment-562729523
build-all() {
VERSION=$1
for ARCH in ${PLATFORMS}; do
echo "Starting ${ARCH} at $(date)..."
build $1 ${ARCH}
echo ""
done
}
# Push locally built versions using above technique
push() {
if [ $2 ]
then
tag_as=$2
else
tag_as=$1
fi
docker manifest create apache/couchdb:$tag_as \
apache/couchdb:amd64-$1 \
apache/couchdb:arm64v8-$1 \
apache/couchdb:ppc64le-$1
docker manifest annotate apache/couchdb:$tag_as \
apache/couchdb:arm64v8-$1 --os linux --arch arm64 --variant v8
docker manifest annotate apache/couchdb:$tag_as \
apache/couchdb:ppc64le-$1 --os linux --arch ppc64le
docker manifest push --purge apache/couchdb:$tag_as
docker manifest inspect apache/couchdb:$tag_as
}
# Builds all platforms for a specific version and pushes to the registry
buildx() {
if [ $2 ]
then
tag_as=$2
else
tag_as=$1
fi
docker buildx rm apache-couchdb >/dev/null 2>&1 || true
docker buildx create --name apache-couchdb
docker buildx use apache-couchdb
docker buildx inspect --bootstrap
echo "Starting buildx build at $(date)..."
docker buildx build --platform ${BUILDX_PLATFORMS} --tag apache/couchdb:$tag_as --push $1
echo ""
}
usage() {
cat << EOF
$0 <command> <-f> <-n> [OPTIONS]
Options:
-f Skip confirmation prompt.
-n Do not install QEMU and binfmt_misc
(build commands only)
General commands:
clean Removes ALL local apache/couchdb images (!!)
clean <regex> Removes ALL local images with matching tags.
\`docker build\` commands:
version #.#.# [all] Builds all platforms for supplied version
Each platform is tagged <arch>-<version>.
version #.#.# <arch> Builds only the specified version and arch.
push #.#.# [as <tag>] Pushes locally-built versions as a multi-arch
manifest. If \`as <tag>\` is specified,
pushes the manifest using that tag instead.
Example workflow:
$0 clean *2.9.7*
$0 version 2.9.7 all
<test, then>
$0 push 2.9.7
$0 push 2.9.7 as 2.9
$0 push 2.9.7 as 2
$0 push 2.9.7 as latest
\`docker buildx\` commands:
buildx #.#.# Builds *and pushes* all platforms for supplied
version, using docker buildx. Built images must
be retrieved with \`docker pull\` for local use.
buildx #.#.# as <tag>
Builds and pushes all platforms for supplied
version, using docker buildx, tagging the
manifest with the supplied <tag>.
Example workflow:
$0 clean *2.9.7*
$0 buildx 2.9.7
$0 buildx 2.9.7 as 2.9
$0 buildx 2.9.7 as 2
$0 buildx 2.9.7 as latest
docker manifest inspect apache/couchdb:2.9.7
docker pull <--platform linux/other-arch> apache/couchdb:2.9.7 (for testing)
NOTE: Requires Docker 19.03+ with experimental features enabled.
Add { "experimental" : "true" } to /etc/docker/daemon.json, then
add { "experimental": "enabled" } to ~/.docker/config.json, then
restart the Docker daemon.
EOF
exit 0
}
# #######################
# handle -f/-n anywhere they appear on the CLI
POSITIONAL=()
while [[ $# -gt 0 ]]
do
# otherwise, we WILL match a regex against top-level directories!
set -f
key="$1"
case $key in
-f|--force)
unset PROMPT
shift
;;
-n|--no-qemu)
unset QEMU
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
set +f
done
# re-set all other arguments into argc
set -- "${POSITIONAL[@]}" # restore positional parameters
case "$1" in
clean)
# removes local images for a given version (and optionally platform)
shift
set -f
clean $*
set +f
;;
version)
# builds a specific version using docker build
# validate/reinstall QEMU
if [ ${QEMU} ]
then
update_qemu
fi
shift
if [ $# -lt 1 -o $# -gt 3 ]
then
usage
fi
# version #.#.# all
if [ "$2" = "all" ]
then
# build all the platforms and test them locally
build-all $1
else
# build a specific platform locally
build $1 $2
fi
;;
push)
# pushes already built local versions as manifest
shift
if [ $# -ne 1 -a $# -ne 3 ]
then
usage
fi
if [ $# -eq 1 ]
then
push $1
elif [ $2 = "as" ]
then
push $1 $3
else
usage
fi
;;
buildx)
# builds and pushes using docker buildx
if [ ${QEMU} ]
then
update_qemu
fi
shift
if [ $# -ne 1 -a $# -ne 3 ]
then
usage
fi
if [ $# -eq 1 ]
then
buildx $1
elif [ $2 = "as" ]
then
buildx $1 $3
else
usage
fi
;;
usage)
usage
;;
*)
usage
;;
esac