From e5c76327004d5940a3b6a9fd063d1af51adcd918 Mon Sep 17 00:00:00 2001 From: Yellowcooln <12516003+yellowcooln@users.noreply.github.com> Date: Thu, 23 Apr 2026 11:05:59 -0400 Subject: [PATCH] Handle Buildroot service restarts --- repeater/service_utils.py | 41 +++++++++++++++++++++++++++++--- repeater/web/update_endpoints.py | 4 ++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/repeater/service_utils.py b/repeater/service_utils.py index 14a1481..a127e48 100644 --- a/repeater/service_utils.py +++ b/repeater/service_utils.py @@ -4,22 +4,57 @@ Provides functions for service control operations like restart. """ import logging +import os import subprocess from typing import Tuple logger = logging.getLogger("ServiceUtils") +INIT_SCRIPT = "/etc/init.d/S80pymc-repeater" + + +def is_buildroot() -> bool: + if os.path.exists("/etc/pymc-image-build-id"): + return True + if os.path.exists("/etc/os-release"): + try: + with open("/etc/os-release", "r", encoding="utf-8") as handle: + return any(line.strip() == "ID=buildroot" for line in handle) + except OSError: + return False + return False def restart_service() -> Tuple[bool, str]: """ - Restart the pymc-repeater service via systemctl. + Restart the pymc-repeater service. - Tries polkit-based restart first (plain systemctl), then falls back - to sudo-based restart (requires sudoers.d rule installed by manage.sh). + On Buildroot/Luckfox, use the shipped init script directly. + On systemd hosts, try polkit-based restart first (plain systemctl), then + fall back to sudo-based restart (requires sudoers.d rule installed by + manage.sh). Returns: Tuple[bool, str]: (success, message) """ + if is_buildroot(): + if not os.path.exists(INIT_SCRIPT): + logger.error("Buildroot init script not found: %s", INIT_SCRIPT) + return False, f"init script not found: {INIT_SCRIPT}" + + try: + subprocess.Popen( + ["/bin/sh", "-c", f"sleep 1; exec {INIT_SCRIPT} restart >/dev/null 2>&1"], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + stdin=subprocess.DEVNULL, + start_new_session=True, + ) + logger.info("Service restart scheduled via Buildroot init script") + return True, "Service restart initiated" + except Exception as exc: + logger.error(f"Buildroot restart failed: {exc}") + return False, f"Restart failed: {exc}" + # Try polkit-based restart first (works on bare metal / VMs with polkit running) try: result = subprocess.run( diff --git a/repeater/web/update_endpoints.py b/repeater/web/update_endpoints.py index 941fa78..800c8d6 100644 --- a/repeater/web/update_endpoints.py +++ b/repeater/web/update_endpoints.py @@ -685,6 +685,10 @@ def _migrate_service_unit() -> None: """Strip legacy PYTHONPATH, fix WorkingDirectory, and ensure ExecStart uses the venv python in the systemd service unit. """ + if os.path.exists("/etc/pymc-image-build-id"): + logger.info("[Update] Buildroot image detected, skipping systemd unit migration.") + return + import subprocess as _sp _SVC_UNIT = "/etc/systemd/system/pymc-repeater.service" _VENV_PYTHON = "/opt/pymc_repeater/venv/bin/python"