Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

build-docker.sh 3.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. BASE_IMAGE=i386/debian:buster
  67. ;;
  68. *)
  69. BASE_IMAGE=debian:buster
  70. ;;
  71. esac
  72. ${DOCKER} build --build-arg BASE_IMAGE=${BASE_IMAGE} -t pi-gen "${DIR}"
  73. if [ "${CONTAINER_EXISTS}" != "" ]; then
  74. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}_cont' SIGINT SIGTERM
  75. time ${DOCKER} run --rm --privileged \
  76. --cap-add=ALL \
  77. -v /dev:/dev \
  78. -v /lib/modules:/lib/modules \
  79. --volume "${CONFIG_FILE}":/config:ro \
  80. -e "GIT_HASH=${GIT_HASH}" \
  81. --volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
  82. pi-gen \
  83. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  84. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  85. rsync -av work/*/build.log deploy/" &
  86. wait "$!"
  87. else
  88. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}' SIGINT SIGTERM
  89. time ${DOCKER} run --name "${CONTAINER_NAME}" --privileged \
  90. --cap-add=ALL \
  91. -v /dev:/dev \
  92. -v /lib/modules:/lib/modules \
  93. --volume "${CONFIG_FILE}":/config:ro \
  94. -e "GIT_HASH=${GIT_HASH}" \
  95. pi-gen \
  96. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  97. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  98. rsync -av work/*/build.log deploy/" &
  99. wait "$!"
  100. fi
  101. echo "copying results from deploy/"
  102. ${DOCKER} cp "${CONTAINER_NAME}":/pi-gen/deploy .
  103. ls -lah deploy
  104. # cleanup
  105. if [ "${PRESERVE_CONTAINER}" != "1" ]; then
  106. ${DOCKER} rm -v "${CONTAINER_NAME}"
  107. fi
  108. echo "Done! Your image(s) should be in deploy/"