194 lines
5.4 KiB
Python
194 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Finish dual deploy: ensure dy Next build has /youtube/keys; deploy dylive when SSH works."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PASS = "Xiao4669805"
|
|
FILES = [
|
|
"backend/app/yt_stream_key_db.py",
|
|
"backend/app/models.py",
|
|
"backend/app/main.py",
|
|
"frontend/src/app/youtube/keys/page.tsx",
|
|
"frontend/src/components/YtAssignPanel.tsx",
|
|
]
|
|
|
|
|
|
def connect(host: str):
|
|
last = None
|
|
for i in range(12):
|
|
try:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
host,
|
|
22,
|
|
"root",
|
|
PASS,
|
|
timeout=60,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
banner_timeout=240,
|
|
auth_timeout=60,
|
|
)
|
|
return c
|
|
except Exception as e:
|
|
last = e
|
|
print(f"ssh_retry {i} {host}: {e}", flush=True)
|
|
time.sleep(4 + i * 2)
|
|
raise SystemExit(f"ssh fail {host}: {last}")
|
|
|
|
|
|
def run(c, cmd: str, t: int = 900) -> tuple[int, str]:
|
|
_i, o, e = c.exec_command(cmd, timeout=t)
|
|
out = (o.read() + e.read()).decode("utf-8", "replace")
|
|
return o.channel.recv_exit_status(), out
|
|
|
|
|
|
def upload(c):
|
|
sftp = c.open_sftp()
|
|
for rel in FILES:
|
|
local = ROOT / rel
|
|
remote = f"/opt/douyinWEB/{rel}"
|
|
# mkdir -p
|
|
d = "/".join(remote.split("/")[:-1])
|
|
run(c, f"mkdir -p {d}")
|
|
print("put", rel, flush=True)
|
|
sftp.put(str(local), remote)
|
|
sftp.close()
|
|
|
|
|
|
def ensure_frontend_build(c, label: str) -> None:
|
|
code, out = run(
|
|
c,
|
|
r"""
|
|
set -euo pipefail
|
|
echo '=== neko before ==='
|
|
docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort
|
|
cd /opt/douyinWEB/frontend
|
|
ls -la page.tsx 2>/dev/null || true
|
|
test -f src/app/youtube/keys/page.tsx && echo keys_page_src_ok
|
|
# Does production build already include keys route?
|
|
if [ -d .next/server/app/youtube/keys ] || [ -f .next/server/app/youtube/keys/page.js ]; then
|
|
echo next_keys_built_already
|
|
else
|
|
echo need_build
|
|
if [ -d node_modules ]; then
|
|
# low-mem friendly
|
|
export NODE_OPTIONS=--max-old-space-size=1536
|
|
npm run build
|
|
else
|
|
echo 'FATAL no node_modules'
|
|
exit 3
|
|
fi
|
|
fi
|
|
systemctl restart douyinweb-web
|
|
sleep 4
|
|
systemctl is-active douyinweb-web
|
|
echo '=== neko after ==='
|
|
docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort
|
|
""",
|
|
t=900,
|
|
)
|
|
print(out[-6000:] if len(out) > 6000 else out, flush=True)
|
|
if code != 0:
|
|
raise SystemExit(f"{label} frontend build failed: {code}")
|
|
|
|
|
|
print("===== dy frontend ensure =====", flush=True)
|
|
dy = connect("108.174.50.68")
|
|
ensure_frontend_build(dy, "dy")
|
|
# verify API still has keys
|
|
_, out = run(
|
|
dy,
|
|
r"""
|
|
cd /opt/douyinWEB/backend
|
|
./.venv/bin/python - <<'PY'
|
|
from app import yt_stream_key_db
|
|
print('total', yt_stream_key_db.list_entries()['total'])
|
|
PY
|
|
curl -sk -m 15 -o /dev/null -w 'keys:%{http_code}\n' https://dy.nomadro.live/youtube/keys
|
|
""",
|
|
)
|
|
print(out, flush=True)
|
|
# export for dylive
|
|
_, exp = run(
|
|
dy,
|
|
r"""
|
|
cd /opt/douyinWEB/backend
|
|
./.venv/bin/python - <<'PY'
|
|
import json
|
|
from app import yt_stream_key_db
|
|
print('EXPORT_JSON_START')
|
|
print(json.dumps(yt_stream_key_db.export_all(), ensure_ascii=False))
|
|
print('EXPORT_JSON_END')
|
|
PY
|
|
""",
|
|
)
|
|
dy.close()
|
|
start = exp.find("EXPORT_JSON_START")
|
|
end = exp.find("EXPORT_JSON_END")
|
|
payload = json.loads(exp[start + len("EXPORT_JSON_START") : end].strip())
|
|
items = payload.get("items") or []
|
|
print(f"export {len(items)} items", flush=True)
|
|
|
|
print("===== dylive deploy =====", flush=True)
|
|
dl = connect("107.173.30.245")
|
|
_, before = run(dl, "docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort")
|
|
print("BEFORE\n", before, flush=True)
|
|
upload(dl)
|
|
code, out = run(
|
|
dl,
|
|
r"""
|
|
set -euo pipefail
|
|
cd /opt/douyinWEB/backend
|
|
./.venv/bin/python -c 'from app import yt_stream_key_db; print("py_ok")'
|
|
systemctl restart douyinweb-api
|
|
sleep 4
|
|
systemctl is-active douyinweb-api
|
|
""",
|
|
)
|
|
print(out, flush=True)
|
|
ensure_frontend_build(dl, "dylive")
|
|
|
|
# import registry from dy export
|
|
sftp = dl.open_sftp()
|
|
with sftp.file("/tmp/yt_stream_keys_import.json", "w") as f:
|
|
f.write(json.dumps({"items": items}, ensure_ascii=False))
|
|
sftp.close()
|
|
_, out2 = run(
|
|
dl,
|
|
r"""
|
|
cd /opt/douyinWEB/backend
|
|
./.venv/bin/python - <<'PY'
|
|
import json
|
|
from app import yt_stream_key_db
|
|
# also snapshot local assign
|
|
print('assign_import', yt_stream_key_db.import_from_assign_groups(replace=False))
|
|
data=json.load(open('/tmp/yt_stream_keys_import.json',encoding='utf-8'))
|
|
print('peer_import', yt_stream_key_db.import_payload(data.get('items') or [], replace=False))
|
|
print('total', yt_stream_key_db.list_entries()['total'])
|
|
PY
|
|
docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort
|
|
curl -sk -m 15 -o /dev/null -w 'keys:%{http_code}\n' https://dylive.nomadro.com/youtube/keys
|
|
""",
|
|
)
|
|
print(out2, flush=True)
|
|
after_up = {ln.split()[0] for ln in out2.splitlines() if " Up " in ln and "neko" in ln}
|
|
before_up = {ln.split()[0] for ln in before.splitlines() if " Up " in ln and "neko" in ln}
|
|
lost = before_up - after_up
|
|
if lost:
|
|
print("CRITICAL neko lost", lost)
|
|
raise SystemExit(2)
|
|
print("neko_ok", sorted(after_up))
|
|
dl.close()
|
|
print("ALL_DONE")
|