Files
Nexus/web/agent/install.sh
T
Your Name 9bba58a529 feat: Telegram test+ChatID, Agent IP allowlist/upgrade, alert history page
Telegram:
- POST /api/settings/telegram/test: send test message (checks token+chat_id)
- GET /api/settings/telegram/chats: call getUpdates, return up to 5 chats
- settings.html: test button + detect chat IDs with click-to-fill

Agent IP allowlist (web/agent/agent.py):
- allowed_ips config: list of trusted IPs/CIDRs from config.json
- _ip_allowed(): checks exact IP, CIDR (ipaddress module), hostname
- verify_api_key(): now checks IP allowlist before API key
- /health: also checks IP allowlist
- install.sh: auto-extracts central server IP from --url, adds to allowed_ips

Agent auto-upgrade (server/api/servers.py):
- POST /api/servers/{id}/upgrade-agent: SSH → curl new agent.py → systemctl restart
- servers.html: 升级 Agent button in Agent tab (only when online)

Alert history (new page):
- domain/models: AlertLog table (server_id, type, value, is_recovery, created_at)
- migrations.py: CREATE TABLE IF NOT EXISTS alert_logs
- websocket.py: _save_alert_log() called from broadcast_alert/recovery
- settings.py: GET /api/alert-history/ (paginated, filters), GET /stats
- main.py: register alert_history_router
- alerts.html: new page with stats cards, top-servers bar chart, alert list
- layout.js: 🔔 告警中心 added to sidebar nav

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-23 18:27:12 +08:00

142 lines
4.4 KiB
Bash

#!/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 ""