# -*- coding: utf-8 -*- """Stash server dirty tree, pull test1, rebuild, verify admin.""" import paramiko import sys import time sys.stdout.reconfigure(encoding="utf-8", errors="replace") HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" REPO = "/opt/douyinWEB" ADMIN_PY = """ import sys sys.path.insert(0, '/opt/douyinWEB/backend') from app import db from app.config import settings m = db.get_settings_map() print('admin_username', m.get('admin_username')) print('has_hash', bool(m.get('admin_password_hash'))) print('hash_tail', str(m.get('admin_password_hash') or '')[-8:]) print('config_user', settings.admin_username) print('config_pw_set', bool(str(settings.admin_password or ''))) """ def main() -> None: 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) def run(cmd: str, timeout: int = 360) -> str: _, o, e = c.exec_command(cmd, timeout=timeout) return (o.read().decode("utf-8", "replace") + e.read().decode("utf-8", "replace")).strip() print("[admin before]") print(run(f"cd {REPO}/backend && .venv/bin/python -c {repr(ADMIN_PY)}", timeout=60)) print("[stash + pull]") pull_cmd = ( f"cd {REPO} && git stash push -u -m pre-deploy-test1 && " f"git fetch origin test1 && git checkout test1 && " f"git pull --ff-only origin test1 2>&1" ) print(run(pull_cmd, timeout=120)) print("[git log]") print(run(f"cd {REPO} && git log --oneline -2")) print("[restart api]") c.exec_command("systemctl restart douyinweb-api", timeout=5) time.sleep(10) print("api:", run("systemctl is-active douyinweb-api")) print("[npm build]") build = run(f"cd {REPO}/frontend && npm run build 2>&1", timeout=360) for line in build.splitlines()[-15:]: print(line.encode("ascii", "replace").decode()) print("[restart web]") c.exec_command("systemctl restart douyinweb-web", timeout=5) time.sleep(8) print("web:", run("systemctl is-active douyinweb-web")) print("[health]") print("api docs:", run("curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/docs")) print("web home:", run("curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:4015/")) print("[admin after]") print(run(f"cd {REPO}/backend && .venv/bin/python -c {repr(ADMIN_PY)}", timeout=60)) c.close() print("DONE") if __name__ == "__main__": main()