134 lines
4.1 KiB
Bash
134 lines
4.1 KiB
Bash
|
|
#!/bin/bash
|
||
|
|
# ================================================================
|
||
|
|
# MultiSync Python Agent Install Script
|
||
|
|
# Usage: curl -fsSL https://主服务器/agent/install.sh | bash -s -- --url https://主服务器 --key KEY
|
||
|
|
# ================================================================
|
||
|
|
|
||
|
|
set -e
|
||
|
|
|
||
|
|
CENTRAL_URL=""
|
||
|
|
WEB_URL=""
|
||
|
|
API_KEY=""
|
||
|
|
AGENT_PORT="8601"
|
||
|
|
WATCH_DIRS="/var/www/html"
|
||
|
|
INSTALL_DIR="/opt/multisync-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 ;;
|
||
|
|
--port) AGENT_PORT="$2"; shift 2 ;;
|
||
|
|
--dirs) WATCH_DIRS="$2"; shift 2 ;;
|
||
|
|
*) shift ;;
|
||
|
|
esac
|
||
|
|
done
|
||
|
|
|
||
|
|
[ -z "$CENTRAL_URL" ] && echo "ERROR: --url is required" && exit 1
|
||
|
|
|
||
|
|
echo ""
|
||
|
|
echo "=========================================="
|
||
|
|
echo " MultiSync Python Agent Install"
|
||
|
|
echo "=========================================="
|
||
|
|
echo " Server: $CENTRAL_URL"
|
||
|
|
echo " Dirs: $WATCH_DIRS"
|
||
|
|
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="$(echo "$CENTRAL_URL" | sed 's|:[0-9]\+||; s|^http://|https://|')"
|
||
|
|
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
|
||
|
|
{
|
||
|
|
"central": { "url": "${CENTRAL_URL}", "api_key": "${API_KEY}" },
|
||
|
|
"api_key": "${API_KEY}",
|
||
|
|
"heartbeat_interval": 30,
|
||
|
|
"watch_dirs": "${WATCH_DIRS}",
|
||
|
|
"log_file": "/var/log/multisync-agent.log",
|
||
|
|
"log_level": "INFO"
|
||
|
|
}
|
||
|
|
EOF
|
||
|
|
|
||
|
|
# 5. systemd + firewall + start
|
||
|
|
echo "[5/5] Setup service + firewall + start..."
|
||
|
|
|
||
|
|
cat > /etc/systemd/system/multisync-agent.service << EOF
|
||
|
|
[Unit]
|
||
|
|
Description=MultiSync Python 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
|
||
|
|
AGENT_PORT="${AGENT_PORT:-8601}"
|
||
|
|
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 multisync-agent
|
||
|
|
systemctl start multisync-agent
|
||
|
|
sleep 2
|
||
|
|
|
||
|
|
STATUS="running"
|
||
|
|
systemctl is-active --quiet multisync-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 ""
|
||
|
|
echo " Commands:"
|
||
|
|
echo " systemctl status multisync-agent"
|
||
|
|
echo " journalctl -u multisync-agent -f"
|
||
|
|
echo " systemctl restart multisync-agent"
|
||
|
|
echo ""
|