|
| 1 | +"""Cross-platform service-manager facade. |
| 2 | +
|
| 3 | +Linux: shells out via the existing :class:`Systemd`. |
| 4 | +macOS: shells out via `brew services`. |
| 5 | +
|
| 6 | +Used by tools that need to talk to host-installed daemons rather |
| 7 | +than container ones — `shimkit db --on-host` is the first |
| 8 | +consumer. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from dataclasses import dataclass |
| 14 | +from typing import Literal |
| 15 | + |
| 16 | +from .command import CommandResult, CommandRunner |
| 17 | +from .platform import Platform |
| 18 | +from .systemd import Systemd |
| 19 | + |
| 20 | +ServiceState = Literal["running", "stopped", "missing"] |
| 21 | + |
| 22 | + |
| 23 | +@dataclass(frozen=True) |
| 24 | +class HostServiceResult: |
| 25 | + """Combined CommandResult-ish + state, returned by start/stop.""" |
| 26 | + |
| 27 | + ok: bool |
| 28 | + state: ServiceState |
| 29 | + stdout: str = "" |
| 30 | + stderr: str = "" |
| 31 | + |
| 32 | + |
| 33 | +class HostService: |
| 34 | + """Abstract interface implemented by SystemdHost / BrewServicesHost. |
| 35 | +
|
| 36 | + Tests mock the concrete subclasses directly. Producers ask for an |
| 37 | + implementation via :meth:`detect`; ``None`` means the host has no |
| 38 | + supported service manager. |
| 39 | + """ |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def detect(cls, platform: Platform | None = None) -> HostService | None: |
| 43 | + plat = platform or Platform.detect() |
| 44 | + if plat.is_linux: |
| 45 | + return SystemdHost() |
| 46 | + if plat.is_macos: |
| 47 | + return BrewServicesHost() |
| 48 | + return None |
| 49 | + |
| 50 | + def state(self, service: str) -> ServiceState: |
| 51 | + raise NotImplementedError |
| 52 | + |
| 53 | + def start(self, service: str) -> HostServiceResult: |
| 54 | + raise NotImplementedError |
| 55 | + |
| 56 | + def stop(self, service: str) -> HostServiceResult: |
| 57 | + raise NotImplementedError |
| 58 | + |
| 59 | + |
| 60 | +class SystemdHost(HostService): |
| 61 | + """Linux: shells out via :class:`Systemd`.""" |
| 62 | + |
| 63 | + def state(self, service: str) -> ServiceState: |
| 64 | + unit = Systemd.state(service if service.endswith(".service") else f"{service}.service") |
| 65 | + if not unit.exists: |
| 66 | + return "missing" |
| 67 | + return "running" if unit.active else "stopped" |
| 68 | + |
| 69 | + def start(self, service: str) -> HostServiceResult: |
| 70 | + r = Systemd.start(service) |
| 71 | + return _wrap(r, self.state(service)) |
| 72 | + |
| 73 | + def stop(self, service: str) -> HostServiceResult: |
| 74 | + r = Systemd.stop(service) |
| 75 | + return _wrap(r, self.state(service)) |
| 76 | + |
| 77 | + |
| 78 | +class BrewServicesHost(HostService): |
| 79 | + """macOS: shells out via `brew services <action> <name>`.""" |
| 80 | + |
| 81 | + def state(self, service: str) -> ServiceState: |
| 82 | + # `brew services list` output (whitespace columns): |
| 83 | + # Name Status User File |
| 84 | + # mysql started you ~/... |
| 85 | + # postgresql@16 none ... |
| 86 | + r = CommandRunner.run(["brew", "services", "list"]) |
| 87 | + if not r.ok: |
| 88 | + return "missing" |
| 89 | + for line in r.stdout.splitlines(): |
| 90 | + parts = line.split() |
| 91 | + if not parts or parts[0] != service: |
| 92 | + continue |
| 93 | + status = parts[1] if len(parts) > 1 else "none" |
| 94 | + if status in {"started", "scheduled"}: |
| 95 | + return "running" |
| 96 | + return "stopped" |
| 97 | + return "missing" |
| 98 | + |
| 99 | + def start(self, service: str) -> HostServiceResult: |
| 100 | + r = CommandRunner.run(["brew", "services", "start", service]) |
| 101 | + return _wrap(r, self.state(service)) |
| 102 | + |
| 103 | + def stop(self, service: str) -> HostServiceResult: |
| 104 | + r = CommandRunner.run(["brew", "services", "stop", service]) |
| 105 | + return _wrap(r, self.state(service)) |
| 106 | + |
| 107 | + |
| 108 | +def _wrap(r: CommandResult, state: ServiceState) -> HostServiceResult: |
| 109 | + return HostServiceResult(ok=r.ok, state=state, stdout=r.stdout, stderr=r.stderr) |
0 commit comments