54 lines
2.4 KiB
Python
54 lines
2.4 KiB
Python
import os, paramiko
|
|
PW = os.environ["REMOTE_PASS"]
|
|
SECRET_ID = "AKIDy2Ln7OZaUPK5cv5GPXS9c4WpHlHdu035"
|
|
SECRET_KEY = "1CBxUmAWifQ1PYpNn9JEwTmqshJzRggS"
|
|
|
|
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()
|
|
if out: print(out, end="")
|
|
if err: print("[err]", err, end="", file=__import__("sys").stderr)
|
|
return out
|
|
|
|
# 1) 备份
|
|
run("cp /srv/news/.env /srv/news/.env.bak.$(date +%s) 2>&1")
|
|
|
|
# 2) 用 sed 替换 TENCENTCLOUD_SECRET_ID / KEY(用 | 分隔避免 / 冲突)
|
|
run(f"sed -i 's|^TENCENTCLOUD_SECRET_ID=.*|TENCENTCLOUD_SECRET_ID={SECRET_ID}|' /srv/news/.env")
|
|
run(f"sed -i 's|^TENCENTCLOUD_SECRET_KEY=.*|TENCENTCLOUD_SECRET_KEY={SECRET_KEY}|' /srv/news/.env")
|
|
|
|
# 3) 确认
|
|
print("\n--- 写入后 .env TENCENT 字段 ---")
|
|
run("grep TENCENTCLOUD /srv/news/.env")
|
|
|
|
# 4) 重启 worker + api
|
|
print("\n--- 重启 worker + api ---")
|
|
run("cd /srv/news && docker compose up -d --force-recreate --no-deps --build worker api 2>&1 | tail -8", t=120)
|
|
import time
|
|
time.sleep(8)
|
|
|
|
# 5) 测翻译(取一条没翻译好的文章,重译)
|
|
print("\n--- 找一条 pending 状态的 article ---")
|
|
aid_out = run("docker exec news-aggregator-postgres-1 psql -U news -d news -tA -c \"SELECT id FROM articles WHERE translation_status IN ('pending', 'failed') LIMIT 1;\"")
|
|
aid = aid_out.strip()
|
|
print(f" article id = {aid!r}")
|
|
|
|
if aid:
|
|
print(f"\n--- 手动重译 article {aid} ---")
|
|
run(f"cd /srv/news && docker exec news-aggregator-api-1 python -c 'import asyncio; from app.workers.pipeline import translate_article; asyncio.run(translate_article({aid}))' 2>&1 | tail -15", t=120)
|
|
|
|
# 6) 查翻译结果
|
|
print("\n--- 看翻译结果 ---")
|
|
if aid:
|
|
run(f"docker exec news-aggregator-postgres-1 psql -U news -d news -c \"SELECT id, translation_status, translation_engine, translation_chars, left(title_zh, 80) as title_zh FROM articles WHERE id = {aid};\"")
|
|
|
|
# 7) 全局统计
|
|
print("\n--- 翻译统计 ---")
|
|
run("docker exec news-aggregator-postgres-1 psql -U news -d news -c \"SELECT translation_status, translation_engine, count(*), sum(translation_chars) FROM articles GROUP BY translation_status, translation_engine ORDER BY 1, 2;\"")
|
|
c.close()
|