f52c44b3af9f901dde3f678bb2ceed2560be3bf0
[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         kibana-azure
57         postgresql
58         redis
59         stunnel
60         }"
61
62 die()
63 {
64         echo "$1"
65         exit 1
66 }
67
68 msg()
69 {
70         echo "$@"
71 }
72
73 REALPATH="${REALPATH:-$(command -v realpath)}"
74 if [ -z "${REALPATH}" ]; then
75         READLINK="${READLINK:-$(command -v readlink)}"
76         if [ -n "${READLINK}" ]; then
77                 REALPATH="${READLINK} -f"
78         else
79                 die "need realpath or readlink to canonicalize paths"
80         fi
81 fi
82
83 # Does "${NAMESPACE}/${REPO}:${DATE}" exist?
84 # Returns 0 (exists) or 1 (missing).
85 #
86 # Arguments:
87 #
88 # 1: REPO
89 repo_exists()
90 {
91         REPO="${1}"
92         IMAGES=$("${DOCKER}" images "${NAMESPACE}/${REPO}")
93         MATCHES=$(echo "${IMAGES}" | grep "${DATE}")
94         if [ -z "${MATCHES}" ]; then
95                 return 1
96         fi
97         return 0
98 }
99
100 # If they don't already exist:
101 #
102 # * download the stage3 and
103 # * create "${NAMESPACE}/gentoo:${DATE}"
104 #
105 # Forcibly tag "${NAMESPACE}/gentoo:${DATE}" with "latest"
106 import_stage3()
107 {
108         msg "import stage3"
109         if ! repo_exists gentoo; then
110                 # import stage3 image from Gentoo mirrors
111
112                 for FILE in "${STAGE3}" "${STAGE3_CONTENTS}" "${STAGE3_DIGESTS}"; do
113                         if [ ! -f "downloads/${FILE}" ]; then
114                                 wget -O "downloads/${FILE}" "${ARCH_URL}${FILE}" ||
115                                         die "failed to download ${ARCH_URL}${FILE}"
116                         fi
117                 done
118
119                 gpg --verify "downloads/${STAGE3_DIGESTS}" || die "insecure digests"
120                 SHA512_HASHES=$(grep -A1 SHA512 "downloads/${STAGE3_DIGESTS}" | grep -v '^--')
121                 SHA512_CHECK=$(cd downloads/ && (echo "${SHA512_HASHES}" | sha512sum -c))
122                 SHA512_FAILED=$(echo "${SHA512_CHECK}" | grep FAILED)
123                 if [ -n "${SHA512_FAILED}" ]; then
124                         die "${SHA512_FAILED}"
125                 fi
126
127                 msg "import ${NAMESPACE}/gentoo:${DATE}"
128                 "${DOCKER}" import - "${NAMESPACE}/gentoo:${DATE}" < "downloads/${STAGE3}" || die "failed to import"
129         fi
130
131         msg "tag ${NAMESPACE}/gentoo:latest"
132         "${DOCKER}" tag -f "${NAMESPACE}/gentoo:${DATE}" "${NAMESPACE}/gentoo:latest" || die "failed to tag"
133 }
134
135 # If they don't already exist:
136 #
137 # * download a portage snapshot and
138 # * create "${NAMESPACE}/portage-import:${DATE}"
139 #
140 # Forcibly tag "${NAMESPACE}/portage-import:${DATE}" with "latest"
141 import_portage()
142 {
143         msg "import portage"
144         if ! repo_exists portage-import; then
145                 # import portage image from Gentoo mirrors
146
147                 for FILE in "${PORTAGE}" "${PORTAGE_SIG}"; do
148                         if [ ! -f "downloads/${FILE}" ]; then
149                                 wget -O "downloads/${FILE}" "${PORTAGE_URL}${FILE}" ||
150                                         die "failed to download ${PORTAGE_URL}${FILE}"
151                         fi
152                 done
153
154                 gpg --verify "downloads/${PORTAGE_SIG}" "downloads/${PORTAGE}" || die "insecure digests"
155
156                 msg "import ${NAMESPACE}/portage-import:${DATE}"
157                 "${DOCKER}" import - "${NAMESPACE}/portage-import:${DATE}" < "downloads/${PORTAGE}" || die "failed to import"
158         fi
159
160         msg "tag ${NAMESPACE}/portage-import:latest"
161         "${DOCKER}" tag -f "${NAMESPACE}/portage-import:${DATE}" "${NAMESPACE}/portage-import:latest" || die "failed to tag"
162 }
163
164 # extract Busybox for the portage image
165 #
166 # Arguments:
167 #
168 # 1: SUBDIR target subdirectory for the busybox binary
169 extract_busybox()
170 {
171         SUBDIR="${1}"
172         msg "extract Busybox binary to ${SUBDIR}"
173         THIS_DIR=$(dirname $($REALPATH $0))
174         CONTAINER="${NAMESPACE}-gentoo-${DATE}-extract-busybox"
175         "${DOCKER}" run --name "${CONTAINER}" -v "${THIS_DIR}/${SUBDIR}/":/tmp "${NAMESPACE}/gentoo:${DATE}" cp /bin/busybox /tmp/
176         "${DOCKER}" rm "${CONTAINER}"
177 }
178
179 # If it doesn't already exist:
180 #
181 # * create "${NAMESPACE}/${REPO}:${DATE}" from
182 #   "${REPO}/Dockerfile.template"
183 #
184 # Forcibly tag "${NAMESPACE}/${REPO}:${DATE}" with "latest"
185 #
186 # Arguments:
187 #
188 # 1: REPO
189 build_repo()
190 {
191         REPO="${1}"
192         msg "build repo ${REPO}"
193         if ! repo_exists "${REPO}"; then
194                 if [ "${REPO}" = portage ]; then
195                         extract_busybox "${REPO}"
196                 fi
197
198                 env -i \
199                         NAMESPACE="${NAMESPACE}" \
200                         TAG="${DATE}" \
201                         MAINTAINER="${AUTHOR}" \
202                         envsubst '
203                                 ${NAMESPACE}
204                                 ${TAG}
205                                 ${MAINTAINER}
206                                 ' \
207                                 < "${REPO}/Dockerfile.template" > "${REPO}/Dockerfile"
208
209                 msg "build ${NAMESPACE}/${REPO}:${DATE}"
210                 "${DOCKER}" build ${BUILD_OPTS} -t "${NAMESPACE}/${REPO}:${DATE}" "${REPO}" || die "failed to build"
211         fi
212         msg "tag ${NAMESPACE}/${REPO}:latest"
213         "${DOCKER}" tag -f "${NAMESPACE}/${REPO}:${DATE}" "${NAMESPACE}/${REPO}:latest" || die "failed to tag"
214 }
215
216 build()
217 {
218         import_stage3
219         import_portage
220
221         for REPO in ${REPOS}; do
222                 build_repo "${REPO}"
223         done
224 }
225
226 missing()
227 {
228         for REPO in gentoo portage-import ${REPOS}; do
229                 if ! repo_exists "${REPO}"; then
230                         msg "${REPO}"
231                 fi
232         done
233 }
234
235 ACTION="${1:-build}"
236
237 case "${ACTION}" in
238 build) build ;;
239 missing) missing ;;
240 --help) msg "usage: ${0} [--help] {build|missing}" ;;
241 *) die "invalid action '${ACTION}'" ;;
242 esac