install.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. """Install the Bot Desktop packages on the gateway host from a Desktop client.
  2. The install runs the distro command from ``runtime.install_command()`` (apt/dnf/pacman) as a child
  3. process on THIS host. Privilege comes from the same masked ``sudo.request`` card the terminal tool
  4. raises: ``sudo -n true`` is probed first (NOPASSWD / cached timestamp hosts never see a prompt); when
  5. a password is needed the caller-supplied ``ask_password`` blocks on the card and the value is written
  6. to sudo's stdin (``-S``) exactly once, never logged, never placed on the command line. Output lines
  7. stream through ``on_line`` so the pane can show apt's progress; the return value is the exit code.
  8. One install per profile at a time; a second request while one runs is refused.
  9. """
  10. from __future__ import annotations
  11. import contextlib
  12. import logging
  13. import os
  14. import selectors
  15. import shlex
  16. import shutil
  17. import signal
  18. import subprocess
  19. import threading
  20. from pathlib import Path
  21. import time
  22. from typing import Callable, Optional
  23. from hermes_constants import hermes_home_key
  24. from tools.bot_desktop import runtime
  25. logger = logging.getLogger(__name__)
  26. _install_lock = threading.Lock()
  27. _running: set[str] = set()
  28. # Installs are host-global but the gateway (Install card) and the CLI (`screen install`) are separate
  29. # processes: the in-process set alone let both drive dpkg for one profile at once. The slot is also a
  30. # non-blocking flock on a file in the profile's state dir, held for the life of the install.
  31. _slot_files: dict[str, object] = {}
  32. NO_SUDO = -2 # install_packages: unprivileged host without sudo; the on_line stream carried the command to run as root
  33. class InstallBusy(RuntimeError):
  34. pass
  35. def _try_flock(path: Path):
  36. """Open + non-blocking exclusive flock; None when another process holds it."""
  37. import fcntl # windows-footgun: ok — Linux-only runtime (is_supported_host)
  38. path.parent.mkdir(parents=True, exist_ok=True)
  39. fh = open(path, "a+", encoding="utf-8")
  40. try:
  41. fcntl.flock(fh.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
  42. except OSError:
  43. fh.close()
  44. return None
  45. return fh
  46. def assert_not_running() -> None:
  47. with _install_lock:
  48. if hermes_home_key() in _running:
  49. raise InstallBusy("an install is already running for this profile")
  50. fh = _try_flock(runtime.state_dir() / "install.lock")
  51. if fh is None:
  52. raise InstallBusy("an install is already running for this profile (another process)")
  53. fh.close()
  54. def claim() -> str:
  55. """Atomically take this profile's install slot; raises :class:`InstallBusy` when taken. A caller that
  56. claims before handing off to a worker passes ``claimed=True`` to :func:`install_packages`, which then
  57. owns releasing it — a check-then-spawn pair (``assert_not_running`` + later claim on the worker) lets
  58. two Install clicks both pass the check."""
  59. key = hermes_home_key()
  60. with _install_lock:
  61. if key in _running:
  62. raise InstallBusy("an install is already running for this profile")
  63. fh = _try_flock(runtime.state_dir() / "install.lock")
  64. if fh is None:
  65. raise InstallBusy("an install is already running for this profile (another process)")
  66. _running.add(key)
  67. _slot_files[key] = fh
  68. return key
  69. def release(key: str) -> None:
  70. with _install_lock:
  71. _running.discard(key)
  72. fh = _slot_files.pop(key, None)
  73. if fh is not None:
  74. fh.close() # closing drops the flock
  75. def install_packages(*, ask_password: Callable[[], str], on_line: Callable[[str], None],
  76. timeout_seconds: float = 900.0, claimed: bool = False) -> int:
  77. """Run the package install; returns the process exit code (0 = success, ``-1`` = cancelled,
  78. :data:`NO_SUDO` = unprivileged host without sudo — the command to run by hand was streamed).
  79. ``claimed=True``: the caller already holds the slot via :func:`claim`; it is released here either way."""
  80. key = hermes_home_key() if claimed else None
  81. try:
  82. if not runtime.is_supported_host():
  83. raise RuntimeError("Bot Desktop runs on Linux gateway hosts only")
  84. cmd = runtime.install_command()
  85. if cmd is None:
  86. raise RuntimeError("no supported package manager (apt-get, dnf, pacman) found on this host")
  87. if key is None:
  88. key = claim()
  89. return _run(cmd, ask_password=ask_password, on_line=on_line, timeout_seconds=timeout_seconds)
  90. finally:
  91. if key is not None:
  92. release(key)
  93. def _sudo_nopasswd() -> bool:
  94. try:
  95. return subprocess.run(["sudo", "-n", "true"], capture_output=True, timeout=3,
  96. stdin=subprocess.DEVNULL, check=False).returncode == 0
  97. except Exception:
  98. return False
  99. def _run(cmd: str, *, ask_password: Callable[[], str], on_line: Callable[[str], None],
  100. timeout_seconds: float) -> int:
  101. argv = shlex.split(cmd)
  102. stdin_payload: Optional[str] = None
  103. if argv[0] == "sudo":
  104. if shutil.which("sudo") is None:
  105. # Minimal containers ship no sudo: a password card would be a dead end. Hand the human the
  106. # exact command for the host instead.
  107. on_line(f"install needs root and this host has no sudo; run on the host as root: {cmd[len('sudo '):]}")
  108. return NO_SUDO
  109. if not _sudo_nopasswd():
  110. password = ask_password() or ""
  111. if not password:
  112. on_line("install cancelled: no sudo password provided")
  113. return -1
  114. # -S: read the password from stdin; -p '': no prompt text mixed into the streamed output.
  115. argv = ["sudo", "-S", "-p", "", *argv[1:]]
  116. stdin_payload = password + "\n"
  117. on_line(f"$ {cmd}")
  118. env = {"DEBIAN_FRONTEND": "noninteractive", "LC_ALL": "C.UTF-8"}
  119. # Short-lived and awaited: the group exists so _kill_group's timeout kill reaches apt/dnf under sudo.
  120. proc = subprocess.Popen( # windows-footgun: ok — Linux-only (is_supported_host)
  121. argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
  122. env={**os.environ, **env}, start_new_session=True) # detached-spawn: ok — awaited, group-killed on timeout
  123. try:
  124. if stdin_payload is not None:
  125. proc.stdin.write(stdin_payload.encode("utf-8")) # type: ignore[union-attr]
  126. proc.stdin.close() # type: ignore[union-attr]
  127. except OSError:
  128. pass
  129. try:
  130. if _drain_until(proc, on_line, time.monotonic() + timeout_seconds):
  131. return proc.wait()
  132. _kill_group(proc)
  133. on_line(f"install timed out after {timeout_seconds:.0f}s; the package manager may still be running as root")
  134. return proc.returncode if proc.returncode is not None else -9
  135. finally:
  136. proc.stdout.close() # type: ignore[union-attr]
  137. if proc.poll() is None: # the drain raised (a failing on_line sink): the slot is released, so no orphan
  138. _kill_group(proc)
  139. _TERM_GRACE_SECONDS = 5.0
  140. def _drain_until(proc: subprocess.Popen, on_line: Callable[[str], None], deadline: float) -> bool:
  141. """Stream ``proc.stdout`` lines to ``on_line`` until EOF (``True``) or ``deadline`` (``False``).
  142. Readiness-polled rather than a blocking ``for line in proc.stdout``: from an unprivileged Hermes no
  143. signal reaches a root-owned apt/dnf child, and that child keeps the pipe's write end open, so a
  144. blocking read would never see EOF and the profile's install slot would be held forever.
  145. """
  146. fd = proc.stdout.fileno() # type: ignore[union-attr]
  147. buf = b""
  148. with selectors.DefaultSelector() as sel:
  149. sel.register(fd, selectors.EVENT_READ)
  150. while True:
  151. remaining = deadline - time.monotonic()
  152. if remaining <= 0:
  153. return False
  154. if not sel.select(timeout=min(remaining, 1.0)):
  155. continue
  156. chunk = os.read(fd, 65536)
  157. if not chunk:
  158. if buf:
  159. on_line(buf.decode("utf-8", "replace"))
  160. return True
  161. *lines, buf = (buf + chunk).split(b"\n")
  162. for line in lines:
  163. on_line(line.decode("utf-8", "replace"))
  164. def _kill_group(proc: subprocess.Popen) -> None:
  165. """The package manager runs in its own session (start_new_session); killing only sudo would leave
  166. apt/dnf running as root with the dpkg lock while the slot is released, so the whole group goes: TERM
  167. first so dpkg can finish its transaction, KILL after the grace. Best effort — as non-root neither
  168. signal reaches a root-owned child, which is why the caller never waits on EOF."""
  169. def _group_gone() -> bool:
  170. try:
  171. os.killpg(proc.pid, 0) # windows-footgun: ok — Linux-only (is_supported_host)
  172. except ProcessLookupError:
  173. return True
  174. except PermissionError:
  175. return False # a root-owned child is still there
  176. return False
  177. for sig, grace in ((signal.SIGTERM, _TERM_GRACE_SECONDS), (signal.SIGKILL, 1.0)): # windows-footgun: ok — Linux-only (is_supported_host)
  178. with contextlib.suppress(ProcessLookupError, PermissionError):
  179. os.killpg(proc.pid, sig) # windows-footgun: ok — Linux-only (is_supported_host)
  180. with contextlib.suppress(subprocess.TimeoutExpired):
  181. proc.wait(timeout=grace)
  182. # The leader (sudo / the package manager) going away is not the end: wait for the whole group so a
  183. # TERM-ignoring descendant gets the KILL round instead of surviving with the dpkg lock.
  184. deadline = time.monotonic() + grace
  185. while time.monotonic() < deadline and not _group_gone():
  186. time.sleep(0.05)
  187. if _group_gone():
  188. return