admin.nomadro.com gets sidebar L1/L2 social ops (paginated lists + entity detail); API adds soft-delete archives/stats and shared nomadro_pb for social/coffee/notify/work. Co-authored-by: Cursor <cursoragent@cursor.com>
187 lines
6.6 KiB
Python
187 lines
6.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Push PocketBase social_ugc admin stack to US nomadmini-api + social_svc.
|
|
|
|
admin.nomadro.com Social page needs:
|
|
GET /api/admin/social/ugc/{stats,archives,detail/...}
|
|
and soft-delete via social_svc.pb_data → PocketBase.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import sys
|
|
import tarfile
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
|
|
|
|
ROOT = Path(__file__).resolve().parents[3]
|
|
US = ("108.174.50.68", "root", "Xiao4669805")
|
|
REMOTE_APP = "/opt/nomadmini/backend/app"
|
|
REMOTE_API = "/var/www/nomadro.cn/apps/api"
|
|
REMOTE_BACKEND = "/opt/nomadmini/backend"
|
|
|
|
LOCAL_UGC = ROOT / "apps/api/services/ebook_svc/services/social_ugc.py"
|
|
LOCAL_ADMIN = ROOT / "apps/api/services/ebook_svc/routers/social_ugc_admin.py"
|
|
LOCAL_SVC = ROOT / "apps/api/services/social_svc"
|
|
LOCAL_PB = ROOT / "apps/api/services/nomadro_pb"
|
|
|
|
|
|
def to_app(text: str) -> str:
|
|
return text.replace("ebook_svc.", "app.")
|
|
|
|
|
|
def connect() -> paramiko.SSHClient:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
h, u, p = US
|
|
c.connect(h, 22, u, p, timeout=45, allow_agent=False, look_for_keys=False, banner_timeout=90)
|
|
t = c.get_transport()
|
|
if t:
|
|
t.set_keepalive(15)
|
|
return c
|
|
|
|
|
|
def sh(c: paramiko.SSHClient, cmd: str, timeout: int = 240) -> tuple[int, str]:
|
|
print(f"$ {cmd[:240].replace(chr(10), ' ')}", flush=True)
|
|
_, o, e = c.exec_command(cmd, timeout=timeout)
|
|
out = o.read().decode("utf-8", "replace")
|
|
err = e.read().decode("utf-8", "replace")
|
|
code = o.channel.recv_exit_status()
|
|
text = out + (("\n" + err) if err.strip() else "")
|
|
if text.strip():
|
|
print(text[-8000:], flush=True)
|
|
print("exit", code, flush=True)
|
|
return code, text
|
|
|
|
|
|
def pack() -> bytes:
|
|
buf = io.BytesIO()
|
|
with tarfile.open(fileobj=buf, mode="w:gz") as tar:
|
|
for src, name in (
|
|
(LOCAL_UGC, "api/services/social_ugc.py"),
|
|
(LOCAL_ADMIN, "api/routers/social_ugc_admin.py"),
|
|
):
|
|
if not src.is_file():
|
|
raise SystemExit(f"missing {src}")
|
|
text = to_app(src.read_text(encoding="utf-8"))
|
|
if "ebook_svc." in text:
|
|
raise SystemExit(f"rewrite failed: {src}")
|
|
data = text.encode("utf-8")
|
|
info = tarfile.TarInfo(name=name)
|
|
info.size = len(data)
|
|
tar.addfile(info, io.BytesIO(data))
|
|
tar.add(LOCAL_SVC, arcname="services/social_svc")
|
|
tar.add(LOCAL_PB, arcname="services/nomadro_pb")
|
|
buf.seek(0)
|
|
return buf.read()
|
|
|
|
|
|
def main() -> int:
|
|
if not LOCAL_SVC.is_dir() or not LOCAL_PB.is_dir():
|
|
raise SystemExit("missing social_svc or nomadro_pb")
|
|
blob = pack()
|
|
print(f"pack {len(blob)} bytes", flush=True)
|
|
|
|
c = connect()
|
|
try:
|
|
sftp = c.open_sftp()
|
|
with sftp.file("/tmp/social_ugc_admin_us.tgz", "wb") as rf:
|
|
rf.write(blob)
|
|
sftp.close()
|
|
|
|
code, _ = sh(
|
|
c,
|
|
f"""
|
|
set -euo pipefail
|
|
mkdir -p /tmp/social_ugc_extract
|
|
rm -rf /tmp/social_ugc_extract/*
|
|
tar -xzf /tmp/social_ugc_admin_us.tgz -C /tmp/social_ugc_extract
|
|
TS=$(date +%Y%m%d%H%M%S)
|
|
|
|
# 1) social_svc + nomadro_pb → social API tree
|
|
mkdir -p {REMOTE_API}/services
|
|
[ -d {REMOTE_API}/services/social_svc ] && cp -a {REMOTE_API}/services/social_svc /tmp/social_svc.bak.$TS || true
|
|
rsync -a --delete /tmp/social_ugc_extract/services/social_svc/ {REMOTE_API}/services/social_svc/
|
|
rsync -a --delete /tmp/social_ugc_extract/services/nomadro_pb/ {REMOTE_API}/services/nomadro_pb/
|
|
|
|
# 2) same packages on nomadmini PYTHONPATH (import social_svc / nomadro_pb)
|
|
rsync -a --delete /tmp/social_ugc_extract/services/social_svc/ {REMOTE_BACKEND}/social_svc/
|
|
rsync -a --delete /tmp/social_ugc_extract/services/nomadro_pb/ {REMOTE_BACKEND}/nomadro_pb/
|
|
# also under services/ for social_ugc parents[2] path probe
|
|
mkdir -p {REMOTE_BACKEND}/services
|
|
rsync -a --delete /tmp/social_ugc_extract/services/social_svc/ {REMOTE_BACKEND}/services/social_svc/
|
|
rsync -a --delete /tmp/social_ugc_extract/services/nomadro_pb/ {REMOTE_BACKEND}/services/nomadro_pb/
|
|
|
|
# 3) hot-patch ugc modules (ebook_svc→app already rewritten)
|
|
cp -a {REMOTE_APP}/services/social_ugc.py {REMOTE_APP}/services/social_ugc.py.bak-$TS 2>/dev/null || true
|
|
cp -a {REMOTE_APP}/routers/social_ugc_admin.py {REMOTE_APP}/routers/social_ugc_admin.py.bak-$TS 2>/dev/null || true
|
|
cp /tmp/social_ugc_extract/api/services/social_ugc.py {REMOTE_APP}/services/social_ugc.py
|
|
cp /tmp/social_ugc_extract/api/routers/social_ugc_admin.py {REMOTE_APP}/routers/social_ugc_admin.py
|
|
|
|
# 4) ensure PB URL in nomadmini env (do not clobber secrets)
|
|
ENVF={REMOTE_BACKEND}/.env
|
|
touch "$ENVF"
|
|
grep -q '^PB_URL=' "$ENVF" || echo 'PB_URL=http://127.0.0.1:8090' >> "$ENVF"
|
|
grep -q '^POCKETBASE_URL=' "$ENVF" || echo 'POCKETBASE_URL=http://127.0.0.1:8090' >> "$ENVF"
|
|
|
|
# 5) import smoke
|
|
cd {REMOTE_BACKEND}
|
|
.venv/bin/python - <<'PY'
|
|
import sys
|
|
sys.path.insert(0, ".")
|
|
sys.path.insert(0, "services")
|
|
from app.routers import social_ugc_admin
|
|
from app.services import social_ugc
|
|
paths = sorted({{getattr(r, "path", "") for r in social_ugc_admin.router.routes}})
|
|
need = [
|
|
"/api/admin/social/ugc/stats",
|
|
"/api/admin/social/ugc/archives",
|
|
"/api/admin/social/ugc/detail/{{entity}}/{{entity_id}}",
|
|
"/api/admin/social/ugc/overview",
|
|
]
|
|
for p in need:
|
|
assert p in paths, (p, paths)
|
|
print("ugc_routes_ok", len(paths))
|
|
print("backend", social_ugc.available())
|
|
PY
|
|
|
|
# 6) restart APIs
|
|
systemctl kill -s9 nomadro-social.service 2>/dev/null || true
|
|
fuser -k 8020/tcp 2>/dev/null || true
|
|
sleep 1
|
|
systemctl reset-failed nomadro-social 2>/dev/null || true
|
|
systemctl start nomadro-social 2>/dev/null || true
|
|
systemctl restart nomadmini-api
|
|
sleep 3
|
|
systemctl is-active nomadmini-api
|
|
systemctl is-active nomadro-social || true
|
|
curl -sS -o /dev/null -w 'social_health:%{{http_code}}\\n' --max-time 8 http://127.0.0.1:8020/api/social/v1/health || true
|
|
curl -sS -o /dev/null -w 'api_health:%{{http_code}}\\n' --max-time 8 http://127.0.0.1:8010/docs || true
|
|
|
|
TOKEN=$(curl -sS --max-time 12 -X POST https://api.nomadro.com/api/admin/login \\
|
|
-H 'content-type: application/json' \\
|
|
-d '{{"username":"hackrobot","password":"Xiao4669805@"}}' | python3 -c 'import sys,json; print(json.load(sys.stdin).get("token",""))')
|
|
echo "token_len ${{#TOKEN}}"
|
|
for path in overview stats archives; do
|
|
code=$(curl -sS -o /tmp/ugc_$path.json -w '%{{http_code}}' --max-time 15 \\
|
|
-H "Authorization: Bearer $TOKEN" \\
|
|
"https://api.nomadro.com/api/admin/social/ugc/$path")
|
|
echo "ugc_$path $code"
|
|
head -c 220 /tmp/ugc_$path.json; echo
|
|
done
|
|
curl -sk -o /dev/null -w 'admin_com %{{http_code}}\\n' https://admin.nomadro.com/
|
|
echo PUSH_SOCIAL_UGC_ADMIN_US_OK
|
|
""",
|
|
)
|
|
return code
|
|
finally:
|
|
c.close()
|
|
print("done", time.strftime("%H:%M:%S"), flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|