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.

build-docker.sh 3.3 KiB

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