fix(cli): tolerate systems without systemctl in upgrade/doctor flows

Bug: On systems without systemd (Unraid OS 7.2.4, BSD, macOS containers,
and other non-systemd Linux hosts), running `muxplex upgrade` or
`muxplex update` crashed immediately with:

  FileNotFoundError: [Errno 2] No such file or directory: 'systemctl'

The check ran unconditionally before any install step, so the upgrade
aborted without even attempting to fetch the new version. Introduced
in v0.6.0.

Fix: Add a module-level `_have_systemctl() -> bool` helper to cli.py
(and service.py) that gates every systemd-specific operation behind
`shutil.which("systemctl") is not None`.

Call sites guarded in cli.py (upgrade() function):
  1. systemctl --user is-active muxplex  (stop-before-upgrade check)
  2. systemctl --user stop muxplex       (pre-install service stop)
  3. Service file regeneration step      (service_install() call)
  4. systemctl --user is-enabled muxplex (post-install restart check)
  5. systemctl --user daemon-reload      (post-install daemon reload)
  6. systemctl --user start muxplex      (post-install service start)

Behaviour on no-systemd systems:
  - Skips the is-active check (treats as unmanaged; prints skip note).
  - Skips the stop step.
  - Still performs the uv/pip install.
  - Skips service-file regeneration (prints skip note).
  - Skips the daemon-reload / start steps.
  - Prints: '! systemd not detected — restart muxplex manually to pick
    up the new version' with the running PID if pgrep finds one.
  - Still runs `muxplex doctor` for verification (no systemd required).

doctor() change:
  - On non-darwin platforms with no systemctl, now prints:
    '! Service: systemd not available on this platform'
    instead of silently doing nothing or crashing.

service.py change:
  - Public API functions (service_install, service_uninstall,
    service_start, service_stop, service_restart, service_status,
    service_logs) now check _have_systemctl() on the Linux path.
  - When absent, print a clear, friendly error pointing the user to
    `muxplex serve` instead of letting subprocess raise FileNotFoundError.

Tests added (muxplex/tests/test_cli.py, +8 tests):
  - test_have_systemctl_helper_exists
  - test_have_systemctl_returns_bool
  - test_upgrade_no_systemctl_runs_to_completion (regression)
  - test_upgrade_no_systemctl_prints_skip_note
  - test_upgrade_no_systemctl_prints_manual_restart_note
  - test_upgrade_with_systemctl_runs_systemd_commands
  - test_doctor_no_systemctl_shows_graceful_message
  - test_doctor_no_systemctl_does_not_crash
This commit is contained in:
Brian Krabach
2026-05-17 12:26:20 -07:00
parent 794e329938
commit d7d07ec07e
10 changed files with 392 additions and 73 deletions
+44 -9
View File
@@ -74,6 +74,11 @@ def _is_darwin() -> bool:
return sys.platform == "darwin"
def _have_systemctl() -> bool:
"""Return True if systemctl is on PATH (gates all systemd service operations)."""
return shutil.which("systemctl") is not None
def _resolve_muxplex_bin() -> str:
"""Return the muxplex binary path.
@@ -116,8 +121,6 @@ def _prompt_host_if_localhost() -> None:
# ---------------------------------------------------------------------------
def _show_tls_nudge_if_needed() -> None:
"""Show TLS setup nudge if host is network and TLS is not configured."""
from muxplex.settings import load_settings
@@ -128,6 +131,8 @@ def _show_tls_nudge_if_needed() -> None:
if host != "127.0.0.1" and not tls_cert:
print(" Tip: Enable HTTPS for clipboard support: muxplex setup-tls")
def _systemd_install() -> None:
muxplex_bin = _resolve_muxplex_bin()
safe_path = os.environ.get("PATH", "/usr/local/bin:/usr/bin:/bin")
@@ -235,57 +240,87 @@ def _launchd_logs() -> None:
# ---------------------------------------------------------------------------
def _no_systemctl_error(command: str) -> None:
"""Print a clear error when systemctl is not available."""
print(
f" ERROR: 'muxplex service {command}' requires systemctl, which was not found on PATH.",
file=sys.stderr,
)
print(
" This system does not appear to use systemd (e.g. Unraid, BSD, macOS, container).",
file=sys.stderr,
)
print(
" Run muxplex serve directly to start the server without a service manager.",
file=sys.stderr,
)
def service_install() -> None:
"""Install the muxplex service unit for the current user."""
if _is_darwin():
_launchd_install()
else:
elif _have_systemctl():
_systemd_install()
else:
_no_systemctl_error("install")
def service_uninstall() -> None:
"""Remove the muxplex service unit for the current user."""
if _is_darwin():
_launchd_uninstall()
else:
elif _have_systemctl():
_systemd_uninstall()
else:
_no_systemctl_error("uninstall")
def service_start() -> None:
"""Start the muxplex service."""
if _is_darwin():
_launchd_start()
else:
elif _have_systemctl():
_systemd_start()
else:
_no_systemctl_error("start")
def service_stop() -> None:
"""Stop the muxplex service."""
if _is_darwin():
_launchd_stop()
else:
elif _have_systemctl():
_systemd_stop()
else:
_no_systemctl_error("stop")
def service_restart() -> None:
"""Restart the muxplex service."""
if _is_darwin():
_launchd_restart()
else:
elif _have_systemctl():
_systemd_restart()
else:
_no_systemctl_error("restart")
def service_status() -> None:
"""Print the current status of the muxplex service."""
if _is_darwin():
_launchd_status()
else:
elif _have_systemctl():
_systemd_status()
else:
_no_systemctl_error("status")
def service_logs() -> None:
"""Stream or print logs for the muxplex service."""
if _is_darwin():
_launchd_logs()
else:
elif _have_systemctl():
_systemd_logs()
else:
_no_systemctl_error("logs")