Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

111 lignes
3.0 KiB

  1. #!/bin/bash -eu
  2. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
  3. curl wtfismyip.com/text
  4. BUILD_OPTS="$*"
  5. DOCKER="docker"
  6. if ! ${DOCKER} ps >/dev/null 2>&1; then
  7. DOCKER="sudo docker"
  8. fi
  9. if ! ${DOCKER} ps >/dev/null; then
  10. echo "error connecting to docker:"
  11. ${DOCKER} ps
  12. exit 1
  13. fi
  14. CONFIG_FILE=""
  15. if [ -f "${DIR}/config" ]; then
  16. CONFIG_FILE="${DIR}/config"
  17. fi
  18. while getopts "c:" flag
  19. do
  20. case "${flag}" in
  21. c)
  22. CONFIG_FILE="${OPTARG}"
  23. ;;
  24. *)
  25. ;;
  26. esac
  27. done
  28. # Ensure that the configuration file is an absolute path
  29. if test -x /usr/bin/realpath; then
  30. CONFIG_FILE=$(realpath -s "$CONFIG_FILE")
  31. fi
  32. # Ensure that the confguration file is present
  33. if test -z "${CONFIG_FILE}"; then
  34. echo "Configuration file need to be present in '${DIR}/config' or path passed as parameter"
  35. exit 1
  36. else
  37. # shellcheck disable=SC1090
  38. source "${CONFIG_FILE}"
  39. fi
  40. CONTAINER_NAME=${CONTAINER_NAME:-pigen_work}
  41. CONTINUE=${CONTINUE:-0}
  42. PRESERVE_CONTAINER=${PRESERVE_CONTAINER:-0}
  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. ${DOCKER} build -t pi-gen "${DIR}"
  65. if [ "${CONTAINER_EXISTS}" != "" ]; then
  66. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}_cont' SIGINT SIGTERM
  67. time ${DOCKER} run --rm --privileged \
  68. --volume "${CONFIG_FILE}":/config:ro \
  69. -e "GIT_HASH=${GIT_HASH}" \
  70. --volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
  71. pi-gen \
  72. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  73. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  74. rsync -av work/*/build.log deploy/" &
  75. wait "$!"
  76. else
  77. trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${CONTAINER_NAME}' SIGINT SIGTERM
  78. time ${DOCKER} run --name "${CONTAINER_NAME}" --privileged \
  79. --volume "${CONFIG_FILE}":/config:ro \
  80. -e "GIT_HASH=${GIT_HASH}" \
  81. pi-gen \
  82. bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
  83. cd /pi-gen; ./build.sh ${BUILD_OPTS} &&
  84. rsync -av work/*/build.log deploy/" &
  85. wait "$!"
  86. fi
  87. echo "copying results from deploy/"
  88. ${DOCKER} cp "${CONTAINER_NAME}":/pi-gen/deploy .
  89. ls -lah deploy
  90. # cleanup
  91. if [ "${PRESERVE_CONTAINER}" != "1" ]; then
  92. ${DOCKER} rm -v "${CONTAINER_NAME}"
  93. fi
  94. echo "Done! Your image(s) should be in deploy/"