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 <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa 2024-12-11 17:04:46 -08:00 committed by Veiko Aasa
parent 0ea0a8b33c
commit f501bc99ab
No known key found for this signature in database
GPG Key ID: 478539CAE680674E

View File

@ -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.