Files
xiaoxia-saas/scripts/install-cmd-agent.sh
T

84 lines
2.2 KiB
Bash

#!/bin/bash
# CMD Agent 安装脚本
# 在 CI Runner 宿主机上安装 CMD Agent 服务
set -e
echo "=== 主机信息 ==="
echo "主机名: $(hostname)"
echo "IP: $(hostname -I 2>/dev/null || ip addr show | grep 'inet ' | awk '{print $2}' | tr '\n' ' ')"
echo "用户: $(whoami)"
echo "Runner 名称: ${RUNNER_NAME:-unknown}"
# 检查是否已安装
if [ -f /opt/xiaoxia-cmd-agent/server.py ] && systemctl is-active xiaoxia-cmd-agent &>/dev/null; then
echo "CMD Agent 已安装且运行中,跳过安装"
systemctl status xiaoxia-cmd-agent --no-pager
exit 0
fi
SUDO=""
[ "$(id -u)" != "0" ] && SUDO="sudo"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
SERVER_PY="$SCRIPT_DIR/../infra/cmd-agent/server.py"
if [ ! -f "$SERVER_PY" ]; then
echo "ERROR: 找不到 server.py: $SERVER_PY"
exit 1
fi
echo "=== 开始安装 CMD Agent ==="
# 创建目录
$SUDO mkdir -p /opt/xiaoxia-cmd-agent
$SUDO cp "$SERVER_PY" /opt/xiaoxia-cmd-agent/server.py
$SUDO chmod +x /opt/xiaoxia-cmd-agent/server.py
# 生成 token
TOKEN="xsa-$(openssl rand -hex 16)"
echo "TOKEN: $TOKEN"
echo "$TOKEN" | $SUDO tee /etc/xiaoxia-cmd-agent.token > /dev/null
$SUDO chmod 600 /etc/xiaoxia-cmd-agent.token
# 修改监听地址为 0.0.0.0(新服务器需要远程访问)
$SUDO sed -i 's/("127.0.0.1", LISTEN_PORT)/("0.0.0.0", LISTEN_PORT)/' /opt/xiaoxia-cmd-agent/server.py
# 创建 installed flag
echo "1" | $SUDO tee /etc/xiaoxia-cmd-agent.installed > /dev/null
# 创建 systemd service
$SUDO tee /etc/systemd/system/xiaoxia-cmd-agent.service > /dev/null <<EOF
[Unit]
Description=Xiaoxia Command Agent
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/python3 /opt/xiaoxia-cmd-agent/server.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
$SUDO systemctl daemon-reload
$SUDO systemctl enable xiaoxia-cmd-agent
$SUDO systemctl start xiaoxia-cmd-agent
sleep 3
echo ""
echo "=== 安装结果 ==="
$SUDO systemctl status xiaoxia-cmd-agent --no-pager || true
echo ""
echo "=== 连接信息 ==="
echo "主机名: $(hostname)"
echo "IP: $(hostname -I 2>/dev/null | awk '{print $1}')"
echo "端口: 18888"
echo "Token: $TOKEN"
# 验证
curl -s -H "Authorization: $TOKEN" http://127.0.0.1:18888/status && echo "" || echo "本地验证失败"