run_update_and_commit.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. before=""
  38. if [[ -f "$DOMAIN_FILE" ]]; then
  39. before="$(tr -d '\r\n' < "$DOMAIN_FILE")"
  40. fi
  41. /usr/bin/python3 "${APP_DIR}/scripts/domain_updater.py" --config "$CONFIG_PATH"
  42. after=""
  43. if [[ -f "$DOMAIN_FILE" ]]; then
  44. after="$(tr -d '\r\n' < "$DOMAIN_FILE")"
  45. fi
  46. if [[ -z "$after" ]]; then
  47. echo "[vmess-domain-rotator] empty selected domain, skip git commit"
  48. exit 0
  49. fi
  50. if [[ "$before" == "$after" ]]; then
  51. echo "[vmess-domain-rotator] domain unchanged (${after}), skip git commit"
  52. exit 0
  53. fi
  54. if ! command -v git >/dev/null 2>&1; then
  55. echo "[vmess-domain-rotator] git not found, skip git commit"
  56. exit 0
  57. fi
  58. if ! git -C "$APP_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  59. echo "[vmess-domain-rotator] not a git repo at ${APP_DIR}, skip git commit"
  60. echo "[vmess-domain-rotator] hint: reinstall service from a git clone path"
  61. exit 0
  62. fi
  63. if ! git -C "$APP_DIR" rev-parse --verify HEAD >/dev/null 2>&1; then
  64. echo "[vmess-domain-rotator] repo has no commits yet, skip git commit"
  65. exit 0
  66. fi
  67. if ! git -C "$APP_DIR" remote get-url "$push_remote" >/dev/null 2>&1; then
  68. push_remote=""
  69. while IFS= read -r r; do
  70. push_remote="$r"
  71. break
  72. done < <(git -C "$APP_DIR" remote)
  73. fi
  74. work_dir=""
  75. cleanup_worktree="0"
  76. current_branch="$(git -C "$APP_DIR" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
  77. if [[ "$current_branch" == "$runtime_branch" ]]; then
  78. work_dir="$APP_DIR"
  79. else
  80. work_dir="$(mktemp -d "${TMPDIR:-/tmp}/vmess-runtime-state.XXXXXX")"
  81. cleanup_worktree="1"
  82. if git -C "$APP_DIR" show-ref --verify --quiet "refs/heads/${runtime_branch}"; then
  83. git -C "$APP_DIR" worktree add --force "$work_dir" "$runtime_branch"
  84. else
  85. if [[ -n "$push_remote" ]] && git_auth "$APP_DIR" ls-remote --exit-code --heads "$push_remote" "$runtime_branch" >/dev/null 2>&1; then
  86. git_auth "$APP_DIR" fetch "$push_remote" "$runtime_branch:$runtime_branch"
  87. git -C "$APP_DIR" worktree add --force "$work_dir" "$runtime_branch"
  88. else
  89. git -C "$APP_DIR" worktree add --force --detach "$work_dir"
  90. git -C "$work_dir" checkout --orphan "$runtime_branch"
  91. git -C "$work_dir" rm -rf . >/dev/null 2>&1 || true
  92. echo "[vmess-domain-rotator] initialized branch ${runtime_branch}"
  93. fi
  94. fi
  95. fi
  96. if [[ "$cleanup_worktree" == "1" ]]; then
  97. cleanup() {
  98. git -C "$APP_DIR" worktree remove --force "$work_dir" >/dev/null 2>&1 || rm -rf "$work_dir"
  99. }
  100. trap cleanup EXIT
  101. fi
  102. work_branch="$(git -C "$work_dir" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
  103. if [[ "$work_branch" != "$runtime_branch" ]]; then
  104. echo "[vmess-domain-rotator] safety check failed: worktree branch is '${work_branch:-detached}', expected '${runtime_branch}'"
  105. exit 1
  106. fi
  107. mkdir -p "$work_dir/runtime"
  108. for file in current_domain.txt current_domain.json state.json substore_vars.json; do
  109. src="$APP_DIR/runtime/$file"
  110. dst="$work_dir/runtime/$file"
  111. if [[ -f "$src" ]]; then
  112. cp "$src" "$dst"
  113. else
  114. rm -f "$dst"
  115. fi
  116. done
  117. git -C "$work_dir" add -A runtime/current_domain.txt runtime/current_domain.json runtime/state.json runtime/substore_vars.json || true
  118. if git -C "$work_dir" diff --cached --quiet; then
  119. echo "[vmess-domain-rotator] no staged changes for ${runtime_branch}, skip git commit"
  120. exit 0
  121. fi
  122. git -C "$work_dir" \
  123. -c user.name="$commit_name" \
  124. -c user.email="$commit_email" \
  125. commit -m "chore: rotate preferred domain to ${after} (${ts})"
  126. if [[ "$push_enabled" != "1" ]]; then
  127. echo "[vmess-domain-rotator] git push disabled by GIT_PUSH_ENABLED=${push_enabled}"
  128. elif [[ -z "$push_remote" ]]; then
  129. if [[ "$push_required" == "1" ]]; then
  130. echo "[vmess-domain-rotator] no remote found but push is required"
  131. exit 1
  132. fi
  133. echo "[vmess-domain-rotator] no remote found, skip git push"
  134. else
  135. push_ok="0"
  136. if git -C "$work_dir" rev-parse --abbrev-ref --symbolic-full-name "@{u}" >/dev/null 2>&1; then
  137. if git_auth "$work_dir" push "$push_remote" "$runtime_branch:$runtime_branch"; then
  138. push_ok="1"
  139. echo "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch}"
  140. fi
  141. else
  142. if git_auth "$work_dir" push -u "$push_remote" "$runtime_branch:$runtime_branch"; then
  143. push_ok="1"
  144. echo "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch} (set upstream)"
  145. fi
  146. fi
  147. if [[ "$push_ok" != "1" ]]; then
  148. echo "[vmess-domain-rotator] git push failed"
  149. echo "[vmess-domain-rotator] hint: configure non-interactive auth (credential.helper store, SSH deploy key, or GIT_HTTP_USERNAME/GIT_HTTP_TOKEN)"
  150. if [[ "$push_required" == "1" ]]; then
  151. exit 1
  152. fi
  153. fi
  154. fi
  155. echo "[vmess-domain-rotator] committed domain change on ${runtime_branch}: ${before} -> ${after}"