#!/bin/bash # ================================================================ # Nexus Agent Install Script # Usage: curl -fsSL https://YOUR_NEXUS_DOMAIN/agent/install.sh | bash -s -- --url https://YOUR_NEXUS_DOMAIN --key API_KEY --id SERVER_ID # ================================================================ set -e CENTRAL_URL="" WEB_URL="" API_KEY="" SERVER_ID="" AGENT_PORT="8601" INSTALL_DIR="/opt/nexus-agent" while [[ $# -gt 0 ]]; do case $1 in --url) CENTRAL_URL="$2"; shift 2 ;; --web-url) WEB_URL="$2"; shift 2 ;; --key) API_KEY="$2"; shift 2 ;; --id) SERVER_ID="$2"; shift 2 ;; --port) AGENT_PORT="$2"; shift 2 ;; *) shift ;; esac done [ -z "$CENTRAL_URL" ] && echo "ERROR: --url is required" && exit 1 [ -z "$SERVER_ID" ] && echo "ERROR: --id is required (server ID from Nexus dashboard)" && exit 1 [ -z "$API_KEY" ] && echo "ERROR: --key is required (API key from Nexus settings)" && exit 1 echo "" echo "==========================================" echo " Nexus Agent Install" echo "==========================================" echo " Server: $CENTRAL_URL" echo " ID: $SERVER_ID" echo " Port: $AGENT_PORT" echo "==========================================" echo "" # 1. Install Python echo "[1/5] Install Python..." if command -v apt-get >/dev/null 2>&1; then apt-get update -qq 2>/dev/null || true apt-get install -y -qq python3 python3-pip curl 2>/dev/null || true elif command -v yum >/dev/null 2>&1; then yum install -y -q python3 python3-pip curl 2>/dev/null || true elif command -v dnf >/dev/null 2>&1; then dnf install -y -q python3 python3-pip curl 2>/dev/null || true fi pip3 install fastapi uvicorn httpx psutil python-multipart 2>/dev/null \ || pip3 install fastapi uvicorn httpx psutil python-multipart --break-system-packages 2>/dev/null \ || true # 2. Create dir echo "[2/5] Create directory..." mkdir -p "$INSTALL_DIR" # 3. Download agent.py echo "[3/5] Download agent.py..." [ -z "$WEB_URL" ] && WEB_URL="${CENTRAL_URL}" curl -fsSL "${WEB_URL}/agent/agent.py" -o "$INSTALL_DIR/agent.py" || { echo "ERROR: Cannot download from $WEB_URL/agent/agent.py" exit 1 } # 4. Config echo "[4/5] Write config..." # Extract central IP from URL for IP allowlist CENTRAL_HOST=$(echo "${CENTRAL_URL}" | sed 's~https\?://~~' | cut -d'/' -f1 | cut -d':' -f1) cat > "$INSTALL_DIR/config.json" << EOF { "server_id": ${SERVER_ID}, "central": { "url": "${CENTRAL_URL}", "api_key": "${API_KEY}" }, "api_key": "${API_KEY}", "heartbeat_interval": 60, "allowed_ips": ["${CENTRAL_HOST}"], "log_file": "/var/log/nexus-agent.log", "log_level": "INFO", "server": { "host": "127.0.0.1", "port": ${AGENT_PORT} } } EOF echo " IP allowlist: ${CENTRAL_HOST}" # 5. systemd + firewall + start echo "[5/5] Setup service + firewall + start..." cat > /etc/systemd/system/nexus-agent.service << EOF [Unit] Description=Nexus Agent After=network.target [Service] Type=simple User=root WorkingDirectory=${INSTALL_DIR} ExecStart=python3 -m uvicorn agent:app --host 0.0.0.0 --port ${AGENT_PORT} --log-level info Restart=always RestartSec=5 [Install] WantedBy=multi-user.target EOF # Firewall if command -v ufw &>/dev/null && ufw status 2>/dev/null | grep -q 'Status: active'; then ufw allow ${AGENT_PORT}/tcp 2>/dev/null && echo " ufw: opened ${AGENT_PORT}/tcp" elif command -v firewall-cmd &>/dev/null && firewall-cmd --state 2>/dev/null | grep -q 'running'; then firewall-cmd --add-port=${AGENT_PORT}/tcp --permanent 2>/dev/null firewall-cmd --reload 2>/dev/null && echo " firewalld: opened ${AGENT_PORT}/tcp" else iptables -C INPUT -p tcp --dport ${AGENT_PORT} -j ACCEPT 2>/dev/null \ || iptables -I INPUT -p tcp --dport ${AGENT_PORT} -j ACCEPT echo " iptables: opened ${AGENT_PORT}/tcp" fi systemctl daemon-reload systemctl enable nexus-agent systemctl start nexus-agent sleep 2 STATUS="running" systemctl is-active --quiet nexus-agent || STATUS="FAILED" HOSTNAME=$(hostname 2>/dev/null || echo "unknown") IP=$(hostname -I 2>/dev/null | awk '{print $1}' || echo "unknown") echo "" echo "==========================================" echo " Done! Status: ${STATUS}" echo "==========================================" echo "" echo " Agent: http://${IP}:${AGENT_PORT}" echo " Server ID: ${SERVER_ID}" echo "" echo " Commands:" echo " systemctl status nexus-agent" echo " journalctl -u nexus-agent -f" echo " systemctl restart nexus-agent" echo ""