#!/usr/bin/env bash set -euo pipefail DEFAULT_INSTALL_DIR="/home/gameserver/GSP" DEFAULT_AGENT_USER="gameserver" DEFAULT_SERVICE_NAME="gsp_agent" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ORIG_ARGS=("$@") usage() { cat <&2 exit 1 } require_root() { if [ "$(id -u)" -eq 0 ]; then return 0 fi if command -v sudo >/dev/null 2>&1; then echo "System service installation requires root. Re-running through sudo..." exec sudo -E bash "$0" "$@" fi fail "Run this installer as root: sudo bash install.sh" } validate_safe_path() { local path="$1" case "$path" in ""|"/"|/bin|/bin/*|/boot|/boot/*|/dev|/dev/*|/etc|/etc/*|/lib|/lib/*|/lib64|/lib64/*|/proc|/proc/*|/root|/root/*|/run|/run/*|/sbin|/sbin/*|/sys|/sys/*|/usr|/usr/*|/var|/var/*) fail "Refusing unsafe install directory: $path" ;; esac } install_service() { local install_dir="$1" local agent_user="$2" local service_name="$3" local unit_path="/etc/systemd/system/${service_name}.service" command -v systemctl >/dev/null 2>&1 || { echo "systemd was not detected. Skipping service installation." return 0 } cp "${install_dir}/systemd/gsp_agent.service" "$unit_path" sed -i \ -e "s#{GSP_AGENT_PATH}#${install_dir}#g" \ -e "s#{GSP_AGENT_USER}#${agent_user}#g" \ -e "s#{GSP_AGENT_RUN_SCRIPT}#${install_dir}/gsp_agent_run#g" \ -e "s#{GSP_AGENT_PID_FILE}#${install_dir}/gsp_agent_run.pid#g" \ "$unit_path" systemctl daemon-reload systemctl enable "$service_name.service" if ask_yes_no "Start ${service_name}.service now?" "yes"; then systemctl restart "$service_name.service" fi } INSTALL_DIR="" AGENT_USER="" SERVICE_NAME="" INSTALL_SERVICE=1 OFFER_PREREQS=1 while [ "$#" -gt 0 ]; do case "$1" in --install-dir) INSTALL_DIR="${2:-}"; shift 2 ;; --agent-user) AGENT_USER="${2:-}"; shift 2 ;; --service-name) SERVICE_NAME="${2:-}"; shift 2 ;; --no-service) INSTALL_SERVICE=0; shift ;; --no-prereqs) OFFER_PREREQS=0; shift ;; --help|-h) usage; exit 0 ;; *) fail "Unknown option: $1" ;; esac done require_root "${ORIG_ARGS[@]}" echo "GSP Linux Agent installer" echo echo "This installer configures the Linux execution agent for GameServer Panel." echo "Use the same key/password that is configured for this remote server in the Panel." echo if [ "$OFFER_PREREQS" -eq 1 ] && [ -x "${SCRIPT_DIR}/install_agent_prereqs.sh" ]; then if ask_yes_no "Run install_agent_prereqs.sh before installing?" "yes"; then bash "${SCRIPT_DIR}/install_agent_prereqs.sh" fi elif [ "$OFFER_PREREQS" -eq 1 ]; then echo "Prerequisite installer is not executable. Run: chmod +x install_agent_prereqs.sh && sudo ./install_agent_prereqs.sh" fi INSTALL_DIR="${INSTALL_DIR:-$(ask "Install directory" "$DEFAULT_INSTALL_DIR")}" AGENT_USER="${AGENT_USER:-$(ask "Agent Linux user" "$DEFAULT_AGENT_USER")}" SERVICE_NAME="${SERVICE_NAME:-$(ask "systemd service name" "$DEFAULT_SERVICE_NAME")}" validate_safe_path "$INSTALL_DIR" INSTALL_DIR="${INSTALL_DIR%/}" if ! id "$AGENT_USER" >/dev/null 2>&1; then if ask_yes_no "User ${AGENT_USER} does not exist. Create it?" "yes"; then useradd --create-home --shell /bin/bash "$AGENT_USER" else fail "Agent user is required." fi fi mkdir -p "$INSTALL_DIR" if [ "$SCRIPT_DIR" != "$INSTALL_DIR" ]; then rsync -a --exclude='.git/' --exclude='Cfg/Config.pm' --exclude='Cfg/Preferences.pm' --exclude='Cfg/bash_prefs.cfg' "${SCRIPT_DIR}/" "${INSTALL_DIR}/" fi mkdir -p \ "${INSTALL_DIR}/Cfg" \ "${INSTALL_DIR}/logs" \ "${INSTALL_DIR}/tmp" \ "${INSTALL_DIR}/screenlogs" \ "${INSTALL_DIR}/runtime_status" \ "${INSTALL_DIR}/events" \ "${INSTALL_DIR}/_gsp_content/hooks" \ "${INSTALL_DIR}/_gsp_content/generated" \ "${INSTALL_DIR}/_gsp_content/runtime" touch "${INSTALL_DIR}/_gsp_content/runtime/server_content.pids" chmod +x "${INSTALL_DIR}/gsp_agent_run" "${INSTALL_DIR}/ogp_agent_run" "${INSTALL_DIR}/agent_conf.sh" "${INSTALL_DIR}/install_agent_prereqs.sh" 2>/dev/null || true chown -R "${AGENT_USER}:${AGENT_USER}" "$INSTALL_DIR" echo echo "Configuring agent files..." bash "${INSTALL_DIR}/agent_conf.sh" \ --install-dir "$INSTALL_DIR" \ --agent-user "$AGENT_USER" \ --guided if [ "$INSTALL_SERVICE" -eq 1 ]; then install_service "$INSTALL_DIR" "$AGENT_USER" "$SERVICE_NAME" fi cat <