# -*- coding: utf-8 -*- """Test PB admin auth with API env credentials; sync collections.""" from __future__ import annotations import sys import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") REMOTE = r''' import os, json, urllib.request, subprocess # load from api process env pid=subprocess.getoutput("systemctl show douyinweb-api -p MainPID --value").strip() env={} for x in open(f"/proc/{pid}/environ","rb").read().decode("utf-8","replace").split("\0"): if "=" in x: k,v=x.split("=",1); env[k]=v email=env.get("DY_PB_ADMIN_EMAIL") or "hackrobot@douyin.local" password=env.get("DY_PB_ADMIN_PASSWORD") or "Xiao4669805@" pb=env.get("DY_PB_URL") or "http://127.0.0.1:8092" print("pb", pb, "email", email, "pass_len", len(password)) for path in [ "/api/admins/auth-with-password", "/api/collections/_superusers/auth-with-password", ]: req=urllib.request.Request( pb+path, data=json.dumps({"identity":email,"password":password}).encode(), headers={"Content-Type":"application/json"}, method="POST", ) try: with urllib.request.urlopen(req, timeout=10) as r: body=r.read().decode()[:200] print(path, r.status, body) except Exception as e: print(path, "ERR", getattr(e,"code",None), e) # also try create collections via python app os.chdir("/opt/douyinWEB/backend") for k,v in env.items(): if k.startswith("DY_"): os.environ[k]=v import sys sys.path.insert(0,".") import asyncio from app import pb_sync async def main(): print("ensure", await pb_sync.ensure_collections()) print("health", await pb_sync.health()) asyncio.run(main()) ''' for label, host, port, user, pw, sudo in [ ("VPS", "107.173.30.245", 22, "root", "Xiao4669805", False), ("PHYS", "107.173.30.245", 4014, "eric", "4669805", True), ]: print(f"===== {label} =====", flush=True) c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(host, port, user, pw, timeout=40, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() with sftp.file("/tmp/_pb_auth.py", "w") as f: f.write(REMOTE.replace("\r\n", "\n")) sftp.close() cmd = "/opt/douyinWEB/backend/.venv/bin/python -u /tmp/_pb_auth.py" if sudo: cmd = f"echo '{pw}' | sudo -S -p '' {cmd}" _, o, e = c.exec_command(cmd, timeout=90) print((o.read() + e.read()).decode("utf-8", "replace")) c.close()