diff --git a/scripts/install_python310.py b/scripts/install_python310.py new file mode 100644 index 000000000..ea745c69e --- /dev/null +++ b/scripts/install_python310.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""在新服务器上源码编译安装Python 3.10,修复CMD Agent端口,重启Runner""" +import subprocess +import os +import sys + +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=600): + full_cmd = f'ssh -i {key_path} -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o ServerAliveInterval=60 root@{NEW_SERVER} "{cmd}"' + print(f"\n$ {cmd[:120]}") + result = subprocess.run(full_cmd, shell=True, capture_output=True, text=True, timeout=timeout) + if result.stdout: + lines = result.stdout.strip().split('\n') + for line in lines[:20]: + print(" ", line[:200]) + if len(lines) > 20: + print(f" ... ({len(lines)-20} more lines)") + if result.stderr: + err_lines = result.stderr.strip().split('\n') + for line in err_lines[:10]: + if "Warning" not in line and "Connection reset" not in line: + print(" ERR:", line[:200]) + print(f" [RC={result.returncode}]") + return result + +def ssh_check(cmd, timeout=600): + r = ssh(cmd, timeout) + if r.returncode != 0: + print(f"命令失败,退出: {cmd[:80]}") + sys.exit(1) + return r + +print("=" * 60) +print("步骤0: 检查当前Python版本") +print("=" * 60) +r = ssh("python3 --version 2>&1; which python3; pip3 --version 2>&1 || true") +if "Python 3.10" in r.stdout or "Python 3.10" in r.stderr: + print("Python 3.10 已安装,跳过编译") + skip_build = True +else: + skip_build = False + +if not skip_build: + print("\n" + "=" * 60) + print("步骤1: 安装编译依赖") + print("=" * 60) + ssh_check("yum install -y gcc gcc-c++ make openssl-devel bzip2-devel libffi-devel zlib-devel readline-devel sqlite-devel wget 2>&1 | tail -5", timeout=300) + + print("\n" + "=" * 60) + print("步骤2: 下载Python 3.10.14源码") + print("=" * 60) + ssh_check("cd /tmp && wget -q https://www.python.org/ftp/python/3.10.14/Python-3.10.14.tgz && echo download_ok", timeout=120) + + print("\n" + "=" * 60) + print("步骤3: 解压并编译安装(约5-10分钟)") + print("=" * 60) + ssh_check("cd /tmp && tar xzf Python-3.10.14.tgz && cd Python-3.10.14 && ./configure --prefix=/usr/local --enable-optimizations --with-ensurepip=install 2>&1 | tail -5", timeout=120) + # make -j$(nproc) 编译 + ssh_check("cd /tmp/Python-3.10.14 && make -j$(nproc) 2>&1 | tail -10", timeout=600) + ssh_check("cd /tmp/Python-3.10.14 && make altinstall 2>&1 | tail -5", timeout=300) + + print("\n" + "=" * 60) + print("步骤4: 设置为默认python3和pip3") + print("=" * 60) + ssh_check("ln -sf /usr/local/bin/python3.10 /usr/local/bin/python3 && ln -sf /usr/local/bin/pip3.10 /usr/local/bin/pip3 && echo link_ok") + +print("\n" + "=" * 60) +print("步骤5: 验证Python 3.10和pip --break-system-packages") +print("=" * 60) +ssh_check("export PATH=/usr/local/bin:$PATH && python3 --version && pip3 --version && pip3 install --help 2>&1 | grep -q 'break-system-packages' && echo 'pip3 supports --break-system-packages'") + +print("\n" + "=" * 60) +print("步骤6: 修复CMD Agent端口 5000 -> 5927") +print("=" * 60) +# 检查server.py中的端口配置 +ssh("grep -n 'port\\|PORT\\|5000\\|5927' /opt/cmd-agent/server.py 2>&1 | head -10") +# 替换端口 +ssh("sed -i 's/port=5000/port=5927/g; s/port = 5000/port = 5927/g; s/5000/5927/g' /opt/cmd-agent/server.py 2>&1 && echo port_fixed || true") +ssh("grep -n '5927\\|5000' /opt/cmd-agent/systemd/cmd-agent.service 2>/dev/null || echo 'no service file found'") +# 重启CMD Agent +ssh("systemctl restart cmd-agent 2>&1 && sleep 2 && systemctl status cmd-agent 2>&1 | head -5") +# 验证端口 +ssh("ss -tlnp 2>/dev/null | grep 5927 || netstat -tlnp 2>/dev/null | grep 5927 || echo 'checking port...'") + +print("\n" + "=" * 60) +print("步骤7: 重启6个Runner服务让新Python生效") +print("=" * 60) +for i in range(1, 7): + ssh(f"systemctl restart act-runner-{i}.service 2>&1 && echo runner-{i}_restarted || systemctl restart gitea-runner-{i}.service 2>&1 && echo runner-{i}_restarted_old || true") + +print("\n" + "=" * 60) +print("步骤8: 最终验证") +print("=" * 60) +ssh("echo '--- Python ---' && export PATH=/usr/local/bin:$PATH && python3 --version && pip3 --version") +ssh("echo '--- Runners ---' && systemctl list-units 'act-runner-*' --no-legend 2>/dev/null | head -10 || systemctl list-units 'gitea-runner-*' --no-legend 2>/dev/null | head -10") +ssh("echo '--- CMD Agent ---' && systemctl status cmd-agent 2>&1 | head -3") +ssh("echo '--- Port 5927 ---' && (ss -tlnp 2>/dev/null | grep 5927 || netstat -tlnp 2>/dev/null | grep 5927 || echo 'port check done')") + +print("\n" + "=" * 60) +print("全部完成!") +print("=" * 60)