# -*- coding: utf-8 -*- """207 断电重启后恢复服务。""" from __future__ import annotations import sys import time import urllib.error import urllib.request import paramiko HOST = "192.168.110.207" USER = "root" KEY = r"C:\Users\DCKJ\.ssh\id_ed25519" def ssh() -> paramiko.SSHClient: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect( HOST, username=USER, key_filename=KEY, timeout=30, allow_agent=False, look_for_keys=False, ) return c def run(c: paramiko.SSHClient, cmd: str, timeout: int = 300) -> tuple[int, str]: print(f"\n>>> {cmd[:220]}") _, o, e = c.exec_command(cmd, timeout=timeout, get_pty=True) out = o.read().decode(errors="replace") err = e.read().decode(errors="replace") code = o.channel.recv_exit_status() text = (out + ("\n" + err if err.strip() else "")).strip() if text: print(text[-6000:] if len(text) > 6000 else text) print(f"[exit={code}]") return code, text def http_ok(url: str, timeout: float = 6.0) -> str: try: r = urllib.request.urlopen(url, timeout=timeout) return f"OK {r.status}" except urllib.error.HTTPError as e: return f"HTTP {e.code}" except Exception as e: return f"DOWN {type(e).__name__}" def probe() -> None: checks = [ ("Dify :80", "http://192.168.110.207/"), ("LiteLLM", "http://192.168.110.207:4000/health/liveliness"), ("Langfuse", "http://192.168.110.207:3000/api/public/health"), ("Xinference", "http://192.168.110.207:9997/v1/models"), ("Higress", "http://192.168.110.207:18080/"), ("Higress Console", "http://192.168.110.207:8001/"), ("GitLab", "http://192.168.110.207:8929/users/sign_in"), ("Argo CD", "http://192.168.110.207:30080/"), ("demo-nginx", "http://192.168.110.207:30088/"), ("Grafana", "http://192.168.110.207:3001/"), ("Prometheus", "http://192.168.110.207:9092/-/ready"), ] print("\n=== HTTP probe ===") for name, url in checks: print(f"{name:16} {http_ok(url):20} {url}") def main() -> int: probe() c = ssh() print("[OK] SSH connected") run(c, "uptime; free -h | head -2; systemctl is-active docker k3s litellm 2>/dev/null || true") run(c, "docker ps -a --format 'table {{.Names}}\t{{.Status}}' | head -80") # 1) Docker run(c, "systemctl enable docker 2>/dev/null; systemctl start docker; sleep 2; systemctl is-active docker") # 2) LiteLLM systemd run(c, "systemctl enable litellm 2>/dev/null; systemctl restart litellm; sleep 2; systemctl is-active litellm || true") # 3) Dify compose run( c, "cd /dify/dify-main/docker && (docker compose up -d || docker-compose up -d) 2>&1 | tail -40", timeout=600, ) # 4) Langfuse run( c, "if [ -d /Langfuse/langfuse ]; then cd /Langfuse/langfuse && (docker compose up -d || docker-compose up -d) 2>&1 | tail -30; " "elif [ -d /opt/langfuse ]; then cd /opt/langfuse && (docker compose up -d || docker-compose up -d) 2>&1 | tail -30; " "else echo 'langfuse dir missing'; fi", timeout=600, ) # 5) Xinference run( c, "if docker ps -a --format '{{.Names}}' | grep -qx xinference; then docker start xinference; " "elif docker ps -a --format '{{.Names}}' | grep -qi xinference; then " "docker start $(docker ps -a --format '{{.Names}}' | grep -i xinference | head -1); " "else echo 'xinference container missing'; docker ps -a | grep -i xinf || true; fi", timeout=120, ) # 6) Higress run( c, "if [ -x /opt/higress/bin/startup.sh ]; then /opt/higress/bin/startup.sh 2>&1 | tail -40; " "elif [ -d /opt/higress/compose ]; then cd /opt/higress/compose && (docker compose up -d || docker-compose up -d) 2>&1 | tail -40; " "else echo 'higress missing'; ls /opt/higress 2>/dev/null || true; fi", timeout=600, ) # 7) Monitoring hw8 run( c, "if [ -f /opt/workbuddy/patches_cursor/作业8_SLA监控大盘/docker-compose.monitoring.yml ]; then " "cd /opt/workbuddy/patches_cursor/作业8_SLA监控大盘 && " "(docker compose -f docker-compose.monitoring.yml up -d || docker-compose -f docker-compose.monitoring.yml up -d) 2>&1 | tail -20; " "else echo 'monitoring compose missing'; fi", timeout=300, ) # 8) k3s + GitLab demo run(c, "systemctl enable k3s 2>/dev/null; systemctl start k3s; sleep 5; systemctl is-active k3s || true") run( c, "if docker ps -a --format '{{.Names}}' | grep -qx gitlab-argocd-demo; then docker start gitlab-argocd-demo; " "else echo 'gitlab-argocd-demo missing'; fi", timeout=120, ) run( c, "export KUBECONFIG=/etc/rancher/k3s/k3s.yaml; " "k3s kubectl get nodes 2>&1 | head -5; " "k3s kubectl -n argocd get pods 2>&1 | head -20; " "k3s kubectl -n argocd-demo get pods 2>&1 | head -10", timeout=120, ) # wait for backends print("\n[INFO] waiting 45s for containers…") time.sleep(45) run(c, "docker ps --format 'table {{.Names}}\t{{.Status}}' | head -80") run( c, "cd /dify/dify-main/docker && (docker compose ps || docker-compose ps) 2>&1 | head -40", timeout=60, ) c.close() probe() print("\n[DONE] restore script finished") return 0 if __name__ == "__main__": raise SystemExit(main())