[enh] Add reboot/shutdown actions in tools (#190)

* [enh] Implements shutdown/reboot helpers.
* [enh] Improve reboot/shutdown help.
This commit is contained in:
Laurent Peuch 2017-08-11 16:16:09 +02:00 committed by Alexandre Aubin
parent cd9444f647
commit 02ea0c0656
3 changed files with 58 additions and 1 deletions

View file

@ -1478,6 +1478,26 @@ tools:
extra:
pattern: *pattern_port
### tools_shutdown()
shutdown:
action_help: Shutdown the server
api: PUT /shutdown
arguments:
-f:
help: skip the shutdown confirmation
full: --force
action: store_true
### tools_reboot()
reboot:
action_help: Reboot the server
api: PUT /reboot
arguments:
-f:
help: skip the reboot confirmation
full: --force
action: store_true
subcategories:
migrations:

View file

@ -276,6 +276,10 @@
"restore_running_app_script": "Running restore script of app '{app:s}'...",
"restore_running_hooks": "Running restoration hooks...",
"restore_system_part_failed": "Unable to restore the '{part:s}' system part",
"server_shutdown": "The server will shutdown",
"server_shutdown_confirm": "The server will shutdown immediatly, are you sure? [{answers:s}]",
"server_reboot": "The server will reboot",
"server_reboot_confirm": "The server will reboot immediatly, are you sure? [{answers:s}]",
"service_add_failed": "Unable to add service '{service:s}'",
"service_added": "The service '{service:s}' has been added",
"service_already_started": "Service '{service:s}' has already been started",

View file

@ -39,7 +39,7 @@ from importlib import import_module
import apt
import apt.progress
from moulinette import msettings, m18n
from moulinette import msettings, msignals, m18n
from moulinette.core import MoulinetteError, init_authenticator
from moulinette.utils.log import getActionLogger
from moulinette.utils.filesystem import read_json, write_to_json
@ -632,6 +632,39 @@ def tools_port_available(port):
return False
def tools_shutdown(force=False):
shutdown = force
if not shutdown:
try:
# Ask confirmation for server shutdown
i = msignals.prompt(m18n.n('server_shutdown_confirm', answers='y/N'))
except NotImplemented:
pass
else:
if i.lower() == 'y' or i.lower() == 'yes':
shutdown = True
if shutdown:
logger.warn(m18n.n('server_shutdown'))
subprocess.check_call(['systemctl', 'poweroff'])
def tools_reboot(force=False):
reboot = force
if not reboot:
try:
# Ask confirmation for restoring
i = msignals.prompt(m18n.n('server_reboot_confirm', answers='y/N'))
except NotImplemented:
pass
else:
if i.lower() == 'y' or i.lower() == 'yes':
reboot = True
if reboot:
logger.warn(m18n.n('server_reboot'))
subprocess.check_call(['systemctl', 'reboot'])
def tools_migrations_list():
"""
List existing migrations