build-docker.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/bash -e
  2. DOCKER="docker"
  3. set +e
  4. $DOCKER ps >/dev/null 2>&1
  5. if [ $? != 0 ]; then
  6. DOCKER="sudo docker"
  7. fi
  8. if ! $DOCKER ps >/dev/null; then
  9. echo "error connecting to docker:"
  10. $DOCKER ps
  11. exit 1
  12. fi
  13. set -e
  14. config_file=()
  15. if [ -f config ]; then
  16. config_file=("--env-file" "$(pwd)/config")
  17. source config
  18. fi
  19. CONTAINER_NAME=${CONTAINER_NAME:-pigen_work}
  20. CONTINUE=${CONTINUE:-0}
  21. if [ "$*" != "" ] || [ -z "${IMG_NAME}" ]; then
  22. if [ -z "${IMG_NAME}" ]; then
  23. echo "IMG_NAME not set in 'config'" 1>&2
  24. echo 1>&2
  25. fi
  26. cat >&2 <<EOF
  27. Usage:
  28. build-docker.sh [options]
  29. Optional environment arguments: ( =<default> )
  30. CONTAINER_NAME=pigen_work set a name for the build container
  31. CONTINUE=0 continue from a previously started container
  32. EOF
  33. exit 1
  34. fi
  35. CONTAINER_EXISTS=$($DOCKER ps -a --filter name="$CONTAINER_NAME" -q)
  36. CONTAINER_RUNNING=$($DOCKER ps --filter name="$CONTAINER_NAME" -q)
  37. if [ "$CONTAINER_RUNNING" != "" ]; then
  38. echo "The build is already running in container $CONTAINER_NAME. Aborting."
  39. exit 1
  40. fi
  41. if [ "$CONTAINER_EXISTS" != "" ] && [ "$CONTINUE" != "1" ]; then
  42. echo "Container $CONTAINER_NAME already exists and you did not specify CONTINUE=1. Aborting."
  43. echo "You can delete the existing container like this:"
  44. echo " $DOCKER rm -v $CONTAINER_NAME"
  45. exit 1
  46. fi
  47. $DOCKER build -t pi-gen .
  48. if [ "$CONTAINER_EXISTS" != "" ]; then
  49. trap "echo 'got CTRL+C... please wait 5s'; $DOCKER stop -t 5 ${CONTAINER_NAME}_cont" SIGINT SIGTERM
  50. time $DOCKER run --rm --privileged \
  51. --volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
  52. -e IMG_NAME=${IMG_NAME}\
  53. pi-gen \
  54. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  55. cd /pi-gen; ./build.sh;
  56. rsync -av work/*/build.log deploy/" &
  57. wait "$!"
  58. else
  59. trap "echo 'got CTRL+C... please wait 5s'; $DOCKER stop -t 5 ${CONTAINER_NAME}" SIGINT SIGTERM
  60. time $DOCKER run --name "${CONTAINER_NAME}" --privileged \
  61. -e IMG_NAME=${IMG_NAME}\
  62. "${config_file[@]}" \
  63. pi-gen \
  64. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  65. cd /pi-gen; ./build.sh &&
  66. rsync -av work/*/build.log deploy/" &
  67. wait "$!"
  68. fi
  69. echo "copying results from deploy/"
  70. $DOCKER cp "${CONTAINER_NAME}":/pi-gen/deploy .
  71. ls -lah deploy
  72. $DOCKER rm -v $CONTAINER_NAME
  73. echo "Done! Your image(s) should be in deploy/"