install_debian.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SERVICE_NAME="vmess-domain-rotator"
  4. RUN_USER=""
  5. RUN_GROUP=""
  6. RUN_USER_SET="0"
  7. RUN_GROUP_SET="0"
  8. RUN_HOME=""
  9. INTERVAL="1h"
  10. INSTALL_DEPS="1"
  11. CONFIG_PATH=""
  12. GIT_PUSH_ENABLED="1"
  13. GIT_PUSH_REMOTE="origin"
  14. GIT_HTTP_USERNAME="git"
  15. GIT_HTTP_TOKEN=""
  16. GIT_HTTP_TOKEN_FILE=""
  17. GIT_USE_CREDENTIAL_STORE="1"
  18. GIT_CREDENTIALS_FILE=""
  19. usage() {
  20. cat <<'EOF'
  21. Usage: sudo bash scripts/install_debian.sh [options]
  22. Default behavior:
  23. - Uses current git repository directory as working directory (in-place mode)
  24. - Uses the user executing sudo as service user
  25. - Enables git push after runtime-state commits
  26. Options:
  27. --user <name> Service user (default: current sudo user)
  28. --group <name> Service group (default: current sudo user's group)
  29. --interval <value> Timer interval, e.g. 1h/10min (default: 1h)
  30. --config <path> Config file path (default: <repo>/config.server.json)
  31. --git-push <0|1> Enable/disable push to remote (default: 1)
  32. --git-push-remote <name> Remote name for push (default: origin)
  33. --git-http-username <u> Username for HTTPS auth (default: git)
  34. --git-http-token <t> HTTPS token for non-interactive push
  35. --git-http-token-file <f> Read HTTPS token from file
  36. --git-use-credential-store <0|1> Use git credential.helper store (default: 1)
  37. --git-credentials-file <f> Custom credentials file for helper store
  38. --no-install-deps Skip apt dependency install
  39. -h, --help Show help
  40. Examples:
  41. sudo bash scripts/install_debian.sh
  42. sudo bash scripts/install_debian.sh --config /opt/vmess-domain-rotator/config.server.json
  43. sudo bash scripts/install_debian.sh --interval 10min
  44. sudo bash scripts/install_debian.sh --git-push 0
  45. sudo bash scripts/install_debian.sh --git-http-username aurora --git-http-token-file /root/.config/vmess-token
  46. sudo bash scripts/install_debian.sh --git-use-credential-store 1 --git-credentials-file /home/aurora/.git-credentials
  47. EOF
  48. }
  49. run_as_service_user() {
  50. runuser -u "$RUN_USER" -- env HOME="$RUN_HOME" "$@"
  51. }
  52. while [[ $# -gt 0 ]]; do
  53. case "$1" in
  54. --user)
  55. RUN_USER="$2"
  56. RUN_USER_SET="1"
  57. shift 2
  58. ;;
  59. --group)
  60. RUN_GROUP="$2"
  61. RUN_GROUP_SET="1"
  62. shift 2
  63. ;;
  64. --interval)
  65. INTERVAL="$2"
  66. shift 2
  67. ;;
  68. --config)
  69. CONFIG_PATH="$2"
  70. shift 2
  71. ;;
  72. --git-push)
  73. GIT_PUSH_ENABLED="$2"
  74. shift 2
  75. ;;
  76. --git-push-remote)
  77. GIT_PUSH_REMOTE="$2"
  78. shift 2
  79. ;;
  80. --git-http-username)
  81. GIT_HTTP_USERNAME="$2"
  82. shift 2
  83. ;;
  84. --git-http-token)
  85. GIT_HTTP_TOKEN="$2"
  86. shift 2
  87. ;;
  88. --git-http-token-file)
  89. GIT_HTTP_TOKEN_FILE="$2"
  90. shift 2
  91. ;;
  92. --git-use-credential-store)
  93. GIT_USE_CREDENTIAL_STORE="$2"
  94. shift 2
  95. ;;
  96. --git-credentials-file)
  97. GIT_CREDENTIALS_FILE="$2"
  98. shift 2
  99. ;;
  100. --no-install-deps)
  101. INSTALL_DEPS="0"
  102. shift
  103. ;;
  104. -h|--help)
  105. usage
  106. exit 0
  107. ;;
  108. *)
  109. echo "Unknown option: $1" >&2
  110. usage
  111. exit 1
  112. ;;
  113. esac
  114. done
  115. if [[ "$(id -u)" -ne 0 ]]; then
  116. echo "Please run as root (use sudo)." >&2
  117. exit 1
  118. fi
  119. if ! command -v runuser >/dev/null 2>&1; then
  120. echo "Error: runuser is required on Debian for configuring service-user git credentials" >&2
  121. exit 1
  122. fi
  123. SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  124. if ! git -C "$SOURCE_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  125. echo "Error: Current directory is not a git repository." >&2
  126. echo "This script must be run from within a git repository." >&2
  127. exit 1
  128. fi
  129. APP_DIR="$SOURCE_DIR"
  130. if [[ -z "$CONFIG_PATH" ]]; then
  131. CONFIG_PATH="${APP_DIR}/config.server.json"
  132. elif [[ "$CONFIG_PATH" != /* ]]; then
  133. CONFIG_PATH="${APP_DIR}/${CONFIG_PATH}"
  134. fi
  135. if [[ ! -r "$CONFIG_PATH" ]]; then
  136. echo "Error: config file not found or unreadable: $CONFIG_PATH" >&2
  137. exit 1
  138. fi
  139. if [[ -n "${SUDO_USER:-}" ]] && [[ "$RUN_USER_SET" != "1" ]]; then
  140. RUN_USER="$SUDO_USER"
  141. fi
  142. if [[ -n "${SUDO_USER:-}" ]] && [[ "$RUN_GROUP_SET" != "1" ]]; then
  143. RUN_GROUP="$(id -gn "$SUDO_USER")"
  144. fi
  145. if [[ -z "$RUN_USER" ]]; then
  146. echo "Error: Could not determine service user. Please run with sudo or specify --user" >&2
  147. exit 1
  148. fi
  149. if [[ -z "$RUN_GROUP" ]]; then
  150. echo "Error: Could not determine service group. Please run with sudo or specify --group" >&2
  151. exit 1
  152. fi
  153. if [[ ! "$GIT_PUSH_ENABLED" =~ ^[01]$ ]]; then
  154. echo "Error: --git-push must be 0 or 1" >&2
  155. exit 1
  156. fi
  157. if [[ ! "$GIT_USE_CREDENTIAL_STORE" =~ ^[01]$ ]]; then
  158. echo "Error: --git-use-credential-store must be 0 or 1" >&2
  159. exit 1
  160. fi
  161. if [[ -z "$GIT_PUSH_REMOTE" ]]; then
  162. echo "Error: --git-push-remote cannot be empty" >&2
  163. exit 1
  164. fi
  165. if [[ -n "$GIT_HTTP_TOKEN" ]] && [[ -n "$GIT_HTTP_TOKEN_FILE" ]]; then
  166. echo "Error: provide either --git-http-token or --git-http-token-file, not both" >&2
  167. exit 1
  168. fi
  169. if [[ -n "$GIT_HTTP_TOKEN_FILE" ]] && [[ ! -r "$GIT_HTTP_TOKEN_FILE" ]]; then
  170. echo "Error: cannot read token file: $GIT_HTTP_TOKEN_FILE" >&2
  171. exit 1
  172. fi
  173. if [[ -n "$GIT_HTTP_TOKEN_FILE" ]]; then
  174. GIT_HTTP_TOKEN="$(tr -d '\r\n' < "$GIT_HTTP_TOKEN_FILE")"
  175. fi
  176. if [[ -n "$GIT_HTTP_TOKEN" ]] && [[ -z "$GIT_HTTP_USERNAME" ]]; then
  177. echo "Error: --git-http-username cannot be empty when token is set" >&2
  178. exit 1
  179. fi
  180. if [[ -n "$GIT_HTTP_TOKEN" ]] && [[ "$RUN_USER" == "root" ]]; then
  181. echo "Error: refusing to store git token for root service user" >&2
  182. echo "Use --user <non-root> or disable push with --git-push 0" >&2
  183. exit 1
  184. fi
  185. RUN_HOME="$(getent passwd "$RUN_USER" | cut -d: -f6)"
  186. if [[ -z "$RUN_HOME" ]]; then
  187. echo "Error: could not determine home directory for user: $RUN_USER" >&2
  188. exit 1
  189. fi
  190. if [[ "$INSTALL_DEPS" == "1" ]]; then
  191. export DEBIAN_FRONTEND=noninteractive
  192. apt-get update -y
  193. apt-get install -y python3 ca-certificates git
  194. fi
  195. RUNTIME_DIR="$(/usr/bin/python3 "${APP_DIR}/scripts/domain_updater.py" --config "$CONFIG_PATH" --print-output-settings | /usr/bin/python3 -c 'import json,sys; print(json.load(sys.stdin)["runtime_dir"])')"
  196. mkdir -p "$RUNTIME_DIR"
  197. chmod +x "$APP_DIR/scripts/run_update_and_commit.sh" || true
  198. chown -R "$RUN_USER:$RUN_GROUP" "$RUNTIME_DIR"
  199. SERVICE_STATE_DIR="/var/lib/${SERVICE_NAME}"
  200. ENV_FILE="/etc/${SERVICE_NAME}.env"
  201. TOKEN_FILE=""
  202. REMOTE_URL=""
  203. AUTH_MODE="header"
  204. if [[ "$GIT_USE_CREDENTIAL_STORE" == "1" ]]; then
  205. AUTH_MODE="credential-helper-store"
  206. fi
  207. mkdir -p "$SERVICE_STATE_DIR"
  208. chown "$RUN_USER:$RUN_GROUP" "$SERVICE_STATE_DIR"
  209. chmod 750 "$SERVICE_STATE_DIR"
  210. if [[ "$GIT_PUSH_ENABLED" == "1" ]]; then
  211. REMOTE_URL="$(git -C "$APP_DIR" remote get-url "$GIT_PUSH_REMOTE" 2>/dev/null || true)"
  212. if [[ -z "$REMOTE_URL" ]]; then
  213. echo "Warning: remote '$GIT_PUSH_REMOTE' not found now. Push may fail until remote is configured." >&2
  214. fi
  215. fi
  216. if [[ -n "$GIT_HTTP_TOKEN" ]]; then
  217. if [[ "$GIT_USE_CREDENTIAL_STORE" == "1" ]]; then
  218. if [[ "$REMOTE_URL" =~ ^https:// ]]; then
  219. helper_value="store"
  220. if [[ -n "$GIT_CREDENTIALS_FILE" ]]; then
  221. helper_value="store --file ${GIT_CREDENTIALS_FILE}"
  222. mkdir -p "$(dirname "$GIT_CREDENTIALS_FILE")"
  223. touch "$GIT_CREDENTIALS_FILE"
  224. chown "$RUN_USER:$RUN_GROUP" "$GIT_CREDENTIALS_FILE"
  225. chmod 600 "$GIT_CREDENTIALS_FILE"
  226. fi
  227. run_as_service_user git config --global credential.helper "$helper_value"
  228. printf 'url=%s\nusername=%s\npassword=%s\n\n' "$REMOTE_URL" "$GIT_HTTP_USERNAME" "$GIT_HTTP_TOKEN" | run_as_service_user git credential approve
  229. else
  230. echo "Warning: token provided but remote is not HTTPS; credential.helper store setup skipped." >&2
  231. echo "Warning: fallback to header-token-file auth mode for this install." >&2
  232. GIT_USE_CREDENTIAL_STORE="0"
  233. fi
  234. fi
  235. if [[ "$GIT_USE_CREDENTIAL_STORE" != "1" ]]; then
  236. TOKEN_FILE="${SERVICE_STATE_DIR}/git_http_token"
  237. printf '%s\n' "$GIT_HTTP_TOKEN" >"$TOKEN_FILE"
  238. chown "$RUN_USER:$RUN_GROUP" "$TOKEN_FILE"
  239. chmod 600 "$TOKEN_FILE"
  240. AUTH_MODE="header-token-file"
  241. fi
  242. fi
  243. run_as_service_user git config --global --add safe.directory "$APP_DIR" || true
  244. cat >"$ENV_FILE" <<EOF
  245. GIT_PUSH_ENABLED=${GIT_PUSH_ENABLED}
  246. GIT_PUSH_REQUIRED=${GIT_PUSH_ENABLED}
  247. GIT_PUSH_REMOTE=${GIT_PUSH_REMOTE}
  248. GIT_RUNTIME_BRANCH=runtime-state
  249. GIT_HTTP_USERNAME=${GIT_HTTP_USERNAME}
  250. HOME=${RUN_HOME}
  251. EOF
  252. if [[ "$GIT_USE_CREDENTIAL_STORE" == "1" ]]; then
  253. if [[ -n "$GIT_CREDENTIALS_FILE" ]]; then
  254. printf 'GIT_CREDENTIAL_HELPER=store --file %s\n' "$GIT_CREDENTIALS_FILE" >>"$ENV_FILE"
  255. else
  256. printf 'GIT_CREDENTIAL_HELPER=store\n' >>"$ENV_FILE"
  257. fi
  258. fi
  259. if [[ -n "$TOKEN_FILE" ]]; then
  260. printf 'GIT_HTTP_TOKEN_FILE=%s\n' "$TOKEN_FILE" >>"$ENV_FILE"
  261. fi
  262. chown root:root "$ENV_FILE"
  263. chmod 600 "$ENV_FILE"
  264. cat >"/etc/systemd/system/${SERVICE_NAME}.service" <<EOF
  265. [Unit]
  266. Description=VMess Domain Rotator updater
  267. After=network-online.target
  268. Wants=network-online.target
  269. [Service]
  270. Type=oneshot
  271. User=${RUN_USER}
  272. Group=${RUN_GROUP}
  273. WorkingDirectory=${APP_DIR}
  274. EnvironmentFile=-${ENV_FILE}
  275. UMask=0077
  276. ExecStart=/bin/bash ${APP_DIR}/scripts/run_update_and_commit.sh ${CONFIG_PATH}
  277. EOF
  278. cat >"/etc/systemd/system/${SERVICE_NAME}.timer" <<EOF
  279. [Unit]
  280. Description=Run VMess Domain Rotator every ${INTERVAL}
  281. [Timer]
  282. OnBootSec=2min
  283. OnUnitActiveSec=${INTERVAL}
  284. AccuracySec=30s
  285. Unit=${SERVICE_NAME}.service
  286. Persistent=true
  287. [Install]
  288. WantedBy=timers.target
  289. EOF
  290. systemctl daemon-reload
  291. systemctl enable --now "${SERVICE_NAME}.timer"
  292. systemctl start "${SERVICE_NAME}.service"
  293. echo ""
  294. echo "✓ Installation complete!"
  295. echo ""
  296. echo "Configuration:"
  297. echo " Working directory: ${APP_DIR}"
  298. echo " Config path: ${CONFIG_PATH}"
  299. echo " Service user: ${RUN_USER}"
  300. echo " Service group: ${RUN_GROUP}"
  301. echo " Timer interval: ${INTERVAL}"
  302. echo " Push enabled: ${GIT_PUSH_ENABLED}"
  303. echo " Push remote: ${GIT_PUSH_REMOTE}"
  304. echo " Auth mode: ${AUTH_MODE}"
  305. echo " Env file: ${ENV_FILE}"
  306. echo ""
  307. echo "Commands:"
  308. echo " Check status: systemctl status ${SERVICE_NAME}.timer"
  309. echo " View logs: journalctl -u ${SERVICE_NAME}.service -n 50 --no-pager"
  310. echo " Manual run: sudo systemctl start ${SERVICE_NAME}.service"
  311. echo " Force commit: sudo -u ${RUN_USER} /bin/bash ${APP_DIR}/scripts/run_update_and_commit.sh --force-commit ${CONFIG_PATH}"
  312. echo ""