Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes. Co-authored-by: Cursor <cursoragent@cursor.com>
166 lines
5.5 KiB
Python
166 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Deploy dymini: API :8011 + dyapi/dyadmin/dy.nomadro.cn."""
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import os
|
|
import sys
|
|
import tarfile
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
from scp import SCPClient
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
HOST = "82.157.112.245"
|
|
USER = "ubuntu"
|
|
PASSWORD = os.environ.get("DYMINI_SSH_PASSWORD", os.environ.get("NOMADMINI_SSH_PASSWORD", "Xiao4669805"))
|
|
REMOTE = "/opt/dymini"
|
|
|
|
ENV_CONTENT = """TT_APPID=tt995defe007255b2701
|
|
TT_SECRET=8628800c6f2290175180b94e255380c2e9d34dc0
|
|
TT_ALLOW_DEVTOOLS_LOGIN=true
|
|
TT_PAY_MOCK=true
|
|
JWT_SECRET=dymini-jwt-prod-2026
|
|
ADMIN_USERNAME=admin
|
|
ADMIN_PASSWORD=Xiao4669805
|
|
USE_POCKETBASE=false
|
|
DEBUG=false
|
|
PUBLIC_API_BASE=https://dyapi.nomadro.cn
|
|
ADMIN_ORIGIN=https://dyadmin.nomadro.cn
|
|
CORS_ORIGINS=https://dyadmin.nomadro.cn,https://dy.nomadro.cn
|
|
DEFAULT_PRODUCT_ID=ebook_unlock_default
|
|
DEFAULT_PRICE_FEN=18800
|
|
DEFAULT_UNLOCK_PERCENT=100
|
|
SQLITE_PATH=/opt/dymini/data/dymini.db
|
|
NTFY_ENABLED=true
|
|
NTFY_BASE_URL=http://127.0.0.1:2586
|
|
NTFY_TOPIC=dymini
|
|
"""
|
|
|
|
|
|
def safe_print(text: str) -> None:
|
|
data = (text.rstrip() + "\n").encode("utf-8", errors="replace")
|
|
try:
|
|
sys.stdout.buffer.write(data)
|
|
sys.stdout.buffer.flush()
|
|
except Exception:
|
|
print(data.decode("utf-8", errors="replace"), end="")
|
|
|
|
|
|
def run(client: paramiko.SSHClient, cmd: str, check: bool = True) -> tuple[int, str, str]:
|
|
print(f"$ {cmd[:120]}{'...' if len(cmd) > 120 else ''}")
|
|
_, stdout, stderr = client.exec_command(cmd, get_pty=True)
|
|
out = stdout.read().decode("utf-8", errors="replace")
|
|
err = stderr.read().decode("utf-8", errors="replace")
|
|
code = stdout.channel.recv_exit_status()
|
|
if out.strip():
|
|
safe_print(out)
|
|
if err.strip():
|
|
safe_print(err)
|
|
if check and code != 0:
|
|
raise RuntimeError(f"Command failed ({code}): {cmd[:200]}")
|
|
return code, out, err
|
|
|
|
|
|
def build_tarball() -> Path:
|
|
tmp = tempfile.NamedTemporaryFile(suffix=".tar.gz", delete=False)
|
|
tmp.close()
|
|
tar_path = Path(tmp.name)
|
|
|
|
def should_skip(path: Path) -> bool:
|
|
skip_names = {".venv", "__pycache__", "node_modules", ".git", "dist"}
|
|
if any(p in skip_names for p in path.parts):
|
|
return True
|
|
return path.suffix in {".pyc", ".pyo"}
|
|
|
|
with tarfile.open(tar_path, "w:gz") as tar:
|
|
backend = ROOT / "backend"
|
|
for p in backend.rglob("*"):
|
|
if p.is_file() and not should_skip(p.relative_to(backend)):
|
|
tar.add(p, arcname=f"backend/{p.relative_to(backend).as_posix()}")
|
|
|
|
ebook = ROOT / "data" / "ebook.md"
|
|
if ebook.exists():
|
|
tar.add(ebook, arcname="data/ebook.md")
|
|
|
|
admin_dist = ROOT / "admin" / "dist"
|
|
if not admin_dist.exists():
|
|
raise FileNotFoundError("admin/dist missing — run npm run build in admin/")
|
|
for p in admin_dist.rglob("*"):
|
|
if p.is_file():
|
|
tar.add(p, arcname=f"admin/dist/{p.relative_to(admin_dist).as_posix()}")
|
|
|
|
landing = ROOT / "deploy" / "dy-landing" / "index.html"
|
|
# landing optional; dy.nomadro.cn is not managed by this project
|
|
if False and landing.exists():
|
|
tar.add(landing, arcname="deploy/dy-landing/index.html")
|
|
|
|
for name in [
|
|
"dymini-api.service",
|
|
"dyapi.nomadro.cn.conf",
|
|
"dyadmin.nomadro.cn.conf",
|
|
"remote-setup-dymini.sh",
|
|
"issue-certs.sh",
|
|
]:
|
|
p = ROOT / "deploy" / name
|
|
if not p.exists():
|
|
continue
|
|
data = p.read_bytes().replace(b"\r\n", b"\n")
|
|
info = tarfile.TarInfo(name=f"deploy/{name}")
|
|
info.size = len(data)
|
|
tar.addfile(info, fileobj=io.BytesIO(data))
|
|
|
|
print(f"Created tarball: {tar_path} ({tar_path.stat().st_size // 1024} KB)")
|
|
return tar_path
|
|
|
|
|
|
def main() -> None:
|
|
tar_path = build_tarball()
|
|
client = paramiko.SSHClient()
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
print(f"Connecting to {USER}@{HOST} ...")
|
|
client.connect(HOST, username=USER, password=PASSWORD, timeout=30)
|
|
|
|
try:
|
|
run(client, f"sudo mkdir -p {REMOTE} && sudo chown -R ubuntu:ubuntu {REMOTE}")
|
|
with SCPClient(client.get_transport()) as scp:
|
|
scp.put(str(tar_path), f"{REMOTE}/release.tar.gz")
|
|
|
|
run(
|
|
client,
|
|
f"cd {REMOTE} && rm -rf backend admin/dist deploy && "
|
|
f"mkdir -p backend admin/dist data deploy && "
|
|
f"tar -xzf release.tar.gz && rm -f release.tar.gz",
|
|
)
|
|
|
|
sftp = client.open_sftp()
|
|
with sftp.file(f"{REMOTE}/backend/.env", "w") as f:
|
|
f.write(ENV_CONTENT)
|
|
sftp.close()
|
|
|
|
run(client, f"bash {REMOTE}/deploy/remote-setup-dymini.sh")
|
|
run(client, f"bash {REMOTE}/deploy/issue-certs.sh", check=False)
|
|
|
|
_, out, _ = run(client, "curl -s http://127.0.0.1:8011/health", check=False)
|
|
print("Local:", out.strip())
|
|
for url in (
|
|
"https://dyapi.nomadro.cn/health",
|
|
"https://dyadmin.nomadro.cn/health",
|
|
):
|
|
_, o, _ = run(client, f"curl -sI {url} | head -n 1", check=False)
|
|
print(url, "->", o.strip())
|
|
finally:
|
|
client.close()
|
|
tar_path.unlink(missing_ok=True)
|
|
|
|
print("\nDeployed:")
|
|
print(" API: https://dyapi.nomadro.cn")
|
|
print(" Admin: https://dyadmin.nomadro.cn")
|
|
print(" (dy.nomadro.cn left untouched — already used by another service)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|