Files
Nexus/web/agent/install.sh
T
Your Name c9a99f4fb3 fix: 全项目文档对齐 + 代码清理
代码修复:
- web/agent/agent.py: datetime.utcnow() → datetime.now(timezone.utc)
- requirements.txt: 移除 paramiko==3.5.0 (已无活跃引用)
- 删除 server/infrastructure/ssh/pool.py (DEPRECATED, 无引用)

连接池三层对齐 (config.py/.env.example/session.py):
- DB_POOL_SIZE 100→160, DB_MAX_OVERFLOW 100→120
- 基于 MySQL max_connections=400, install.php 公式

文档修复 (21项):
- P0: 硬编码域名/IP替换为配置变量占位符
- P1: 10个过时设计文档加归档标注 (引用旧文件结构)
- P2: step-3-webssh报告 paramiko引用修正
- 6份审查报告连接池参数勘误 100/100→160/120
- ECC安全报告 EC5 datetime.utcnow 标记已修复
- docs/README.md 文档索引重写
- docs/memory/mem_nexus_overview.md 移除硬编码凭证
- docs/project/tech-stack-inventory.md paramiko标记已移除

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-22 08:19:56 +08:00

137 lines
4.2 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..."
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,
"log_file": "/var/log/nexus-agent.log",
"log_level": "INFO",
"server": { "host": "0.0.0.0", "port": ${AGENT_PORT} }
}
EOF
# 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 ""