sh2/d2ypp2/tools/probe_board_stack.py
2026-06-02 03:19:23 -05:00

83 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""Probe ENABLE_NEKO and Docker service state on the ARM board.
Environment defaults match deploy_live_paramiko.py:
LIVE_DEPLOY_HOST: 192.168.31.56
LIVE_DEPLOY_USER: orangepi
LIVE_DEPLOY_PASS: orangepi
"""
from __future__ import annotations
import os
import sys
try:
import paramiko
except ImportError:
print("请先执行: pip install paramiko", file=sys.stderr)
sys.exit(1)
HOST = os.environ.get("LIVE_DEPLOY_HOST", "192.168.31.56")
USER = os.environ.get("LIVE_DEPLOY_USER", "orangepi")
PASS = os.environ.get("LIVE_DEPLOY_PASS", "orangepi")
SSH_PORT = int(os.environ.get("LIVE_DEPLOY_PORT", os.environ.get("LIVE_SSH_PORT", "22")))
JUMP_HOST = os.environ.get("LIVE_DEPLOY_JUMP_HOST", "")
JUMP_PORT = int(os.environ.get("LIVE_DEPLOY_JUMP_PORT", "22"))
JUMP_USER = os.environ.get("LIVE_DEPLOY_JUMP_USER", "")
JUMP_PASS = os.environ.get("LIVE_DEPLOY_JUMP_PASS", "")
REMOTE_SH = r"""
echo "--- ENABLE_NEKO in known env files ---"
for f in /opt/live/config/system-stack.env /opt/live/generated/system-stack.env /home/orangepi/d2ypp2-src/config/system-stack.env; do
if [ -f "$f" ]; then
echo "=== $f ==="
grep -E '^ENABLE_NEKO=' "$f" 2>/dev/null || echo '(no ENABLE_NEKO line)'
fi
done
echo "--- docker: srs / neko (running or all) ---"
docker ps -a --format '{{.Names}} {{.Status}}' 2>/dev/null | grep -iE 'neko|srs' || echo '(no container name matching neko|srs)'
echo "--- docker ps -a (first 25) ---"
docker ps -a --format '{{.Names}} {{.Status}}' 2>/dev/null | head -25
"""
def main() -> int:
sock = None
jump = None
if JUMP_HOST:
jump = paramiko.SSHClient()
jump.set_missing_host_key_policy(paramiko.AutoAddPolicy())
jump.connect(
JUMP_HOST,
port=JUMP_PORT,
username=JUMP_USER or USER,
password=JUMP_PASS or PASS,
timeout=20,
)
sock = jump.get_transport().open_channel("direct-tcpip", (HOST, SSH_PORT), ("127.0.0.1", 0), timeout=20)
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
c.connect(HOST, port=SSH_PORT, username=USER, password=PASS, sock=sock, timeout=20)
except Exception as e:
print(f"SSH_CONNECT_FAILED {HOST} {type(e).__name__}: {e}", file=sys.stderr)
return 2
_, stdout, stderr = c.exec_command(
"export LC_ALL=C.UTF-8 LANG=C.UTF-8; " + REMOTE_SH,
timeout=90,
)
out = stdout.read().decode("utf-8", errors="replace")
err = stderr.read().decode("utf-8", errors="replace")
code = stdout.channel.recv_exit_status()
c.close()
if jump:
jump.close()
print(out, end="")
if err.strip():
print(err, file=sys.stderr, end="")
return code
if __name__ == "__main__":
raise SystemExit(main())