From 8db46d6ab646e0532a3c4ea7faf730ad0d93641c Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 9 Aug 2021 20:48:22 -0400 Subject: [PATCH] Added option to remove server files on delete. Added confirmation for server delete. --- app/classes/shared/controller.py | 5 +- app/classes/shared/models.py | 1 + app/classes/shared/tasks.py | 10 +++ app/classes/web/ajax_handler.py | 2 +- .../templates/panel/server_config.html | 76 ++++++++++++++++++- app/translations/en_EN.json | 10 ++- 6 files changed, 99 insertions(+), 5 deletions(-) diff --git a/app/classes/shared/controller.py b/app/classes/shared/controller.py index 74174e27..ce021d37 100644 --- a/app/classes/shared/controller.py +++ b/app/classes/shared/controller.py @@ -344,7 +344,7 @@ class Controller: return new_id - def remove_server(self, server_id): + def remove_server(self, server_id, files): counter = 0 for s in self.servers_list: @@ -361,7 +361,8 @@ class Controller: if running: self.stop_server(server_id) - + if files: + shutil.rmtree(db_helper.get_server_data_by_id(server_id)['path']) # remove the server from the DB db_helper.remove_server(server_id) diff --git a/app/classes/shared/models.py b/app/classes/shared/models.py index 7f7461d4..5d9924e0 100644 --- a/app/classes/shared/models.py +++ b/app/classes/shared/models.py @@ -347,6 +347,7 @@ class db_shortcuts: def remove_server(server_id): with database.atomic(): Role_Servers.delete().where(Role_Servers.server_id == server_id).execute() + User_Servers.delete().where(User_Servers.server_id == server_id).execute() Servers.delete().where(Servers.server_id == server_id).execute() @staticmethod diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index 15789111..20b0ffa4 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -5,6 +5,7 @@ import time import logging import threading import asyncio +import shutil from app.classes.shared.helpers import helper from app.classes.shared.console import console @@ -112,6 +113,15 @@ class TasksManager: elif command == "update_executable": svr.jar_update() + elif command == "delete_server": + logger.info( + "Removing server from panel for server: {}".format(c['server_id']['server_name'])) + self.controller.remove_server(c['server_id']['server_id'], False) + + elif command == "delete_server_files": + logger.info( + "Removing server and all associated files for server: {}".format(c['server_id']['server_name'])) + self.controller.remove_server(c['server_id']['server_id'], True) db_helper.mark_command_complete(c.get('command_id', None)) diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index 0796527e..700994b6 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -210,7 +210,7 @@ class AjaxHandler(BaseHandler): server_id = self.get_argument('id', None) print(server_id) - console.warning("delete {} for server {}".format(file_path, server_id)) + console.warning("delete {} for server {}".format(dir_path, server_id)) if not self.check_server_id(server_id, 'del_dir'): return False else: server_id = bleach.clean(server_id) diff --git a/app/frontend/templates/panel/server_config.html b/app/frontend/templates/panel/server_config.html index 156b3154..c9cce8b5 100644 --- a/app/frontend/templates/panel/server_config.html +++ b/app/frontend/templates/panel/server_config.html @@ -169,7 +169,7 @@ {{ translate('serverConfig', 'stopBeforeDeleting') }} {% else %} - {{ translate('serverConfig', 'deleteServer') }} + {% end %} @@ -226,6 +226,80 @@ let server_id = '{{ data['server_stats']['server_id']['server_id'] }}'; }); } + function deleteServer (){ + path = "{{data['server_stats']['server_id']['path']}}"; + name = "{{data['server_stats']['server_id']['server_name']}}"; + bootbox.confirm({ + size: "", + title: "{% raw translate('serverTerm', 'deleteFilesQuestion') %}", + closeButton: false, + message: "{% raw translate('serverTerm', 'deleteFilesQuestionMessage') %}", + buttons: { + confirm: { + label: "{{ translate('serverTerm', 'yesDeleteFiles') }}", + className: 'btn-danger', + }, + cancel: { + label: "{{ translate('serverTerm', 'noDeleteFiles') }}", + className: 'btn-link', + } + }, + callback: function(result) { + if (!result){ + send_command(server_id, 'delete_server'); + setTimeout(function(){ window.location = '/panel/dashboard'; }, 5000); + bootbox.dialog({ + backdrop: true, + closeButton: false, + title: '{% raw translate("serverConfig", "sendingRequest") %}', + message: '
  {% raw translate("serverConfig", "bePatientUpdate") %}
' + }); + + return;} + else{ + send_command(server_id, 'delete_server_files'); + setTimeout(function(){ window.location = '/panel/dashboard'; }, 5000); + bootbox.dialog({ + backdrop: true, + closeButton: false, + title: '{% raw translate("serverConfig", "sendingRequest") %}', + message: '
  {% raw translate("serverConfig", "bePatientUpdate") %}
' + }); + } + + } + }); + } + function deleteConfirm (){ + path = "{{data['server_stats']['server_id']['path']}}"; + name = "{{data['server_stats']['server_id']['server_name']}}"; + bootbox.confirm({ + size: "", + title: "{% raw translate('serverTerm', 'deleteServerQuestion') %}", + closeButton: false, + message: "{% raw translate('serverTerm', 'deleteServerQuestionMessage') %}", + buttons: { + confirm: { + label: "{{ translate('serverTerm', 'yesDelete') }}", + className: 'btn-danger', + }, + cancel: { + label: "{{ translate('serverTerm', 'noDelete') }}", + className: 'btn-link', + } + }, + callback: function(result) { + if (!result){ + return; + return;} + else{ + deleteServer(); + } + + } + }); + } + {% end %} \ No newline at end of file diff --git a/app/translations/en_EN.json b/app/translations/en_EN.json index caeb0d6b..6affac60 100644 --- a/app/translations/en_EN.json +++ b/app/translations/en_EN.json @@ -126,7 +126,15 @@ "start": "Start", "restart": "Restart", "stop": "Stop", - "updating": "Updating..." + "updating": "Updating...", + "deleteServerQuestion": "Delete Server?", + "deleteServerQuestionMessage": "Are you sure you want to delete this server? After this there is no going back...", + "yesDelete": "Yes, delete", + "noDelete": "No, go back", + "deleteFilesQuestion": "Delete server files from machine?", + "deleteFilesQuestionMessage": "Would you like Crafty to delete all server files from the host machine?", + "yesDeleteFiles": "Yes, delete files", + "noDeleteFiles": "No, just remove from panel" }, "serverPlayerManagement": { "players": "Players",