From a488ee4d435509d055eec2f360f885d6b271637a Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 7 Mar 2024 17:54:42 -0800 Subject: [PATCH] action_utils: Implement method for starting a service temporarily Used when a service is needed for an operation but we don't wish to keep it running after the operation. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/action_utils.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/plinth/action_utils.py b/plinth/action_utils.py index 3e6bffaca..4bf5a1a1b 100644 --- a/plinth/action_utils.py +++ b/plinth/action_utils.py @@ -48,6 +48,20 @@ def service_is_running(servicename): return False +@contextmanager +def service_ensure_running(service_name): + """Ensure a service is running and return to previous state.""" + starting_state = service_is_running(service_name) + if not starting_state: + service_enable(service_name) + + try: + yield starting_state + finally: + if not starting_state: + service_disable(service_name) + + def service_is_enabled(service_name, strict_check=False): """Check if service is enabled in systemd.