Displaying any wrolpi-* service in the Controller, now that we have these scripts.

This commit is contained in:
Roland 2026-03-05 18:11:25 -07:00
parent 71c2b6cd6a
commit e8bc2b32c4
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 326 additions and 53 deletions

View file

@ -79,6 +79,9 @@ def get_service_status(name: str) -> dict:
"""
service_config = get_service_config(name)
if not service_config:
# Allow dynamically discovered wrolpi-* services.
if name.startswith("wrolpi-"):
return get_discovered_service_status(name)
return {"error": f"Unknown service: {name}"}
systemd_name = service_config.get("systemd_name", name)
@ -142,9 +145,115 @@ def get_service_status(name: str) -> dict:
}
def discover_running_wrolpi_services() -> list[str]:
"""
Discover running wrolpi-* systemd services not in the managed services config.
Returns:
list of systemd unit names (e.g., ["wrolpi-fix-media-permissions.service"])
"""
managed_systemd_names = {
s.get("systemd_name", s["name"])
for s in get_managed_services()
}
try:
result = subprocess.run(
["systemctl", "list-units", "wrolpi-*", "--type=service", "--no-legend", "--no-pager"],
capture_output=True,
text=True,
timeout=10,
)
if result.returncode != 0:
return []
except (subprocess.TimeoutExpired, FileNotFoundError):
return []
discovered = []
for line in result.stdout.strip().splitlines():
parts = line.split()
if not parts:
continue
# Unit name is the first column, e.g. "wrolpi-fix-media-permissions.service"
unit_name = parts[0]
# Strip .service suffix to get the base name
service_name = unit_name.removesuffix(".service")
if service_name not in managed_systemd_names:
discovered.append(service_name)
return discovered
def get_discovered_service_status(name: str) -> dict:
"""
Get status of a dynamically discovered wrolpi-* service (not in managed config).
Args:
name: Service name (e.g., "wrolpi-fix-media-permissions")
Returns:
dict with status fields
"""
try:
active_result = subprocess.run(
["systemctl", "is-active", name],
capture_output=True,
text=True,
timeout=5,
)
is_active = active_result.stdout.strip()
enabled_result = subprocess.run(
["systemctl", "is-enabled", name],
capture_output=True,
text=True,
timeout=5,
)
is_enabled = enabled_result.stdout.strip()
except (subprocess.TimeoutExpired, FileNotFoundError):
return {
"name": name,
"systemd_name": name,
"status": "unknown",
"active": "unknown",
"enabled": False,
"port": None,
"viewable": False,
"view_path": "",
"use_https": False,
"description": "",
"error": "systemctl not available",
}
if is_active == "active":
status = "running"
elif is_active == "activating":
status = "running"
elif is_active == "inactive":
status = "stopped"
elif is_active == "failed":
status = "failed"
else:
status = "unknown"
return {
"name": name,
"systemd_name": name,
"status": status,
"active": is_active,
"enabled": is_enabled == "enabled",
"port": None,
"viewable": False,
"view_path": "",
"use_https": False,
"description": "",
}
def get_all_services_status() -> list[dict]:
"""
Get status of all managed services.
Get status of all managed services, plus any running wrolpi-* services
discovered dynamically.
Services with show_only_when_running=True are excluded when not running.
@ -158,9 +267,26 @@ def get_all_services_status() -> list[dict]:
if service.get("show_only_when_running") and status.get("status") != "running":
continue
results.append(status)
# Discover any running wrolpi-* services not in the managed list.
for name in discover_running_wrolpi_services():
status = get_discovered_service_status(name)
if status.get("status") == "running":
results.append(status)
return results
def _get_systemd_name(name: str) -> Optional[str]:
"""Get the systemd unit name for a service, supporting both managed and discovered wrolpi-* services."""
service_config = get_service_config(name)
if service_config:
return service_config.get("systemd_name", name)
if name.startswith("wrolpi-"):
return name
return None
def start_service(name: str) -> dict:
"""
Start a managed service.
@ -171,11 +297,9 @@ def start_service(name: str) -> dict:
Returns:
dict with success status
"""
service_config = get_service_config(name)
if not service_config:
systemd_name = _get_systemd_name(name)
if not systemd_name:
return {"success": False, "error": f"Unknown service: {name}"}
systemd_name = service_config.get("systemd_name", name)
result = _run_systemctl("start", systemd_name)
return {
@ -196,11 +320,9 @@ def stop_service(name: str) -> dict:
Returns:
dict with success status
"""
service_config = get_service_config(name)
if not service_config:
systemd_name = _get_systemd_name(name)
if not systemd_name:
return {"success": False, "error": f"Unknown service: {name}"}
systemd_name = service_config.get("systemd_name", name)
result = _run_systemctl("stop", systemd_name)
return {
@ -221,12 +343,10 @@ def restart_service(name: str) -> dict:
Returns:
dict with success status
"""
service_config = get_service_config(name)
if not service_config:
systemd_name = _get_systemd_name(name)
if not systemd_name:
return {"success": False, "error": f"Unknown service: {name}"}
systemd_name = service_config.get("systemd_name", name)
# Special handling for self-restart: use Popen to avoid blocking
# (the process will be killed before subprocess.run can return)
if systemd_name == "wrolpi-controller":
@ -266,11 +386,9 @@ def enable_service(name: str) -> dict:
Returns:
dict with success status
"""
service_config = get_service_config(name)
if not service_config:
systemd_name = _get_systemd_name(name)
if not systemd_name:
return {"success": False, "error": f"Unknown service: {name}"}
systemd_name = service_config.get("systemd_name", name)
result = _run_systemctl("enable", systemd_name)
return {
@ -291,11 +409,9 @@ def disable_service(name: str) -> dict:
Returns:
dict with success status
"""
service_config = get_service_config(name)
if not service_config:
systemd_name = _get_systemd_name(name)
if not systemd_name:
return {"success": False, "error": f"Unknown service: {name}"}
systemd_name = service_config.get("systemd_name", name)
result = _run_systemctl("disable", systemd_name)
return {
@ -318,12 +434,10 @@ def get_service_logs(name: str, lines: int = 100, since: Optional[str] = None) -
Returns:
dict with logs and metadata
"""
service_config = get_service_config(name)
if not service_config:
systemd_name = _get_systemd_name(name)
if not systemd_name:
return {"error": f"Unknown service: {name}"}
systemd_name = service_config.get("systemd_name", name)
cmd = ["journalctl", "-u", systemd_name, "-n", str(lines), "--no-pager"]
if since:
cmd.extend(["--since", since])