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 <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2024-03-07 17:54:42 -08:00 committed by James Valleroy
parent 22671510e6
commit a488ee4d43
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

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