run_update_and_commit.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # --- journald tee: log to journal even when run manually ---
  4. if [[ -z "${INVOCATION_ID:-}" ]] && command -v systemd-cat >/dev/null 2>&1; then
  5. exec > >(tee >(systemd-cat -t vmess-domain-rotator -p info)) \
  6. 2> >(tee >(systemd-cat -t vmess-domain-rotator -p err) >&2)
  7. fi
  8. timestamp() {
  9. date '+%Y-%m-%d %H:%M:%S'
  10. }
  11. log() {
  12. printf '[%s] %s\n' "$(timestamp)" "$*"
  13. }
  14. log_err() {
  15. printf '[%s] %s\n' "$(timestamp)" "$*" >&2
  16. }
  17. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  18. DEFAULT_APP_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
  19. APP_DIR="$(git -C "$DEFAULT_APP_DIR" rev-parse --show-toplevel 2>/dev/null || printf '%s' "$DEFAULT_APP_DIR")"
  20. force_commit="${GIT_FORCE_COMMIT:-0}"
  21. if [[ "${1:-}" == "--force-commit" ]]; then
  22. force_commit="1"
  23. shift
  24. fi
  25. if [[ ! "$force_commit" =~ ^[01]$ ]]; then
  26. log "[vmess-domain-rotator] invalid GIT_FORCE_COMMIT=${force_commit}, expected 0 or 1"
  27. exit 1
  28. fi
  29. CONFIG_PATH="${1:-${APP_DIR}/config.server.json}"
  30. # --- adaptive interval check ---
  31. if [[ "$force_commit" != "1" ]]; then
  32. peak_start="${PEAK_START_HOUR:-19}"
  33. peak_end="${PEAK_END_HOUR:-24}"
  34. peak_tz="${PEAK_TZ:-Asia/Shanghai}"
  35. peak_int="${PEAK_INTERVAL_MIN:-10}"
  36. offpeak_int="${OFFPEAK_INTERVAL_MIN:-30}"
  37. # Get current hour and minute in peak timezone
  38. curr_hour=$(TZ="$peak_tz" date '+%H' | sed 's/^0//')
  39. curr_hour=${curr_hour:-0}
  40. curr_min=$(TZ="$peak_tz" date '+%M' | sed 's/^0//')
  41. curr_min=${curr_min:-0}
  42. # Normalize peak_end=24 to 0 (both mean "end of day / midnight")
  43. [[ "$peak_end" -eq 24 ]] && peak_end=0
  44. in_peak=0
  45. if [[ "$peak_start" -eq "$peak_end" ]]; then
  46. # start == end: no peak window defined, always off-peak
  47. in_peak=0
  48. elif [[ "$peak_start" -lt "$peak_end" ]]; then
  49. # Normal range (e.g. 8 to 22)
  50. if [[ "$curr_hour" -ge "$peak_start" ]] && [[ "$curr_hour" -lt "$peak_end" ]]; then
  51. in_peak=1
  52. fi
  53. else
  54. # Crossing midnight (e.g. peak-start 22, peak-end 2)
  55. if [[ "$curr_hour" -ge "$peak_start" ]] || [[ "$curr_hour" -lt "$peak_end" ]]; then
  56. in_peak=1
  57. fi
  58. fi
  59. if [[ "$in_peak" -eq 0 ]]; then
  60. # Outside peak hours: only execute near offpeak_int minute boundaries (e.g. 0, 30)
  61. # Allow drift tolerance of half the timer tick (peak_int / 2)
  62. remainder=$((curr_min % offpeak_int))
  63. tolerance=$(( (peak_int + 1) / 2 ))
  64. if [[ $remainder -gt $tolerance ]] && [[ $remainder -lt $((offpeak_int - tolerance)) ]]; then
  65. log "[vmess-domain-rotator] off-peak run skipped: hour=${curr_hour} minute=${curr_min} (remainder=${remainder}m, next at ${offpeak_int}m boundary)"
  66. exit 0
  67. fi
  68. fi
  69. # During peak hours: always execute (timer already fires at peak_int interval)
  70. fi
  71. export GIT_TERMINAL_PROMPT=0
  72. commit_name="${GIT_COMMIT_NAME:-vmess-domain-rotator}"
  73. commit_email="${GIT_COMMIT_EMAIL:-vmess-domain-rotator@localhost}"
  74. runtime_branch="${GIT_RUNTIME_BRANCH:-runtime-state}"
  75. push_remote="${GIT_PUSH_REMOTE:-origin}"
  76. push_enabled="${GIT_PUSH_ENABLED:-1}"
  77. push_required="${GIT_PUSH_REQUIRED:-${push_enabled}}"
  78. http_user="${GIT_HTTP_USERNAME:-git}"
  79. http_token="${GIT_HTTP_TOKEN:-}"
  80. http_token_file="${GIT_HTTP_TOKEN_FILE:-}"
  81. credential_helper="${GIT_CREDENTIAL_HELPER:-}"
  82. ts="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
  83. if [[ -z "$http_token" ]] && [[ -n "$http_token_file" ]] && [[ -r "$http_token_file" ]]; then
  84. http_token="$(tr -d '\r\n' < "$http_token_file")"
  85. fi
  86. git_auth_opts=()
  87. if [[ -n "$http_token" ]]; then
  88. auth_b64="$(printf '%s:%s' "$http_user" "$http_token" | base64 | tr -d '\n')"
  89. git_auth_opts=(-c "http.extraHeader=Authorization: Basic ${auth_b64}")
  90. fi
  91. git_auth() {
  92. local dir="$1"
  93. shift
  94. if [[ -n "$credential_helper" ]]; then
  95. git -C "$dir" -c credential.helper="$credential_helper" "${git_auth_opts[@]}" "$@"
  96. else
  97. git -C "$dir" "${git_auth_opts[@]}" "$@"
  98. fi
  99. }
  100. output_settings_json="$(/usr/bin/python3 "${APP_DIR}/scripts/domain_updater.py" --config "$CONFIG_PATH" --print-output-settings)"
  101. mapfile -t output_settings < <(
  102. printf '%s' "$output_settings_json" | /usr/bin/python3 -c '
  103. import json, sys
  104. settings = json.load(sys.stdin)
  105. for key in ["runtime_dir", "selected_text_path", "selected_json_path", "state_path", "vars_path"]:
  106. print(settings[key])
  107. '
  108. )
  109. RUNTIME_DIR="${output_settings[0]}"
  110. SELECTED_TEXT_FILE="${output_settings[1]}"
  111. SELECTED_JSON_FILE="${output_settings[2]}"
  112. STATE_FILE="${output_settings[3]}"
  113. VARS_FILE="${output_settings[4]}"
  114. repo_relpath() {
  115. /usr/bin/python3 - "$APP_DIR" "$1" <<'PY'
  116. import os
  117. import sys
  118. base = os.path.realpath(sys.argv[1])
  119. path = os.path.realpath(sys.argv[2])
  120. try:
  121. common = os.path.commonpath([base, path])
  122. except ValueError:
  123. common = ""
  124. if common != base:
  125. print("")
  126. else:
  127. print(os.path.relpath(path, base))
  128. PY
  129. }
  130. updater_output="$(/usr/bin/python3 "${APP_DIR}/scripts/domain_updater.py" --config "$CONFIG_PATH")"
  131. printf '%s\n' "$updater_output"
  132. updater_status="$(printf '%s' "$updater_output" | /usr/bin/python3 -c 'import json,sys; print(json.load(sys.stdin).get("status",""))' 2>/dev/null || echo "")"
  133. if [[ "$updater_status" == "error_use_last_good" ]]; then
  134. log "[vmess-domain-rotator] warning: updater using fallback value (source error)"
  135. fi
  136. if [[ ! -f "$SELECTED_TEXT_FILE" ]]; then
  137. log "[vmess-domain-rotator] selected value file missing after updater run (${SELECTED_TEXT_FILE}), skip git commit"
  138. exit 0
  139. fi
  140. after="$(tr -d '\r\n' < "$SELECTED_TEXT_FILE")"
  141. if [[ -z "$after" ]]; then
  142. log "[vmess-domain-rotator] empty selected value, skip git commit"
  143. exit 0
  144. fi
  145. selected_rel="$(repo_relpath "$SELECTED_TEXT_FILE")"
  146. if [[ -z "$selected_rel" ]]; then
  147. log "[vmess-domain-rotator] selected value file is outside repo (${SELECTED_TEXT_FILE}), skip git commit"
  148. exit 0
  149. fi
  150. if ! command -v git >/dev/null 2>&1; then
  151. log "[vmess-domain-rotator] git not found, skip git commit"
  152. exit 0
  153. fi
  154. if ! git -C "$APP_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  155. log "[vmess-domain-rotator] not a git repo at ${APP_DIR}, skip git commit"
  156. log "[vmess-domain-rotator] hint: reinstall service from a git clone path"
  157. exit 0
  158. fi
  159. if ! git -C "$APP_DIR" rev-parse --verify HEAD >/dev/null 2>&1; then
  160. log "[vmess-domain-rotator] repo has no commits yet, skip git commit"
  161. exit 0
  162. fi
  163. if ! git -C "$APP_DIR" remote get-url "$push_remote" >/dev/null 2>&1; then
  164. push_remote=""
  165. while IFS= read -r r; do
  166. push_remote="$r"
  167. break
  168. done < <(git -C "$APP_DIR" remote)
  169. fi
  170. work_dir=""
  171. cleanup_worktree="0"
  172. current_branch="$(git -C "$APP_DIR" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
  173. if [[ "$current_branch" == "$runtime_branch" ]]; then
  174. work_dir="$APP_DIR"
  175. else
  176. work_dir="$(mktemp -d "${TMPDIR:-/tmp}/vmess-runtime-state.XXXXXX")"
  177. cleanup_worktree="1"
  178. if git -C "$APP_DIR" show-ref --verify --quiet "refs/heads/${runtime_branch}"; then
  179. git -C "$APP_DIR" worktree add --force "$work_dir" "$runtime_branch"
  180. else
  181. if [[ -n "$push_remote" ]] && git_auth "$APP_DIR" ls-remote --exit-code --heads "$push_remote" "$runtime_branch" >/dev/null 2>&1; then
  182. git_auth "$APP_DIR" fetch "$push_remote" "$runtime_branch:$runtime_branch"
  183. git -C "$APP_DIR" worktree add --force "$work_dir" "$runtime_branch"
  184. else
  185. git -C "$APP_DIR" worktree add --force --detach "$work_dir"
  186. git -C "$work_dir" checkout --orphan "$runtime_branch"
  187. git -C "$work_dir" rm -rf . >/dev/null 2>&1 || true
  188. log "[vmess-domain-rotator] initialized branch ${runtime_branch}"
  189. fi
  190. fi
  191. fi
  192. if [[ "$cleanup_worktree" == "1" ]]; then
  193. cleanup() {
  194. trap - EXIT INT TERM
  195. git -C "$APP_DIR" worktree remove --force "$work_dir" >/dev/null 2>&1 || {
  196. rm -rf "$work_dir"
  197. git -C "$APP_DIR" worktree prune >/dev/null 2>&1 || true
  198. }
  199. }
  200. trap cleanup EXIT
  201. trap 'cleanup; exit 1' INT TERM
  202. fi
  203. work_branch="$(git -C "$work_dir" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
  204. if [[ "$work_branch" != "$runtime_branch" ]]; then
  205. log "[vmess-domain-rotator] safety check failed: worktree branch is '${work_branch:-detached}', expected '${runtime_branch}'"
  206. exit 1
  207. fi
  208. before=""
  209. if before_raw="$(git -C "$work_dir" show "HEAD:${selected_rel}" 2>/dev/null)"; then
  210. before="$(printf '%s' "$before_raw" | tr -d '\r\n')"
  211. fi
  212. if [[ "$force_commit" != "1" ]] && [[ -n "$before" ]] && [[ "$after" == "$before" ]]; then
  213. log "[vmess-domain-rotator] selected value unchanged (${after}), skip git commit and push"
  214. exit 0
  215. fi
  216. tracked_src_files=("$SELECTED_TEXT_FILE" "$SELECTED_JSON_FILE" "$STATE_FILE" "$VARS_FILE")
  217. tracked_rel_files=()
  218. for src in "${tracked_src_files[@]}"; do
  219. rel="$(repo_relpath "$src")"
  220. if [[ -z "$rel" ]]; then
  221. log "[vmess-domain-rotator] skip non-repo output file: ${src}"
  222. continue
  223. fi
  224. tracked_rel_files+=("$rel")
  225. dst="$work_dir/$rel"
  226. mkdir -p "$(dirname "$dst")"
  227. if [[ -f "$src" ]]; then
  228. cp "$src" "$dst"
  229. else
  230. rm -f "$dst"
  231. fi
  232. done
  233. if [[ "${#tracked_rel_files[@]}" -eq 0 ]]; then
  234. log "[vmess-domain-rotator] no repo-local output files from config (${CONFIG_PATH}), skip git commit"
  235. exit 0
  236. fi
  237. git -C "$work_dir" add -A -- "${tracked_rel_files[@]}" || true
  238. staged_changed="1"
  239. if git -C "$work_dir" diff --cached --quiet; then
  240. staged_changed="0"
  241. fi
  242. if [[ "$staged_changed" == "0" ]] && [[ "$force_commit" != "1" ]]; then
  243. log "[vmess-domain-rotator] no staged changes for ${runtime_branch}, skip git commit"
  244. exit 0
  245. fi
  246. commit_extra_args=()
  247. if [[ "$staged_changed" == "0" ]] && [[ "$force_commit" == "1" ]]; then
  248. commit_extra_args+=(--allow-empty)
  249. log "[vmess-domain-rotator] force commit enabled with unchanged content, creating empty commit"
  250. fi
  251. commit_message="chore: rotate preferred value to ${after} (${ts})"
  252. if [[ "$updater_status" == "error_use_last_good" ]]; then
  253. commit_message="chore(fallback): rotate preferred value to ${after} (${ts})"
  254. fi
  255. if [[ "$force_commit" == "1" ]]; then
  256. commit_message="manual: value ${after}, updated at ${ts}"
  257. fi
  258. git -C "$work_dir" \
  259. -c user.name="$commit_name" \
  260. -c user.email="$commit_email" \
  261. commit "${commit_extra_args[@]}" -m "$commit_message"
  262. if [[ "$push_enabled" != "1" ]]; then
  263. log "[vmess-domain-rotator] git push disabled by GIT_PUSH_ENABLED=${push_enabled}"
  264. elif [[ -z "$push_remote" ]]; then
  265. if [[ "$push_required" == "1" ]]; then
  266. log "[vmess-domain-rotator] no remote found but push is required"
  267. exit 1
  268. fi
  269. log "[vmess-domain-rotator] no remote found, skip git push"
  270. else
  271. push_ok="0"
  272. if git -C "$work_dir" rev-parse --abbrev-ref --symbolic-full-name "@{u}" >/dev/null 2>&1; then
  273. if git_auth "$work_dir" push "$push_remote" "$runtime_branch:$runtime_branch"; then
  274. push_ok="1"
  275. log "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch}"
  276. fi
  277. else
  278. if git_auth "$work_dir" push -u "$push_remote" "$runtime_branch:$runtime_branch"; then
  279. push_ok="1"
  280. log "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch} (set upstream)"
  281. fi
  282. fi
  283. if [[ "$push_ok" != "1" ]]; then
  284. log "[vmess-domain-rotator] git push failed"
  285. log "[vmess-domain-rotator] hint: configure non-interactive auth (credential.helper store, SSH deploy key, or GIT_HTTP_USERNAME/GIT_HTTP_TOKEN)"
  286. if [[ "$push_required" == "1" ]]; then
  287. exit 1
  288. fi
  289. fi
  290. fi
  291. log "[vmess-domain-rotator] committed output changes on ${runtime_branch}: selected value ${after} from ${RUNTIME_DIR}"