install_debian.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. INTERVAL="1h"
  9. INSTALL_DEPS="1"
  10. usage() {
  11. cat <<'EOF'
  12. Usage: sudo bash scripts/install_debian.sh [options]
  13. Default behavior:
  14. - Uses current git repository directory as working directory (in-place mode)
  15. - Uses the user executing sudo as service user
  16. Options:
  17. --user <name> Service user (default: current sudo user)
  18. --group <name> Service group (default: current sudo user's group)
  19. --interval <value> Timer interval, e.g. 1h/10min (default: 1h)
  20. --no-install-deps Skip apt dependency install
  21. -h, --help Show help
  22. Examples:
  23. sudo bash scripts/install_debian.sh
  24. sudo bash scripts/install_debian.sh --interval 10min
  25. sudo bash scripts/install_debian.sh --user root --group root
  26. EOF
  27. }
  28. while [[ $# -gt 0 ]]; do
  29. case "$1" in
  30. --user)
  31. RUN_USER="$2"
  32. RUN_USER_SET="1"
  33. shift 2
  34. ;;
  35. --group)
  36. RUN_GROUP="$2"
  37. RUN_GROUP_SET="1"
  38. shift 2
  39. ;;
  40. --interval)
  41. INTERVAL="$2"
  42. shift 2
  43. ;;
  44. --no-install-deps)
  45. INSTALL_DEPS="0"
  46. shift
  47. ;;
  48. -h|--help)
  49. usage
  50. exit 0
  51. ;;
  52. *)
  53. echo "Unknown option: $1" >&2
  54. usage
  55. exit 1
  56. ;;
  57. esac
  58. done
  59. if [[ "$(id -u)" -ne 0 ]]; then
  60. echo "Please run as root (use sudo)." >&2
  61. exit 1
  62. fi
  63. # Get source directory (current git repo)
  64. SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  65. # Verify we're in a git repository
  66. if ! git -C "$SOURCE_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  67. echo "Error: Current directory is not a git repository." >&2
  68. echo "This script must be run from within a git repository." >&2
  69. exit 1
  70. fi
  71. APP_DIR="$SOURCE_DIR"
  72. # Set default user/group from SUDO_USER if available
  73. if [[ -n "${SUDO_USER:-}" ]] && [[ "$RUN_USER_SET" != "1" ]]; then
  74. RUN_USER="$SUDO_USER"
  75. fi
  76. if [[ -n "${SUDO_USER:-}" ]] && [[ "$RUN_GROUP_SET" != "1" ]]; then
  77. RUN_GROUP="$(id -gn "$SUDO_USER")"
  78. fi
  79. # Validate that we have a user set
  80. if [[ -z "$RUN_USER" ]]; then
  81. echo "Error: Could not determine service user. Please run with sudo or specify --user" >&2
  82. exit 1
  83. fi
  84. if [[ -z "$RUN_GROUP" ]]; then
  85. echo "Error: Could not determine service group. Please run with sudo or specify --group" >&2
  86. exit 1
  87. fi
  88. if [[ "$INSTALL_DEPS" == "1" ]]; then
  89. export DEBIAN_FRONTEND=noninteractive
  90. apt-get update -y
  91. apt-get install -y python3 ca-certificates git
  92. fi
  93. # Ensure runtime directory exists with correct permissions
  94. mkdir -p "$APP_DIR/runtime"
  95. chmod +x "$APP_DIR/scripts/run_update_and_commit.sh" || true
  96. chown -R "$RUN_USER:$RUN_GROUP" "$APP_DIR/runtime"
  97. # Generate systemd service unit
  98. cat >"/etc/systemd/system/${SERVICE_NAME}.service" <<EOF
  99. [Unit]
  100. Description=VMess Domain Rotator updater
  101. After=network-online.target
  102. Wants=network-online.target
  103. [Service]
  104. Type=oneshot
  105. User=${RUN_USER}
  106. Group=${RUN_GROUP}
  107. WorkingDirectory=${APP_DIR}
  108. ExecStart=/bin/bash ${APP_DIR}/scripts/run_update_and_commit.sh ${APP_DIR}/config.json
  109. EOF
  110. # Generate systemd timer unit
  111. cat >"/etc/systemd/system/${SERVICE_NAME}.timer" <<EOF
  112. [Unit]
  113. Description=Run VMess Domain Rotator every ${INTERVAL}
  114. [Timer]
  115. OnBootSec=2min
  116. OnUnitActiveSec=${INTERVAL}
  117. AccuracySec=30s
  118. Unit=${SERVICE_NAME}.service
  119. Persistent=true
  120. [Install]
  121. WantedBy=timers.target
  122. EOF
  123. # Enable and start service
  124. systemctl daemon-reload
  125. systemctl enable --now "${SERVICE_NAME}.timer"
  126. systemctl start "${SERVICE_NAME}.service"
  127. echo ""
  128. echo "✓ Installation complete!"
  129. echo ""
  130. echo "Configuration:"
  131. echo " Working directory: ${APP_DIR}"
  132. echo " Service user: ${RUN_USER}"
  133. echo " Service group: ${RUN_GROUP}"
  134. echo " Timer interval: ${INTERVAL}"
  135. echo ""
  136. echo "Commands:"
  137. echo " Check status: systemctl status ${SERVICE_NAME}.timer"
  138. echo " View logs: journalctl -u ${SERVICE_NAME}.service -n 50 --no-pager"
  139. echo " Manual run: systemctl start ${SERVICE_NAME}.service"
  140. echo ""