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