import os, paramiko, urllib.request, urllib.error, 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=15): 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 # 服务器 pull + 重建 api run("cd /srv/news && sudo -u news git pull --rebase 2>&1 | tail -3") run("cd /srv/news && sg docker -c 'docker compose up -d --force-recreate --no-deps --build api' 2>&1 | tail -5", t=120) # 重设 owner 角色为 owner run("cd /srv/news && sg docker -c \"docker compose exec -T postgres psql -U news -d news -c 'UPDATE users SET role = '\\''owner'\\'' WHERE username = '\\''owner'\\'';'\" 2>&1 | tail -3") # 触发一次抓取(等久点) print("\n=== 触发抓取(等 60 秒)===") import time run("cd /srv/news && sg docker -c \"docker compose exec -T worker python -c 'import asyncio; from app.workers.pipeline import run_once; asyncio.run(run_once())'\" 2>&1 | tail -15", t=180) time.sleep(5) # 查 article 数 run("cd /srv/news && sg docker -c \"docker compose exec -T postgres psql -U news -d news -c 'SELECT count(*) AS total, count(title_zh) AS translated FROM articles;'\" 2>&1 | tail -5") # 拿新密码 new_pw = "owner_pass_2026" # 登录 + 拉 req = urllib.request.Request( "http://localhost/api/v1/auth/login", data=json.dumps({"username": "owner", "password": new_pw}).encode(), headers={"Content-Type": "application/json"}, ) try: resp = urllib.request.urlopen(req, timeout=10) data = json.loads(resp.read()) print(f"\n=== 登录 OK!token 前 40: {data['access_token'][:40]} ===") # 测拉 articles req2 = urllib.request.Request( "http://localhost/api/v1/articles?limit=5", headers={"Authorization": f"Bearer {data['access_token']}"}, ) resp2 = urllib.request.urlopen(req2, timeout=10) ad = json.loads(resp2.read()) print(f" articles: {len(ad.get('items', []))} 条") for a in ad.get("items", [])[:3]: print(f" - {a['source']['name']:20s} [{a['translation_status']:7s}] {a['title'][:50]}") if a.get("title_zh"): print(f" zh: {a['title_zh'][:50]}") # 测 /me req3 = urllib.request.Request( "http://localhost/api/v1/me", headers={"Authorization": f"Bearer {data['access_token']}"}, ) me = json.loads(urllib.request.urlopen(req3, timeout=10).read()) print(f"\n /me: {me}") # 测 /me/usage req4 = urllib.request.Request( "http://localhost/api/v1/me/usage", headers={"Authorization": f"Bearer {data['access_token']}"}, ) usage = json.loads(urllib.request.urlopen(req4, timeout=10).read()) print(f" /me/usage: {usage}") except urllib.error.HTTPError as e: print(f"\n[FAIL] {e.code}") print(e.read().decode()) except Exception as e: print(f"\n[ERR] {e}") c.close()