3 # Copyright (C) 2013 W. Trevor King <wking@tremily.us>
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright notice, this
9 # list of conditions and the following disclaimer.
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.
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.
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/current-stage3/}"
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}"
39 DOCKER_IO=$(command -v docker.io)
40 DOCKER="${DOCKER:-${DOCKER_IO:-docker}}"
70 REALPATH="${REALPATH:-$(command -v realpath)}"
71 if [ -z "${REALPATH}" ]; then
72 READLINK="${READLINK:-$(command -v readlink)}"
73 if [ -n "${READLINK}" ]; then
74 REALPATH="${READLINK} -f"
76 die "need realpath or readlink to canonicalize paths"
80 # Does "${NAMESPACE}/${REPO}:${DATE}" exist?
81 # Returns 0 (exists) or 1 (missing).
89 IMAGES=$("${DOCKER}" images "${NAMESPACE}/${REPO}")
90 MATCHES=$(echo "${IMAGES}" | grep "${DATE}")
91 if [ -z "${MATCHES}" ]; then
97 # If they don't already exist:
99 # * download the stage3 and
100 # * create "${NAMESPACE}/gentoo:${DATE}"
102 # Forcibly tag "${NAMESPACE}/gentoo:${DATE}" with "latest"
106 if ! repo_exists gentoo; then
107 # import stage3 image from Gentoo mirrors
109 for FILE in "${STAGE3}" "${STAGE3_CONTENTS}" "${STAGE3_DIGESTS}"; do
110 if [ ! -f "downloads/${FILE}" ]; then
111 wget -O "downloads/${FILE}" "${ARCH_URL}${FILE}"
115 gpg --verify "downloads/${STAGE3_DIGESTS}" || die "insecure digests"
116 SHA512_HASHES=$(grep -A1 SHA512 "downloads/${STAGE3_DIGESTS}" | grep -v '^--')
117 SHA512_CHECK=$(cd downloads/ && (echo "${SHA512_HASHES}" | sha512sum -c))
118 SHA512_FAILED=$(echo "${SHA512_CHECK}" | grep FAILED)
119 if [ -n "${SHA512_FAILED}" ]; then
120 die "${SHA512_FAILED}"
123 msg "import ${NAMESPACE}/gentoo:${DATE}"
124 "${DOCKER}" import - "${NAMESPACE}/gentoo:${DATE}" < "downloads/${STAGE3}" || die "failed to import"
127 msg "tag ${NAMESPACE}/gentoo:latest"
128 "${DOCKER}" tag -f "${NAMESPACE}/gentoo:${DATE}" "${NAMESPACE}/gentoo:latest" || die "failed to tag"
131 # If they don't already exist:
133 # * download a portage snapshot and
134 # * create "${NAMESPACE}/portage-import:${DATE}"
136 # Forcibly tag "${NAMESPACE}/portage-import:${DATE}" with "latest"
140 if ! repo_exists portage-import; then
141 # import portage image from Gentoo mirrors
143 for FILE in "${PORTAGE}" "${PORTAGE_SIG}"; do
144 if [ ! -f "downloads/${FILE}" ]; then
145 wget -O "downloads/${FILE}" "${PORTAGE_URL}${FILE}"
149 gpg --verify "downloads/${PORTAGE_SIG}" "downloads/${PORTAGE}" || die "insecure digests"
151 msg "import ${NAMESPACE}/portage-import:${DATE}"
152 "${DOCKER}" import - "${NAMESPACE}/portage-import:${DATE}" < "downloads/${PORTAGE}" || die "failed to import"
155 msg "tag ${NAMESPACE}/portage-import:latest"
156 "${DOCKER}" tag -f "${NAMESPACE}/portage-import:${DATE}" "${NAMESPACE}/portage-import:latest" || die "failed to tag"
159 # extract Busybox for the portage image
163 # 1: SUBDIR target subdirectory for the busybox binary
167 msg "extract Busybox binary to ${SUBDIR}"
168 THIS_DIR=$(dirname $($REALPATH $0))
169 CONTAINER="${NAMESPACE}-gentoo-${DATE}-extract-busybox"
170 "${DOCKER}" run -name "${CONTAINER}" -v "${THIS_DIR}/${SUBDIR}/":/tmp "${NAMESPACE}/gentoo:${DATE}" cp /bin/busybox /tmp/
171 "${DOCKER}" rm "${CONTAINER}"
174 # If it doesn't already exist:
176 # * create "${NAMESPACE}/${REPO}:${DATE}" from
177 # "${REPO}/Dockerfile.template"
179 # Forcibly tag "${NAMESPACE}/${REPO}:${DATE}" with "latest"
187 msg "build repo ${REPO}"
188 if ! repo_exists "${REPO}"; then
189 if [ "${REPO}" = portage ]; then
190 extract_busybox "${REPO}"
194 NAMESPACE="${NAMESPACE}" \
196 MAINTAINER="${AUTHOR}" \
202 < "${REPO}/Dockerfile.template" > "${REPO}/Dockerfile"
204 msg "build ${NAMESPACE}/${REPO}:${DATE}"
205 "${DOCKER}" build -t "${NAMESPACE}/${REPO}:${DATE}" "${REPO}" || die "failed to build"
207 msg "tag ${NAMESPACE}/${REPO}:latest"
208 "${DOCKER}" tag -f "${NAMESPACE}/${REPO}:${DATE}" "${NAMESPACE}/${REPO}:latest" || die "failed to tag"
216 for REPO in ${REPOS}; do
223 for REPO in gentoo portage-import ${REPOS}; do
224 if ! repo_exists "${REPO}"; then
235 --help) msg "usage: ${0} [--help] {build|missing}" ;;
236 *) die "invalid action '${ACTION}'" ;;