add restart and reload functionality to yunohost service

This commit is contained in:
Pierre Bourré 2019-01-05 03:11:08 +01:00
parent 1c80e7242b
commit d1a822211c
2 changed files with 83 additions and 2 deletions

View file

@ -1178,6 +1178,33 @@ service:
nargs: "+" nargs: "+"
metavar: NAME metavar: NAME
### service_reload()
reload:
action_help: Reload one or more services
arguments:
names:
help: Service name to reload
nargs: "+"
metavar: NAME
### service_restart()
restart:
action_help: Restart one or more services. If the services are not running yet, they will be started.
arguments:
names:
help: Service name to restart
nargs: "+"
metavar: NAME
### service_reload_or_restart()
reload_or_restart:
action_help: Reload one or more services if they support it. If not, restart them instead. If the services are not running yet, they will be started.
arguments:
names:
help: Service name to reload or restart
nargs: "+"
metavar: NAME
### service_enable() ### service_enable()
enable: enable:
action_help: Enable one or more services action_help: Enable one or more services

View file

@ -152,6 +152,60 @@ def service_stop(names):
logger.debug(m18n.n('service_already_stopped', service=name)) logger.debug(m18n.n('service_already_stopped', service=name))
def service_reload(names):
"""
Reload one or more services
Keyword argument:
name -- Services name to reload
"""
if isinstance(names, str):
names = [names]
for name in names:
if _run_service_command('reload', name):
logger.success(m18n.n('service_reloaded', service=name))
else:
if service_status(name)['status'] != 'inactive':
raise YunohostError('service_reload_failed', service=name, logs=_get_journalctl_logs(name))
def service_restart(names):
"""
Restart one or more services. If the services are not running yet, they will be started.
Keyword argument:
name -- Services name to restart
"""
if isinstance(names, str):
names = [names]
for name in names:
if _run_service_command('restart', name):
logger.success(m18n.n('service_restarted', service=name))
else:
if service_status(name)['status'] != 'inactive':
raise YunohostError('service_restart_failed', service=name, logs=_get_journalctl_logs(name))
def service_reload_or_restart(names):
"""
Reload one or more services if they support it. If not, restart them instead. If the services are not running yet, they will be started.
Keyword argument:
name -- Services name to reload or restart
"""
if isinstance(names, str):
names = [names]
for name in names:
if _run_service_command('reload-or-restart', name):
logger.success(m18n.n('service_reloaded_or_restarted', service=name))
else:
if service_status(name)['status'] != 'inactive':
raise YunohostError('service_reloaded_or_restarted_failed', service=name, logs=_get_journalctl_logs(name))
@is_unit_operation() @is_unit_operation()
def service_enable(operation_logger, names): def service_enable(operation_logger, names):
""" """
@ -597,14 +651,14 @@ def _run_service_command(action, service):
if service not in services.keys(): if service not in services.keys():
raise YunohostError('service_unknown', service=service) raise YunohostError('service_unknown', service=service)
possible_actions = ['start', 'stop', 'restart', 'reload', 'enable', 'disable'] possible_actions = ['start', 'stop', 'restart', 'reload', 'reload-or-restart', 'enable', 'disable']
if action not in possible_actions: if action not in possible_actions:
raise ValueError("Unknown action '%s', available actions are: %s" % (action, ", ".join(possible_actions))) raise ValueError("Unknown action '%s', available actions are: %s" % (action, ", ".join(possible_actions)))
cmd = 'systemctl %s %s' % (action, service) cmd = 'systemctl %s %s' % (action, service)
need_lock = services[service].get('need_lock', False) \ need_lock = services[service].get('need_lock', False) \
and action in ['start', 'stop', 'restart', 'reload'] and action in ['start', 'stop', 'restart', 'reload', 'reload-or-restart']
try: try:
# Launch the command # Launch the command