2019-07-02 13:56:41 +00:00
|
|
|
#!/bin/bash -eu
|
|
|
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
2017-07-27 10:11:07 +00:00
|
|
|
|
2019-02-18 12:54:15 +00:00
|
|
|
BUILD_OPTS="$*"
|
2019-02-16 14:59:32 +00:00
|
|
|
|
2017-01-23 12:44:03 +00:00
|
|
|
DOCKER="docker"
|
2019-07-02 13:56:41 +00:00
|
|
|
|
|
|
|
if ! ${DOCKER} ps >/dev/null 2>&1; then
|
2017-01-23 12:44:03 +00:00
|
|
|
DOCKER="sudo docker"
|
|
|
|
fi
|
2019-07-02 13:56:41 +00:00
|
|
|
if ! ${DOCKER} ps >/dev/null; then
|
2017-01-23 12:44:03 +00:00
|
|
|
echo "error connecting to docker:"
|
2019-07-02 13:56:41 +00:00
|
|
|
${DOCKER} ps
|
2017-01-23 12:44:03 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-07-02 13:56:41 +00:00
|
|
|
CONFIG_FILE=""
|
|
|
|
if [ -f "${DIR}/config" ]; then
|
|
|
|
CONFIG_FILE="${DIR}/config"
|
2017-01-23 12:44:03 +00:00
|
|
|
fi
|
|
|
|
|
2018-12-18 21:06:18 +00:00
|
|
|
while getopts "c:" flag
|
|
|
|
do
|
2019-07-02 13:56:41 +00:00
|
|
|
case "${flag}" in
|
2018-12-18 21:06:18 +00:00
|
|
|
c)
|
2019-07-02 13:56:41 +00:00
|
|
|
CONFIG_FILE="${OPTARG}"
|
2018-12-18 21:06:18 +00:00
|
|
|
;;
|
2019-02-18 12:54:15 +00:00
|
|
|
*)
|
|
|
|
;;
|
2018-12-18 21:06:18 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2019-07-30 11:38:26 +00:00
|
|
|
# Ensure that the configuration file is an absolute path
|
2019-11-11 18:23:58 +00:00
|
|
|
if test -x /usr/bin/realpath; then
|
2020-02-20 17:20:03 +00:00
|
|
|
CONFIG_FILE=$(realpath -s "$CONFIG_FILE" || realpath "$CONFIG_FILE")
|
2019-11-11 18:23:58 +00:00
|
|
|
fi
|
2019-07-30 11:38:26 +00:00
|
|
|
|
2019-07-02 13:56:41 +00:00
|
|
|
# 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
|
2020-02-20 17:20:03 +00:00
|
|
|
source ${CONFIG_FILE}
|
2019-07-02 13:56:41 +00:00
|
|
|
fi
|
|
|
|
|
2017-01-23 12:44:03 +00:00
|
|
|
CONTAINER_NAME=${CONTAINER_NAME:-pigen_work}
|
|
|
|
CONTINUE=${CONTINUE:-0}
|
2018-03-05 15:35:11 +00:00
|
|
|
PRESERVE_CONTAINER=${PRESERVE_CONTAINER:-0}
|
2017-01-23 12:44:03 +00:00
|
|
|
|
2018-12-18 21:06:18 +00:00
|
|
|
if [ -z "${IMG_NAME}" ]; then
|
|
|
|
echo "IMG_NAME not set in 'config'" 1>&2
|
|
|
|
echo 1>&2
|
|
|
|
exit 1
|
2017-01-23 12:44:03 +00:00
|
|
|
fi
|
|
|
|
|
2019-08-11 15:44:00 +00:00
|
|
|
# Ensure the Git Hash is recorded before entering the docker container
|
|
|
|
GIT_HASH=${GIT_HASH:-"$(git rev-parse HEAD)"}
|
|
|
|
|
2019-07-02 13:56:41 +00:00
|
|
|
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."
|
2017-01-23 12:44:03 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2019-07-02 13:56:41 +00:00
|
|
|
if [ "${CONTAINER_EXISTS}" != "" ] && [ "${CONTINUE}" != "1" ]; then
|
|
|
|
echo "Container ${CONTAINER_NAME} already exists and you did not specify CONTINUE=1. Aborting."
|
2017-01-23 12:44:03 +00:00
|
|
|
echo "You can delete the existing container like this:"
|
2019-07-02 13:56:41 +00:00
|
|
|
echo " ${DOCKER} rm -v ${CONTAINER_NAME}"
|
2017-01-23 12:44:03 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-07-02 13:56:41 +00:00
|
|
|
# Modify original build-options to allow config file to be mounted in the docker container
|
2019-07-30 11:38:26 +00:00
|
|
|
BUILD_OPTS="$(echo "${BUILD_OPTS:-}" | sed -E 's@\-c\s?([^ ]+)@-c /config@')"
|
2019-07-02 13:56:41 +00:00
|
|
|
|
2020-10-06 09:58:18 +00:00
|
|
|
# Check the arch of the machine we're running on. If it's 64-bit, use a 32-bit base image instead
|
|
|
|
case "$(uname -m)" in
|
|
|
|
x86_64|aarch64)
|
|
|
|
BASE_IMAGE=i386/debian:buster
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
BASE_IMAGE=debian:buster
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
${DOCKER} build --build-arg BASE_IMAGE=${BASE_IMAGE} -t pi-gen "${DIR}"
|
|
|
|
|
2019-07-02 13:56:41 +00:00
|
|
|
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 \
|
2019-08-11 15:44:00 +00:00
|
|
|
-e "GIT_HASH=${GIT_HASH}" \
|
2017-01-23 12:44:03 +00:00
|
|
|
--volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
|
|
|
|
pi-gen \
|
|
|
|
bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
|
2019-07-30 11:38:26 +00:00
|
|
|
cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
|
2017-01-23 12:44:03 +00:00
|
|
|
rsync -av work/*/build.log deploy/" &
|
2017-08-31 17:26:05 +00:00
|
|
|
wait "$!"
|
2017-01-23 12:44:03 +00:00
|
|
|
else
|
2019-07-02 13:56:41 +00:00
|
|
|
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 \
|
2019-08-11 15:44:00 +00:00
|
|
|
-e "GIT_HASH=${GIT_HASH}" \
|
2017-01-23 12:44:03 +00:00
|
|
|
pi-gen \
|
|
|
|
bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
|
2019-02-16 14:59:32 +00:00
|
|
|
cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
|
2017-01-23 12:44:03 +00:00
|
|
|
rsync -av work/*/build.log deploy/" &
|
2017-08-31 17:26:05 +00:00
|
|
|
wait "$!"
|
2017-01-23 12:44:03 +00:00
|
|
|
fi
|
2017-08-31 17:26:05 +00:00
|
|
|
echo "copying results from deploy/"
|
2019-07-02 13:56:41 +00:00
|
|
|
${DOCKER} cp "${CONTAINER_NAME}":/pi-gen/deploy .
|
2017-08-31 17:26:05 +00:00
|
|
|
ls -lah deploy
|
2018-03-05 15:35:11 +00:00
|
|
|
|
|
|
|
# cleanup
|
2019-07-02 13:56:41 +00:00
|
|
|
if [ "${PRESERVE_CONTAINER}" != "1" ]; then
|
|
|
|
${DOCKER} rm -v "${CONTAINER_NAME}"
|
2018-03-05 15:35:11 +00:00
|
|
|
fi
|
2017-08-31 17:26:05 +00:00
|
|
|
|
2017-01-23 12:44:03 +00:00
|
|
|
echo "Done! Your image(s) should be in deploy/"
|