# -*- coding: utf-8 -*- """Recover Neko containers stopped by API boot reconcile — docker start only.""" from __future__ import annotations 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 = "4669805" FILES = [ ("backend/app/main.py", "/opt/douyinWEB/backend/app/main.py", "_fix_boot_main.py"), ("backend/app/youtube_neko.py", "/opt/douyinWEB/backend/app/youtube_neko.py", "_fix_boot_neko.py"), ] SH = r"""#!/bin/bash set -euo pipefail TS=$(date +%s) ins(){ cp -a "$2" "$2.bak-bootfix-$TS" 2>/dev/null || true; install -m 644 "$1" "$2"; } ins /tmp/_fix_boot_main.py /opt/douyinWEB/backend/app/main.py ins /tmp/_fix_boot_neko.py /opt/douyinWEB/backend/app/youtube_neko.py cd /opt/douyinWEB/backend .venv/bin/python -m py_compile app/main.py app/youtube_neko.py echo COMPILE_OK echo "=== docker start all existing neko containers (no recreate) ===" # Start every douyinweb-neko-* that exists, whether exited or created. mapfile -t ALL < <(docker ps -a --filter name=douyinweb-neko- --format '{{.Names}}' | sort -V) for name in "${ALL[@]}"; do st=$(docker inspect -f '{{.State.Status}}' "$name" 2>/dev/null || echo missing) if [ "$st" = "running" ]; then echo "already_running $name" continue fi echo "starting $name (was $st)" docker start "$name" || echo "START_FAIL $name" done sleep 5 echo "=== after ===" docker ps -a --filter name=douyinweb-neko- --format '{{.Names}} {{.Status}}' RUNNING=$(docker ps -q --filter name=douyinweb-neko- | wc -l | tr -d ' ') echo "RUNNING=$RUNNING" # Restart API with fixed boot so it won't stop them again. systemctl restart douyinweb-api sleep 5 systemctl is-active douyinweb-api sleep 3 RUNNING2=$(docker ps -q --filter name=douyinweb-neko- | wc -l | tr -d ' ') echo "RUNNING_AFTER_API=$RUNNING2" docker ps --filter name=douyinweb-neko- --format '{{.Names}} {{.Status}}' if [ "$RUNNING2" -ge "$RUNNING" ]; then echo RECOVER_OK else echo RECOVER_REGRESSED exit 2 fi """ def main() -> int: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) for i in range(12): try: c.connect( "107.173.30.245", 4014, "eric", PASS, timeout=60, banner_timeout=120, allow_agent=False, look_for_keys=False, ) break except Exception as ex: print("ssh", i, type(ex).__name__, flush=True) time.sleep(2 + i) else: raise SystemExit("ssh fail") sftp = c.open_sftp() for rel, _dst, tmp in FILES: with sftp.file(f"/tmp/{tmp}", "wb") as f: f.write((ROOT / rel).read_bytes()) print("up", rel, flush=True) with sftp.file("/tmp/_recover_neko.sh", "w") as f: f.write(SH) sftp.close() _, o, e = c.exec_command(f"echo '{PASS}' | sudo -S -p '' bash /tmp/_recover_neko.sh", timeout=300) out = (o.read() + e.read()).decode("utf-8", "replace") print(out, flush=True) c.close() return 0 if "RECOVER_OK" in out else 1 if __name__ == "__main__": raise SystemExit(main())