From f501bc99abbee89b3d9bcee76c02638414108924 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 11 Dec 2024 17:04:46 -0800 Subject: [PATCH] action_utils: Add utility to ensure that service is stopped - This could be used when the daemon can't be running while configuration is being updated. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Veiko Aasa --- plinth/action_utils.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/plinth/action_utils.py b/plinth/action_utils.py index de1973d2d..51ba3e866 100644 --- a/plinth/action_utils.py +++ b/plinth/action_utils.py @@ -64,6 +64,20 @@ def service_ensure_running(service_name): service_disable(service_name) +@contextmanager +def service_ensure_stopped(service_name): + """Ensure a service is stopped and return to previous state.""" + starting_state = service_is_running(service_name) + if starting_state: + service_disable(service_name) + + try: + yield starting_state + finally: + if starting_state: + service_enable(service_name) + + def service_is_enabled(service_name, strict_check=False): """Check if service is enabled in systemd.