121 lines
4.0 KiB
Python
121 lines
4.0 KiB
Python
import subprocess
|
|
import os
|
|
|
|
key = "\n".join([
|
|
"-----BEGIN OPENSSH PRIVATE KEY-----",
|
|
"b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW",
|
|
"QyNTUxOQAAACD6GquAk5vBEfbtaJCTSfnQEhjzC8e5GF60hSbPP0BJbAAAAJiQxGNokMRj",
|
|
"aAAAAAtzc2gtZWQyNTUxOQAAACD6GquAk5vBEfbtaJCTSfnQEhjzC8e5GF60hSbPP0BJbA",
|
|
"AAAED2muzuU4BAiCqbg0ayGxgiDvfS/xI1SFvb32oLzTnn8/oaq4CTm8ER9u1okJNJ+dAS",
|
|
"GPMLx7kYXrSFJs8/QElsAAAAFHJ1bm5lci1hZG1pbkB4aWFveGlhAQ==",
|
|
"-----END OPENSSH PRIVATE KEY-----",
|
|
"",
|
|
])
|
|
|
|
key_path = "/tmp/ns_key"
|
|
with open(key_path, "w") as f:
|
|
f.write(key)
|
|
os.chmod(key_path, 0o600)
|
|
|
|
def ssh_run(cmd):
|
|
r = subprocess.run(
|
|
["ssh", "-i", key_path, "-o", "StrictHostKeyChecking=no",
|
|
"-o", "ConnectTimeout=30", "root@172.30.18.199", cmd],
|
|
capture_output=True, text=True
|
|
)
|
|
return r.stdout, r.stderr, r.returncode
|
|
|
|
# 1. 查看当前状态
|
|
print("=== Step 1: Current runner status ===")
|
|
out, err, rc = ssh_run("""for i in 1 2 3 4 5; do
|
|
echo "--- runner-$i ---"
|
|
/opt/act-runner/runner-$i/act_runner --version 2>&1 | head -1
|
|
grep -E "mode:|container_mode:" /opt/act-runner/runner-$i/config.yml 2>/dev/null || echo " no config.yml"
|
|
systemctl is-active gitea-runner-$i 2>/dev/null || echo " service not found"
|
|
done""")
|
|
print(out)
|
|
if err.strip():
|
|
print("stderr:", err.strip()[:200])
|
|
|
|
# 2. 停掉所有Runner服务
|
|
print("\n=== Step 2: Stop all runner services ===")
|
|
out, err, rc = ssh_run("for i in 1 2 3 4 5; do systemctl stop gitea-runner-$i 2>/dev/null; echo \"stopped runner-$i\"; done")
|
|
print(out)
|
|
|
|
# 3. 改config.yml为host模式
|
|
print("\n=== Step 3: Change to host mode ===")
|
|
out, err, rc = ssh_run("""for i in 1 2 3 4 5; do
|
|
if [ -f /opt/act-runner/runner-$i/config.yml ]; then
|
|
sed -i 's/mode: docker/mode: host/g' /opt/act-runner/runner-$i/config.yml
|
|
sed -i 's/container_mode: docker/container_mode: host/g' /opt/act-runner/runner-$i/config.yml
|
|
echo "runner-$i: $(grep -E '^ mode:' /opt/act-runner/runner-$i/config.yml)"
|
|
else
|
|
echo "runner-$i: no config"
|
|
fi
|
|
done""")
|
|
print(out)
|
|
|
|
# 4. 注册第6个Runner
|
|
print("\n=== Step 4: Create runner-6 and register all 6 as repo-level ===")
|
|
reg_cmd = '''
|
|
mkdir -p /opt/act-runner/runner-6
|
|
cp /opt/act-runner/runner-1/act_runner /opt/act-runner/runner-6/
|
|
cp /opt/act-runner/runner-1/config.yml /opt/act-runner/runner-6/
|
|
sed -i 's/mode: docker/mode: host/g' /opt/act-runner/runner-6/config.yml
|
|
sed -i 's/container_mode: docker/container_mode: host/g' /opt/act-runner/runner-6/config.yml
|
|
|
|
# 清理旧的注册信息,重新注册
|
|
for i in 1 2 3 4 5 6; do
|
|
cd /opt/act-runner/runner-$i
|
|
rm -f .runner
|
|
./act_runner register \\
|
|
--instance https://git.xiaoxiajianji.com/ \\
|
|
--token ZMFH02WcdElay5ATmhhQYe1WNTESO9XMXcF0H9Tj \\
|
|
--name "saas-runner-$i" \\
|
|
--labels "saas,runtime-builder,host,ubuntu-latest,ubuntu-22.04" \\
|
|
--no-interactive 2>&1
|
|
echo "registered runner-$i, exit=$?"
|
|
done
|
|
'''
|
|
out, err, rc = ssh_run(reg_cmd)
|
|
print(out[-2000:] if len(out) > 2000 else out)
|
|
if err.strip():
|
|
print("stderr:", err.strip()[-500:])
|
|
|
|
# 5. 创建/更新systemd服务并启动
|
|
print("\n=== Step 5: Create systemd services and start ===")
|
|
svc_cmd = '''
|
|
for i in 1 2 3 4 5 6; do
|
|
cat > /etc/systemd/system/gitea-runner-$i.service << EOF
|
|
[Unit]
|
|
Description=Gitea Actions Runner $i
|
|
After=network.target
|
|
|
|
[Service]
|
|
WorkingDirectory=/opt/act-runner/runner-$i
|
|
ExecStart=/opt/act-runner/runner-$i/act_runner daemon
|
|
Restart=always
|
|
RestartSec=5
|
|
User=root
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
systemctl daemon-reload
|
|
systemctl enable gitea-runner-$i
|
|
systemctl start gitea-runner-$i
|
|
echo "started runner-$i"
|
|
done
|
|
'''
|
|
out, err, rc = ssh_run(svc_cmd)
|
|
print(out)
|
|
|
|
# 6. 验证状态
|
|
print("\n=== Step 6: Verify all running ===")
|
|
import time
|
|
time.sleep(5)
|
|
out, err, rc = ssh_run("systemctl status gitea-runner-{1,2,3,4,5,6} --no-pager | grep -E 'Active:|gitea-runner' | head -20")
|
|
print(out)
|
|
|
|
os.unlink(key_path)
|
|
print("\n=== DONE ===") |