From 485f13a1cdcaa1139fd86c0851f92cc76f52051f Mon Sep 17 00:00:00 2001 From: datadunia Date: Fri, 29 May 2026 05:16:12 +0700 Subject: [PATCH] feat: add install/uninstall scripts for non-Docker deployment --- nexusguard-install.sh | 222 ++++++++++++++++++++++++++++++++++++++++ nexusguard-uninstall.sh | 87 ++++++++++++++++ 2 files changed, 309 insertions(+) create mode 100644 nexusguard-install.sh create mode 100644 nexusguard-uninstall.sh diff --git a/nexusguard-install.sh b/nexusguard-install.sh new file mode 100644 index 0000000..ef67d1a --- /dev/null +++ b/nexusguard-install.sh @@ -0,0 +1,222 @@ +#!/bin/bash +set -e + +# NexusGuard Server Installer +# Installs server-core, dashboard-ui, and configures systemd + nginx + +# Defaults +SERVER_PORT=8080 +WEB_PORT=80 +CONF_DIR="/etc/nexusguard" +CONF_FILE="$CONF_DIR/nexusguard.conf" +SERVICE_NAME="nexusguard-server" +NGINX_CONF="/etc/nginx/conf.d/nexusguard.conf" +DASHBOARD_DIR="/usr/share/nexusguard/dashboard" +BINARY_DIR="/usr/local/bin" + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +info() { echo -e "${GREEN}[INFO]${NC} $1"; } +warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } +error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; } + +usage() { + cat << EOF +NexusGuard Server Installer + +Usage: $0 [OPTIONS] + +Options: + --server-port PORT API server port (default: 8080) + --web-port PORT Nginx web port (default: 80) + --help Show this help message + +Prerequisites: + - nginx installed and running + - PostgreSQL installed and running + - Redis installed and running + - Pre-built binaries in ./bin/ directory + +This script will: + 1. Copy server-core binary to /usr/local/bin/ + 2. Copy dashboard dist to /usr/share/nexusguard/dashboard/ + 3. Create /etc/nexusguard/nexusguard.conf + 4. Create systemd service + 5. Configure nginx +EOF + exit 0 +} + +# Parse arguments +while [[ "$#" -gt 0 ]]; do + case $1 in + --server-port) SERVER_PORT="$2"; shift ;; + --web-port) WEB_PORT="$2"; shift ;; + --help) usage ;; + *) error "Unknown parameter: $1" ;; + esac + shift +done + +# Check root +if [ "$EUID" -ne 0 ]; then + error "Please run as root" +fi + +# Check dependencies +command -v nginx >/dev/null 2>&1 || error "nginx is not installed" +command -v systemctl >/dev/null 2>&1 || error "systemctl is not installed" + +# Check for pre-built binaries +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SERVER_BINARY="$SCRIPT_DIR/bin/server-core" +DASHBOARD_DIST="$SCRIPT_DIR/apps/dashboard-ui/dist" + +if [ ! -f "$SERVER_BINARY" ]; then + error "Server binary not found at $SERVER_BINARY. Build first: cd apps/server-core && go build -o ../../bin/server-core ." +fi + +if [ ! -d "$DASHBOARD_DIST" ]; then + error "Dashboard dist not found at $DASHBOARD_DIST. Build first: cd apps/dashboard-ui && npm run build" +fi + +info "Installing NexusGuard Server..." + +# Create directories +info "Creating directories..." +mkdir -p "$CONF_DIR" +mkdir -p "$DASHBOARD_DIR" +mkdir -p "$BINARY_DIR" + +# Copy server binary +info "Installing server-core binary..." +cp "$SERVER_BINARY" "$BINARY_DIR/$SERVICE_NAME" +chmod +x "$BINARY_DIR/$SERVICE_NAME" + +# Copy dashboard dist +info "Installing dashboard files..." +cp -r "$DASHBOARD_DIST"/* "$DASHBOARD_DIR/" + +# Create config file (only if not exists) +if [ ! -f "$CONF_FILE" ]; then + info "Creating config file..." + cat << EOF > "$CONF_FILE" +# NexusGuard Configuration +# This file is sourced by the server-core and nginx + +# Database +export DB_HOST=127.0.0.1 +export DB_PORT=5432 +export DB_USER=nexusguard +export DB_PASSWORD=nexusguard +export DB_NAME=nexusguard + +# Redis +export REDIS_ADDR=127.0.0.1:6379 + +# Security (CHANGE THESE!) +export JWT_SECRET=change-me-to-a-random-string +export SERVER_SALT=change-me-to-another-random-string + +# Network +export NFTABLES_TABLE=nexusguard +export IPAM_POOL=10.8.0.0/16 + +# Server +export GIN_MODE=release +export PORT=$SERVER_PORT + +# Dashboard +export API_BASE_URL=http://localhost:$SERVER_PORT/api/v1 +export WEB_PORT=$WEB_PORT +EOF + chmod 600 "$CONF_FILE" + warn "Config file created at $CONF_FILE" + warn "IMPORTANT: Edit $CONF_FILE and set JWT_SECRET and SERVER_SALT!" +else + info "Config file already exists, skipping..." +fi + +# Create systemd service +info "Creating systemd service..." +cat << EOF > "/etc/systemd/system/$SERVICE_NAME.service" +[Unit] +Description=NexusGuard SD-WAN Server +After=network.target postgresql.service redis.service +Wants=postgresql.service redis.service + +[Service] +Type=simple +User=root +WorkingDirectory=$BINARY_DIR +EnvironmentFile=$CONF_FILE +ExecStart=$BINARY_DIR/$SERVICE_NAME +Restart=always +RestartSec=5 +StandardOutput=journal +StandardError=journal + +# Security hardening +NoNewPrivileges=false +ProtectSystem=false +ProtectHome=false + +[Install] +WantedBy=multi-user.target +EOF + +# Create nginx config +info "Creating nginx configuration..." +cat << EOF > "$NGINX_CONF" +server { + listen $WEB_PORT; + listen [::]:$WEB_PORT; + server_name localhost; + + # Inject window.__CONFIG__ into HTML responses + sub_filter '' ''; + sub_filter_once on; + sub_filter_types text/html; + + location /api/ { + proxy_pass http://127.0.0.1:$SERVER_PORT; + proxy_set_header Host \$host; + proxy_set_header X-Real-IP \$remote_addr; + proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto \$scheme; + } + + location / { + root $DASHBOARD_DIR; + try_files \$uri \$uri/ /index.html; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root $DASHBOARD_DIR; + } +} +EOF + +# Enable and start service +info "Enabling and starting service..." +systemctl daemon-reload +systemctl enable "$SERVICE_NAME" +systemctl start "$SERVICE_NAME" + +# Reload nginx +info "Reloading nginx..." +nginx -t && systemctl reload nginx + +info "Installation complete!" +echo "" +info "Access URL: http://localhost:$WEB_PORT" +info "Config file: $CONF_FILE" +info "Service status: systemctl status $SERVICE_NAME" +echo "" +warn "IMPORTANT: Edit $CONF_FILE to set secure JWT_SECRET and SERVER_SALT values!" +warn "Then restart the service: systemctl restart $SERVICE_NAME" diff --git a/nexusguard-uninstall.sh b/nexusguard-uninstall.sh new file mode 100644 index 0000000..6686f77 --- /dev/null +++ b/nexusguard-uninstall.sh @@ -0,0 +1,87 @@ +#!/bin/bash +set -e + +# NexusGuard Server Uninstaller + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +info() { echo -e "${GREEN}[INFO]${NC} $1"; } +warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } +error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; } + +# Check root +if [ "$EUID" -ne 0 ]; then + error "Please run as root" +fi + +SERVICE_NAME="nexusguard-server" +CONF_DIR="/etc/nexusguard" +CONF_FILE="$CONF_DIR/nexusguard.conf" +NGINX_CONF="/etc/nginx/conf.d/nexusguard.conf" +DASHBOARD_DIR="/usr/share/nexusguard/dashboard" +BINARY_DIR="/usr/local/bin" + +info "Uninstalling NexusGuard Server..." + +# Stop and disable service +if systemctl is-active --quiet "$SERVICE_NAME" 2>/dev/null; then + info "Stopping service..." + systemctl stop "$SERVICE_NAME" +fi + +if systemctl is-enabled --quiet "$SERVICE_NAME" 2>/dev/null; then + info "Disabling service..." + systemctl disable "$SERVICE_NAME" +fi + +# Remove files +info "Removing files..." + +if [ -f "$BINARY_DIR/$SERVICE_NAME" ]; then + rm -f "$BINARY_DIR/$SERVICE_NAME" + info "Removed binary: $BINARY_DIR/$SERVICE_NAME" +fi + +if [ -d "$DASHBOARD_DIR" ]; then + rm -rf "$DASHBOARD_DIR" + info "Removed dashboard: $DASHBOARD_DIR" +fi + +if [ -f "$CONF_FILE" ]; then + rm -f "$CONF_FILE" + info "Removed config: $CONF_FILE" +fi + +if [ -d "$CONF_DIR" ]; then + rmdir "$CONF_DIR" 2>/dev/null || true + info "Removed config directory: $CONF_DIR" +fi + +if [ -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then + rm -f "/etc/systemd/system/$SERVICE_NAME.service" + info "Removed systemd service" +fi + +if [ -f "$NGINX_CONF" ]; then + rm -f "$NGINX_CONF" + info "Removed nginx config: $NGINX_CONF" +fi + +# Reload systemd and nginx +info "Reloading systemd daemon..." +systemctl daemon-reload + +if command -v nginx >/dev/null 2>&1; then + info "Reloading nginx..." + nginx -t && systemctl reload nginx 2>/dev/null || true +fi + +info "Uninstall complete!" +echo "" +info "NexusGuard has been removed." +info "Note: PostgreSQL and Redis data were NOT removed." +info "To remove database data, run: docker compose down -v (if using Docker)"