Files
diary-news/scripts/_recover.py

37 lines
1.2 KiB
Python
Raw Normal View History

import os, paramiko
PW = os.environ["REMOTE_PASS"]
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("207.57.129.228", port=19717, username="root", password=PW, timeout=15, allow_agent=False, look_for_keys=False)
def run(cmd, t=60):
si, so, se = c.exec_command(cmd, timeout=t)
out = so.read().decode("utf-8", "replace")
err = se.read().decode("utf-8", "replace")
rc = so.channel.recv_exit_status()
print(f"$ {cmd}")
if out: print(out, end="")
if err: print("[err]", err, end="", file=__import__("sys").stderr)
print(f" rc={rc}")
return out
# 1) 服务器 pull
run("cd /srv/news && sudo -u news git pull --rebase 2>&1 | tail -5")
# 2) 重建 api + worker 容器(代码变更需要重启)
run("cd /srv/news && sg docker -c 'docker compose up -d --build --no-deps api worker' 2>&1 | tail -10")
# 3) 等启动
import time
time.sleep(15)
# 4) 跑 seed
run("cd /srv/news && sg docker -c \"docker compose exec -T api python -m app.scripts.seed_sources\" 2>&1 | tail -20")
# 5) 看 docker ps
run("cd /srv/news && sg docker -c 'docker compose ps' 2>&1 | head -15")
# 6) 健康
run("curl -s http://localhost/api/v1/healthz 2>&1")
c.close()