From fe2830809d06281bd4ea6c0a32fbbae2fcb49fff Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Mon, 13 Jul 2026 18:43:00 +0800 Subject: [PATCH] fix: install node 20 via ssh and restart runners --- scripts/check_new_server.py | 92 +++++++++++++++++++++++++++++++------ 1 file changed, 79 insertions(+), 13 deletions(-) diff --git a/scripts/check_new_server.py b/scripts/check_new_server.py index 9975ac214..36faa44f4 100644 --- a/scripts/check_new_server.py +++ b/scripts/check_new_server.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +"""SSH到新服务器,检查CMD Agent并安装Node.js""" import subprocess, os NEW_SERVER = "172.30.18.199" @@ -10,22 +11,87 @@ AAAED2muzuU4BAiCqbg0ayGxgiDvfS/xI1SFvb32oLzTnn8/oaq4CTm8ER9u1okJNJ+dAS GPMLx7kYXrSFJs8/QElsAAAAFHJ1bm5lci1hZG1pbkB4aWFveGlhAQ== -----END OPENSSH PRIVATE KEY----- """ + key_path = "/tmp/new_server_ed25519" with open(key_path, "w") as f: f.write(KEY_CONTENT) os.chmod(key_path, 0o600) -def ssh(cmd): - r = subprocess.run(f'ssh -i {key_path} -o StrictHostKeyChecking=no root@{NEW_SERVER} "{cmd}"', - shell=True, capture_output=True, text=True) - return r.stdout.strip() + r.stderr.strip() +def ssh(cmd, timeout=60): + r = subprocess.run( + f'ssh -i {key_path} -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o ServerAliveInterval=30 root@{NEW_SERVER} "{cmd}"', + shell=True, capture_output=True, text=True, timeout=timeout+10 + ) + return r.returncode, r.stdout.strip(), r.stderr.strip() -print("=== 新服务器环境检查 ===") -print("OS:", ssh("cat /etc/os-release | head -5")) -print("Python版本:", ssh("python3 --version 2>&1")) -print("pip版本:", ssh("pip3 --version 2>&1")) -print("Docker版本:", ssh("docker --version 2>&1")) -print("Runner进程数:", ssh("ps aux | grep act_runner_v0.2.6 | grep -v grep | wc -l")) -print("Runner服务状态:") -print(ssh("for i in 1 2 3 4 5 6; do echo -n \" runner-$i: \"; systemctl is-active act-runner-$i.service; done")) -print("本地镜像:", ssh("docker images | head -10")) +print("=" * 50) +print("步骤1: 系统信息") +print("=" * 50) +rc, out, err = ssh("cat /etc/os-release | head -5 && echo '---' && uname -r") +print(out[:300]) + +print("\n" + "=" * 50) +print("步骤2: CMD Agent状态") +print("=" * 50) +rc, out, err = ssh("systemctl status cmd-agent 2>&1 | head -10 && echo '---' && ss -tlnp | grep -E '5000|5927|8080' || echo 'no listening port found'") +print(out[:500]) + +print("\n" + "=" * 50) +print("步骤3: 安装Node.js 20(二进制包方式)") +print("=" * 50) + +# 先检查是否已装 +rc, out, err = ssh("which node 2>/dev/null && node --version 2>&1 || echo 'NOT_INSTALLED'") +print(f"当前状态: {out[:100]}") + +if "NOT_INSTALLED" in out or "v20" not in out: + print("开始安装Node.js...") + # 下载二进制包 + rc, out, err = ssh(""" +cd /tmp +curl -fsSL https://nodejs.org/dist/v20.15.0/node-v20.15.0-linux-x64.tar.xz -o node.tar.xz 2>&1 | tail -3 +ls -lh node.tar.xz +""", timeout=120) + print(f"下载 RC={rc}") + print(out[:300]) + if err: print(f"下载错误: {err[:200]}") + + # 解压安装 + rc, out, err = ssh(""" +cd /tmp +tar -xf node.tar.xz +rm -rf /usr/local/node +mv node-v20.15.0-linux-x64 /usr/local/node +ln -sf /usr/local/node/bin/node /usr/local/bin/node +ln -sf /usr/local/node/bin/npm /usr/local/bin/npm +ln -sf /usr/local/node/bin/npx /usr/local/bin/npx +node --version +npm --version +which node +""") + print(f"安装 RC={rc}") + print(out[:300]) + if err and rc != 0: print(f"安装错误: {err[:200]}") +else: + print("Node.js 20 已安装,跳过") + +print("\n" + "=" * 50) +print("步骤4: 重启所有Runner服务") +print("=" * 50) +rc, out, err = ssh(""" +for i in 1 2 3 4 5 6; do + systemctl restart act-runner-$i 2>&1 + echo "runner-$i restarted" +done +sleep 5 +systemctl is-active act-runner-{1,2,3,4,5,6} 2>&1 +""", timeout=60) +print(out[:300]) + +print("\n" + "=" * 50) +print("步骤5: 验证Node可用") +print("=" * 50) +rc, out, err = ssh("node -e 'console.log(\"OK: \" + process.version)' && npm -v") +print(out.strip()) + +print("\n全部完成!")