From 863ded782a66e4f3b95d445fe8bc1ace63927137 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Mon, 30 Jan 2023 15:23:10 -0500 Subject: [PATCH] Add move servers dir functionality --- app/classes/shared/main_controller.py | 50 +++++++++++++++++++ app/classes/web/ajax_handler.py | 19 +++++++ app/classes/web/panel_handler.py | 3 ++ .../templates/panel/panel_config.html | 42 +++++++++++++++- 4 files changed, 112 insertions(+), 2 deletions(-) diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 2aae7c64..d4c5535d 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -1005,5 +1005,55 @@ class Controller: HelperUsers.clear_support_status() def set_master_server_dir(self, server_dir): + # This method should only be used on a first run basis if the server dir is "" self.helper.servers_dir = server_dir HelpersManagement.set_master_server_dir(server_dir) + + def update_master_server_dir(self, server_dir, user_id): + if self.management.get_master_server_dir() == server_dir: + logger.info( + "Admin tried to change server dir to current server dir. Canceling..." + ) + return + if self.helper.is_subdir(server_dir, self.management.get_master_server_dir()): + logger.info( + "Admin tried to change server dir to be inside a sub directory of the" + " current server dir. This will result in a copy loop." + ) + self.helper.servers_dir = server_dir + + if not self.helper.ensure_dir_exists(os.path.join(server_dir, "servers")): + self.helper.websocket_helper.broadcast_user( + user_id, + "send_start_error", + { + "error": "Crafty failed to move server dir. " + "It seems Crafty lacks permission to write to " + "the new directory." + }, + ) + return + + HelpersManagement.set_master_server_dir(server_dir) + servers = self.servers.get_all_defined_servers() + for server in servers: + server_path = server.get("path") + new_server_path = os.path.join( + server_dir, "servers", server.get("server_uuid") + ) + if os.path.isdir(server_path): + self.file_helper.move_dir( + server_path, + new_server_path, + ) + server_obj = self.servers.get_server_obj(server.get("server_id")) + server_obj.path = new_server_path + failed = False + for s in self.servers.failed_servers: + if int(s["server_id"]) == int(server.get("server_id")): + failed = True + if not failed: + self.servers.update_server(server_obj) + else: + self.servers.update_unloaded_server(server_obj) + self.servers.init_all_servers() diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index 97de6b13..7484ccaa 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -575,6 +575,25 @@ class AjaxHandler(BaseHandler): self.controller.server_jars.manual_refresh_cache() return + elif page == "update_server_dir": + for server in self.controller.servers.get_all_servers_stats(): + if server["stats"]["running"]: + self.helper.websocket_helper.broadcast_user( + exec_user["user_id"], + "send_start_error", + { + "error": "You must stop all servers before " + "starting a storage migration." + }, + ) + return + if not superuser: + self.redirect("/panel/error?error=Not a super user") + return + new_dir = urllib.parse.unquote(self.get_argument("server_dir")) + self.controller.update_master_server_dir(new_dir, exec_user["user_id"]) + return + @tornado.web.authenticated def delete(self, page): api_key, _, exec_user = self.current_user diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 74c93b88..ddfdd5a2 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -841,6 +841,9 @@ class PanelHandler(BaseHandler): page_data["auth-servers"] = auth_servers page_data["role-servers"] = auth_role_servers page_data["user-roles"] = user_roles + page_data[ + "servers_dir" + ] = self.controller.management.get_master_server_dir() page_data["users"] = self.controller.users.user_query(exec_user["user_id"]) page_data["roles"] = self.controller.users.user_role_query( diff --git a/app/frontend/templates/panel/panel_config.html b/app/frontend/templates/panel/panel_config.html index 249daf70..2cc409de 100644 --- a/app/frontend/templates/panel/panel_config.html +++ b/app/frontend/templates/panel/panel_config.html @@ -50,7 +50,9 @@

{{ translate('panelConfig', 'users', data['lang']) }}

{% if data['user_data']['hints'] %} - + {% end %}
  {{ @@ -148,7 +150,9 @@

{{ translate('panelConfig', 'roles', data['lang']) }}

{% if data['user_data']['hints'] %} - + {% end %}
  {{ translate('panelConfig', 'newRole', data['lang']) }}
@@ -234,6 +238,16 @@

{{ translate('panelConfig', 'adminControls', data['lang']) }}

+
+
+ + + +   +
@@ -258,6 +272,30 @@ {% block js %}