diff --git a/data/actionsmap/yunohost.yml b/data/actionsmap/yunohost.yml index af072c1bc..24b099451 100644 --- a/data/actionsmap/yunohost.yml +++ b/data/actionsmap/yunohost.yml @@ -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: diff --git a/locales/en.json b/locales/en.json index a59f87b4d..3d70ab2b3 100644 --- a/locales/en.json +++ b/locales/en.json @@ -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", diff --git a/src/yunohost/tools.py b/src/yunohost/tools.py index 6dc3fcba4..5b74883f1 100644 --- a/src/yunohost/tools.py +++ b/src/yunohost/tools.py @@ -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