選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

build-docker.sh 3.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. ${DOCKER} build -t pi-gen "${DIR}"
  64. if [ "${CONTAINER_EXISTS}" != "" ]; then
  65. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}_cont' SIGINT SIGTERM
  66. time ${DOCKER} run --rm --privileged \
  67. --volume "${CONFIG_FILE}":/config:ro \
  68. -e "GIT_HASH=${GIT_HASH}" \
  69. --volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
  70. pi-gen \
  71. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  72. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  73. rsync -av work/*/build.log deploy/" &
  74. wait "$!"
  75. else
  76. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}' SIGINT SIGTERM
  77. time ${DOCKER} run --name "${CONTAINER_NAME}" --privileged \
  78. --volume "${CONFIG_FILE}":/config:ro \
  79. -e "GIT_HASH=${GIT_HASH}" \
  80. pi-gen \
  81. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  82. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  83. rsync -av work/*/build.log deploy/" &
  84. wait "$!"
  85. fi
  86. echo "copying results from deploy/"
  87. ${DOCKER} cp "${CONTAINER_NAME}":/pi-gen/deploy .
  88. ls -lah deploy
  89. # cleanup
  90. if [ "${PRESERVE_CONTAINER}" != "1" ]; then
  91. ${DOCKER} rm -v "${CONTAINER_NAME}"
  92. fi
  93. echo "Done! Your image(s) should be in deploy/"