5590391779
Agent (agent.py): - Rebrand MultiSync → Nexus throughout - Add server_id to heartbeat payload (required by backend AgentHeartbeat schema) - Add /exec endpoint for remote command execution via Nexus - Update heartbeat fields to match backend: cpu_usage, mem_usage, disk_usage - Use 60s default interval (matches CLAUDE.md spec) Agent (agent.sh): - Rebrand, add SERVER_ID env var, send in heartbeat payload - Update service name to nexus-agent Agent (install.sh): - Rebrand, add --id parameter for server_id - Write server_id to config.json - Service name: nexus-agent - Add config.example.json for reference SyncEngineV2: - Add trigger_type parameter to sync_files() (was hardcoded "manual") - Schedule/retry runners can now pass "schedule"/"retry" trigger types Nginx: - Add /agent/ to static asset locations for agent file downloads Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
137 lines
4.2 KiB
Bash
137 lines
4.2 KiB
Bash
#!/bin/bash
|
|
# ================================================================
|
|
# Nexus Agent Install Script
|
|
# Usage: curl -fsSL https://api.synaglobal.vip/agent/install.sh | bash -s -- --url https://api.synaglobal.vip --key 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 ""
|