Files

53 lines
1.8 KiB
Python

#!/usr/bin/env python3
"""Sync numes keys bank -> prod shim /opt/numes-shim/keys.json (shim re-reads by mtime)."""
import json
import os
import subprocess
import sys
ROOT = os.environ.get("NUMES_ROOT", os.path.dirname(os.path.abspath(__file__)))
BANK = ROOT + "/bank.jsonl"
LOCAL = ROOT + "/keys.json"
SSH_KEY = os.environ.get("NUMES_SSH_KEY", os.path.expanduser("~/.ssh/id_ed25519"))
HOST = os.environ.get("NUMES_PROD", "root@host")
REMOTE = os.environ.get("NUMES_REMOTE_KEYS", "/opt/numes-shim/keys.json")
def main():
keys = []
if os.path.exists(BANK):
with open(BANK, encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
k = json.loads(line).get("key", "")
except Exception:
continue
if k and k not in keys:
keys.append(k)
if not keys:
print("no keys in bank")
return 0
with open(LOCAL, "w", encoding="utf-8") as f:
json.dump(keys, f)
r = subprocess.run(
["scp", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=25",
"-i", SSH_KEY, LOCAL, HOST + ":" + REMOTE],
capture_output=True, text=True, timeout=120,
)
if r.returncode != 0:
print("scp failed:", (r.stderr or "")[:300])
return 1
chk = subprocess.run(
["ssh", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=25",
"-i", SSH_KEY, HOST, "python3 -c \"import json;print(len(json.load(open('%s'))))\"" % REMOTE],
capture_output=True, text=True, timeout=90,
)
print("synced keys=%d remote=%s" % (len(keys), (chk.stdout or "").strip()))
return 0
if __name__ == "__main__":
sys.exit(main())