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