Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

137 rindas
3.7 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. 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. # Check the arch of the machine we're running on. If it's 64-bit, use a 32-bit base image instead
  65. case "$(uname -m)" in
  66. x86_64|aarch64)
  67. case "$(uname -m)" in
  68. aarch64)
  69. BASE_IMAGE=arm32v7/debian:bullseye
  70. ;;
  71. *)
  72. BASE_IMAGE=i386/debian:bullseye
  73. ;;
  74. esac
  75. ;;
  76. *)
  77. BASE_IMAGE=debian:bullseye
  78. ;;
  79. esac
  80. ${DOCKER} build --build-arg BASE_IMAGE=${BASE_IMAGE} -t pi-gen "${DIR}"
  81. if [ "${CONTAINER_EXISTS}" != "" ]; then
  82. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}_cont' SIGINT SIGTERM
  83. time ${DOCKER} run --rm --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. --volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
  91. pi-gen \
  92. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  93. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  94. rsync -av work/*/build.log deploy/" &
  95. wait "$!"
  96. else
  97. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}' SIGINT SIGTERM
  98. time ${DOCKER} run --name "${CONTAINER_NAME}" --privileged \
  99. --cap-add=ALL \
  100. -v /dev:/dev \
  101. -v /lib/modules:/lib/modules \
  102. ${PIGEN_DOCKER_OPTS} \
  103. --volume "${CONFIG_FILE}":/config:ro \
  104. -e "GIT_HASH=${GIT_HASH}" \
  105. pi-gen \
  106. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  107. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  108. rsync -av work/*/build.log deploy/" &
  109. wait "$!"
  110. fi
  111. echo "copying results from deploy/"
  112. ${DOCKER} cp "${CONTAINER_NAME}":/pi-gen/deploy .
  113. ls -lah deploy
  114. # cleanup
  115. if [ "${PRESERVE_CONTAINER}" != "1" ]; then
  116. ${DOCKER} rm -v "${CONTAINER_NAME}"
  117. fi
  118. echo "Done! Your image(s) should be in deploy/"