fix(services): only the exit status decides manage_service

manage_service returned success for every action, so a failed start or
a missing init script showed up as done. The command now prints its
exit status after a marker, and only that status decides.

Closes #3
This commit is contained in:
Christian Manivong committed 2026-11-01 21:43:56 +01:00
1 parent 45f5b65427
commit 16e0fa9d5e
3 files changed
+78 -3

No files matched your search

+20 -3
View File
@@ -32,6 +32,19 @@ def _split_status(raw: str) -> tuple[str, int | None]:
return output, int(statuses[-1]) if statuses else None
def _status_result(raw: str, command: str) -> dict[str, Any]:
"""``{"success", "output"}`` from what a command marked with ``_RC_MARKER`` printed.
Only the exit status decides; output without a status is no success.
"""
output, status = _split_status(raw)
if status is None:
return {"success": False, "output": output or "No exit status came back from the device."}
if status:
return {"success": False, "output": output or f"{command} exited with status {status}."}
return {"success": True, "output": output}
class OpenWrtSystemMixin:
"""Mixin providing system-level NAPALM getters.
@@ -389,15 +402,19 @@ class OpenWrtSystemMixin:
return result
def manage_service(self, name: str, action: str) -> dict[str, Any]:
"""Execute a lifecycle action (start/stop/restart/enable/disable) on a service."""
"""Execute a lifecycle action (start/stop/restart/enable/disable) on a service.
Only the init script's exit status decides ``success``; a missing script
or a failed action comes back with what the device printed.
"""
import re as _re
if not _re.match(r"^[a-zA-Z0-9_\-]+$", name):
raise ValueError(f"Invalid service name: {name!r}")
if action not in ("start", "stop", "restart", "enable", "disable"):
raise ValueError(f"Invalid action: {action!r}")
output = self._send_command(f"/etc/init.d/{name} {action} 2>&1")
return {"success": True, "output": output}
command = f"/etc/init.d/{name} {action}"
return _status_result(self._send_command(f"{command} 2>&1; echo {_RC_MARKER}$?"), command)
def get_available_updates(self) -> list[dict[str, Any]]:
"""Return list of upgradable packages from the local package manager cache.