run_update_and_commit.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  4. DEFAULT_APP_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
  5. APP_DIR="$(git -C "$DEFAULT_APP_DIR" rev-parse --show-toplevel 2>/dev/null || printf '%s' "$DEFAULT_APP_DIR")"
  6. CONFIG_PATH="${1:-${APP_DIR}/config.json}"
  7. DOMAIN_FILE="${APP_DIR}/runtime/current_domain.txt"
  8. export GIT_TERMINAL_PROMPT=0
  9. commit_name="${GIT_COMMIT_NAME:-vmess-domain-rotator}"
  10. commit_email="${GIT_COMMIT_EMAIL:-vmess-domain-rotator@localhost}"
  11. runtime_branch="${GIT_RUNTIME_BRANCH:-runtime-state}"
  12. push_remote="${GIT_PUSH_REMOTE:-origin}"
  13. push_enabled="${GIT_PUSH_ENABLED:-1}"
  14. push_required="${GIT_PUSH_REQUIRED:-${push_enabled}}"
  15. http_user="${GIT_HTTP_USERNAME:-git}"
  16. http_token="${GIT_HTTP_TOKEN:-}"
  17. http_token_file="${GIT_HTTP_TOKEN_FILE:-}"
  18. credential_helper="${GIT_CREDENTIAL_HELPER:-}"
  19. ts="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
  20. if [[ -z "$http_token" ]] && [[ -n "$http_token_file" ]] && [[ -r "$http_token_file" ]]; then
  21. http_token="$(tr -d '\r\n' < "$http_token_file")"
  22. fi
  23. git_auth_opts=()
  24. if [[ -n "$http_token" ]]; then
  25. auth_b64="$(printf '%s:%s' "$http_user" "$http_token" | base64 | tr -d '\n')"
  26. git_auth_opts=(-c "http.extraHeader=Authorization: Basic ${auth_b64}")
  27. fi
  28. git_auth() {
  29. local dir="$1"
  30. shift
  31. if [[ -n "$credential_helper" ]]; then
  32. git -C "$dir" -c credential.helper="$credential_helper" "${git_auth_opts[@]}" "$@"
  33. else
  34. git -C "$dir" "${git_auth_opts[@]}" "$@"
  35. fi
  36. }
  37. /usr/bin/python3 "${APP_DIR}/scripts/domain_updater.py" --config "$CONFIG_PATH"
  38. if [[ ! -f "$DOMAIN_FILE" ]]; then
  39. echo "[vmess-domain-rotator] runtime/current_domain.txt missing after updater run, skip git commit"
  40. exit 0
  41. fi
  42. after="$(tr -d '\r\n' < "$DOMAIN_FILE")"
  43. if [[ -z "$after" ]]; then
  44. echo "[vmess-domain-rotator] empty selected domain, skip git commit"
  45. exit 0
  46. fi
  47. if ! command -v git >/dev/null 2>&1; then
  48. echo "[vmess-domain-rotator] git not found, skip git commit"
  49. exit 0
  50. fi
  51. if ! git -C "$APP_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  52. echo "[vmess-domain-rotator] not a git repo at ${APP_DIR}, skip git commit"
  53. echo "[vmess-domain-rotator] hint: reinstall service from a git clone path"
  54. exit 0
  55. fi
  56. if ! git -C "$APP_DIR" rev-parse --verify HEAD >/dev/null 2>&1; then
  57. echo "[vmess-domain-rotator] repo has no commits yet, skip git commit"
  58. exit 0
  59. fi
  60. if ! git -C "$APP_DIR" remote get-url "$push_remote" >/dev/null 2>&1; then
  61. push_remote=""
  62. while IFS= read -r r; do
  63. push_remote="$r"
  64. break
  65. done < <(git -C "$APP_DIR" remote)
  66. fi
  67. work_dir=""
  68. cleanup_worktree="0"
  69. current_branch="$(git -C "$APP_DIR" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
  70. if [[ "$current_branch" == "$runtime_branch" ]]; then
  71. work_dir="$APP_DIR"
  72. else
  73. work_dir="$(mktemp -d "${TMPDIR:-/tmp}/vmess-runtime-state.XXXXXX")"
  74. cleanup_worktree="1"
  75. if git -C "$APP_DIR" show-ref --verify --quiet "refs/heads/${runtime_branch}"; then
  76. git -C "$APP_DIR" worktree add --force "$work_dir" "$runtime_branch"
  77. else
  78. if [[ -n "$push_remote" ]] && git_auth "$APP_DIR" ls-remote --exit-code --heads "$push_remote" "$runtime_branch" >/dev/null 2>&1; then
  79. git_auth "$APP_DIR" fetch "$push_remote" "$runtime_branch:$runtime_branch"
  80. git -C "$APP_DIR" worktree add --force "$work_dir" "$runtime_branch"
  81. else
  82. git -C "$APP_DIR" worktree add --force --detach "$work_dir"
  83. git -C "$work_dir" checkout --orphan "$runtime_branch"
  84. git -C "$work_dir" rm -rf . >/dev/null 2>&1 || true
  85. echo "[vmess-domain-rotator] initialized branch ${runtime_branch}"
  86. fi
  87. fi
  88. fi
  89. if [[ "$cleanup_worktree" == "1" ]]; then
  90. cleanup() {
  91. git -C "$APP_DIR" worktree remove --force "$work_dir" >/dev/null 2>&1 || rm -rf "$work_dir"
  92. }
  93. trap cleanup EXIT
  94. fi
  95. work_branch="$(git -C "$work_dir" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
  96. if [[ "$work_branch" != "$runtime_branch" ]]; then
  97. echo "[vmess-domain-rotator] safety check failed: worktree branch is '${work_branch:-detached}', expected '${runtime_branch}'"
  98. exit 1
  99. fi
  100. mkdir -p "$work_dir/runtime"
  101. for file in current_domain.txt current_domain.json state.json substore_vars.json; do
  102. src="$APP_DIR/runtime/$file"
  103. dst="$work_dir/runtime/$file"
  104. if [[ -f "$src" ]]; then
  105. cp "$src" "$dst"
  106. else
  107. rm -f "$dst"
  108. fi
  109. done
  110. git -C "$work_dir" add -A runtime/current_domain.txt runtime/current_domain.json runtime/state.json runtime/substore_vars.json || true
  111. if git -C "$work_dir" diff --cached --quiet; then
  112. echo "[vmess-domain-rotator] no staged changes for ${runtime_branch}, skip git commit"
  113. exit 0
  114. fi
  115. git -C "$work_dir" \
  116. -c user.name="$commit_name" \
  117. -c user.email="$commit_email" \
  118. commit -m "chore: rotate preferred domain to ${after} (${ts})"
  119. if [[ "$push_enabled" != "1" ]]; then
  120. echo "[vmess-domain-rotator] git push disabled by GIT_PUSH_ENABLED=${push_enabled}"
  121. elif [[ -z "$push_remote" ]]; then
  122. if [[ "$push_required" == "1" ]]; then
  123. echo "[vmess-domain-rotator] no remote found but push is required"
  124. exit 1
  125. fi
  126. echo "[vmess-domain-rotator] no remote found, skip git push"
  127. else
  128. push_ok="0"
  129. if git -C "$work_dir" rev-parse --abbrev-ref --symbolic-full-name "@{u}" >/dev/null 2>&1; then
  130. if git_auth "$work_dir" push "$push_remote" "$runtime_branch:$runtime_branch"; then
  131. push_ok="1"
  132. echo "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch}"
  133. fi
  134. else
  135. if git_auth "$work_dir" push -u "$push_remote" "$runtime_branch:$runtime_branch"; then
  136. push_ok="1"
  137. echo "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch} (set upstream)"
  138. fi
  139. fi
  140. if [[ "$push_ok" != "1" ]]; then
  141. echo "[vmess-domain-rotator] git push failed"
  142. echo "[vmess-domain-rotator] hint: configure non-interactive auth (credential.helper store, SSH deploy key, or GIT_HTTP_USERNAME/GIT_HTTP_TOKEN)"
  143. if [[ "$push_required" == "1" ]]; then
  144. exit 1
  145. fi
  146. fi
  147. fi
  148. echo "[vmess-domain-rotator] committed runtime changes on ${runtime_branch}: selected domain ${after}"