98 lines
3.0 KiB
Python
98 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""SSH到新服务器,检查CMD Agent并安装Node.js"""
|
|
import subprocess, os
|
|
|
|
NEW_SERVER = "172.30.18.199"
|
|
KEY_CONTENT = """-----BEGIN OPENSSH PRIVATE KEY-----
|
|
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
|
QyNTUxOQAAACD6GquAk5vBEfbtaJCTSfnQEhjzC8e5GF60hSbPP0BJbAAAAJiQxGNokMRj
|
|
aAAAAAtzc2gtZWQyNTUxOQAAACD6GquAk5vBEfbtaJCTSfnQEhjzC8e5GF60hSbPP0BJbA
|
|
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, 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("=" * 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全部完成!")
|