Burrow Raspberry Pi Image
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.9 KiB

  1. #!/bin/bash -eu
  2. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
  3. BUILD_OPTS="$*"
  4. DOCKER="docker"
  5. if ! ${DOCKER} ps >/dev/null 2>&1; 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. CONFIG_FILE=""
  14. if [ -f "${DIR}/config" ]; then
  15. CONFIG_FILE="${DIR}/config"
  16. fi
  17. while getopts "c:" flag
  18. do
  19. case "${flag}" in
  20. c)
  21. CONFIG_FILE="${OPTARG}"
  22. ;;
  23. *)
  24. ;;
  25. esac
  26. done
  27. # Ensure that the configuration file is an absolute path
  28. if test -x /usr/bin/realpath; then
  29. CONFIG_FILE=$(realpath -s "$CONFIG_FILE" || realpath "$CONFIG_FILE")
  30. fi
  31. # Ensure that the confguration file is present
  32. if test -z "${CONFIG_FILE}"; then
  33. echo "Configuration file need to be present in '${DIR}/config' or path passed as parameter"
  34. exit 1
  35. else
  36. # shellcheck disable=SC1090
  37. source ${CONFIG_FILE}
  38. fi
  39. CONTAINER_NAME=${CONTAINER_NAME:-pigen_work}
  40. CONTINUE=${CONTINUE:-0}
  41. PRESERVE_CONTAINER=${PRESERVE_CONTAINER:-0}
  42. # Ensure the Git Hash is recorded before entering the docker container
  43. GIT_HASH=${GIT_HASH:-"$(git rev-parse HEAD)"}
  44. CONTAINER_EXISTS=$(${DOCKER} ps -a --filter name="${CONTAINER_NAME}" -q)
  45. CONTAINER_RUNNING=$(${DOCKER} ps --filter name="${CONTAINER_NAME}" -q)
  46. if [ "${CONTAINER_RUNNING}" != "" ]; then
  47. echo "The build is already running in container ${CONTAINER_NAME}. Aborting."
  48. exit 1
  49. fi
  50. if [ "${CONTAINER_EXISTS}" != "" ] && [ "${CONTINUE}" != "1" ]; then
  51. echo "Container ${CONTAINER_NAME} already exists and you did not specify CONTINUE=1. Aborting."
  52. echo "You can delete the existing container like this:"
  53. echo " ${DOCKER} rm -v ${CONTAINER_NAME}"
  54. exit 1
  55. fi
  56. # Modify original build-options to allow config file to be mounted in the docker container
  57. BUILD_OPTS="$(echo "${BUILD_OPTS:-}" | sed -E '[email protected]\-c\s?([^ ]+)@-c /[email protected]')"
  58. ${DOCKER} build -t pi-gen "${DIR}"
  59. if [ "${CONTAINER_EXISTS}" != "" ]; then
  60. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}_cont' SIGINT SIGTERM
  61. time ${DOCKER} run --rm --privileged \
  62. --volume "${CONFIG_FILE}":/config:ro \
  63. -e "GIT_HASH=${GIT_HASH}" \
  64. --volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
  65. pi-gen \
  66. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  67. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  68. rsync -av work/*/build.log deploy/" &
  69. wait "$!"
  70. else
  71. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}' SIGINT SIGTERM
  72. time ${DOCKER} run --name "${CONTAINER_NAME}" --privileged \
  73. --volume "${CONFIG_FILE}":/config:ro \
  74. -e "GIT_HASH=${GIT_HASH}" \
  75. pi-gen \
  76. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  77. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  78. rsync -av work/*/build.log deploy/" &
  79. wait "$!"
  80. fi
  81. echo "copying results from deploy/"
  82. ${DOCKER} cp "${CONTAINER_NAME}":/pi-gen/deploy .
  83. ls -lah deploy
  84. # cleanup
  85. if [ "${PRESERVE_CONTAINER}" != "1" ]; then
  86. ${DOCKER} rm -v "${CONTAINER_NAME}"
  87. fi
  88. echo "Done! Your image(s) should be in deploy/"