74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""通过CMD Agent修复新服务器环境:安装Node.js"""
|
|
import json
|
|
import urllib.request
|
|
|
|
NEW_SERVER = "172.30.18.199"
|
|
API_KEY = "xiaoxia-cmd-agent-2026"
|
|
GITEA_TOKEN = "1f8058d097e3942a9ed31c44382baf7f08311272"
|
|
GITEA_API = "https://git.xiaoxiajianji.com/api/v1"
|
|
REPO = "xiaoxia/xiaoxia-saas"
|
|
BRANCH = "ops/downgrade-runners"
|
|
|
|
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)
|
|
|
|
# 先检查Node是否已装
|
|
rc, out, err = cmd("which node && node --version 2>&1 || echo NOT_INSTALLED")
|
|
if "v20" in out:
|
|
print("Node.js 20 已经装好了:", out.strip())
|
|
else:
|
|
print("Node.js 未安装,开始安装...")
|
|
print("=" * 50)
|
|
print("步骤1: 下载Node.js 20二进制")
|
|
print("=" * 50)
|
|
rc, out, err = cmd("""
|
|
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 -2
|
|
ls -lh /tmp/node.tar.xz 2>&1
|
|
""", timeout=120)
|
|
print(f"下载 RC={rc}")
|
|
print(out[:400])
|
|
|
|
print("\n步骤2: 解压安装")
|
|
rc, out, err = cmd("""
|
|
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
|
|
""", timeout=60)
|
|
print(f"安装 RC={rc}")
|
|
print(out[:300])
|
|
|
|
print("\n" + "=" * 50)
|
|
print("步骤3: 最终验证")
|
|
print("=" * 50)
|
|
rc, out, err = cmd("node -e 'console.log(\"OK: \" + process.version)' 2>&1 && npm -v 2>&1")
|
|
print(out.strip())
|
|
|
|
print("\n" + "=" * 50)
|
|
print("步骤4: 重启6个Runner服务让环境生效")
|
|
print("=" * 50)
|
|
rc, out, err = cmd("""
|
|
for i in 1 2 3 4 5 6; do
|
|
systemctl restart act-runner-$i.service 2>&1
|
|
echo "act-runner-$i restarted"
|
|
done
|
|
sleep 3
|
|
systemctl list-units 'act-runner-*' --no-legend | head -10
|
|
""", timeout=60)
|
|
print(out[:400])
|
|
|
|
print("\n全部完成!")
|