build-docker.sh does not assume PWD=<repo root> (#302)

* bash variables in build-docker.sh are wrapped by curly brackets

* Ensure presence of config file while running build-docker.sh

* Do not assume that build-docker.sh is run from the repository root directory

* Mount config file in predictable location in docker container
This commit is contained in:
Samuele Maci 2019-07-02 15:56:41 +02:00 committed by XECDesign
parent 57ef9b88e3
commit ae4ec6445e

View file

@ -1,36 +1,44 @@
#!/bin/bash -e
#!/bin/bash -eu
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
BUILD_OPTS="$*"
DOCKER="docker"
set +e
if ! $DOCKER ps >/dev/null 2>&1; then
if ! ${DOCKER} ps >/dev/null 2>&1; then
DOCKER="sudo docker"
fi
if ! $DOCKER ps >/dev/null; then
if ! ${DOCKER} ps >/dev/null; then
echo "error connecting to docker:"
$DOCKER ps
${DOCKER} ps
exit 1
fi
set -e
if [ -f config ]; then
# shellcheck disable=SC1091
source config
CONFIG_FILE=""
if [ -f "${DIR}/config" ]; then
CONFIG_FILE="${DIR}/config"
fi
while getopts "c:" flag
do
case "$flag" in
case "${flag}" in
c)
# shellcheck disable=SC1090
source "$OPTARG"
CONFIG_FILE="${OPTARG}"
;;
*)
;;
esac
done
# Ensure that the confguration file is present
if test -z "${CONFIG_FILE}"; then
echo "Configuration file need to be present in '${DIR}/config' or path passed as parameter"
exit 1
else
# shellcheck disable=SC1090
source "${CONFIG_FILE}"
fi
CONTAINER_NAME=${CONTAINER_NAME:-pigen_work}
CONTINUE=${CONTINUE:-0}
PRESERVE_CONTAINER=${PRESERVE_CONTAINER:-0}
@ -41,23 +49,27 @@ if [ -z "${IMG_NAME}" ]; then
exit 1
fi
CONTAINER_EXISTS=$($DOCKER ps -a --filter name="$CONTAINER_NAME" -q)
CONTAINER_RUNNING=$($DOCKER ps --filter name="$CONTAINER_NAME" -q)
if [ "$CONTAINER_RUNNING" != "" ]; then
echo "The build is already running in container $CONTAINER_NAME. Aborting."
CONTAINER_EXISTS=$(${DOCKER} ps -a --filter name="${CONTAINER_NAME}" -q)
CONTAINER_RUNNING=$(${DOCKER} ps --filter name="${CONTAINER_NAME}" -q)
if [ "${CONTAINER_RUNNING}" != "" ]; then
echo "The build is already running in container ${CONTAINER_NAME}. Aborting."
exit 1
fi
if [ "$CONTAINER_EXISTS" != "" ] && [ "$CONTINUE" != "1" ]; then
echo "Container $CONTAINER_NAME already exists and you did not specify CONTINUE=1. Aborting."
if [ "${CONTAINER_EXISTS}" != "" ] && [ "${CONTINUE}" != "1" ]; then
echo "Container ${CONTAINER_NAME} already exists and you did not specify CONTINUE=1. Aborting."
echo "You can delete the existing container like this:"
echo " $DOCKER rm -v $CONTAINER_NAME"
echo " ${DOCKER} rm -v ${CONTAINER_NAME}"
exit 1
fi
$DOCKER build -t pi-gen .
if [ "$CONTAINER_EXISTS" != "" ]; then
trap 'echo "got CTRL+C... please wait 5s"; $DOCKER stop -t 5 ${CONTAINER_NAME}_cont' SIGINT SIGTERM
time $DOCKER run --rm --privileged \
# Modify original build-options to allow config file to be mounted in the docker container
BUILD_OPTS="$(echo ${BUILD_OPTS:-} | sed -r 's@\-c\s?([^ ]+)@-c /config@')"
${DOCKER} build -t pi-gen "${DIR}"
if [ "${CONTAINER_EXISTS}" != "" ]; then
trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}_cont' SIGINT SIGTERM
time ${DOCKER} run --rm --privileged \
--volume "${CONFIG_FILE}":/config:ro \
--volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
pi-gen \
bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
@ -65,8 +77,9 @@ if [ "$CONTAINER_EXISTS" != "" ]; then
rsync -av work/*/build.log deploy/" &
wait "$!"
else
trap 'echo "got CTRL+C... please wait 5s"; $DOCKER stop -t 5 ${CONTAINER_NAME}' SIGINT SIGTERM
time $DOCKER run --name "${CONTAINER_NAME}" --privileged \
trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}' SIGINT SIGTERM
time ${DOCKER} run --name "${CONTAINER_NAME}" --privileged \
--volume "${CONFIG_FILE}":/config:ro \
pi-gen \
bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
@ -74,12 +87,12 @@ else
wait "$!"
fi
echo "copying results from deploy/"
$DOCKER cp "${CONTAINER_NAME}":/pi-gen/deploy .
${DOCKER} cp "${CONTAINER_NAME}":/pi-gen/deploy .
ls -lah deploy
# cleanup
if [ "$PRESERVE_CONTAINER" != "1" ]; then
$DOCKER rm -v "$CONTAINER_NAME"
if [ "${PRESERVE_CONTAINER}" != "1" ]; then
${DOCKER} rm -v "${CONTAINER_NAME}"
fi
echo "Done! Your image(s) should be in deploy/"