nomadro/apps/hackrobot/deploy/deploy_nomadmini.py
Eric 1dac5fd5fd Sync vip.hackrobot.cn and admin.hackrobot.cn into apps/hackrobot with mandatory server backups.
Keep VIP stack isolated from Nomadro admin; pull/deploy scripts always snapshot /opt/hackrobot-backup first.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-22 10:48:49 +08:00

65 lines
1.8 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy apps/hackrobot/nomadmini → /opt/nomadmini (backup first, restart systemd)."""
from __future__ import annotations
import tarfile
import tempfile
from pathlib import Path
from _ssh import connect, require_backup, run
SRC = Path(__file__).resolve().parents[1] / "nomadmini"
REMOTE = "/opt/nomadmini"
def main() -> None:
if not (SRC / "backend").exists():
raise SystemExit("missing nomadmini/backend")
c = connect()
print("[1] backup…")
require_backup(c, "pre_nomadmini")
with tempfile.TemporaryDirectory() as td:
tgz = Path(td) / "nomadmini.tgz"
with tarfile.open(tgz, "w:gz") as tar:
for p in SRC.rglob("*"):
if not p.is_file():
continue
rel = str(p.relative_to(SRC)).replace("\\", "/")
if "/.venv/" in f"/{rel}/" or rel.endswith(".pyc") or "__pycache__" in rel:
continue
if p.name.startswith(".env") and "example" not in p.name:
continue
tar.add(p, arcname=rel)
sftp = c.open_sftp()
sftp.put(str(tgz), "/tmp/nomadmini-deploy.tgz")
sftp.close()
print("[2] sync + restart…")
run(
c,
rf"""
set -e
TMP=/tmp/nomadmini-unpack-$$
rm -rf "$TMP" && mkdir -p "$TMP"
tar -xzf /tmp/nomadmini-deploy.tgz -C "$TMP"
# keep live .env and .venv
sudo rsync -a \
--exclude .venv --exclude '__pycache__' --exclude .env \
"$TMP"/ {REMOTE}/
sudo systemctl restart nomadmini-api.service
sleep 1
systemctl is-active nomadmini-api.service
curl -sS -o /dev/null -w 'live_health:%{{http_code}}\n' http://127.0.0.1:8010/docs || true
echo DEPLOY_NOMADMINI_OK
""",
timeout=180,
)
c.close()
print("[done] nomadmini")
if __name__ == "__main__":
main()