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.
 
 
 
 
 

134 lines
3.6 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. if [ -z "${IMG_NAME}" ]; then
  43. echo "IMG_NAME not set in 'config'" 1>&2
  44. echo 1>&2
  45. exit 1
  46. fi
  47. # Ensure the Git Hash is recorded before entering the docker container
  48. GIT_HASH=${GIT_HASH:-"$(git rev-parse HEAD)"}
  49. CONTAINER_EXISTS=$(${DOCKER} ps -a --filter name="${CONTAINER_NAME}" -q)
  50. CONTAINER_RUNNING=$(${DOCKER} ps --filter name="${CONTAINER_NAME}" -q)
  51. if [ "${CONTAINER_RUNNING}" != "" ]; then
  52. echo "The build is already running in container ${CONTAINER_NAME}. Aborting."
  53. exit 1
  54. fi
  55. if [ "${CONTAINER_EXISTS}" != "" ] && [ "${CONTINUE}" != "1" ]; then
  56. echo "Container ${CONTAINER_NAME} already exists and you did not specify CONTINUE=1. Aborting."
  57. echo "You can delete the existing container like this:"
  58. echo " ${DOCKER} rm -v ${CONTAINER_NAME}"
  59. exit 1
  60. fi
  61. # Modify original build-options to allow config file to be mounted in the docker container
  62. BUILD_OPTS="$(echo "${BUILD_OPTS:-}" | sed -E 's@\-c\s?([^ ]+)@-c /config@')"
  63. # Check the arch of the machine we're running on. If it's 64-bit, use a 32-bit base image instead
  64. case "$(uname -m)" in
  65. x86_64|aarch64)
  66. case "$(uname -m)" in
  67. aarch64)
  68. BASE_IMAGE=arm32v7/debian:buster
  69. ;;
  70. *)
  71. BASE_IMAGE=i386/debian:buster
  72. ;;
  73. esac
  74. ;;
  75. *)
  76. BASE_IMAGE=debian:buster
  77. ;;
  78. esac
  79. ${DOCKER} build --build-arg BASE_IMAGE=${BASE_IMAGE} -t pi-gen "${DIR}"
  80. if [ "${CONTAINER_EXISTS}" != "" ]; then
  81. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}_cont' SIGINT SIGTERM
  82. time ${DOCKER} run --rm --privileged \
  83. --cap-add=ALL \
  84. -v /dev:/dev \
  85. -v /lib/modules:/lib/modules \
  86. --volume "${CONFIG_FILE}":/config:ro \
  87. -e "GIT_HASH=${GIT_HASH}" \
  88. --volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
  89. pi-gen \
  90. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  91. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  92. rsync -av work/*/build.log deploy/" &
  93. wait "$!"
  94. else
  95. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}' SIGINT SIGTERM
  96. time ${DOCKER} run --name "${CONTAINER_NAME}" --privileged \
  97. --cap-add=ALL \
  98. -v /dev:/dev \
  99. -v /lib/modules:/lib/modules \
  100. --volume "${CONFIG_FILE}":/config:ro \
  101. -e "GIT_HASH=${GIT_HASH}" \
  102. pi-gen \
  103. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  104. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  105. rsync -av work/*/build.log deploy/" &
  106. wait "$!"
  107. fi
  108. echo "copying results from deploy/"
  109. ${DOCKER} cp "${CONTAINER_NAME}":/pi-gen/deploy .
  110. ls -lah deploy
  111. # cleanup
  112. if [ "${PRESERVE_CONTAINER}" != "1" ]; then
  113. ${DOCKER} rm -v "${CONTAINER_NAME}"
  114. fi
  115. echo "Done! Your image(s) should be in deploy/"