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