58 lines
2.3 KiB
Python
58 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Push new minara tokens from local bank.jsonl to prod shim and reload it.
|
|
|
|
Dedupe by token suffix; appends only new lines; restarts shim if anything changed.
|
|
"""
|
|
import json, os, subprocess, sys
|
|
|
|
HERE = os.environ.get("MINARA_HOME") or os.path.dirname(os.path.abspath(__file__))
|
|
BANK = HERE + "/bank.jsonl"
|
|
PROD = os.environ.get("MINARA_PROD", "root@127.0.0.1")
|
|
REMOTE = "/opt/minara-shim/tokens.jsonl"
|
|
|
|
|
|
def sh(args, inp=None):
|
|
p = subprocess.run(args, input=inp, capture_output=True, text=True, timeout=120)
|
|
return p.returncode, p.stdout.strip(), p.stderr.strip()[:200]
|
|
|
|
|
|
def main():
|
|
rows = []
|
|
if os.path.exists(BANK):
|
|
for l in open(BANK, encoding="utf8"):
|
|
l = l.strip()
|
|
if not l:
|
|
continue
|
|
try:
|
|
r = json.loads(l)
|
|
except Exception:
|
|
continue
|
|
if r.get("ok") and r.get("token"):
|
|
rows.append({"email": r["email"], "token": r["token"]})
|
|
if not rows:
|
|
print("bank empty")
|
|
return
|
|
rc, out, err = sh(["ssh", "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=no", PROD,
|
|
"cut -c1-0 %s >/dev/null; python3 - <<'PY'\nimport json\nseen=set()\nfor l in open('%s'):\n try: seen.add(json.loads(l)['token'])\n except Exception: pass\nprint(len(seen))\nPY" % (REMOTE, REMOTE)])
|
|
try:
|
|
remote_n = int(out.split("\n")[-1])
|
|
except Exception:
|
|
remote_n = -1
|
|
# append whole deduped set (shim dedupes nothing, so rebuild list and replace)
|
|
uniq, seen = [], set()
|
|
for r in rows:
|
|
if r["token"] in seen:
|
|
continue
|
|
seen.add(r["token"])
|
|
uniq.append(r)
|
|
payload = "\n".join(json.dumps(r, ensure_ascii=False) for r in uniq) + "\n"
|
|
rc, out, err = sh(["ssh", "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=no", PROD,
|
|
("cp -f %s %s.bak 2>/dev/null; cat > %s.tmp && mv -f %s.tmp %s && "
|
|
"systemctl restart minara-shim && sleep 2 && "
|
|
"curl -s -m 10 http://127.0.0.1:48140/health") % (REMOTE, REMOTE, REMOTE, REMOTE, REMOTE)],
|
|
inp=payload)
|
|
print("local=%d remote_before=%s rc=%d %s %s" % (len(uniq), remote_n, rc, out[:200], err))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |