#!/usr/bin/env python3 """Сбор coarena-ключей и заливка на прод-шим (coa-shim). Молчит, если новых нет.""" import json import os import subprocess BASE = r"C:/Users/User/tmp/coarena" OUT = os.path.join(BASE, "coa_keys_sync.txt") SSH = ["ssh", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=25", "-i", r"C:/Users/User/.ssh/id_ed25519", "root@2.27.98.196"] keys = [] seen = set() for f in ("keys_bank2.jsonl", "keys_bank.jsonl"): p = os.path.join(BASE, f) if not os.path.exists(p): continue for line in open(p, encoding="utf8"): line = line.strip() if not line: continue try: r = json.loads(line) except Exception: continue if not isinstance(r, dict): continue k = r.get("key") if not k or not str(k).startswith("coa_") or k in seen: continue seen.add(k) keys.append("%s|%s" % ((r.get("email") or "coa")[:30], k)) if len(keys) == int(open(os.path.join(BASE, ".last_sync_count")).read()) if os.path.exists(os.path.join(BASE, ".last_sync_count")) else -1: print("no new keys") raise SystemExit open(OUT, "w", encoding="utf8").write("\n".join(keys) + "\n") scp = subprocess.run(["scp", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=25", "-i", r"C:/Users/User/.ssh/id_ed25519", OUT, "root@2.27.98.196:/opt/coa-shim/coa_keys.txt"], capture_output=True, text=True, timeout=120) if scp.returncode: print("scp fail", (scp.stderr or "")[-100:]) raise SystemExit(1) r = subprocess.run(SSH + ["systemctl restart coa-shim && sleep 3 && curl -s -m 5 http://127.0.0.1:48960/health"], capture_output=True, text=True, timeout=90) open(os.path.join(BASE, ".last_sync_count"), "w").write(str(len(keys))) print("synced", len(keys), (r.stdout or "").strip()[:120])