run_update_and_commit.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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="${APP_DIR:-$(git -C "$DEFAULT_APP_DIR" rev-parse --show-toplevel 2>/dev/null || printf '%s' "$DEFAULT_APP_DIR")}"
  20. PYTHON_BIN="${PYTHON_BIN:-}"
  21. if [[ -z "$PYTHON_BIN" ]]; then
  22. if [[ -x "/usr/bin/python3" ]]; then
  23. PYTHON_BIN="/usr/bin/python3"
  24. else
  25. PYTHON_BIN="$(command -v python3 || echo "python3")"
  26. fi
  27. fi
  28. force_commit="${GIT_FORCE_COMMIT:-0}"
  29. if [[ "${1:-}" == "--force-commit" ]]; then
  30. force_commit="1"
  31. shift
  32. fi
  33. if [[ ! "$force_commit" =~ ^[01]$ ]]; then
  34. log "[vmess-domain-rotator] invalid GIT_FORCE_COMMIT=${force_commit}, expected 0 or 1"
  35. exit 1
  36. fi
  37. CONFIG_PATH="${1:-${APP_DIR}/config.server.json}"
  38. # --- adaptive interval check ---
  39. if [[ "$force_commit" != "1" ]]; then
  40. peak_start=$((10#${PEAK_START_HOUR:-19}))
  41. peak_end=$((10#${PEAK_END_HOUR:-24}))
  42. peak_tz="${PEAK_TZ:-Asia/Shanghai}"
  43. peak_int=$((10#${PEAK_INTERVAL_MIN:-10}))
  44. offpeak_int=$((10#${OFFPEAK_INTERVAL_MIN:-30}))
  45. # Get current hour and minute in peak timezone
  46. curr_hour=$((10#$(TZ="$peak_tz" date '+%H')))
  47. curr_min=$((10#$(TZ="$peak_tz" date '+%M')))
  48. in_peak=0
  49. if [[ "$peak_start" -eq "$peak_end" ]]; then
  50. # start == end: no peak window defined, always off-peak
  51. in_peak=0
  52. elif [[ "$peak_start" -lt "$peak_end" ]]; then
  53. # Normal range (e.g. 8 to 22)
  54. if [[ "$curr_hour" -ge "$peak_start" ]] && [[ "$curr_hour" -lt "$peak_end" ]]; then
  55. in_peak=1
  56. fi
  57. else
  58. # Crossing midnight (e.g. peak-start 22, peak-end 2)
  59. if [[ "$curr_hour" -ge "$peak_start" ]] || [[ "$curr_hour" -lt "$peak_end" ]]; then
  60. in_peak=1
  61. fi
  62. fi
  63. if [[ "$in_peak" -eq 0 ]]; then
  64. # Outside peak hours: only execute near offpeak_int minute boundaries (e.g. 0, 30)
  65. # Allow drift tolerance of half the timer tick (peak_int / 2)
  66. remainder=$((curr_min % offpeak_int))
  67. tolerance=$(( (peak_int + 1) / 2 ))
  68. if [[ $remainder -gt $tolerance ]] && [[ $remainder -lt $((offpeak_int - tolerance)) ]]; then
  69. log "[vmess-domain-rotator] off-peak run skipped: hour=${curr_hour} minute=${curr_min} (remainder=${remainder}m, next at ${offpeak_int}m boundary)"
  70. exit 0
  71. fi
  72. fi
  73. # During peak hours: always execute (timer already fires at peak_int interval)
  74. fi
  75. export GIT_TERMINAL_PROMPT=0
  76. commit_name="${GIT_COMMIT_NAME:-vmess-domain-rotator}"
  77. commit_email="${GIT_COMMIT_EMAIL:-vmess-domain-rotator@localhost}"
  78. runtime_branch="${GIT_RUNTIME_BRANCH:-runtime-state}"
  79. push_remote="${GIT_PUSH_REMOTE:-origin}"
  80. push_enabled="${GIT_PUSH_ENABLED:-1}"
  81. push_required="${GIT_PUSH_REQUIRED:-${push_enabled}}"
  82. http_user="${GIT_HTTP_USERNAME:-git}"
  83. http_token="${GIT_HTTP_TOKEN:-}"
  84. http_token_file="${GIT_HTTP_TOKEN_FILE:-}"
  85. credential_helper="${GIT_CREDENTIAL_HELPER:-}"
  86. max_state_commits="${GIT_MAX_STATE_COMMITS:-50}"
  87. ts="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
  88. if [[ -z "$http_token" ]] && [[ -n "$http_token_file" ]] && [[ -r "$http_token_file" ]]; then
  89. http_token="$(tr -d '\r\n' < "$http_token_file")"
  90. fi
  91. git_auth() {
  92. local dir="$1"
  93. shift
  94. local -a env_vars=()
  95. if [[ -n "$http_token" ]]; then
  96. local auth_b64
  97. auth_b64="$(printf '%s:%s' "$http_user" "$http_token" | base64 | tr -d '\n')"
  98. env_vars+=(
  99. "GIT_CONFIG_COUNT=1"
  100. "GIT_CONFIG_KEY_0=http.extraHeader"
  101. "GIT_CONFIG_VALUE_0=Authorization: Basic ${auth_b64}"
  102. )
  103. fi
  104. if [[ -n "$credential_helper" ]]; then
  105. env "${env_vars[@]}" git -C "$dir" -c credential.helper="$credential_helper" "$@"
  106. else
  107. env "${env_vars[@]}" git -C "$dir" "$@"
  108. fi
  109. }
  110. output_settings_json="$("$PYTHON_BIN" "${APP_DIR}/scripts/domain_updater.py" --config "$CONFIG_PATH" --print-output-settings)"
  111. output_settings=()
  112. while IFS= read -r line; do
  113. output_settings+=("$line")
  114. done < <(
  115. printf '%s' "$output_settings_json" | "$PYTHON_BIN" -c '
  116. import json, sys
  117. settings = json.load(sys.stdin)
  118. for key in ["runtime_dir", "selected_text_path", "selected_json_path", "state_path", "vars_path"]:
  119. print(settings[key])
  120. '
  121. )
  122. RUNTIME_DIR="${output_settings[0]}"
  123. SELECTED_TEXT_FILE="${output_settings[1]}"
  124. SELECTED_JSON_FILE="${output_settings[2]}"
  125. STATE_FILE="${output_settings[3]}"
  126. VARS_FILE="${output_settings[4]}"
  127. repo_relpath() {
  128. "$PYTHON_BIN" - "$APP_DIR" "$1" <<'PY'
  129. import os
  130. import sys
  131. base = os.path.realpath(sys.argv[1])
  132. path = os.path.realpath(sys.argv[2])
  133. try:
  134. common = os.path.commonpath([base, path])
  135. except ValueError:
  136. common = ""
  137. if common != base:
  138. print("")
  139. else:
  140. print(os.path.relpath(path, base))
  141. PY
  142. }
  143. json_field() {
  144. local key="$1"
  145. "$PYTHON_BIN" -c '
  146. import json
  147. import sys
  148. key = sys.argv[1]
  149. try:
  150. obj = json.load(sys.stdin)
  151. except Exception:
  152. print("")
  153. else:
  154. value = obj.get(key, "")
  155. print("" if value is None else str(value))
  156. ' "$key"
  157. }
  158. tmp_result_json="$(mktemp "${TMPDIR:-/tmp}/vmess_result.XXXXXX")"
  159. trap 'rm -f "$tmp_result_json"' EXIT INT TERM
  160. "$PYTHON_BIN" "${APP_DIR}/scripts/domain_updater.py" --config "$CONFIG_PATH" --result-json-file "$tmp_result_json"
  161. if [[ -s "$tmp_result_json" ]]; then
  162. updater_status="$(json_field status < "$tmp_result_json" 2>/dev/null || echo "")"
  163. else
  164. updater_status=""
  165. fi
  166. rm -f "$tmp_result_json"
  167. if [[ "$updater_status" == "error_use_last_good" || "$updater_status" == "fallback_last_good" ]]; then
  168. log "[vmess-domain-rotator] warning: updater using fallback value (${updater_status})"
  169. fi
  170. if [[ ! -f "$SELECTED_TEXT_FILE" ]]; then
  171. log "[vmess-domain-rotator] selected value file missing after updater run (${SELECTED_TEXT_FILE}), skip git commit"
  172. exit 0
  173. fi
  174. after="$(tr -d '\r\n' < "$SELECTED_TEXT_FILE")"
  175. if [[ -z "$after" ]]; then
  176. log "[vmess-domain-rotator] empty selected value, skip git commit"
  177. exit 0
  178. fi
  179. selected_rel="$(repo_relpath "$SELECTED_TEXT_FILE")"
  180. selected_json_rel="$(repo_relpath "$SELECTED_JSON_FILE")"
  181. vars_rel="$(repo_relpath "$VARS_FILE")"
  182. if [[ -z "$selected_rel" ]]; then
  183. log "[vmess-domain-rotator] selected value file is outside repo (${SELECTED_TEXT_FILE}), skip git commit"
  184. exit 0
  185. fi
  186. if ! command -v git >/dev/null 2>&1; then
  187. log "[vmess-domain-rotator] git not found, skip git commit"
  188. exit 0
  189. fi
  190. if ! git -C "$APP_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  191. log "[vmess-domain-rotator] not a git repo at ${APP_DIR}, skip git commit"
  192. log "[vmess-domain-rotator] hint: reinstall service from a git clone path"
  193. exit 0
  194. fi
  195. if ! git -C "$APP_DIR" rev-parse --verify HEAD >/dev/null 2>&1; then
  196. log "[vmess-domain-rotator] repo has no commits yet, skip git commit"
  197. exit 0
  198. fi
  199. if ! git -C "$APP_DIR" remote get-url "$push_remote" >/dev/null 2>&1; then
  200. push_remote=""
  201. while IFS= read -r r; do
  202. push_remote="$r"
  203. break
  204. done < <(git -C "$APP_DIR" remote)
  205. fi
  206. work_dir=""
  207. cleanup_worktree="0"
  208. current_branch="$(git -C "$APP_DIR" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
  209. if [[ "$current_branch" == "$runtime_branch" ]]; then
  210. work_dir="$APP_DIR"
  211. else
  212. work_dir="$(mktemp -d "${TMPDIR:-/tmp}/vmess-runtime-state.XXXXXX")"
  213. cleanup_worktree="1"
  214. if git -C "$APP_DIR" show-ref --verify --quiet "refs/heads/${runtime_branch}"; then
  215. git -C "$APP_DIR" worktree add --force "$work_dir" "$runtime_branch"
  216. else
  217. remote_branch_exists="0"
  218. if [[ -n "$push_remote" ]]; then
  219. set +e
  220. git_auth "$APP_DIR" ls-remote --exit-code --heads "$push_remote" "$runtime_branch" >/dev/null 2>&1
  221. ls_remote_rc=$?
  222. set -e
  223. if [[ $ls_remote_rc -eq 0 ]]; then
  224. remote_branch_exists="1"
  225. elif [[ $ls_remote_rc -eq 2 ]]; then
  226. remote_branch_exists="0"
  227. else
  228. log_err "[vmess-domain-rotator] fatal: git ls-remote failed with exit code ${ls_remote_rc} on remote ${push_remote}"
  229. exit 1
  230. fi
  231. fi
  232. if [[ "$remote_branch_exists" == "1" ]]; then
  233. git_auth "$APP_DIR" fetch "$push_remote" "$runtime_branch:$runtime_branch"
  234. git -C "$APP_DIR" worktree add --force "$work_dir" "$runtime_branch"
  235. else
  236. git -C "$APP_DIR" worktree add --force --detach "$work_dir"
  237. git -C "$work_dir" checkout --orphan "$runtime_branch"
  238. git -C "$work_dir" rm -rf . >/dev/null 2>&1 || true
  239. log "[vmess-domain-rotator] initialized branch ${runtime_branch}"
  240. fi
  241. fi
  242. fi
  243. if [[ "$cleanup_worktree" == "1" ]]; then
  244. cleanup() {
  245. trap - EXIT INT TERM
  246. git -C "$APP_DIR" worktree remove --force "$work_dir" >/dev/null 2>&1 || {
  247. rm -rf "$work_dir"
  248. git -C "$APP_DIR" worktree prune >/dev/null 2>&1 || true
  249. }
  250. }
  251. trap cleanup EXIT
  252. trap 'cleanup; exit 1' INT TERM
  253. fi
  254. work_branch="$(git -C "$work_dir" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
  255. if [[ "$work_branch" != "$runtime_branch" ]]; then
  256. log "[vmess-domain-rotator] safety check failed: worktree branch is '${work_branch:-detached}', expected '${runtime_branch}'"
  257. exit 1
  258. fi
  259. before=""
  260. if before_raw="$(git -C "$work_dir" show "HEAD:${selected_rel}" 2>/dev/null)"; then
  261. before="$(printf '%s' "$before_raw" | tr -d '\r\n')"
  262. fi
  263. before_status=""
  264. if [[ -n "$selected_json_rel" ]] && before_json_raw="$(git -C "$work_dir" show "HEAD:${selected_json_rel}" 2>/dev/null)"; then
  265. before_status="$(printf '%s' "$before_json_raw" | json_field status 2>/dev/null || echo "")"
  266. fi
  267. before_vars_status=""
  268. if [[ -n "$vars_rel" ]] && before_vars_raw="$(git -C "$work_dir" show "HEAD:${vars_rel}" 2>/dev/null)"; then
  269. before_vars_status="$(printf '%s' "$before_vars_raw" | json_field STATUS 2>/dev/null || echo "")"
  270. fi
  271. status_compared="0"
  272. status_unchanged="0"
  273. if [[ -n "$updater_status" ]]; then
  274. status_unchanged="1"
  275. if [[ -n "$before_status" ]]; then
  276. status_compared="1"
  277. [[ "$updater_status" != "$before_status" ]] && status_unchanged="0"
  278. fi
  279. if [[ -n "$before_vars_status" ]]; then
  280. status_compared="1"
  281. [[ "$updater_status" != "$before_vars_status" ]] && status_unchanged="0"
  282. fi
  283. if [[ "$status_compared" != "1" ]]; then
  284. status_unchanged="0"
  285. fi
  286. fi
  287. is_fallback="0"
  288. if [[ "$updater_status" == "error_use_last_good" || "$updater_status" == "fallback_last_good" ]]; then
  289. is_fallback="1"
  290. fi
  291. if [[ "$force_commit" != "1" ]] && [[ -n "$before" ]] && [[ "$after" == "$before" ]]; then
  292. if [[ "$status_unchanged" == "1" || "$is_fallback" == "1" ]]; then
  293. log "[vmess-domain-rotator] selected value unchanged (${after}) [status=${updater_status:-unknown}], skip git commit and push"
  294. exit 0
  295. fi
  296. fi
  297. tracked_src_files=("$SELECTED_TEXT_FILE" "$SELECTED_JSON_FILE" "$STATE_FILE" "$VARS_FILE")
  298. tracked_rel_files=()
  299. for src in "${tracked_src_files[@]}"; do
  300. rel="$(repo_relpath "$src")"
  301. if [[ -z "$rel" ]]; then
  302. log "[vmess-domain-rotator] skip non-repo output file: ${src}"
  303. continue
  304. fi
  305. tracked_rel_files+=("$rel")
  306. dst="$work_dir/$rel"
  307. mkdir -p "$(dirname "$dst")"
  308. if [[ -f "$src" ]]; then
  309. if [[ "$src" != "$dst" ]]; then
  310. cp "$src" "$dst"
  311. fi
  312. else
  313. rm -f "$dst"
  314. fi
  315. done
  316. if [[ "${#tracked_rel_files[@]}" -eq 0 ]]; then
  317. log "[vmess-domain-rotator] no repo-local output files from config (${CONFIG_PATH}), skip git commit"
  318. exit 0
  319. fi
  320. git -C "$work_dir" add -A -- "${tracked_rel_files[@]}" || true
  321. staged_changed="1"
  322. if git -C "$work_dir" diff --cached --quiet; then
  323. staged_changed="0"
  324. fi
  325. if [[ "$staged_changed" == "0" ]] && [[ "$force_commit" != "1" ]]; then
  326. log "[vmess-domain-rotator] no staged changes for ${runtime_branch}, skip git commit"
  327. exit 0
  328. fi
  329. commit_extra_args=()
  330. if [[ "$staged_changed" == "0" ]] && [[ "$force_commit" == "1" ]]; then
  331. commit_extra_args+=(--allow-empty)
  332. log "[vmess-domain-rotator] force commit enabled with unchanged content, creating empty commit"
  333. fi
  334. commit_message="chore: rotate preferred value to ${after} (${ts})"
  335. if [[ "$updater_status" == "error_use_last_good" || "$updater_status" == "fallback_last_good" ]]; then
  336. commit_message="chore(fallback): rotate preferred value to ${after} (${ts})"
  337. fi
  338. if [[ "$force_commit" == "1" ]]; then
  339. commit_message="manual: value ${after}, updated at ${ts}"
  340. fi
  341. commit_cmd=(git -C "$work_dir" -c user.name="$commit_name" -c user.email="$commit_email" commit)
  342. if [[ "${#commit_extra_args[@]}" -gt 0 ]]; then
  343. commit_cmd+=("${commit_extra_args[@]}")
  344. fi
  345. commit_cmd+=(-m "$commit_message")
  346. "${commit_cmd[@]}"
  347. history_trimmed="0"
  348. if [[ "$max_state_commits" -gt 0 ]]; then
  349. commit_count="$(git -C "$work_dir" rev-list --count HEAD 2>/dev/null || echo "1")"
  350. if [[ "$commit_count" -gt "$max_state_commits" ]]; then
  351. log "[vmess-domain-rotator] runtime history reached ${commit_count} commits, trimming older history (retaining last ${max_state_commits})"
  352. git -C "$work_dir" branch -f "${runtime_branch}-untrimmed" HEAD >/dev/null 2>&1 || true
  353. base_commit="$(git -C "$work_dir" rev-parse "HEAD~$((max_state_commits - 1))" 2>/dev/null || true)"
  354. if [[ -n "$base_commit" ]]; then
  355. tree_sha="$(git -C "$work_dir" rev-parse "${base_commit}^{tree}" 2>/dev/null || true)"
  356. if [[ -n "$tree_sha" ]]; then
  357. root_commit="$(git -C "$work_dir" commit-tree "$tree_sha" -m "chore(prune): baseline snapshot before rolling rotation" 2>/dev/null || true)"
  358. if [[ -n "$root_commit" ]] && git -C "$work_dir" rebase --onto "$root_commit" "$base_commit" "$runtime_branch" >/dev/null 2>&1; then
  359. history_trimmed="1"
  360. log "[vmess-domain-rotator] history successfully pruned to ${max_state_commits} commits"
  361. else
  362. git -C "$work_dir" reset --hard "${runtime_branch}-untrimmed" >/dev/null 2>&1 || true
  363. fi
  364. fi
  365. fi
  366. fi
  367. fi
  368. if [[ "$push_enabled" != "1" ]]; then
  369. log "[vmess-domain-rotator] git push disabled by GIT_PUSH_ENABLED=${push_enabled}"
  370. elif [[ -z "$push_remote" ]]; then
  371. if [[ "$push_required" == "1" ]]; then
  372. log "[vmess-domain-rotator] no remote found but push is required"
  373. exit 1
  374. fi
  375. log "[vmess-domain-rotator] no remote found, skip git push"
  376. else
  377. push_opts=()
  378. if [[ "$history_trimmed" == "1" ]]; then
  379. push_opts+=(--force-with-lease)
  380. fi
  381. push_opts+=("$push_remote" "$runtime_branch:$runtime_branch")
  382. push_ok="0"
  383. if git -C "$work_dir" rev-parse --abbrev-ref --symbolic-full-name "@{u}" >/dev/null 2>&1; then
  384. if git_auth "$work_dir" push "${push_opts[@]}"; then
  385. push_ok="1"
  386. log "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch}"
  387. elif [[ "$history_trimmed" == "1" ]]; then
  388. log "[vmess-domain-rotator] remote rejected force-with-lease for history trimming, restoring full history and retrying normal push"
  389. git -C "$work_dir" reset --hard "${runtime_branch}-untrimmed" >/dev/null 2>&1 || true
  390. if git_auth "$work_dir" push "$push_remote" "$runtime_branch:$runtime_branch"; then
  391. push_ok="1"
  392. log "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch} (fallback to append-only)"
  393. fi
  394. fi
  395. else
  396. if git_auth "$work_dir" push -u "${push_opts[@]}"; then
  397. push_ok="1"
  398. log "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch} (set upstream)"
  399. elif [[ "$history_trimmed" == "1" ]]; then
  400. log "[vmess-domain-rotator] remote rejected force-with-lease for history trimming, restoring full history and retrying normal push"
  401. git -C "$work_dir" reset --hard "${runtime_branch}-untrimmed" >/dev/null 2>&1 || true
  402. if git_auth "$work_dir" push -u "$push_remote" "$runtime_branch:$runtime_branch"; then
  403. push_ok="1"
  404. log "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch} (set upstream fallback)"
  405. fi
  406. fi
  407. fi
  408. git -C "$work_dir" branch -D "${runtime_branch}-untrimmed" >/dev/null 2>&1 || true
  409. if [[ "$push_ok" != "1" ]]; then
  410. log "[vmess-domain-rotator] git push failed"
  411. log "[vmess-domain-rotator] hint: configure non-interactive auth (credential.helper store, SSH deploy key, or GIT_HTTP_USERNAME/GIT_HTTP_TOKEN)"
  412. if [[ "$push_required" == "1" ]]; then
  413. exit 1
  414. fi
  415. fi
  416. fi
  417. log "[vmess-domain-rotator] committed output changes on ${runtime_branch}: selected value ${after} from ${RUNTIME_DIR}"