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