"""Sync local crowllm bank -> prod crowl-shim keys + reload. Safe to run repeatedly.""" import json, os, subprocess, sys, time LOCAL = "C:/Users/User/tmp/crowllm/crowllm_bank.jsonl" TMP = "C:/Users/User/tmp/crowllm/keys.jsonl" KEY = "C:/Users/User/.ssh/id_ed25519" HOST = "root@2.27.98.196" SSH = ["ssh", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=20", "-i", KEY, HOST] def log(*a): print(" ".join(str(x) for x in a), flush=True) def main(): if not os.path.exists(LOCAL): log("no bank") return 0 rows, seen = [], set() for line in open(LOCAL, encoding="utf-8"): line = line.strip() if not line: continue try: j = json.loads(line) except Exception: continue k = j.get("key") if isinstance(k, dict): k = k.get("key") if not k or k in seen: continue seen.add(k) rows.append({"key": k, "email": j.get("email"), "uid": j.get("uid"), "status": "active"}) if not rows: log("empty bank") return 0 with open(TMP, "w", encoding="utf-8") as f: for r in rows: f.write(json.dumps(r, ensure_ascii=False) + "\n") p = subprocess.run(["scp", "-o", "StrictHostKeyChecking=no", "-i", KEY, TMP, HOST + ":/opt/crowl-shim/keys.jsonl"], capture_output=True, text=True) if p.returncode != 0: log("scp failed", p.stderr[:200]) return 1 p = subprocess.run(SSH + ["curl -s -X POST --max-time 15 http://127.0.0.1:48974/admin/reload; echo; " "curl -s --max-time 10 http://127.0.0.1:48974/healthz"], capture_output=True, text=True) log("remote:", (p.stdout or "").strip().replace("\n", " | ")[:200]) log("synced keys:", len(rows)) return 0 if __name__ == "__main__": sys.exit(main())