docker: Add build container files
authorGeoff Levand <geoff@infradead.org>
Tue, 1 May 2018 19:41:10 +0000 (12:41 -0700)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Fri, 4 May 2018 00:26:51 +0000 (10:26 +1000)
Add dockerfiles and helper scripts that create a docker image
with the tools needed to build petitboot.  See the docker/README.md
file for more info.

Signed-off-by: Geoff Levand <geoff@infradead.org>
Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
docker/Dockerfile.builder.amd64 [new file with mode: 0644]
docker/Dockerfile.builder.arm64 [new file with mode: 0644]
docker/README.md [new file with mode: 0644]
docker/build-builder [new file with mode: 0755]
docker/build-pb [new file with mode: 0755]
docker/builder-include [new file with mode: 0644]

diff --git a/docker/Dockerfile.builder.amd64 b/docker/Dockerfile.builder.amd64
new file mode 100644 (file)
index 0000000..dc0cce4
--- /dev/null
@@ -0,0 +1,24 @@
+# Image for compiling petitboot.
+
+FROM ubuntu:17.10
+
+ENV LANG C.UTF-8
+ENV LC_ALL C.UTF-8
+
+RUN apt-get update && apt-get install -y \
+       apt-utils \
+       autoconf \
+       autopoint \
+       bison \
+       flex \
+       gettext \
+       gcc \
+       git \
+       libtool \
+       libuv-dev \
+       libdevmapper-dev \
+       libncurses-dev \
+       pkg-config \
+       && rm -rf /var/lib/apt/lists/*
+
+CMD /bin/bash
diff --git a/docker/Dockerfile.builder.arm64 b/docker/Dockerfile.builder.arm64
new file mode 100644 (file)
index 0000000..d46581f
--- /dev/null
@@ -0,0 +1,24 @@
+# Image for compiling petitboot.
+
+FROM arm64v8/ubuntu:17.10
+
+ENV LANG C.UTF-8
+ENV LC_ALL C.UTF-8
+
+RUN apt-get update && apt-get install -y \
+       apt-utils \
+       autoconf \
+       autopoint \
+       bison \
+       flex \
+       gettext \
+       gcc \
+       git \
+       libtool \
+       libuv-dev \
+       libdevmapper-dev \
+       libncurses-dev \
+       pkg-config \
+       && rm -rf /var/lib/apt/lists/*
+
+CMD /bin/bash
diff --git a/docker/README.md b/docker/README.md
new file mode 100644 (file)
index 0000000..7e3966d
--- /dev/null
@@ -0,0 +1,29 @@
+# Petitboot Build Container Support
+
+## Command Summary
+
+ - `build-builder` Builds a docker image that contains tools for building petitboot.  Default docker image tag is `pb-builder:${VERSION}${ARCH_TAG}`.
+ - `build-pb` Builds the petitboot programs using a pb-builder container.
+
+## Examples
+
+### Build the petitboot programs
+
+    ./build-builder -v
+    ./build-pb -vc
+
+### Run petitboot programs in a pb-builder container
+
+    docker run --rm -v $(pwd):/opt/pb -w /opt/pb $(./docker/build-pb -t) ./ui/ncurses/petitboot-nc --help
+
+## Debugging Build Problems
+
+### Run an interactive pb-builder container
+
+As current user:
+
+    docker run --rm -it --user $(id -u):$(id -g) -v /etc/group:/etc/group:ro -v /etc/passwd:/etc/passwd:ro -v $(pwd):/opt/pb -w /opt/pb $(./docker/build-pb -t) bash
+
+As root:
+
+    docker run --rm -it -v /etc/group:/etc/group:ro -v /etc/passwd:/etc/passwd:ro -v $(pwd):/opt/pb -w /opt/pb $(./docker/build-pb -t) bash
diff --git a/docker/build-builder b/docker/build-builder
new file mode 100755 (executable)
index 0000000..8a5015c
--- /dev/null
@@ -0,0 +1,104 @@
+#!/usr/bin/env bash
+
+set -e
+
+name="$(basename $0)"
+
+: ${TOP_DIR:="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"}
+
+source ${TOP_DIR}/docker/builder-include
+
+: ${DOCKER_FILE:="${TOP_DIR}/docker/Dockerfile.builder.$(arch)"}
+
+usage () {
+       echo "${name} - Builds a docker image that contains tools for building petitboot." >&2
+       echo "Usage: ${name} [flags]" >&2
+       echo "Option flags:" >&2
+       echo "  -d --dry-run  - Do not run build commands." >&2
+       echo "  -h --help     - Show this help and exit." >&2
+       echo "  -p --purge    - Remove existing docker image and rebuild." >&2
+       echo "  -r --rebuild  - Rebuild existing docker image." >&2
+       echo "  -t --tag      - Print Docker tag to stdout and exit." >&2
+       echo "  -v --verbose  - Verbose execution." >&2
+       echo "Environment:" >&2
+       echo "  DOCKER_FILE   - Default: '${DOCKER_FILE}'" >&2
+       echo "  DOCKER_TAG    - Default: '${DOCKER_TAG}'" >&2
+       echo "Examples:" >&2
+       echo "  ${name} -v"
+}
+
+short_opts="dhprtv"
+long_opts="dry-run,help,purge,rebuild,tag,verbose"
+
+opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${name}" -- "$@")
+
+if [ $? != 0 ]; then
+       echo "${name}: ERROR: Internal getopt" >&2 
+       exit 1
+fi
+
+eval set -- "${opts}"
+
+while true ; do
+       case "${1}" in
+       -d | --dry-run)
+               dry_run=1
+               shift
+               ;;
+       -h | --help)
+               usage=1
+               shift
+               ;;
+       -p | --purge)
+               purge=1
+               shift
+               ;;
+       -r | --rebuild)
+               rebuild=1
+               shift
+               ;;
+       -t | --tag)
+               tag=1
+               shift
+               ;;
+       -v | --verbose)
+               set -x
+               verbose=1
+               shift
+               ;;
+       --)
+               shift
+               break
+               ;;
+       *)
+               echo "${name}: ERROR: Internal opts" >&2 
+               exit 1
+               ;;
+       esac
+done
+
+if [[ -n "${usage}" ]]; then
+       usage
+       exit 0
+fi
+
+if [[ -n "${tag}" ]]; then
+       show_tag
+       exit 0
+fi
+
+if [[ -n "${purge}" ]] && docker inspect --type image ${DOCKER_TAG} >/dev/null 2>/dev/null; then
+       echo "Removing docker image: ${DOCKER_TAG}" >&2
+       run_cmd "docker rmi --force ${DOCKER_TAG}"
+elif [[ -z "${rebuild}" ]] && docker inspect --type image ${DOCKER_TAG} >/dev/null 2>/dev/null; then
+       echo "Docker image exists: ${DOCKER_TAG}" >&2
+       show_tag
+       exit 0
+fi
+
+echo "Building docker image: ${DOCKER_TAG}" >&2
+
+cd ${TOP_DIR}
+run_cmd "docker build --file ${DOCKER_FILE} --tag ${DOCKER_TAG} ."
+
+show_tag
diff --git a/docker/build-pb b/docker/build-pb
new file mode 100755 (executable)
index 0000000..ce6c41f
--- /dev/null
@@ -0,0 +1,104 @@
+#!/usr/bin/env bash
+
+set -e
+
+name="$(basename $0)"
+
+: ${TOP_DIR:="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"}
+
+source ${TOP_DIR}/docker/builder-include
+
+usage () {
+       echo "${name} - Builds the petitboot programs using a pb-builder container." >&2
+       echo "Usage: ${name} [flags]" >&2
+       echo "Option flags:" >&2
+       echo "  -c --check    - Run 'make check'." >&2
+       echo "  -d --dry-run  - Do not run docker commands." >&2
+       echo "  -h --help     - Show this help and exit." >&2
+       echo "  -t --tag      - Print Docker tag to stdout and exit." >&2
+       echo "  -v --verbose  - Verbose execution." >&2
+       echo "Environment:" >&2
+       echo "  DOCKER_TAG    - Default: '${DOCKER_TAG}'" >&2
+       echo "Examples:" >&2
+       echo "  ${name} -vc"
+       echo "  ${name} -v"
+}
+
+short_opts="cdhtv"
+long_opts="check,dry-run,help,tag,verbose"
+
+opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${name}" -- "$@")
+
+if [ $? != 0 ]; then
+       echo "${name}: ERROR: Internal getopt" >&2 
+       exit 1
+fi
+
+eval set -- "${opts}"
+
+while true ; do
+       case "${1}" in
+       -c | --check)
+               check=1
+               shift
+               ;;
+       -d | --dry-run)
+               dry_run=1
+               shift
+               ;;
+       -h | --help)
+               usage=1
+               shift
+               ;;
+       -t | --tag)
+               tag=1
+               shift
+               ;;
+       -v | --verbose)
+               set -x
+               verbose=1
+               shift
+               ;;
+       --)
+               shift
+               break
+               ;;
+       *)
+               echo "${name}: ERROR: Internal opts" >&2 
+               exit 1
+               ;;
+       esac
+done
+
+if [[ -n "${usage}" ]]; then
+       usage
+       exit 0
+fi
+
+if [[ -n "${tag}" ]]; then
+       show_tag
+       exit 0
+fi
+
+docker_args="--rm \
+       -u $(id -u):$(id -g) \
+       -v /etc/group:/etc/group:ro \
+       -v /etc/passwd:/etc/passwd:ro \
+       -v ${TOP_DIR}:/opt/pb \
+       -w /opt/pb \
+"
+
+if [[ -n "$verbose}" ]]; then
+       bash_debug="-x"
+fi
+
+if [[ -n "${check}" ]]; then
+       docker_extra="make check"
+else
+       docker_extra="true"
+fi
+
+cd "${TOP_DIR}"
+run_cmd "docker run ${docker_args} ${DOCKER_TAG} /bin/bash \
+       -e ${bash_debug} \
+       -c './bootstrap && ./configure --with-ncurses && make && ${docker_extra}'"
diff --git a/docker/builder-include b/docker/builder-include
new file mode 100644 (file)
index 0000000..40e0120
--- /dev/null
@@ -0,0 +1,43 @@
+#!/usr/bin/env bash
+
+arch() {
+       local m="$(uname -m)"
+
+       case "${m}" in
+               aarch64) echo "arm64" ;;
+               x86_64)  echo "amd64" ;;
+               *)       echo "${m}" ;;
+       esac
+}
+
+arch_tag() {
+       local a="$(arch)"
+
+       case "${a}" in
+               amd64) echo "" ;;
+               *)     echo "-${a}" ;;
+       esac
+}
+
+run_cmd () {
+       local cmd="${*}"
+
+       if [[ -n ${verbose} || -n "${dry_run}" ]]; then
+               echo "==> ${cmd}"
+       fi
+
+       if [[ -n "${dry_run}" ]]; then
+               true
+       else
+               eval "${cmd}"
+       fi
+}
+
+show_tag () {
+       echo "${DOCKER_TAG}"
+}
+
+: ${VERSION:="1"}
+: ${ARCH_TAG:="$(arch_tag)"}
+: ${DOCKER_NAME:="pb-builder"}
+: ${DOCKER_TAG:="${DOCKER_NAME}:${VERSION}${ARCH_TAG}"}