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