douyinWEB/scripts/_start_nomadro_next_only.py
2026-09-13 22:10:49 -05:00

93 lines
3.0 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Start nomadro.com Next.js only; point edge to :4003; leave WP alone."""
from __future__ import annotations
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
# Read-only peek then start — Caddy change only if currently pointing at router/WP
PROBE = r"""#!/bin/bash
echo '=== active caddy root ==='
head -80 /etc/caddy/Caddyfile
echo '---'
ls -la /etc/caddy/sites/*.caddy 2>/dev/null | grep -iE 'nomadro[^.]|nomadro\.caddy' || true
grep -n 'nomadro.com\|4403\|4003\|8088' /etc/caddy/Caddyfile 2>/dev/null | head -40
# imported sites?
grep -n 'import\|nomadro' /etc/caddy/Caddyfile 2>/dev/null | head -20
"""
START = r"""#!/bin/bash
set -e
echo '=== start Next.js nomadro.service ==='
systemctl enable nomadro.service
systemctl restart nomadro.service
sleep 2
systemctl is-active nomadro.service
ss -lntp | grep ':4003' || true
# Do NOT start nomadro-router (WP split) or WordPress containers
systemctl is-active nomadro-router.service 2>/dev/null || echo router=inactive
# Ensure Caddy apex points at Next :4003 (not router :4403 / WP :8088)
SITE=/etc/caddy/sites/nomadro.com.caddy
if [ ! -f "$SITE" ]; then
cat > "$SITE" <<'EOF'
www.nomadro.com {
redir https://nomadro.com{uri} permanent
}
nomadro.com {
encode gzip
reverse_proxy 127.0.0.1:4003
}
EOF
echo created $SITE
else
echo existing $SITE
cat "$SITE"
fi
# Ensure main Caddyfile imports sites
if ! grep -q 'sites/\*\.caddy\|sites/\*' /etc/caddy/Caddyfile 2>/dev/null; then
# check how other sites are loaded
grep -n import /etc/caddy/Caddyfile | head -10
fi
caddy validate --config /etc/caddy/Caddyfile 2>&1 | tail -20
systemctl reload caddy
sleep 1
curl -sS -m 10 -o /dev/null -w 'local4003=%{http_code}\n' http://127.0.0.1:4003/ || true
curl -sS -m 12 -o /dev/null -w 'public=%{http_code}\n' -L https://nomadro.com/ || true
curl -sS -m 12 -o /dev/null -w 'www=%{http_code}\n' -L https://www.nomadro.com/ || true
systemctl is-active nomadro.service caddy
# confirm WP containers left as-is (not started by us; may already be up)
docker ps --format '{{.Names}} {{.Status}}' | grep -i wordpress || echo 'no wp running (ok)'
"""
def run(c: paramiko.SSHClient, script: str, path: str, timeout: int = 120) -> str:
sftp = c.open_sftp()
with sftp.file(path, "w") as f:
f.write(script)
sftp.close()
_, o, e = c.exec_command(f"bash {path}", timeout=timeout)
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out, flush=True)
return out
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=40, allow_agent=False, look_for_keys=False)
print("===== PROBE =====", flush=True)
run(c, PROBE, "/tmp/_probe_caddy_nomadro.sh", 40)
print("===== START =====", flush=True)
run(c, START, "/tmp/_start_nomadro_next_only.sh", 120)
c.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())