Include hub Next API routes and admin module select so the China-only Hub ship can build and mount on nomadro.com. Co-authored-by: Cursor <cursoragent@cursor.com>
247 lines
8.0 KiB
Python
247 lines
8.0 KiB
Python
"""Deploy Nomadro Hub API backend to US VPS only (no frontend)."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import tarfile
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
HOST, USER, PASSWORD = "108.174.50.68", "root", "Xiao4669805"
|
|
REMOTE_API = "/var/www/nomadro.cn/apps/api"
|
|
VENV_PY = f"{REMOTE_API}/.venv-social/bin/python"
|
|
|
|
INCLUDE = [
|
|
"hub_main.py",
|
|
"services/hub_svc",
|
|
"services/nomadro_pb/schemas.py",
|
|
"services/ebook_svc/services/hub_experience.py",
|
|
"services/ebook_svc/services/hub_ugc.py",
|
|
"services/ebook_svc/services/hub_locale_geo.py",
|
|
"services/ebook_svc/services/hub_blog_ops.py",
|
|
"services/ebook_svc/services/hub_ops.py",
|
|
"services/ebook_svc/services/hub_join_config.py",
|
|
"services/ebook_svc/routers/hub_ugc_admin.py",
|
|
"services/ebook_svc/routers/hub_locale_geo_admin.py",
|
|
"services/ebook_svc/routers/hub_blog_admin.py",
|
|
]
|
|
|
|
SERVICE = r"""[Unit]
|
|
Description=Nomadro Hub API (independent fork of social)
|
|
After=network.target
|
|
|
|
[Service]
|
|
EnvironmentFile=-/etc/nomadro/centrifugo.env
|
|
EnvironmentFile=-/etc/nomadro/portal.env
|
|
Type=simple
|
|
WorkingDirectory=/var/www/nomadro.cn/apps/api
|
|
Environment=HOST=127.0.0.1
|
|
Environment=PORT=8025
|
|
Environment=HUB_ENABLED=true
|
|
Environment=HUB_DB_PATH=/var/www/nomadro-data/hub/hub.db
|
|
Environment=PYTHONPATH=/var/www/nomadro.cn/apps/api:/var/www/nomadro.cn/apps/api/services
|
|
Environment=PB_URL=http://127.0.0.1:8090
|
|
Environment=POCKETBASE_URL=http://127.0.0.1:8090
|
|
Environment=PB_ADMIN_EMAIL=xiaoshuang.eric@gmail.com
|
|
Environment=PB_ADMIN_PASSWORD=Xiao4669805@
|
|
Environment=POCKETBASE_EMAIL=xiaoshuang.eric@gmail.com
|
|
Environment=POCKETBASE_PASSWORD=Xiao4669805@
|
|
Environment=S3_ENABLED=true
|
|
Environment=S3_PUBLIC_URL=https://s3.nomadro.com
|
|
Environment=S3_ENDPOINT=http://127.0.0.1:8333
|
|
Environment=S3_BUCKET=nomadweb
|
|
Environment=S3_ACCESS_KEY=nomadweb
|
|
Environment=S3_SECRET_KEY=Xiao4669805@
|
|
ExecStart=/var/www/nomadro.cn/apps/api/.venv-social/bin/python -m uvicorn hub_main:app --host 127.0.0.1 --port 8025
|
|
Restart=on-failure
|
|
RestartSec=5
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
"""
|
|
|
|
NGINX_SNIPPET = r"""
|
|
# Nomadro Hub API (independent)
|
|
location /api/hub/ {
|
|
proxy_pass http://127.0.0.1:8025;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_read_timeout 120s;
|
|
}
|
|
"""
|
|
|
|
|
|
def pack() -> Path:
|
|
api = ROOT / "apps" / "api"
|
|
tmp = Path(tempfile.mkdtemp(prefix="hub-be-"))
|
|
tar_path = tmp / "hub-backend.tgz"
|
|
with tarfile.open(tar_path, "w:gz") as tar:
|
|
for rel in INCLUDE:
|
|
src = api / rel
|
|
if not src.exists():
|
|
raise FileNotFoundError(src)
|
|
tar.add(src, arcname=rel)
|
|
return tar_path
|
|
|
|
|
|
def main() -> int:
|
|
tar_path = pack()
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(HOST, 22, USER, PASSWORD, timeout=30, allow_agent=False, look_for_keys=False)
|
|
sftp = c.open_sftp()
|
|
remote_tar = "/tmp/hub-backend.tgz"
|
|
sftp.put(str(tar_path), remote_tar)
|
|
print("uploaded", tar_path.name)
|
|
|
|
def run(cmd: str, timeout: int = 120) -> str:
|
|
print(">>", cmd[:160])
|
|
_, stdout, stderr = c.exec_command(cmd, timeout=timeout)
|
|
code = stdout.channel.recv_exit_status()
|
|
out = stdout.read().decode("utf-8", "replace")
|
|
err = stderr.read().decode("utf-8", "replace")
|
|
if out.strip():
|
|
print(out[-3000:])
|
|
if err.strip():
|
|
print("ERR", err[-1500:])
|
|
if code != 0:
|
|
raise RuntimeError(f"cmd failed ({code}): {cmd}")
|
|
return out
|
|
|
|
run(f"mkdir -p {REMOTE_API}/services /var/www/nomadro-data/hub/uploads")
|
|
run(f"tar -xzf {remote_tar} -C {REMOTE_API}")
|
|
|
|
# Patch ebook_svc main + app main on server if hub routers not included
|
|
run(
|
|
r"""python3 - <<'PY'
|
|
from pathlib import Path
|
|
paths = [
|
|
Path('/var/www/nomadro.cn/apps/api/services/ebook_svc/main.py'),
|
|
Path('/var/www/nomadro.cn/apps/api/app/main.py'),
|
|
]
|
|
needle_import = 'from ebook_svc.routers import hub_ugc_admin'
|
|
for p in paths:
|
|
if not p.exists():
|
|
print('skip missing', p); continue
|
|
t = p.read_text(encoding='utf-8')
|
|
if needle_import in t:
|
|
print('already wired', p); continue
|
|
# insert after social_blog_admin include if present
|
|
if 'social_blog_admin' in t and 'hub_ugc_admin' not in t:
|
|
t = t.replace(
|
|
'from ebook_svc.routers import social_blog_admin',
|
|
'from ebook_svc.routers import social_blog_admin\nfrom ebook_svc.routers import hub_ugc_admin\nfrom ebook_svc.routers import hub_locale_geo_admin\nfrom ebook_svc.routers import hub_blog_admin',
|
|
)
|
|
t = t.replace(
|
|
'app.include_router(social_blog_admin.router)',
|
|
'app.include_router(social_blog_admin.router)\napp.include_router(hub_ugc_admin.router)\napp.include_router(hub_locale_geo_admin.router)\napp.include_router(hub_blog_admin.router)',
|
|
)
|
|
# app/main.py style bundle import
|
|
if 'social_ugc_admin,' in t and 'hub_ugc_admin,' not in t:
|
|
t = t.replace(
|
|
'social_blog_admin,\n community_admin,',
|
|
'social_blog_admin,\n hub_ugc_admin,\n hub_locale_geo_admin,\n hub_blog_admin,\n community_admin,',
|
|
)
|
|
p.write_text(t, encoding='utf-8')
|
|
print('patched', p)
|
|
else:
|
|
print('manual check', p)
|
|
PY"""
|
|
)
|
|
|
|
# systemd unit
|
|
sftp.file("/etc/systemd/system/nomadro-hub.service", "w").write(SERVICE)
|
|
run("systemctl daemon-reload && systemctl enable --now nomadro-hub.service")
|
|
run("systemctl restart nomadro-hub.service; sleep 2; systemctl is-active nomadro-hub.service")
|
|
|
|
# ensure hub collections via social venv
|
|
run(
|
|
f"""{VENV_PY} - <<'PY'
|
|
import os, sys
|
|
sys.path[:0] = ['{REMOTE_API}', '{REMOTE_API}/services']
|
|
os.environ['PB_URL']='http://127.0.0.1:8090'
|
|
os.environ['POCKETBASE_URL']='http://127.0.0.1:8090'
|
|
os.environ['PB_ADMIN_EMAIL']='xiaoshuang.eric@gmail.com'
|
|
os.environ['PB_ADMIN_PASSWORD']='Xiao4669805@'
|
|
from nomadro_pb.ensure import ensure_selected
|
|
names={{
|
|
'hub_roster','hub_liveness_audits','hub_skill_posts','hub_meetup_reviews',
|
|
'hub_blocks','hub_reports','hub_messages','hub_channel_meta','hub_channel_reads',
|
|
'hub_files','hub_trips','hub_cafe_reviews','hub_feedback','hub_join_leads',
|
|
'hub_gig_apps','hub_archives',
|
|
}}
|
|
print('ensure', ensure_selected(names))
|
|
PY""",
|
|
timeout=180,
|
|
)
|
|
|
|
# nginx: inject snippet if missing
|
|
run(
|
|
r"""python3 - <<'PY'
|
|
from pathlib import Path
|
|
roots = list(Path('/etc/nginx').rglob('*.conf'))
|
|
hit = None
|
|
for p in roots:
|
|
try:
|
|
t = p.read_text(encoding='utf-8', errors='ignore')
|
|
except Exception:
|
|
continue
|
|
if 'nomadro.com' in t and 'server_name' in t and 'ssl' in t.lower():
|
|
if '/api/social/' in t or 'proxy_pass http://127.0.0.1:8020' in t:
|
|
hit = p
|
|
break
|
|
print('candidate', hit)
|
|
if not hit:
|
|
raise SystemExit('nginx conf not found')
|
|
t = hit.read_text(encoding='utf-8')
|
|
if '/api/hub/' in t:
|
|
print('nginx hub already present')
|
|
else:
|
|
needle = 'location /api/social/'
|
|
snip = '''
|
|
location /api/hub/ {
|
|
proxy_pass http://127.0.0.1:8025;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_read_timeout 120s;
|
|
}
|
|
'''
|
|
if needle in t:
|
|
t = t.replace(needle, snip + '\n ' + needle, 1)
|
|
else:
|
|
# insert before last closing brace of server
|
|
idx = t.rfind('}')
|
|
t = t[:idx] + snip + '\n' + t[idx:]
|
|
bak = hit.with_suffix(hit.suffix + '.bak-hub')
|
|
bak.write_text(hit.read_text(encoding='utf-8'), encoding='utf-8')
|
|
hit.write_text(t, encoding='utf-8')
|
|
print('wrote', hit)
|
|
PY"""
|
|
)
|
|
run("nginx -t && systemctl reload nginx")
|
|
|
|
# restart main API if present (admin hub routes)
|
|
run(
|
|
"systemctl restart nomadmini-api.service 2>/dev/null || systemctl restart nomadro-api.service 2>/dev/null || true; "
|
|
"sleep 1; curl -sS http://127.0.0.1:8025/health; echo; "
|
|
"curl -sS http://127.0.0.1:8025/api/hub/v1/health; echo; "
|
|
"curl -sk https://nomadro.com/api/hub/v1/health; echo"
|
|
)
|
|
|
|
sftp.close()
|
|
c.close()
|
|
print("HUB BACKEND DEPLOYED")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|