#!/usr/bin/env python3 """Upload stop script and run on streaming hosts.""" from __future__ import annotations import io import paramiko from pathlib import Path ROOT = Path(__file__).resolve().parent.parent HOSTS = [ ("192.168.2.237", "eric", "4669805", "/home/eric/d2ypp2-src"), ("192.168.2.101", "eric", "4669805", "/opt/live/d2ypp"), ] UPLOAD = [ "scripts/linux/stop_optional_hub_services.sh", "scripts/linux/service_ctl.sh", "scripts/linux/lib/common.sh", ] STOP_INLINE = """#!/usr/bin/env bash set -e PAT='live-frigate|live-homeassistant|live-openclaw|live-srs|live-mitmproxy|live-code-server|live-homepage|live-filebrowser|live-uptime-kuma|live-ntfy|redroid' for u in live-redroid-host live-frigate-usb-watch live-android-panel live-webtty live-pulseaudio; do systemctl disable --now "$u.service" 2>/dev/null || true done mapfile -t names < <(docker ps --format '{{.Names}}' 2>/dev/null | grep -E "$PAT" || true) if [ "${#names[@]}" -gt 0 ]; then docker stop "${names[@]}" 2>/dev/null || true; fi STACK_ENV='__BASE__/config/system-stack.env' mkdir -p "$(dirname "$STACK_ENV")" touch "$STACK_ENV" for k in ENABLE_REDROID ENABLE_FRIGATE FRIGATE_USB_AUTOSTART ENABLE_HOMEASSISTANT HOMEASSISTANT_AUTOSTART ENABLE_OPENCLAW ENABLE_SRS ENABLE_MITMPROXY ENABLE_VSCODE_SERVER ENABLE_HOMEPAGE ENABLE_FILEBROWSER ENABLE_UPTIME_KUMA ENABLE_NTFY ENABLE_ANDROID_PANEL ENABLE_WEBTTY ENABLE_PULSEAUDIO; do if grep -q "^${k}=" "$STACK_ENV" 2>/dev/null; then sed -i "s/^${k}=.*/${k}=0/" "$STACK_ENV"; else echo "${k}=0" >>"$STACK_ENV"; fi done echo inline-stop-ok """ def put_lf(sftp, local: Path, remote: str) -> None: data = local.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n") sftp.putfo(io.BytesIO(data), remote) for host, user, pw, base in HOSTS: print(f"\n{'='*70}\n{host} ({base})\n{'='*70}") ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=user, password=pw, timeout=25) sftp = ssh.open_sftp() reinstall = [] for rel in UPLOAD: local = ROOT / rel tmp = f"/tmp/_deploy_{rel.replace('/', '_')}" dest = f"{base}/{rel.replace(chr(92), '/')}" put_lf(sftp, local, tmp) mode = "0755" if rel.endswith(".sh") else "0644" reinstall.append(f"install -m {mode} {tmp} {dest}") print(f"staged {rel}") sftp.close() install = " && ".join(reinstall) _, o, e = ssh.exec_command(f"echo '{pw}' | sudo -S -p '' bash -lc '{install}'", timeout=120) print((o.read() + e.read()).decode("utf-8", "replace")[:300]) inline_path = "/tmp/stop_optional_inline.sh" sftp2 = ssh.open_sftp() sftp2.putfo(io.BytesIO(STOP_INLINE.replace("__BASE__", base).encode("utf-8")), inline_path) sftp2.chmod(inline_path, 0o755) sftp2.close() _, o, e = ssh.exec_command(f"echo '{pw}' | sudo -S -p '' bash {inline_path}", timeout=300) print((o.read() + e.read()).decode("utf-8", "replace")) _, o, e = ssh.exec_command( f"echo '{pw}' | sudo -S -p '' bash {base}/scripts/linux/stop_optional_hub_services.sh", timeout=300, ) print((o.read() + e.read()).decode("utf-8", "replace")) _, o2, _ = ssh.exec_command( f"echo '{pw}' | sudo -S -p '' bash -lc 'docker ps --format \"{{{{.Names}}}}\\t{{{{.Status}}}}\" | sort; echo ---; uptime; curl -sf -m 5 http://127.0.0.1:8001/health'", timeout=60, ) print((o2.read()).decode("utf-8", "replace")) ssh.close()