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