|
@@ -1,6 +1,18 @@
|
|
|
#!/usr/bin/env bash
|
|
#!/usr/bin/env bash
|
|
|
set -euo pipefail
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
|
|
+timestamp() {
|
|
|
|
|
+ date '+%Y-%m-%d %H:%M:%S'
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+log() {
|
|
|
|
|
+ printf '[%s] %s\n' "$(timestamp)" "$*"
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+log_err() {
|
|
|
|
|
+ printf '[%s] %s\n' "$(timestamp)" "$*" >&2
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
DEFAULT_APP_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
|
DEFAULT_APP_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
|
|
APP_DIR="$(git -C "$DEFAULT_APP_DIR" rev-parse --show-toplevel 2>/dev/null || printf '%s' "$DEFAULT_APP_DIR")"
|
|
APP_DIR="$(git -C "$DEFAULT_APP_DIR" rev-parse --show-toplevel 2>/dev/null || printf '%s' "$DEFAULT_APP_DIR")"
|
|
@@ -10,7 +22,7 @@ if [[ "${1:-}" == "--force-commit" ]]; then
|
|
|
shift
|
|
shift
|
|
|
fi
|
|
fi
|
|
|
if [[ ! "$force_commit" =~ ^[01]$ ]]; then
|
|
if [[ ! "$force_commit" =~ ^[01]$ ]]; then
|
|
|
- echo "[vmess-domain-rotator] invalid GIT_FORCE_COMMIT=${force_commit}, expected 0 or 1"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] invalid GIT_FORCE_COMMIT=${force_commit}, expected 0 or 1"
|
|
|
exit 1
|
|
exit 1
|
|
|
fi
|
|
fi
|
|
|
CONFIG_PATH="${1:-${APP_DIR}/config.server.json}"
|
|
CONFIG_PATH="${1:-${APP_DIR}/config.server.json}"
|
|
@@ -85,36 +97,36 @@ PY
|
|
|
/usr/bin/python3 "${APP_DIR}/scripts/domain_updater.py" --config "$CONFIG_PATH"
|
|
/usr/bin/python3 "${APP_DIR}/scripts/domain_updater.py" --config "$CONFIG_PATH"
|
|
|
|
|
|
|
|
if [[ ! -f "$SELECTED_TEXT_FILE" ]]; then
|
|
if [[ ! -f "$SELECTED_TEXT_FILE" ]]; then
|
|
|
- echo "[vmess-domain-rotator] selected value file missing after updater run (${SELECTED_TEXT_FILE}), skip git commit"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] selected value file missing after updater run (${SELECTED_TEXT_FILE}), skip git commit"
|
|
|
exit 0
|
|
exit 0
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
after="$(tr -d '\r\n' < "$SELECTED_TEXT_FILE")"
|
|
after="$(tr -d '\r\n' < "$SELECTED_TEXT_FILE")"
|
|
|
|
|
|
|
|
if [[ -z "$after" ]]; then
|
|
if [[ -z "$after" ]]; then
|
|
|
- echo "[vmess-domain-rotator] empty selected value, skip git commit"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] empty selected value, skip git commit"
|
|
|
exit 0
|
|
exit 0
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
selected_rel="$(repo_relpath "$SELECTED_TEXT_FILE")"
|
|
selected_rel="$(repo_relpath "$SELECTED_TEXT_FILE")"
|
|
|
if [[ -z "$selected_rel" ]]; then
|
|
if [[ -z "$selected_rel" ]]; then
|
|
|
- echo "[vmess-domain-rotator] selected value file is outside repo (${SELECTED_TEXT_FILE}), skip git commit"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] selected value file is outside repo (${SELECTED_TEXT_FILE}), skip git commit"
|
|
|
exit 0
|
|
exit 0
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
if ! command -v git >/dev/null 2>&1; then
|
|
if ! command -v git >/dev/null 2>&1; then
|
|
|
- echo "[vmess-domain-rotator] git not found, skip git commit"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] git not found, skip git commit"
|
|
|
exit 0
|
|
exit 0
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
if ! git -C "$APP_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
|
if ! git -C "$APP_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
|
|
- echo "[vmess-domain-rotator] not a git repo at ${APP_DIR}, skip git commit"
|
|
|
|
|
- echo "[vmess-domain-rotator] hint: reinstall service from a git clone path"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] not a git repo at ${APP_DIR}, skip git commit"
|
|
|
|
|
+ log "[vmess-domain-rotator] hint: reinstall service from a git clone path"
|
|
|
exit 0
|
|
exit 0
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
if ! git -C "$APP_DIR" rev-parse --verify HEAD >/dev/null 2>&1; then
|
|
if ! git -C "$APP_DIR" rev-parse --verify HEAD >/dev/null 2>&1; then
|
|
|
- echo "[vmess-domain-rotator] repo has no commits yet, skip git commit"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] repo has no commits yet, skip git commit"
|
|
|
exit 0
|
|
exit 0
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
@@ -146,7 +158,7 @@ else
|
|
|
git -C "$APP_DIR" worktree add --force --detach "$work_dir"
|
|
git -C "$APP_DIR" worktree add --force --detach "$work_dir"
|
|
|
git -C "$work_dir" checkout --orphan "$runtime_branch"
|
|
git -C "$work_dir" checkout --orphan "$runtime_branch"
|
|
|
git -C "$work_dir" rm -rf . >/dev/null 2>&1 || true
|
|
git -C "$work_dir" rm -rf . >/dev/null 2>&1 || true
|
|
|
- echo "[vmess-domain-rotator] initialized branch ${runtime_branch}"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] initialized branch ${runtime_branch}"
|
|
|
fi
|
|
fi
|
|
|
fi
|
|
fi
|
|
|
fi
|
|
fi
|
|
@@ -160,7 +172,7 @@ fi
|
|
|
|
|
|
|
|
work_branch="$(git -C "$work_dir" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
|
|
work_branch="$(git -C "$work_dir" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
|
|
|
if [[ "$work_branch" != "$runtime_branch" ]]; then
|
|
if [[ "$work_branch" != "$runtime_branch" ]]; then
|
|
|
- echo "[vmess-domain-rotator] safety check failed: worktree branch is '${work_branch:-detached}', expected '${runtime_branch}'"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] safety check failed: worktree branch is '${work_branch:-detached}', expected '${runtime_branch}'"
|
|
|
exit 1
|
|
exit 1
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
@@ -170,7 +182,7 @@ if before_raw="$(git -C "$work_dir" show "HEAD:${selected_rel}" 2>/dev/null)"; t
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$force_commit" != "1" ]] && [[ -n "$before" ]] && [[ "$after" == "$before" ]]; then
|
|
if [[ "$force_commit" != "1" ]] && [[ -n "$before" ]] && [[ "$after" == "$before" ]]; then
|
|
|
- echo "[vmess-domain-rotator] selected value unchanged (${after}), skip git commit and push"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] selected value unchanged (${after}), skip git commit and push"
|
|
|
exit 0
|
|
exit 0
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
@@ -179,7 +191,7 @@ tracked_rel_files=()
|
|
|
for src in "${tracked_src_files[@]}"; do
|
|
for src in "${tracked_src_files[@]}"; do
|
|
|
rel="$(repo_relpath "$src")"
|
|
rel="$(repo_relpath "$src")"
|
|
|
if [[ -z "$rel" ]]; then
|
|
if [[ -z "$rel" ]]; then
|
|
|
- echo "[vmess-domain-rotator] skip non-repo output file: ${src}"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] skip non-repo output file: ${src}"
|
|
|
continue
|
|
continue
|
|
|
fi
|
|
fi
|
|
|
tracked_rel_files+=("$rel")
|
|
tracked_rel_files+=("$rel")
|
|
@@ -193,7 +205,7 @@ for src in "${tracked_src_files[@]}"; do
|
|
|
done
|
|
done
|
|
|
|
|
|
|
|
if [[ "${#tracked_rel_files[@]}" -eq 0 ]]; then
|
|
if [[ "${#tracked_rel_files[@]}" -eq 0 ]]; then
|
|
|
- echo "[vmess-domain-rotator] no repo-local output files from config (${CONFIG_PATH}), skip git commit"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] no repo-local output files from config (${CONFIG_PATH}), skip git commit"
|
|
|
exit 0
|
|
exit 0
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
@@ -205,14 +217,14 @@ if git -C "$work_dir" diff --cached --quiet; then
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$staged_changed" == "0" ]] && [[ "$force_commit" != "1" ]]; then
|
|
if [[ "$staged_changed" == "0" ]] && [[ "$force_commit" != "1" ]]; then
|
|
|
- echo "[vmess-domain-rotator] no staged changes for ${runtime_branch}, skip git commit"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] no staged changes for ${runtime_branch}, skip git commit"
|
|
|
exit 0
|
|
exit 0
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
commit_extra_args=()
|
|
commit_extra_args=()
|
|
|
if [[ "$staged_changed" == "0" ]] && [[ "$force_commit" == "1" ]]; then
|
|
if [[ "$staged_changed" == "0" ]] && [[ "$force_commit" == "1" ]]; then
|
|
|
commit_extra_args+=(--allow-empty)
|
|
commit_extra_args+=(--allow-empty)
|
|
|
- echo "[vmess-domain-rotator] force commit enabled with unchanged content, creating empty commit"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] force commit enabled with unchanged content, creating empty commit"
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
commit_message="chore: rotate preferred value to ${after} (${ts})"
|
|
commit_message="chore: rotate preferred value to ${after} (${ts})"
|
|
@@ -226,34 +238,34 @@ git -C "$work_dir" \
|
|
|
commit "${commit_extra_args[@]}" -m "$commit_message"
|
|
commit "${commit_extra_args[@]}" -m "$commit_message"
|
|
|
|
|
|
|
|
if [[ "$push_enabled" != "1" ]]; then
|
|
if [[ "$push_enabled" != "1" ]]; then
|
|
|
- echo "[vmess-domain-rotator] git push disabled by GIT_PUSH_ENABLED=${push_enabled}"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] git push disabled by GIT_PUSH_ENABLED=${push_enabled}"
|
|
|
elif [[ -z "$push_remote" ]]; then
|
|
elif [[ -z "$push_remote" ]]; then
|
|
|
if [[ "$push_required" == "1" ]]; then
|
|
if [[ "$push_required" == "1" ]]; then
|
|
|
- echo "[vmess-domain-rotator] no remote found but push is required"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] no remote found but push is required"
|
|
|
exit 1
|
|
exit 1
|
|
|
fi
|
|
fi
|
|
|
- echo "[vmess-domain-rotator] no remote found, skip git push"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] no remote found, skip git push"
|
|
|
else
|
|
else
|
|
|
push_ok="0"
|
|
push_ok="0"
|
|
|
if git -C "$work_dir" rev-parse --abbrev-ref --symbolic-full-name "@{u}" >/dev/null 2>&1; then
|
|
if git -C "$work_dir" rev-parse --abbrev-ref --symbolic-full-name "@{u}" >/dev/null 2>&1; then
|
|
|
if git_auth "$work_dir" push "$push_remote" "$runtime_branch:$runtime_branch"; then
|
|
if git_auth "$work_dir" push "$push_remote" "$runtime_branch:$runtime_branch"; then
|
|
|
push_ok="1"
|
|
push_ok="1"
|
|
|
- echo "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch}"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch}"
|
|
|
fi
|
|
fi
|
|
|
else
|
|
else
|
|
|
if git_auth "$work_dir" push -u "$push_remote" "$runtime_branch:$runtime_branch"; then
|
|
if git_auth "$work_dir" push -u "$push_remote" "$runtime_branch:$runtime_branch"; then
|
|
|
push_ok="1"
|
|
push_ok="1"
|
|
|
- echo "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch} (set upstream)"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] pushed to ${push_remote}/${runtime_branch} (set upstream)"
|
|
|
fi
|
|
fi
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$push_ok" != "1" ]]; then
|
|
if [[ "$push_ok" != "1" ]]; then
|
|
|
- echo "[vmess-domain-rotator] git push failed"
|
|
|
|
|
- echo "[vmess-domain-rotator] hint: configure non-interactive auth (credential.helper store, SSH deploy key, or GIT_HTTP_USERNAME/GIT_HTTP_TOKEN)"
|
|
|
|
|
|
|
+ log "[vmess-domain-rotator] git push failed"
|
|
|
|
|
+ log "[vmess-domain-rotator] hint: configure non-interactive auth (credential.helper store, SSH deploy key, or GIT_HTTP_USERNAME/GIT_HTTP_TOKEN)"
|
|
|
if [[ "$push_required" == "1" ]]; then
|
|
if [[ "$push_required" == "1" ]]; then
|
|
|
exit 1
|
|
exit 1
|
|
|
fi
|
|
fi
|
|
|
fi
|
|
fi
|
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
-echo "[vmess-domain-rotator] committed output changes on ${runtime_branch}: selected value ${after} from ${RUNTIME_DIR}"
|
|
|
|
|
|
|
+log "[vmess-domain-rotator] committed output changes on ${runtime_branch}: selected value ${after} from ${RUNTIME_DIR}"
|