diff --git a/plinth/action_utils.py b/plinth/action_utils.py index 552194756..83c4a4425 100644 --- a/plinth/action_utils.py +++ b/plinth/action_utils.py @@ -135,6 +135,14 @@ def service_reload(service_name): service_action(service_name, 'reload') +def service_try_reload_or_restart(service_name): + """Reload a service if it supports reloading, otherwise restart. + + Do nothing if service is not running. + """ + service_action(service_name, 'try-reload-or-restart') + + def service_action(service_name, action): """Perform the given action on the service_name.""" subprocess.run(['systemctl', action, service_name], diff --git a/plinth/privileged/__init__.py b/plinth/privileged/__init__.py index 3b4a81fc2..9c1093b9d 100644 --- a/plinth/privileged/__init__.py +++ b/plinth/privileged/__init__.py @@ -5,11 +5,12 @@ from .config import dropin_is_valid, dropin_link, dropin_unlink from .packages import (filter_conffile_packages, install, is_package_manager_busy, remove, update) from .service import (disable, enable, is_enabled, is_running, mask, reload, - restart, start, stop, try_restart, unmask) + restart, start, stop, try_reload_or_restart, try_restart, + unmask) __all__ = [ 'filter_conffile_packages', 'install', 'is_package_manager_busy', 'remove', 'update', 'disable', 'enable', 'is_enabled', 'is_running', 'mask', - 'reload', 'restart', 'start', 'stop', 'try_restart', 'unmask', - 'dropin_is_valid', 'dropin_link', 'dropin_unlink' + 'reload', 'restart', 'start', 'stop', 'try_reload_or_restart', + 'try_restart', 'unmask', 'dropin_is_valid', 'dropin_link', 'dropin_unlink' ] diff --git a/plinth/privileged/service.py b/plinth/privileged/service.py index f9e5ffad9..d9c5450e3 100644 --- a/plinth/privileged/service.py +++ b/plinth/privileged/service.py @@ -57,6 +57,16 @@ def reload(service: str): action_utils.service_reload(service) +@privileged +def try_reload_or_restart(service: str): + """Reload a service if it supports reloading, otherwise restart. + + Do nothing if service is not running. + """ + _assert_service_is_managed_by_plinth(service) + action_utils.service_try_reload_or_restart(service) + + @privileged def mask(service: str): """Mask a service.""" diff --git a/plinth/tests/test_action_utils.py b/plinth/tests/test_action_utils.py index b347cedae..3887c29ef 100644 --- a/plinth/tests/test_action_utils.py +++ b/plinth/tests/test_action_utils.py @@ -16,6 +16,7 @@ from plinth.action_utils import (get_addresses, get_hostname, service_is_enabled, service_is_running, service_reload, service_restart, service_start, service_stop, + service_try_reload_or_restart, service_try_restart, service_unmask) UNKNOWN = 'unknowndeamon' @@ -86,6 +87,8 @@ def test_service_actions(mock): mock.assert_called_with(UNKNOWN, 'try-restart') service_reload(UNKNOWN) mock.assert_called_with(UNKNOWN, 'reload') + service_try_reload_or_restart(UNKNOWN) + mock.assert_called_with(UNKNOWN, 'try-reload-or-restart') @pytest.mark.usefixtures('needs_root')