-
Notifications
You must be signed in to change notification settings - Fork 17
/
build.sh
executable file
·265 lines (239 loc) · 6.79 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
#!/bin/sh
#
# Copyright (C) 2013-2014 W. Trevor King <wking@tremily.us>
# Copyright (C) 2014 Naohiro Aota <naota@gentoo.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
AUTHOR="${AUTHOR:-W. Trevor King <wking@tremily.us>}"
NAMESPACE="${NAMESPACE:-$(id -u -n)}"
DATE="${DATE:-20160804}"
MIRROR="${MIRROR:-http://distfiles.gentoo.org/}"
ARCH_URL="${ARCH_URL:-${MIRROR}releases/amd64/autobuilds/${DATE}/}"
STAGE3="${STAGE3:-stage3-amd64-${DATE}.tar.bz2}"
STAGE3_CONTENTS="${STAGE3_CONTENTS:-${STAGE3}.CONTENTS}"
STAGE3_DIGESTS="${STAGE3_DIGESTS:-${STAGE3}.DIGESTS.asc}"
PORTAGE_URL="${PORTAGE_URL:-${MIRROR}snapshots/}"
PORTAGE="${PORTAGE:-portage-${DATE}.tar.xz}"
PORTAGE_SIG="${PORTAGE_SIG:-${PORTAGE}.gpgsig}"
DOCKER_IO=$(command -v docker.io)
DOCKER="${DOCKER:-${DOCKER_IO:-docker}}"
BUILD_OPTS="${BUILD_OPTS:-}"
REPOS="${REPOS:-
portage
gentoo-portage
gentoo-python
gentoo-utc
gentoo-en-us
gentoo-openrc
gentoo-syslog
gentoo-java
gentoo-layman
gentoo-node
buildbot
docker-registry
elasticsearch
hubot
irker
memcached
nagios
nginx
nginx-proxy
kibana
kibana-azure
ngircd
package-cache
postgresql
redis
salt
salt-master
salt-minion
stunnel
thumbor
}"
die()
{
echo "$1"
exit 1
}
msg()
{
echo "$@"
}
REALPATH="${REALPATH:-$(command -v realpath)}"
if [ -z "${REALPATH}" ]; then
READLINK="${READLINK:-$(command -v readlink)}"
if [ -n "${READLINK}" ]; then
REALPATH="${READLINK} -f"
else
die "need realpath or readlink to canonicalize paths"
fi
fi
# Does "${NAMESPACE}/${REPO}:${DATE}" exist?
# Returns 0 (exists) or 1 (missing).
#
# Arguments:
#
# 1: REPO
repo_exists()
{
REPO="${1}"
IMAGES=$("${DOCKER}" images "${NAMESPACE}/${REPO}")
MATCHES=$(echo "${IMAGES}" | grep "${DATE}")
if [ -z "${MATCHES}" ]; then
return 1
fi
return 0
}
# If they don't already exist:
#
# * download the stage3 and
# * create "${NAMESPACE}/gentoo:${DATE}"
#
# Forcibly tag "${NAMESPACE}/gentoo:${DATE}" with "latest"
import_stage3()
{
msg "import stage3"
if ! repo_exists gentoo; then
# import stage3 image from Gentoo mirrors
for FILE in "${STAGE3}" "${STAGE3_CONTENTS}" "${STAGE3_DIGESTS}"; do
if [ ! -f "downloads/${FILE}" ]; then
wget -O "downloads/${FILE}" "${ARCH_URL}${FILE}" ||
die "failed to download ${ARCH_URL}${FILE}"
fi
done
gpg --verify "downloads/${STAGE3_DIGESTS}" ||
die "insecure digests for gentoo"
SHA512_HASHES=$(grep -A1 SHA512 "downloads/${STAGE3_DIGESTS}" | grep -v '^--')
SHA512_CHECK=$(cd downloads/ && (echo "${SHA512_HASHES}" | sha512sum -c))
SHA512_FAILED=$(echo "${SHA512_CHECK}" | grep FAILED)
if [ -n "${SHA512_FAILED}" ]; then
die "${SHA512_FAILED}"
fi
msg "import ${NAMESPACE}/gentoo:${DATE}"
"${DOCKER}" import - "${NAMESPACE}/gentoo:${DATE}" < "downloads/${STAGE3}" ||
die "failed to import gentoo"
fi
msg "tag ${NAMESPACE}/gentoo:latest"
"${DOCKER}" tag -f "${NAMESPACE}/gentoo:${DATE}" "${NAMESPACE}/gentoo:latest" ||
die "failed to tag gentoo"
}
# If they don't already exist:
#
# * download a portage snapshot and
# * create "${NAMESPACE}/portage-import:${DATE}"
#
# Forcibly tag "${NAMESPACE}/portage-import:${DATE}" with "latest"
import_portage()
{
msg "import portage"
if ! repo_exists portage-import; then
# import portage image from Gentoo mirrors
for FILE in "${PORTAGE}" "${PORTAGE_SIG}"; do
if [ ! -f "downloads/${FILE}" ]; then
wget -O "downloads/${FILE}" "${PORTAGE_URL}${FILE}" ||
die "failed to download ${PORTAGE_URL}${FILE}"
fi
done
gpg --verify "downloads/${PORTAGE_SIG}" "downloads/${PORTAGE}" ||
die "insecure digests for portage-import"
msg "import ${NAMESPACE}/portage-import:${DATE}"
"${DOCKER}" import - "${NAMESPACE}/portage-import:${DATE}" < "downloads/${PORTAGE}" ||
die "failed to import portage-import"
fi
msg "tag ${NAMESPACE}/portage-import:latest"
"${DOCKER}" tag -f "${NAMESPACE}/portage-import:${DATE}" "${NAMESPACE}/portage-import:latest" ||
die "failed to tag portage-import"
}
# extract Busybox for the portage image
#
# Arguments:
#
# 1: SUBDIR target subdirectory for the busybox binary
extract_busybox()
{
SUBDIR="${1}"
msg "extract Busybox binary to ${SUBDIR}"
THIS_DIR=$(dirname $($REALPATH $0))
CONTAINER="${NAMESPACE}-gentoo-${DATE}-extract-busybox"
"${DOCKER}" run --name "${CONTAINER}" -v "${THIS_DIR}/${SUBDIR}/":/tmp "${NAMESPACE}/gentoo:${DATE}" cp /bin/busybox /tmp/
"${DOCKER}" rm "${CONTAINER}"
}
# If it doesn't already exist:
#
# * create "${NAMESPACE}/${REPO}:${DATE}" from
# "${REPO}/Dockerfile.template"
#
# Forcibly tag "${NAMESPACE}/${REPO}:${DATE}" with "latest"
#
# Arguments:
#
# 1: REPO
build_repo()
{
REPO="${1}"
msg "build repo ${REPO}"
if ! repo_exists "${REPO}"; then
if [ "${REPO}" = portage ]; then
extract_busybox "${REPO}"
fi
env -i \
NAMESPACE="${NAMESPACE}" \
TAG="${DATE}" \
MAINTAINER="${AUTHOR}" \
envsubst '
${NAMESPACE}
${TAG}
${MAINTAINER}
' \
< "${REPO}/Dockerfile.template" > "${REPO}/Dockerfile"
msg "build ${NAMESPACE}/${REPO}:${DATE}"
"${DOCKER}" build ${BUILD_OPTS} -t "${NAMESPACE}/${REPO}:${DATE}" "${REPO}" ||
die "failed to build ${REPO}"
fi
msg "tag ${NAMESPACE}/${REPO}:latest"
"${DOCKER}" tag -f "${NAMESPACE}/${REPO}:${DATE}" "${NAMESPACE}/${REPO}:latest" ||
die "failed to tag ${REPO}"
}
build()
{
import_stage3
import_portage
for REPO in ${REPOS}; do
build_repo "${REPO}"
done
}
missing()
{
for REPO in gentoo portage-import ${REPOS}; do
if ! repo_exists "${REPO}"; then
msg "${REPO}"
fi
done
}
ACTION="${1:-build}"
case "${ACTION}" in
build) build ;;
missing) missing ;;
--help) msg "usage: ${0} [--help] {build|missing}" ;;
*) die "invalid action '${ACTION}'" ;;
esac