thumbor: Update to post v6.0
[dockerfile.git] / build.sh
1 #!/bin/sh
2 #
3 # Copyright (C) 2013-2014 W. Trevor King <wking@tremily.us>
4 # Copyright (C) 2014 Naohiro Aota <naota@gentoo.org>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # * Redistributions of source code must retain the above copyright notice, this
10 # list of conditions and the following disclaimer.
11 #
12 # * Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 AUTHOR="${AUTHOR:-W. Trevor King <wking@tremily.us>}"
29 NAMESPACE="${NAMESPACE:-$(id -u -n)}"
30 DATE="${DATE:-20160804}"
31 MIRROR="${MIRROR:-http://distfiles.gentoo.org/}"
32 ARCH_URL="${ARCH_URL:-${MIRROR}releases/amd64/autobuilds/${DATE}/}"
33 STAGE3="${STAGE3:-stage3-amd64-${DATE}.tar.bz2}"
34 STAGE3_CONTENTS="${STAGE3_CONTENTS:-${STAGE3}.CONTENTS}"
35 STAGE3_DIGESTS="${STAGE3_DIGESTS:-${STAGE3}.DIGESTS.asc}"
36 PORTAGE_URL="${PORTAGE_URL:-${MIRROR}snapshots/}"
37 PORTAGE="${PORTAGE:-portage-${DATE}.tar.xz}"
38 PORTAGE_SIG="${PORTAGE_SIG:-${PORTAGE}.gpgsig}"
39
40 DOCKER_IO=$(command -v docker.io)
41 DOCKER="${DOCKER:-${DOCKER_IO:-docker}}"
42 BUILD_OPTS="${BUILD_OPTS:-}"
43
44 REPOS="${REPOS:-
45         portage
46         gentoo-portage
47         gentoo-python
48         gentoo-utc
49         gentoo-en-us
50         gentoo-openrc
51         gentoo-syslog
52         gentoo-java
53         gentoo-layman
54         gentoo-node
55         buildbot
56         docker-registry
57         elasticsearch
58         hubot
59         irker
60         memcached
61         nagios
62         nginx
63         nginx-proxy
64         kibana
65         kibana-azure
66         ngircd
67         package-cache
68         postgresql
69         redis
70         salt
71         salt-master
72         salt-minion
73         stunnel
74         thumbor
75         }"
76
77 die()
78 {
79         echo "$1"
80         exit 1
81 }
82
83 msg()
84 {
85         echo "$@"
86 }
87
88 REALPATH="${REALPATH:-$(command -v realpath)}"
89 if [ -z "${REALPATH}" ]; then
90         READLINK="${READLINK:-$(command -v readlink)}"
91         if [ -n "${READLINK}" ]; then
92                 REALPATH="${READLINK} -f"
93         else
94                 die "need realpath or readlink to canonicalize paths"
95         fi
96 fi
97
98 # Does "${NAMESPACE}/${REPO}:${DATE}" exist?
99 # Returns 0 (exists) or 1 (missing).
100 #
101 # Arguments:
102 #
103 # 1: REPO
104 repo_exists()
105 {
106         REPO="${1}"
107         IMAGES=$("${DOCKER}" images "${NAMESPACE}/${REPO}")
108         MATCHES=$(echo "${IMAGES}" | grep "${DATE}")
109         if [ -z "${MATCHES}" ]; then
110                 return 1
111         fi
112         return 0
113 }
114
115 # If they don't already exist:
116 #
117 # * download the stage3 and
118 # * create "${NAMESPACE}/gentoo:${DATE}"
119 #
120 # Forcibly tag "${NAMESPACE}/gentoo:${DATE}" with "latest"
121 import_stage3()
122 {
123         msg "import stage3"
124         if ! repo_exists gentoo; then
125                 # import stage3 image from Gentoo mirrors
126
127                 for FILE in "${STAGE3}" "${STAGE3_CONTENTS}" "${STAGE3_DIGESTS}"; do
128                         if [ ! -f "downloads/${FILE}" ]; then
129                                 wget -O "downloads/${FILE}" "${ARCH_URL}${FILE}" ||
130                                         die "failed to download ${ARCH_URL}${FILE}"
131                         fi
132                 done
133
134                 gpg --verify "downloads/${STAGE3_DIGESTS}" ||
135                         die "insecure digests for gentoo"
136                 SHA512_HASHES=$(grep -A1 SHA512 "downloads/${STAGE3_DIGESTS}" | grep -v '^--')
137                 SHA512_CHECK=$(cd downloads/ && (echo "${SHA512_HASHES}" | sha512sum -c))
138                 SHA512_FAILED=$(echo "${SHA512_CHECK}" | grep FAILED)
139                 if [ -n "${SHA512_FAILED}" ]; then
140                         die "${SHA512_FAILED}"
141                 fi
142
143                 msg "import ${NAMESPACE}/gentoo:${DATE}"
144                 "${DOCKER}" import - "${NAMESPACE}/gentoo:${DATE}" < "downloads/${STAGE3}" ||
145                         die "failed to import gentoo"
146         fi
147
148         msg "tag ${NAMESPACE}/gentoo:latest"
149         "${DOCKER}" tag -f "${NAMESPACE}/gentoo:${DATE}" "${NAMESPACE}/gentoo:latest" ||
150                         die "failed to tag gentoo"
151 }
152
153 # If they don't already exist:
154 #
155 # * download a portage snapshot and
156 # * create "${NAMESPACE}/portage-import:${DATE}"
157 #
158 # Forcibly tag "${NAMESPACE}/portage-import:${DATE}" with "latest"
159 import_portage()
160 {
161         msg "import portage"
162         if ! repo_exists portage-import; then
163                 # import portage image from Gentoo mirrors
164
165                 for FILE in "${PORTAGE}" "${PORTAGE_SIG}"; do
166                         if [ ! -f "downloads/${FILE}" ]; then
167                                 wget -O "downloads/${FILE}" "${PORTAGE_URL}${FILE}" ||
168                                         die "failed to download ${PORTAGE_URL}${FILE}"
169                         fi
170                 done
171
172                 gpg --verify "downloads/${PORTAGE_SIG}" "downloads/${PORTAGE}" ||
173                         die "insecure digests for portage-import"
174
175                 msg "import ${NAMESPACE}/portage-import:${DATE}"
176                 "${DOCKER}" import - "${NAMESPACE}/portage-import:${DATE}" < "downloads/${PORTAGE}" ||
177                         die "failed to import portage-import"
178         fi
179
180         msg "tag ${NAMESPACE}/portage-import:latest"
181         "${DOCKER}" tag -f "${NAMESPACE}/portage-import:${DATE}" "${NAMESPACE}/portage-import:latest" ||
182                 die "failed to tag portage-import"
183 }
184
185 # extract Busybox for the portage image
186 #
187 # Arguments:
188 #
189 # 1: SUBDIR target subdirectory for the busybox binary
190 extract_busybox()
191 {
192         SUBDIR="${1}"
193         msg "extract Busybox binary to ${SUBDIR}"
194         THIS_DIR=$(dirname $($REALPATH $0))
195         CONTAINER="${NAMESPACE}-gentoo-${DATE}-extract-busybox"
196         "${DOCKER}" run --name "${CONTAINER}" -v "${THIS_DIR}/${SUBDIR}/":/tmp "${NAMESPACE}/gentoo:${DATE}" cp /bin/busybox /tmp/
197         "${DOCKER}" rm "${CONTAINER}"
198 }
199
200 # If it doesn't already exist:
201 #
202 # * create "${NAMESPACE}/${REPO}:${DATE}" from
203 #   "${REPO}/Dockerfile.template"
204 #
205 # Forcibly tag "${NAMESPACE}/${REPO}:${DATE}" with "latest"
206 #
207 # Arguments:
208 #
209 # 1: REPO
210 build_repo()
211 {
212         REPO="${1}"
213         msg "build repo ${REPO}"
214         if ! repo_exists "${REPO}"; then
215                 if [ "${REPO}" = portage ]; then
216                         extract_busybox "${REPO}"
217                 fi
218
219                 env -i \
220                         NAMESPACE="${NAMESPACE}" \
221                         TAG="${DATE}" \
222                         MAINTAINER="${AUTHOR}" \
223                         envsubst '
224                                 ${NAMESPACE}
225                                 ${TAG}
226                                 ${MAINTAINER}
227                                 ' \
228                                 < "${REPO}/Dockerfile.template" > "${REPO}/Dockerfile"
229
230                 msg "build ${NAMESPACE}/${REPO}:${DATE}"
231                 "${DOCKER}" build ${BUILD_OPTS} -t "${NAMESPACE}/${REPO}:${DATE}" "${REPO}" ||
232                         die "failed to build ${REPO}"
233         fi
234         msg "tag ${NAMESPACE}/${REPO}:latest"
235         "${DOCKER}" tag -f "${NAMESPACE}/${REPO}:${DATE}" "${NAMESPACE}/${REPO}:latest" ||
236                         die "failed to tag ${REPO}"
237 }
238
239 build()
240 {
241         import_stage3
242         import_portage
243
244         for REPO in ${REPOS}; do
245                 build_repo "${REPO}"
246         done
247 }
248
249 missing()
250 {
251         for REPO in gentoo portage-import ${REPOS}; do
252                 if ! repo_exists "${REPO}"; then
253                         msg "${REPO}"
254                 fi
255         done
256 }
257
258 ACTION="${1:-build}"
259
260 case "${ACTION}" in
261 build) build ;;
262 missing) missing ;;
263 --help) msg "usage: ${0} [--help] {build|missing}" ;;
264 *) die "invalid action '${ACTION}'" ;;
265 esac