mirror-synchronize.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. #
  3. # Script for automate mirroring
  4. # You can run this script manually
  5. #
  6. # -----------------------------------------------------------------------------
  7. # Common bash
  8. if [[ ! -z ${DEBUG_SHELL} ]]
  9. then
  10. set -x # Activate the expand mode if DEBUG is anything but empty.
  11. fi
  12. set -o errexit # Exit if command failed.
  13. set -o pipefail # Exit if pipe failed.
  14. set -o nounset # Exit if variable not set.
  15. die() {
  16. echo "${1:-"Unknown Error"}" 1>&2
  17. exit 1
  18. }
  19. # -----------------------------------------------------------------------------
  20. [ -z ${GITLAB_SSH_SERVER:-} ] && die "Have to set up GITLAB_SSH_SERVER environment variable"
  21. REPO_DIR=${1:-"${PWD}"}
  22. REPO_DIR=$(readlink -f -- "${REPO_DIR}")
  23. SCRIPT_DIR=$(dirname -- "${0}")
  24. SCRIPT_DIR=$(readlink -f -- "${SCRIPT_DIR}")
  25. SCRIPT_SH=$(readlink -f -- "${0}")
  26. MIRRORLIST=${SCRIPT_DIR}/mirror-list.txt
  27. [ -d "${REPO_DIR}" ] || die "${REPO_DIR} is not directory!"
  28. [ -f "${SCRIPT_SH}" ] || die "${SCRIPT_SH} does not exist!"
  29. [ -f "${MIRRORLIST}" ] || die "${MIRRORLIST} does not exist!"
  30. pushd ${REPO_DIR} >/dev/null
  31. # 0
  32. [ -f ".gitmodules" ] || exit 0
  33. # 1
  34. # Recursion
  35. git submodule update --init
  36. git submodule foreach "${SCRIPT_SH}" # No '--recursive'
  37. # 2
  38. git submodule deinit --force .
  39. git submodule update --init
  40. # 3
  41. # Mirroring from original URLs to ones listed in the MIRRORLIST
  42. # SED parses the strings like:
  43. #
  44. #-b991c67c1d91574ef22336cc3a5944d1e63230c9 roms/ipxe
  45. #b991c67c1d91574ef22336cc3a5944d1e63230c9 roms/ipxe (v1.0.0-2388-gb991c67)
  46. #
  47. for SUBPATH in $(git submodule status | sed -E 's/.*[[:space:]](.*)([[:space:]].*|$)/\1/')
  48. do
  49. SUBURL=$(git config -f .gitmodules --get submodule.$SUBPATH.url)
  50. SUBMIRROR=$(join -o"2.2" <(echo ${SUBPATH}) <(sort ${MIRRORLIST}))
  51. [ ${SUBMIRROR} ] || continue
  52. SUBMIRROR=${SUBMIRROR//@GENERAL_MIRROR_SERVER@/${GITLAB_SSH_SERVER}}
  53. ALLOW_TO_SYNC=$(join -o"2.3" <(echo ${SUBPATH}) <(sort ${MIRRORLIST}))
  54. if [ "${ALLOW_TO_SYNC}" != "ALLOW_TO_SYNC_FROM_PUBLIC" ]
  55. then
  56. echo "[should not to sync mirror] ${SUBMIRROR}"
  57. continue
  58. fi
  59. echo -e "[mirror sync] ${SUBURL} \tto\t ${SUBMIRROR}"
  60. pushd ${SUBPATH} >/dev/null
  61. git fetch --prune
  62. git push --prune ${SUBMIRROR} +refs/remotes/origin/*:refs/heads/* +refs/tags/*:refs/tags/*
  63. popd >/dev/null
  64. done
  65. popd >/dev/null