download.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. set -xe
  3. cd "$(dirname "$0")"
  4. source util/vars.sh dl only
  5. if docker info -f "{{println .SecurityOptions}}" | grep rootless >/dev/null 2>&1; then
  6. UIDARGS=()
  7. else
  8. UIDARGS=( -u "$(id -u):$(id -g)" )
  9. fi
  10. [[ -t 1 ]] && TTY_ARG="-t" || TTY_ARG=""
  11. DL_SCRIPT_DIR="$(mktemp -d)"
  12. trap "rm -rf -- '$DL_SCRIPT_DIR'" EXIT
  13. mkdir -p "${PWD}"/.cache/downloads
  14. for STAGE in scripts.d/*.sh scripts.d/*/*.sh; do
  15. STAGENAME="$(basename "$STAGE" | sed 's/.sh$//')"
  16. cat <<-EOF >"${DL_SCRIPT_DIR}/${STAGENAME}.sh"
  17. set -xe -o pipefail
  18. shopt -s dotglob
  19. source /dl_functions.sh
  20. source "/$STAGE"
  21. STG="\$(ffbuild_dockerdl)"
  22. if [[ -z "\$STG" ]]; then
  23. exit 0
  24. fi
  25. DLHASH="\$(sha256sum <<<"\$STG" | cut -d" " -f1)"
  26. DLNAME="$STAGENAME"
  27. if [[ "$1" == "hashonly" ]]; then
  28. echo "\$DLHASH"
  29. exit 0
  30. fi
  31. TGT="/dldir/\${DLNAME}_\${DLHASH}.tar.xz"
  32. if [[ -f "\$TGT" ]]; then
  33. rm -f "/dldir/\${DLNAME}.tar.xz"
  34. ln -s "\${DLNAME}_\${DLHASH}.tar.xz" "/dldir/\${DLNAME}.tar.xz"
  35. exit 0
  36. fi
  37. WORKDIR="\$(mktemp -d)"
  38. trap "rm -rf -- '\$WORKDIR'" EXIT
  39. cd "\$WORKDIR"
  40. eval "\$STG"
  41. tar cpJf "\$TGT.tmp" .
  42. mv "\$TGT.tmp" "\$TGT"
  43. rm -f "/dldir/\${DLNAME}.tar.xz"
  44. ln -s "\${DLNAME}_\${DLHASH}.tar.xz" "/dldir/\${DLNAME}.tar.xz"
  45. EOF
  46. done
  47. docker run -i $TTY_ARG --rm "${UIDARGS[@]}" -v "${DL_SCRIPT_DIR}":/stages -v "${PWD}/.cache/downloads":/dldir -v "${PWD}/scripts.d":/scripts.d -v "${PWD}/util/dl_functions.sh":/dl_functions.sh "${REGISTRY}/${REPO}/base:latest" \
  48. bash -c 'set -xe && for STAGE in /stages/*.sh; do bash $STAGE; done'