spoolbuddy_ssh.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. """SSH-based update service for SpoolBuddy devices.
  2. Instead of the daemon updating itself (fragile: permission issues, self-modifying
  3. code, hardcoded branch), Bambuddy SSHes into the SpoolBuddy Pi and drives the
  4. update remotely: git fetch/checkout, pip install, systemctl restart.
  5. Uses `asyncssh` (pure-Python async SSH client) rather than shelling out to the
  6. OpenSSH `ssh` binary. The subprocess approach fails in Docker: both `ssh` and
  7. `ssh-keygen` call `getpwuid(getuid())` during startup and abort with
  8. "No user exists for uid <N>" when the container runs under a UID that is not
  9. listed in /etc/passwd (e.g. PUID=1000 on python:3.13-slim, which only has
  10. entries for root). asyncssh does all of its work in-process.
  11. """
  12. import asyncio
  13. import logging
  14. import os
  15. from pathlib import Path
  16. import asyncssh
  17. from cryptography.hazmat.primitives import serialization
  18. from cryptography.hazmat.primitives.asymmetric import ed25519
  19. from backend.app.core.config import settings
  20. logger = logging.getLogger(__name__)
  21. SSH_USER = "spoolbuddy"
  22. DEFAULT_INSTALL_PATH = "/opt/bambuddy"
  23. # Project root — where the `.git` directory lives for native installs and for
  24. # Docker containers that bind-mount the repo. This is intentionally distinct
  25. # from `settings.base_dir`, which points at the persistent *data* directory
  26. # (e.g. `DATA_DIR=/app/data` in Docker) and therefore never contains `.git`.
  27. # `backend/app/services/spoolbuddy_ssh.py` → parents[3] = project root.
  28. _APP_DIR = Path(__file__).resolve().parents[3]
  29. # Note for Docker: asyncssh.connect() internally calls getpass.getuser() to
  30. # resolve the *local* username for ~/.ssh/config host matching. Under an
  31. # arbitrary PUID with no /etc/passwd entry this would raise OSError. The
  32. # Dockerfile sets LOGNAME/USER/HOME so getpass.getuser() succeeds via env-var
  33. # lookup before ever touching the passwd database.
  34. def _get_ssh_key_dir() -> Path:
  35. """Return (and create if needed) the directory for SpoolBuddy SSH keys."""
  36. key_dir = settings.base_dir / "spoolbuddy" / "ssh"
  37. if not key_dir.exists():
  38. key_dir.mkdir(mode=0o700, parents=True)
  39. return key_dir
  40. async def get_or_create_keypair() -> tuple[Path, Path]:
  41. """Return (private_key_path, public_key_path), generating if missing.
  42. Uses the in-process `cryptography` library instead of shelling out to
  43. `ssh-keygen`. The subprocess approach fails inside Docker containers when
  44. the image runs under an arbitrary UID (e.g. PUID=1001) that is not listed
  45. in /etc/passwd — `ssh-keygen` calls `getpwuid()` for the current user's
  46. home directory and aborts with "no user exists for uid <N>".
  47. """
  48. key_dir = _get_ssh_key_dir()
  49. private_key = key_dir / "id_ed25519"
  50. public_key = key_dir / "id_ed25519.pub"
  51. if private_key.exists() and public_key.exists():
  52. return private_key, public_key
  53. logger.info("Generating SSH keypair for SpoolBuddy updates")
  54. priv_obj = ed25519.Ed25519PrivateKey.generate()
  55. pub_obj = priv_obj.public_key()
  56. private_bytes = priv_obj.private_bytes(
  57. encoding=serialization.Encoding.PEM,
  58. format=serialization.PrivateFormat.OpenSSH,
  59. encryption_algorithm=serialization.NoEncryption(),
  60. )
  61. public_bytes = pub_obj.public_bytes(
  62. encoding=serialization.Encoding.OpenSSH,
  63. format=serialization.PublicFormat.OpenSSH,
  64. )
  65. # OpenSSH public format has no comment field by default; append one to match
  66. # the previous ssh-keygen output so the authorized_keys line is identifiable.
  67. public_line = public_bytes + b" bambuddy-spoolbuddy\n"
  68. private_key.write_bytes(private_bytes)
  69. private_key.chmod(0o600)
  70. public_key.write_bytes(public_line)
  71. logger.info("SSH keypair generated at %s", key_dir)
  72. return private_key, public_key
  73. async def get_public_key() -> str:
  74. """Return the SSH public key content for pairing."""
  75. _, public_key = await get_or_create_keypair()
  76. return public_key.read_text().strip()
  77. def detect_current_branch() -> str:
  78. """Detect the git branch Bambuddy is running on.
  79. Reads `.git/HEAD` directly from the application root (``_APP_DIR``) rather
  80. than shelling out to `git`. The application root is deliberately distinct
  81. from ``settings.base_dir``: in Docker, ``base_dir`` points at the data
  82. volume (``/app/data``) which never contains ``.git``, while the repo is
  83. bind-mounted (or COPYd) to ``/app``. This works for native installs,
  84. bare Docker containers (no ``.git`` — fall through to the env var), and
  85. Docker containers that bind-mount the repo (``.git`` is present, no
  86. ``git`` binary required, and no ``getpwuid()`` call that could fail under
  87. an arbitrary PUID).
  88. Fallback order: ``.git/HEAD`` → ``GIT_BRANCH`` env var → ``"main"``.
  89. """
  90. git_path = _APP_DIR / ".git"
  91. try:
  92. if git_path.exists():
  93. # Git worktrees use a file containing `gitdir: <path>` instead of
  94. # a directory — follow the pointer.
  95. if git_path.is_file():
  96. content = git_path.read_text(encoding="utf-8").strip()
  97. if content.startswith("gitdir:"):
  98. git_path = (_APP_DIR / content.removeprefix("gitdir:").strip()).resolve()
  99. head_file = git_path / "HEAD"
  100. if head_file.is_file():
  101. head = head_file.read_text(encoding="utf-8").strip()
  102. # Normal case: `ref: refs/heads/<branch>`.
  103. # Detached HEAD stores a raw commit hash — fall through to env var.
  104. if head.startswith("ref: refs/heads/"):
  105. return head.removeprefix("ref: refs/heads/").strip()
  106. except OSError as exc:
  107. logger.debug("Could not read .git/HEAD, falling back: %s", exc)
  108. return os.environ.get("GIT_BRANCH", "main")
  109. async def _run_ssh_command(
  110. ip: str,
  111. command: str,
  112. private_key: Path,
  113. timeout: int = 60,
  114. ) -> tuple[int, str, str]:
  115. """Execute a command on a SpoolBuddy device via SSH.
  116. Uses asyncssh rather than the OpenSSH `ssh` binary — see module docstring
  117. for the Docker/PUID rationale.
  118. Returns (returncode, stdout, stderr). On connection failure the return
  119. code is 255 (matching `ssh`'s own convention) and stderr carries the
  120. asyncssh error message. On timeout the return code is -1.
  121. """
  122. try:
  123. async with asyncio.timeout(timeout):
  124. async with asyncssh.connect(
  125. host=ip,
  126. username=SSH_USER,
  127. client_keys=[str(private_key)],
  128. known_hosts=None, # equivalent to StrictHostKeyChecking=no + UserKnownHostsFile=/dev/null
  129. config=[], # do not load ~/.ssh/config — HOME may not resolve under arbitrary Docker PUIDs
  130. connect_timeout=10,
  131. ) as conn:
  132. result = await conn.run(command, check=False)
  133. except TimeoutError:
  134. return -1, "", "SSH command timed out"
  135. except (asyncssh.Error, OSError) as exc:
  136. return 255, "", str(exc)
  137. stdout = result.stdout if isinstance(result.stdout, str) else (result.stdout or b"").decode(errors="replace")
  138. stderr = result.stderr if isinstance(result.stderr, str) else (result.stderr or b"").decode(errors="replace")
  139. # asyncssh's exit_status is None when the remote closed without setting one
  140. returncode = result.exit_status if result.exit_status is not None else 0
  141. return returncode, stdout, stderr
  142. async def perform_ssh_update(device_id: str, ip_address: str, install_path: str | None = None) -> None:
  143. """SSH into a SpoolBuddy device and update it to match Bambuddy's branch.
  144. Updates device.update_status/update_message in the DB and broadcasts
  145. progress via WebSocket at each step.
  146. """
  147. from sqlalchemy import select
  148. from backend.app.api.routes.spoolbuddy import ws_manager
  149. from backend.app.core.database import async_session
  150. from backend.app.models.spoolbuddy_device import SpoolBuddyDevice
  151. install_path = install_path or DEFAULT_INSTALL_PATH
  152. branch = detect_current_branch()
  153. async def _update_progress(status: str, message: str) -> None:
  154. """Update device status in DB and broadcast via WebSocket."""
  155. async with async_session() as db:
  156. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  157. device = result.scalar_one_or_none()
  158. if device:
  159. device.update_status = status
  160. device.update_message = message[:255] if message else None
  161. if status in ("complete", "error"):
  162. device.pending_command = None
  163. await db.commit()
  164. await ws_manager.broadcast(
  165. {
  166. "type": "spoolbuddy_update",
  167. "device_id": device_id,
  168. "update_status": status,
  169. "update_message": message[:255] if message else None,
  170. }
  171. )
  172. try:
  173. private_key, _ = await get_or_create_keypair()
  174. # Step 1: Test SSH connectivity
  175. await _update_progress("updating", "Connecting via SSH...")
  176. rc, _, stderr = await _run_ssh_command(ip_address, "echo ok", private_key)
  177. if rc != 0:
  178. await _update_progress("error", f"SSH connection failed: {stderr[:200]}")
  179. return
  180. # Step 2: Git fetch
  181. await _update_progress("updating", f"Fetching latest code (branch: {branch})...")
  182. rc, _, stderr = await _run_ssh_command(
  183. ip_address,
  184. f"cd {install_path} && git -c safe.directory={install_path} fetch origin {branch}",
  185. private_key,
  186. timeout=120,
  187. )
  188. if rc != 0:
  189. await _update_progress("error", f"git fetch failed: {stderr[:200]}")
  190. return
  191. # Step 3: Git checkout + reset
  192. await _update_progress("updating", "Applying update...")
  193. rc, _, stderr = await _run_ssh_command(
  194. ip_address,
  195. f"cd {install_path} && git -c safe.directory={install_path} checkout {branch} "
  196. f"&& git -c safe.directory={install_path} reset --hard origin/{branch}",
  197. private_key,
  198. )
  199. if rc != 0:
  200. await _update_progress("error", f"git checkout/reset failed: {stderr[:200]}")
  201. return
  202. # Step 4: Install dependencies
  203. await _update_progress("updating", "Installing dependencies...")
  204. venv_pip = f"{install_path}/spoolbuddy/venv/bin/pip"
  205. rc, _, stderr = await _run_ssh_command(
  206. ip_address,
  207. f"{venv_pip} install --upgrade spidev gpiod smbus2 httpx 2>&1",
  208. private_key,
  209. timeout=120,
  210. )
  211. if rc != 0:
  212. logger.warning("SpoolBuddy %s: pip install returned non-zero (continuing): %s", device_id, stderr[:200])
  213. # Step 5: Restart daemon
  214. await _update_progress("updating", "Restarting daemon...")
  215. rc, _, stderr = await _run_ssh_command(
  216. ip_address,
  217. "sudo /usr/bin/systemctl restart spoolbuddy.service",
  218. private_key,
  219. )
  220. if rc != 0:
  221. await _update_progress("error", f"Service restart failed: {stderr[:200]}")
  222. return
  223. # Step 6: Clear browser cache and restart kiosk
  224. # Remove Chromium's Service Worker + cache storage to prevent stale frontend
  225. await _run_ssh_command(
  226. ip_address,
  227. "sudo find /home -maxdepth 5 -path '*/chromium/Default/Service Worker' -type d -exec rm -rf {} + 2>/dev/null; true",
  228. private_key,
  229. )
  230. rc, _, stderr = await _run_ssh_command(
  231. ip_address,
  232. "sudo /usr/bin/systemctl restart getty@tty1.service",
  233. private_key,
  234. )
  235. if rc != 0:
  236. logger.warning("SpoolBuddy %s: kiosk restart failed (non-fatal): %s", device_id, stderr[:200])
  237. logger.info("SpoolBuddy %s: SSH update complete (branch=%s)", device_id, branch)
  238. except Exception as e:
  239. logger.error("SpoolBuddy %s: SSH update failed: %s", device_id, e)
  240. await _update_progress("error", f"Update failed: {str(e)[:200]}")