douyinWEB/scripts/deploy_ports.py
eric 2515f438c2 Update README for dylive/feiniu production and sync deploy scripts.
Document Caddy domains, SeaweedFS covers, VPS ports, and keep monitor DB current.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-08 10:14:27 -05:00

163 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy douyinWEB on free ports: web=4014 api=8014 pb=8092."""
import paramiko
HOST = "107.173.30.245"
USER = "root"
PASSWORD = "Xiao4669805"
APP = "/opt/douyinWEB"
WEB_PORT = 4014
API_PORT = 8014
PB_PORT = 8092
def run(c, cmd, timeout=300):
print(f"\n$ {cmd[:220]}", flush=True)
_, out, _ = c.exec_command(cmd, timeout=timeout, get_pty=True)
text = out.read().decode("utf-8", errors="replace")
code = out.channel.recv_exit_status()
print(text[-4500:], flush=True)
print(f"[exit {code}]", flush=True)
return code, text
def main():
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)
print("connected", flush=True)
# stop only our app processes
run(
c,
f"""
set +e
fuser -k {WEB_PORT}/tcp 2>/dev/null || true
fuser -k {API_PORT}/tcp 2>/dev/null || true
fuser -k {PB_PORT}/tcp 2>/dev/null || true
pkill -9 -f '{APP}/pb/pocketbase' || true
pkill -9 -f '{APP}/backend/.venv/bin/python -m uvicorn' || true
pkill -9 -f 'next start --hostname 0.0.0.0 --port {WEB_PORT}' || true
sleep 2
ss -lntp | grep -E ':{WEB_PORT}|:{API_PORT}|:{PB_PORT}' || echo free
""".strip(),
)
# patch next rewrite to API_PORT and rebuild if needed
run(
c,
f"""
set -e
cd {APP}
git remote set-url origin 'https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git'
git fetch origin
git reset --hard origin/main
# rewrite /api -> local API
cat > frontend/next.config.mjs <<'EOF'
/** @type {{import('next').NextConfig}} */
const nextConfig = {{
reactStrictMode: true,
async rewrites() {{
return [
{{
source: "/api/:path*",
destination: "http://127.0.0.1:{API_PORT}/api/:path*",
}},
{{
source: "/covers/:path*",
destination: "http://127.0.0.1:{API_PORT}/covers/:path*",
}},
];
}},
}};
export default nextConfig;
EOF
cd frontend
npm run build
""".strip(),
timeout=900,
)
# systemd units
run(
c,
f"""
set -e
cat >/etc/systemd/system/douyinweb-pb.service <<'EOF'
[Unit]
Description=DouyinWEB PocketBase
After=network.target
[Service]
Type=simple
WorkingDirectory={APP}/pb
ExecStart={APP}/pb/pocketbase serve --http=0.0.0.0:{PB_PORT}
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
EOF
cat >/etc/systemd/system/douyinweb-api.service <<'EOF'
[Unit]
Description=DouyinWEB FastAPI
After=network.target douyinweb-pb.service
[Service]
Type=simple
WorkingDirectory={APP}/backend
ExecStart={APP}/backend/.venv/bin/python -m uvicorn app.main:app --host 0.0.0.0 --port {API_PORT}
Restart=always
RestartSec=3
Environment=DY_PB_URL=http://127.0.0.1:{PB_PORT}
[Install]
WantedBy=multi-user.target
EOF
cat >/etc/systemd/system/douyinweb-web.service <<'EOF'
[Unit]
Description=DouyinWEB Next.js
After=network.target douyinweb-api.service
[Service]
Type=simple
WorkingDirectory={APP}/frontend
ExecStart=/usr/bin/npm run start -- --hostname 0.0.0.0 --port {WEB_PORT}
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable douyinweb-pb douyinweb-api douyinweb-web
systemctl restart douyinweb-pb douyinweb-api douyinweb-web
sleep 5
systemctl --no-pager --full status douyinweb-pb douyinweb-api douyinweb-web | sed -n '1,80p'
ss -lntp | grep -E ':{WEB_PORT}|:{API_PORT}|:{PB_PORT}'
echo HEALTH
curl -s --max-time 10 http://127.0.0.1:{API_PORT}/api/health
echo
echo WEB
curl -s -o /dev/null -w 'web:%{{http_code}}\\n' --max-time 20 http://127.0.0.1:{WEB_PORT}/
echo ROOMS
curl -s --max-time 10 'http://127.0.0.1:{API_PORT}/api/rooms?limit=1' | head -c 400
echo
""".strip(),
timeout=180,
)
c.close()
print(f"\nOpen http://{HOST}:{WEB_PORT}/", flush=True)
print(f"API http://{HOST}:{API_PORT}/docs", flush=True)
print(f"PB http://{HOST}:{PB_PORT}/_/", flush=True)
if __name__ == "__main__":
main()