2017-01-23 12:44:03 +00:00
|
|
|
#!/bin/bash -e
|
2017-07-27 10:11:07 +00:00
|
|
|
|
2017-01-23 12:44:03 +00:00
|
|
|
DOCKER="docker"
|
|
|
|
set +e
|
|
|
|
$DOCKER ps >/dev/null 2>&1
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
DOCKER="sudo docker"
|
|
|
|
fi
|
|
|
|
if ! $DOCKER ps >/dev/null; then
|
|
|
|
echo "error connecting to docker:"
|
|
|
|
$DOCKER ps
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
set -e
|
|
|
|
|
2017-08-31 17:26:05 +00:00
|
|
|
config_file=()
|
2017-01-23 12:44:03 +00:00
|
|
|
if [ -f config ]; then
|
2017-08-31 17:26:05 +00:00
|
|
|
config_file=("--env-file" "$(pwd)/config")
|
2017-01-23 12:44:03 +00:00
|
|
|
source config
|
|
|
|
fi
|
|
|
|
|
2018-12-18 21:06:18 +00:00
|
|
|
while getopts "c:" flag
|
|
|
|
do
|
|
|
|
case "$flag" in
|
|
|
|
c)
|
|
|
|
EXTRA_CONFIG="$OPTARG"
|
|
|
|
config_file=( "${config_file[@]}" "--env-file" "$(pwd)/${EXTRA_CONFIG}")
|
|
|
|
source "$EXTRA_CONFIG"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2019-02-18 09:22:58 +00:00
|
|
|
IMG_VERSION=${IMG_VERSION:-$(git describe)}
|
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
|
|
|
|
|
|
|
|
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."
|
|
|
|
echo "You can delete the existing container like this:"
|
2017-08-31 11:51:08 +00:00
|
|
|
echo " $DOCKER rm -v $CONTAINER_NAME"
|
2017-01-23 12:44:03 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
$DOCKER build -t pi-gen .
|
|
|
|
if [ "$CONTAINER_EXISTS" != "" ]; then
|
2017-08-31 11:51:08 +00:00
|
|
|
trap "echo 'got CTRL+C... please wait 5s'; $DOCKER stop -t 5 ${CONTAINER_NAME}_cont" SIGINT SIGTERM
|
2017-01-23 12:44:03 +00:00
|
|
|
time $DOCKER run --rm --privileged \
|
|
|
|
--volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
|
2018-03-02 20:08:24 +00:00
|
|
|
-e IMG_NAME="${IMG_NAME}"\
|
2019-02-18 09:22:58 +00:00
|
|
|
-e IMG_VERSION="${IMG_VERSION}"\
|
2017-01-23 12:44:03 +00:00
|
|
|
pi-gen \
|
|
|
|
bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
|
|
|
|
cd /pi-gen; ./build.sh;
|
|
|
|
rsync -av work/*/build.log deploy/" &
|
2017-08-31 17:26:05 +00:00
|
|
|
wait "$!"
|
2017-01-23 12:44:03 +00:00
|
|
|
else
|
2017-08-31 11:51:08 +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 \
|
2018-03-02 20:08:24 +00:00
|
|
|
-e IMG_NAME="${IMG_NAME}"\
|
2019-02-18 09:22:58 +00:00
|
|
|
-e IMG_VERSION="${IMG_VERSION}"\
|
2017-08-31 17:26:05 +00:00
|
|
|
"${config_file[@]}" \
|
2017-01-23 12:44:03 +00:00
|
|
|
pi-gen \
|
|
|
|
bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
|
|
|
|
cd /pi-gen; ./build.sh &&
|
|
|
|
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/"
|
|
|
|
$DOCKER cp "${CONTAINER_NAME}":/pi-gen/deploy .
|
|
|
|
ls -lah deploy
|
2018-03-05 15:35:11 +00:00
|
|
|
|
|
|
|
# cleanup
|
|
|
|
if [ "$PRESERVE_CONTAINER" != "1" ]; then
|
|
|
|
$DOCKER rm -v $CONTAINER_NAME
|
|
|
|
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/"
|