#!/usr/bin/env bash set -Eeuo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck source=common.sh source "$SCRIPT_DIR/common.sh" cd "$PROJECT_ROOT" RUN_TESTS=1 START_SERVICE=1 usage() { cat <<'TXT' Install GREE Controller as a systemd service in a Debian/Ubuntu LXC container. Usage: sudo ./scripts/install.sh sudo ./scripts/install.sh --skip-tests sudo ./scripts/install.sh --no-start The installer preserves an existing /etc/gree-controller.env file and database. Use scripts/update.sh for later releases. TXT } while [[ $# -gt 0 ]]; do case "$1" in --skip-tests) RUN_TESTS=0; shift ;; --no-start) START_SERVICE=0; shift ;; -h|--help) usage; exit 0 ;; *) echo "Unknown argument: $1" >&2; usage; exit 2 ;; esac done require_root require_systemd if [[ -x "$INSTALL_BINARY" && -f "$SERVICE_FILE" ]]; then fail "An existing installation was detected. Use sudo ./scripts/update.sh instead." fi install_build_dependencies ensure_rust if command -v systemd-detect-virt >/dev/null 2>&1; then virt="$(systemd-detect-virt --container 2>/dev/null || true)" [[ -n "$virt" ]] || warn "No container runtime was detected. Installation can still continue on a regular systemd host." fi version="$(project_version)" say "Installing GREE Controller ${version:-unknown}" if [[ "$RUN_TESTS" -eq 1 ]]; then say "Running Rust tests before installation" cargo test --all-targets fi say "Building release binary" cargo build --release getent group "$SERVICE_GROUP" >/dev/null 2>&1 || groupadd --system "$SERVICE_GROUP" id "$SERVICE_USER" >/dev/null 2>&1 || \ useradd --system --gid "$SERVICE_GROUP" --home "$DATA_DIR" --shell /usr/sbin/nologin "$SERVICE_USER" install -d -o "$SERVICE_USER" -g "$SERVICE_GROUP" -m 0750 "$DATA_DIR" install -d -o root -g root -m 0755 "$INSTALL_DIR" install -d -o root -g root -m 0700 "$BACKUP_ROOT" install -o root -g root -m 0755 target/release/gree-controller "$INSTALL_BINARY" install -o root -g root -m 0644 systemd/gree-controller.service "$SERVICE_FILE" if [[ ! -f "$ENV_FILE" ]]; then token="$(od -An -N24 -tx1 /dev/urandom | tr -d ' \n')" cat > "$ENV_FILE" </dev/null if [[ "$START_SERVICE" -eq 1 ]]; then say "Starting GREE Controller" systemctl restart "$SERVICE_NAME" if ! wait_for_health 80; then systemctl --no-pager --full status "$SERVICE_NAME" || true journalctl -u "$SERVICE_NAME" -n 80 --no-pager || true fail "Service did not pass the health check." fi say "Installation complete" printf 'Panel: %s\n' "$(panel_url)" printf 'Status: %s status\n' "$SCRIPT_DIR/service.sh" printf 'Logs: %s logs\n' "$SCRIPT_DIR/service.sh" else say "Installation complete; service was not started (--no-start)." fi