#!/usr/bin/env python3 """通过CMD Agent修复新服务器环境:安装Node.js、确认系统信息""" import subprocess import json import urllib.request import time NEW_SERVER = "172.30.18.199" API_KEY = "xiaoxia-cmd-agent-2026" def cmd(cmd_str, timeout=120): url = f"http://{NEW_SERVER}:5927/cmd" data = json.dumps({"cmd": cmd_str, "timeout": timeout}).encode() req = urllib.request.Request(url, data=data, method="POST") req.add_header("Content-Type", "application/json") req.add_header("X-API-Key", API_KEY) try: with urllib.request.urlopen(req, timeout=timeout+10) as resp: result = json.loads(resp.read().decode()) return result.get("returncode", -1), result.get("stdout", ""), result.get("stderr", "") except Exception as e: return -1, "", str(e) print("=" * 50) print("步骤1: 系统信息确认") print("=" * 50) rc, out, err = cmd("cat /etc/os-release | head -5 && echo '---' && uname -a && echo '---' && which yum dnf apt-get 2>/dev/null || true") print(out[:500]) print("\n" + "=" * 50) print("步骤2: 用yum安装Node.js 20 (nodesource)") print("=" * 50) # CentOS用curl下载nodesource的rpm包或者直接用yum模块 rc, out, err = cmd(""" # 先试curl下载Node.js二进制包(最稳妥,不依赖包管理器) 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[:500]) if rc != 0: print(f"ERR: {err[:300]}") print("\n" + "=" * 50) print("步骤3: 解压并安装到/usr/local") print("=" * 50) rc, out, err = cmd(""" cd /tmp tar -xf node.tar.xz 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 """, timeout=60) print(f"RC={rc}") print(out[:300]) if err and rc != 0: print(f"ERR: {err[:200]}") print("\n" + "=" * 50) print("步骤4: 验证Node.js可用") print("=" * 50) rc, out, err = cmd("node -e 'console.log(\"node works: \" + process.version)'") print(f"RC={rc}: {out.strip()}") print("\n" + "=" * 50) print("步骤5: 检查Runner进程和配置") print("=" * 50) rc, out, err = cmd("systemctl list-units 'act-runner-*' --no-legend | head -10 && echo '---' && ps aux | grep act_runner | grep -v grep | wc -l") print(out[:500]) print("\n" + "=" * 50) print("全部完成!") print("=" * 50)