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