import os, paramiko, base64, json 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=120): 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 rpw = run("grep ^REDIS_PASSWORD /srv/news/.env | cut -d= -f2").strip() # 1) 服务器 pull print("--- pull ---") run("cd /srv/news && sudo -u news git pull --rebase 2>&1 | tail -3") # 2) 重建 worker + api print("--- 重建 ---") 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) # 3) 重置 usage = 0 run(f"docker exec news-aggregator-redis-1 redis-cli -a '{rpw}' DEL 'translation:month:202606' 2>&1 | grep -v Warning") print("--- usage reset to 0 ---") # 4) 把 5 篇文章重置为 pending 触发翻译 print("--- 触发翻译(5 篇)---") run("docker exec news-aggregator-postgres-1 psql -U news -d news -c \"UPDATE articles SET translation_status = 'pending' WHERE id IN (SELECT id FROM articles WHERE translation_status = 'ok' ORDER BY id LIMIT 5);\" 2>&1 | tail -2") # 5) 跑 worker pipeline 重译 script_b64 = base64.b64encode(b''' import asyncio from app.workers.pipeline import translate_article from app.database import AsyncSessionLocal from app.models.article import Article from sqlalchemy import select async def main(): async with AsyncSessionLocal() as s: rows = (await s.execute(select(Article.id).where(Article.translation_status == 'pending').limit(10))).all() ids = [r[0] for r in rows] print(f"translating {len(ids)} pending") for aid in ids: await translate_article(aid) asyncio.run(main()) ''').decode() run(f"docker exec news-aggregator-worker-1 sh -c 'echo {script_b64} | base64 -d > /app/_tt.py'") run("docker exec -w /app news-aggregator-worker-1 python /app/_tt.py 2>&1 | tail -10", t=120) # 6) 看 usage print("\n--- redis usage ---") out = run(f"docker exec news-aggregator-redis-1 redis-cli -a '{rpw}' GET 'translation:month:202606' 2>&1 | grep -v Warning") print(f" usage: {out.strip()}") # 7) /me/usage out = run("curl -s -X POST http://localhost/api/v1/auth/login -H 'Content-Type: application/json' -d '{\"username\":\"owner\",\"password\":\"Owner2026!\"}'") token = json.loads(out)["access_token"] u = json.loads(run(f"curl -s -H 'Authorization: Bearer {token}' 'http://localhost/api/v1/me/usage'")) print(f"--- /me/usage ---\n {u}") # 8) 容器状态 print("\n--- docker ps ---") run("docker ps --format 'table {{.Names}}\\t{{.Status}}\\t{{.Ports}}' 2>&1 | tail -10") # 9) 翻译后统计 print("\n--- 翻译统计 ---") run("docker exec news-aggregator-postgres-1 psql -U news -d news -c \"SELECT translation_status, count(*) FROM articles GROUP BY 1 ORDER BY 1;\"") c.close()