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