diff --git a/.gitea/workflows/install-cmd-agent.yml b/.gitea/workflows/install-cmd-agent.yml index 792ec9fa3..cec88fcda 100755 --- a/.gitea/workflows/install-cmd-agent.yml +++ b/.gitea/workflows/install-cmd-agent.yml @@ -1,4 +1,4 @@ -name: Install CMD Agent on New Server +name: Install CMD Agent + Node.js on New Server on: workflow_dispatch: @@ -10,7 +10,22 @@ jobs: install: runs-on: saas steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Install CMD Agent - run: python3 scripts/install_cmd_agent.py + - name: Download code + shell: sh + run: | + set -eu + archive_url="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/archive/${GITHUB_SHA}.tar.gz" + mkdir -p repo + curl -sL -H "Authorization: token ${GITHUB_TOKEN}" -o /tmp/repo.tar.gz "$archive_url" + tar -xzf /tmp/repo.tar.gz --strip-components=1 -C repo + rm -f /tmp/repo.tar.gz + cd repo + echo "代码下载完成,文件列表:" + ls scripts/ | head -20 + + - name: Install CMD Agent + Node.js + shell: sh + run: | + set -eu + cd repo + python3 scripts/install_cmd_agent.py diff --git a/scripts/install_cmd_agent.py b/scripts/install_cmd_agent.py index b1f662cce..1fc0b5bdf 100755 --- a/scripts/install_cmd_agent.py +++ b/scripts/install_cmd_agent.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""在新服务器安装CMD Agent""" +"""在新服务器安装CMD Agent + Node.js + 基础CI环境""" import subprocess import os import base64 @@ -27,17 +27,39 @@ def ssh(cmd): ) return r.returncode, r.stdout.strip(), r.stderr.strip() -print("=== 步骤1: 检查环境 ===") -rc, out, err = ssh("python3 --version && pip3 --version 2>&1") -print(f"Python: {out[:200]}") +print("=" * 50) +print("步骤1: 环境检查") +print("=" * 50) +rc, out, err = ssh("cat /etc/os-release | head -3 && echo '---' && python3 --version && echo '---' && which docker && docker --version && echo '---' && which node 2>/dev/null && node --version 2>/dev/null || echo 'node: not installed'") +print(out[:500]) -print("\n=== 步骤2: 安装flask ===") +print("\n" + "=" * 50) +print("步骤2: 安装Node.js 20.x (CI checkout action需要)") +print("=" * 50) +rc, out, err = ssh("curl -fsSL https://deb.nodesource.com/setup_20.x | bash - 2>&1 | tail -3 && apt-get install -y nodejs 2>&1 | tail -3 && node --version && npm --version") +print(f"结果: RC={rc}") +print(out[:300]) +if err and rc != 0: + print(f"ERR: {err[:300]}") + +print("\n" + "=" * 50) +print("步骤3: 安装flask (CMD Agent依赖)") +print("=" * 50) +# 先试pip install,如果失败试试--break-system-packages rc, out, err = ssh("pip3 install flask 2>&1 | tail -5") -print(f"{out[:300]}") -if err: - print(f"ERR: {err[:200]}") +if rc != 0: + print("pip3 install 失败,尝试 --break-system-packages...") + rc, out, err = ssh("pip3 install --break-system-packages flask 2>&1 | tail -5") +print(f"结果: RC={rc}") +print(out[:300]) -print("\n=== 步骤3: 写入CMD Agent ===") +# 验证 +rc2, out2, err2 = ssh("python3 -c 'import flask; print(flask.__version__)' 2>&1") +print(f"flask验证: {out2[:100]}") + +print("\n" + "=" * 50) +print("步骤4: 写入CMD Agent") +print("=" * 50) agent_code = """#!/usr/bin/env python3 import subprocess from flask import Flask, request, jsonify @@ -77,10 +99,13 @@ if __name__ == "__main__": """ code_b64 = base64.b64encode(agent_code.encode()).decode() -rc, out, err = ssh(f'mkdir -p /opt/cmd-agent && echo "{code_b64}" | base64 -d > /opt/cmd-agent/server.py && chmod +x /opt/cmd-agent/server.py && echo "written"') -print(f"写入: {'成功' if rc==0 else '失败'} - {out[:100]}") +rc, out, err = ssh(f'mkdir -p /opt/cmd-agent && echo "{code_b64}" | base64 -d > /opt/cmd-agent/server.py && chmod +x /opt/cmd-agent/server.py && ls -la /opt/cmd-agent/') +print(f"写入: {'成功' if rc==0 else '失败'}") +print(out[:200]) -print("\n=== 步骤4: 创建systemd服务 ===") +print("\n" + "=" * 50) +print("步骤5: 创建systemd服务") +print("=" * 50) svc = """[Unit] Description=CMD Agent After=network.target @@ -98,16 +123,28 @@ WantedBy=multi-user.target """ svc_b64 = base64.b64encode(svc.encode()).decode() rc, out, err = ssh(f'echo "{svc_b64}" | base64 -d > /etc/systemd/system/cmd-agent.service && systemctl daemon-reload && systemctl enable cmd-agent.service && systemctl start cmd-agent.service && sleep 2 && systemctl is-active cmd-agent.service') -print(f"服务状态: {out[:200] if rc==0 else '失败'}") +print(f"服务状态: {out[:100] if rc==0 else '失败'}") if err and rc != 0: print(f"ERR: {err[:300]}") -print("\n=== 步骤5: 健康检查 ===") +print("\n" + "=" * 50) +print("步骤6: CMD Agent健康检查") +print("=" * 50) rc, out, err = ssh("curl -s -H 'X-API-Key: xiaoxia-cmd-agent-2026' http://127.0.0.1:5927/health") print(f"健康检查: {out[:200]}") -print("\n=== 步骤6: Docker镜像检查 ===") +print("\n" + "=" * 50) +print("步骤7: Docker镜像检查") +print("=" * 50) rc, out, err = ssh("docker images 2>&1 | head -15") -print(f"镜像列表:\n{out[:400]}") +print(f"镜像:\n{out[:400]}") -print("\n=== 完成 ===") +print("\n" + "=" * 50) +print("步骤8: 最终环境核验") +print("=" * 50) +rc, out, err = ssh("echo 'Python:' && python3 --version && echo 'Node:' && node --version && echo 'Docker:' && docker --version && echo 'Git:' && git --version && echo 'Runner进程:' && ps aux | grep act_runner_v0.2.6 | grep -v grep | wc -l") +print(out) + +print("\n" + "=" * 50) +print("全部完成!") +print("=" * 50)