From 65c8ca680b56c0ebce0f7eac2d473c08e17528f4 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Wed, 17 Aug 2022 14:48:05 -0400 Subject: [PATCH 1/7] Add importing threading to jar servers. Refactor "downloading" status --- app/classes/controllers/servers_controller.py | 12 ++--- app/classes/minecraft/serverjars.py | 2 +- app/classes/models/server_stats.py | 17 +++--- app/classes/shared/main_controller.py | 54 ++++++++++++------- app/classes/shared/server.py | 2 +- app/classes/web/panel_handler.py | 8 +-- app/frontend/templates/panel/dashboard.html | 4 +- app/frontend/templates/panel/server_term.html | 4 +- .../20220817_schedule_rename_downloading.py | 17 ++++++ app/translations/en_EN.json | 4 +- 10 files changed, 78 insertions(+), 46 deletions(-) create mode 100644 app/migrations/20220817_schedule_rename_downloading.py diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index 650a16b0..b125590e 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -108,19 +108,19 @@ class ServersController(metaclass=Singleton): return ret @staticmethod - def set_download(server_id): + def set_import(server_id): srv = ServersController().get_server_instance_by_id(server_id) - return srv.stats_helper.set_download() + return srv.stats_helper.set_import() @staticmethod - def finish_download(server_id): + def finish_import(server_id): srv = ServersController().get_server_instance_by_id(server_id) - return srv.stats_helper.finish_download() + return srv.stats_helper.finish_import() @staticmethod - def get_download_status(server_id): + def get_import_status(server_id): server = ServersController().get_server_instance_by_id(server_id) - return server.stats_helper.get_download_status() + return server.stats_helper.get_import_status() def remove_server(self, server_id): roles_list = PermissionsServers.get_roles_from_server(server_id) diff --git a/app/classes/minecraft/serverjars.py b/app/classes/minecraft/serverjars.py index a5eb11ba..90b49236 100644 --- a/app/classes/minecraft/serverjars.py +++ b/app/classes/minecraft/serverjars.py @@ -175,7 +175,7 @@ class ServerJars: # we submit a db update for it's stats. while True: try: - ServersController.set_download(server_id) + ServersController.set_import(server_id) for user in server_users: self.helper.websocket_helper.broadcast_user( user, "send_start_reload", {} diff --git a/app/classes/models/server_stats.py b/app/classes/models/server_stats.py index e3c943e3..906eed8b 100644 --- a/app/classes/models/server_stats.py +++ b/app/classes/models/server_stats.py @@ -53,7 +53,7 @@ class ServerStats(Model): waiting_start = BooleanField(default=False) first_run = BooleanField(default=True) crashed = BooleanField(default=False) - downloading = BooleanField(default=False) + importing = BooleanField(default=False) class Meta: table_name = "server_stats" @@ -207,26 +207,26 @@ class HelperServerStats: ServerStats.server_id == self.server_id ).execute(self.database) - def set_download(self): + def set_import(self): # self.select_database(self.server_id) - ServerStats.update(downloading=True).where( + ServerStats.update(importing=True).where( ServerStats.server_id == self.server_id ).execute(self.database) - def finish_download(self): + def finish_import(self): # self.select_database(self.server_id) - ServerStats.update(downloading=False).where( + ServerStats.update(importing=False).where( ServerStats.server_id == self.server_id ).execute(self.database) - def get_download_status(self): + def get_import_status(self): # self.select_database(self.server_id) - download_status = ( + import_status = ( ServerStats.select() .where(ServerStats.server_id == self.server_id) .get(self.database) ) - return download_status.downloading + return import_status.importing def server_crash_reset(self): if self.server_id is None: @@ -249,7 +249,6 @@ class HelperServerStats: def set_update(self, value): if self.server_id is None: return - # self.select_database(self.server_id) try: # Checks if server even exists diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index bca24fe8..2332355c 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -5,6 +5,7 @@ import platform import shutil import time import logging +import threading from peewee import DoesNotExist # TZLocal is set as a hidden import on win pipeline @@ -16,6 +17,7 @@ from app.classes.models.server_permissions import EnumPermissionsServer from app.classes.shared.main_models import DatabaseShortcuts from app.classes.models.users import HelperUsers from app.classes.models.roles import HelperRoles +from app.classes.models.server_permissions import PermissionsServers from app.classes.models.management import HelpersManagement from app.classes.models.servers import HelperServers from app.classes.controllers.crafty_perms_controller import CraftyPermsController @@ -537,25 +539,6 @@ class Controller: Helpers.ensure_dir_exists(new_server_dir) Helpers.ensure_dir_exists(backup_path) server_path = Helpers.get_os_understandable_path(server_path) - try: - FileHelpers.copy_dir(server_path, new_server_dir, True) - except shutil.Error as ex: - logger.error(f"Server import failed with error: {ex}") - - has_properties = False - for item in os.listdir(new_server_dir): - if str(item) == "server.properties": - has_properties = True - if not has_properties: - logger.info( - f"No server.properties found on zip file import. " - f"Creating one with port selection of {str(port)}" - ) - with open( - os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8" - ) as file: - file.write(f"server-port={port}") - file.close() full_jar_path = os.path.join(new_server_dir, server_jar) @@ -586,8 +569,41 @@ class Controller: port, server_type="minecraft-java", ) + import_thread = threading.Thread( + name=f"server_import-{server_id}", + target=self.import_threaded_jar_server, + daemon=True, + args=(server_path, new_server_dir, port, new_id), + ) + self.servers.set_import(new_id) + import_thread.start() return new_id + def import_threaded_jar_server(self, server_path, new_server_dir, port, new_id): + try: + FileHelpers.copy_dir(server_path, new_server_dir, True) + except shutil.Error as ex: + logger.error(f"Server import failed with error: {ex}") + + has_properties = False + for item in os.listdir(new_server_dir): + if str(item) == "server.properties": + has_properties = True + if not has_properties: + logger.info( + f"No server.properties found on zip file import. " + f"Creating one with port selection of {str(port)}" + ) + with open( + os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8" + ) as file: + file.write(f"server-port={port}") + file.close() + self.servers.finish_import(new_id) + server_users = PermissionsServers.get_server_user_list(new_id) + for user in server_users: + self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {}) + def import_zip_server( self, server_name: str, diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index 499a0080..e0e0552f 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -293,7 +293,7 @@ class ServerInstance: else: user_lang = HelperUsers.get_user_lang_by_id(user_id) - if self.stats_helper.get_download_status(): + if self.stats_helper.get_import_status(): if user_id: self.helper.websocket_helper.broadcast_user( user_id, diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index a5e56029..5e2c495d 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -453,8 +453,8 @@ class PanelHandler(BaseHandler): for server in un_used_servers[:]: if flag == 0: server["stats"][ - "downloading" - ] = self.controller.servers.get_download_status( + "importing" + ] = self.controller.servers.get_import_status( str(server["stats"]["server_id"]["server_id"]) ) server["stats"]["crashed"] = self.controller.servers.is_crashed( @@ -571,11 +571,11 @@ class PanelHandler(BaseHandler): "started": "False", } if not self.failed_server: - page_data["downloading"] = self.controller.servers.get_download_status( + page_data["importing"] = self.controller.servers.get_import_status( server_id ) else: - page_data["downloading"] = False + page_data["importing"] = False page_data["server_id"] = server_id try: page_data["waiting_start"] = self.controller.servers.get_waiting_start( diff --git a/app/frontend/templates/panel/dashboard.html b/app/frontend/templates/panel/dashboard.html index ec5a21db..59805ddf 100644 --- a/app/frontend/templates/panel/dashboard.html +++ b/app/frontend/templates/panel/dashboard.html @@ -399,12 +399,12 @@ data['lang']) }} - {% elif server['stats']['downloading']%} + {% elif server['stats']['importing']%}
- {{ translate('serverTerm', 'downloading', data['lang']) }} + {{ translate('serverTerm', 'importing', data['lang']) }}
{% else %} diff --git a/app/frontend/templates/panel/server_term.html b/app/frontend/templates/panel/server_term.html index ffc5e51c..83f31519 100644 --- a/app/frontend/templates/panel/server_term.html +++ b/app/frontend/templates/panel/server_term.html @@ -65,9 +65,9 @@ - {% elif data['downloading'] %} + {% elif data['importing'] %}
- diff --git a/app/migrations/20220817_schedule_rename_downloading.py b/app/migrations/20220817_schedule_rename_downloading.py new file mode 100644 index 00000000..f74ce96d --- /dev/null +++ b/app/migrations/20220817_schedule_rename_downloading.py @@ -0,0 +1,17 @@ +# Generated by database migrator +import peewee + + +def migrate(migrator, database, **kwargs): + migrator.rename_column("server_stats", "downloading", "importing") + + """ + Write your migrations here. + """ + + +def rollback(migrator, database, **kwargs): + migrator.rename_column("server_stats", "importing", "downloading") + """ + Write your rollback migrations here. + """ diff --git a/app/translations/en_EN.json b/app/translations/en_EN.json index f398ccf5..c4ae1402 100644 --- a/app/translations/en_EN.json +++ b/app/translations/en_EN.json @@ -463,7 +463,7 @@ "serverTerm": { "commandInput": "Enter your command", "delay-explained": "The service/agent has recently started and is delaying the start of the minecraft server instance", - "downloading": "Downloading...", + "importing": "Importing...", "restart": "Restart", "sendCommand": "Send command", "start": "Start", @@ -558,4 +558,4 @@ "userSettings": "User Settings", "uses": "Number of uses allowed (-1==No Limit)" } -} +} \ No newline at end of file From eea5e329af87be22d620883e1912e69fafb97850 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Wed, 17 Aug 2022 17:22:03 -0400 Subject: [PATCH 2/7] Refactor and add import_helpers. Working jar imports --- app/classes/minecraft/serverjars.py | 4 +- app/classes/shared/import_helper.py | 67 +++++++++++++++++++ app/classes/shared/main_controller.py | 39 ++--------- app/frontend/templates/panel/dashboard.html | 4 +- .../20220817_schedule_rename_downloading.py | 0 main.py | 4 +- 6 files changed, 79 insertions(+), 39 deletions(-) create mode 100644 app/classes/shared/import_helper.py rename app/migrations/{ => stats}/20220817_schedule_rename_downloading.py (100%) diff --git a/app/classes/minecraft/serverjars.py b/app/classes/minecraft/serverjars.py index 90b49236..1ecfc0f1 100644 --- a/app/classes/minecraft/serverjars.py +++ b/app/classes/minecraft/serverjars.py @@ -190,7 +190,7 @@ class ServerJars: try: with open(path, "wb") as output: shutil.copyfileobj(r.raw, output) - ServersController.finish_download(server_id) + ServersController.finish_import(server_id) for user in server_users: self.helper.websocket_helper.broadcast_user( @@ -203,7 +203,7 @@ class ServerJars: return True except Exception as e: logger.error(f"Unable to save jar to {path} due to error:{e}") - ServersController.finish_download(server_id) + ServersController.finish_import(server_id) server_users = PermissionsServers.get_server_user_list(server_id) for user in server_users: self.helper.websocket_helper.broadcast_user( diff --git a/app/classes/shared/import_helper.py b/app/classes/shared/import_helper.py new file mode 100644 index 00000000..61e1203d --- /dev/null +++ b/app/classes/shared/import_helper.py @@ -0,0 +1,67 @@ +from genericpath import isdir +import shutil +import os +import time +import logging +import threading + +from app.classes.controllers.server_perms_controller import PermissionsServers +from app.classes.controllers.servers_controller import ServersController +from app.classes.shared.helpers import Helpers +from app.classes.shared.file_helpers import FileHelpers + +logger = logging.getLogger(__name__) + + +class ImportHelpers: + allowed_quotes = ['"', "'", "`"] + + def __init__(self, helper, file_helper): + self.file_helper: FileHelpers = file_helper + self.helper: Helpers = helper + + def import_jar_server(self, server_path, new_server_dir, port, new_id): + import_thread = threading.Thread( + target=self.import_threaded_jar_server, + daemon=True, + args=(server_path, new_server_dir, port, new_id), + name=f"{new_id}_import", + ) + import_thread.start() + + def import_threaded_jar_server(self, server_path, new_server_dir, port, new_id): + for item in os.listdir(server_path): + if not item == "db_stats": + try: + if os.path.isdir(os.path.join(server_path, item)): + FileHelpers.copy_dir( + os.path.join(server_path, item), + os.path.join(new_server_dir, item), + ) + else: + FileHelpers.copy_file( + os.path.join(server_path, item), + os.path.join(new_server_dir, item), + ) + except shutil.Error as ex: + logger.error(f"Server import failed with error: {ex}") + + has_properties = False + for item in os.listdir(new_server_dir): + if str(item) == "server.properties": + has_properties = True + if not has_properties: + logger.info( + f"No server.properties found on zip file import. " + f"Creating one with port selection of {str(port)}" + ) + with open( + os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8" + ) as file: + file.write(f"server-port={port}") + file.close() + time.sleep(5) + ServersController.finish_import(new_id) + server_users = PermissionsServers.get_server_user_list(new_id) + for user in server_users: + self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {}) diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 2332355c..ad0e46bc 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -30,15 +30,17 @@ from app.classes.shared.authentication import Authentication from app.classes.shared.console import Console from app.classes.shared.helpers import Helpers from app.classes.shared.file_helpers import FileHelpers +from app.classes.shared.import_helper import ImportHelpers from app.classes.minecraft.serverjars import ServerJars logger = logging.getLogger(__name__) class Controller: - def __init__(self, database, helper, file_helper): + def __init__(self, database, helper, file_helper, import_helper): self.helper: Helpers = helper self.file_helper: FileHelpers = file_helper + self.import_helper: ImportHelpers = import_helper self.server_jars: ServerJars = ServerJars(helper) self.users_helper: HelperUsers = HelperUsers(database, self.helper) self.roles_helper: HelperRoles = HelperRoles(database) @@ -569,41 +571,10 @@ class Controller: port, server_type="minecraft-java", ) - import_thread = threading.Thread( - name=f"server_import-{server_id}", - target=self.import_threaded_jar_server, - daemon=True, - args=(server_path, new_server_dir, port, new_id), - ) - self.servers.set_import(new_id) - import_thread.start() + ServersController.set_import(new_id) + self.import_helper.import_jar_server(server_path, new_server_dir, port, new_id) return new_id - def import_threaded_jar_server(self, server_path, new_server_dir, port, new_id): - try: - FileHelpers.copy_dir(server_path, new_server_dir, True) - except shutil.Error as ex: - logger.error(f"Server import failed with error: {ex}") - - has_properties = False - for item in os.listdir(new_server_dir): - if str(item) == "server.properties": - has_properties = True - if not has_properties: - logger.info( - f"No server.properties found on zip file import. " - f"Creating one with port selection of {str(port)}" - ) - with open( - os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8" - ) as file: - file.write(f"server-port={port}") - file.close() - self.servers.finish_import(new_id) - server_users = PermissionsServers.get_server_user_list(new_id) - for user in server_users: - self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {}) - def import_zip_server( self, server_name: str, diff --git a/app/frontend/templates/panel/dashboard.html b/app/frontend/templates/panel/dashboard.html index 59805ddf..617c1ebe 100644 --- a/app/frontend/templates/panel/dashboard.html +++ b/app/frontend/templates/panel/dashboard.html @@ -193,9 +193,9 @@ {{ translate('dashboard', 'starting', data['lang']) }} - {% elif server['stats']['downloading']%} + {% elif server['stats']['importing']%} - {{ translate('serverTerm', 'downloading', + {{ translate('serverTerm', 'importing', data['lang']) }} {% else %} Date: Wed, 17 Aug 2022 17:41:44 -0400 Subject: [PATCH 3/7] Starting zip imports --- app/classes/shared/file_helpers.py | 5 +++- app/classes/shared/import_helper.py | 43 +++++++++++++++++++++++++++ app/classes/shared/main_controller.py | 29 ++---------------- 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/app/classes/shared/file_helpers.py b/app/classes/shared/file_helpers.py index 5cd38bbf..dc2eb282 100644 --- a/app/classes/shared/file_helpers.py +++ b/app/classes/shared/file_helpers.py @@ -27,7 +27,10 @@ class FileHelpers: FileHelpers.del_dirs(sub) else: # Delete file if it is a file: - sub.unlink() + try: + sub.unlink() + except: + logger.error(f"Unable to delete file {sub}") # This removes the top-level folder: path.rmdir() diff --git a/app/classes/shared/import_helper.py b/app/classes/shared/import_helper.py index 61e1203d..4767a2c1 100644 --- a/app/classes/shared/import_helper.py +++ b/app/classes/shared/import_helper.py @@ -65,3 +65,46 @@ class ImportHelpers: server_users = PermissionsServers.get_server_user_list(new_id) for user in server_users: self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {}) + + def import_java_zip_server(self, temp_dir, new_server_dir, port, new_id): + import_thread = threading.Thread( + target=self.import_threaded_java_zip_server, + daemon=True, + args=(temp_dir, new_server_dir, port, new_id), + name=f"{new_id}_import", + ) + import_thread.start() + + def import_threaded_java_zip_server(self, temp_dir, new_server_dir, port, new_id): + has_properties = False + # extracts archive to temp directory + for item in os.listdir(temp_dir): + if str(item) == "server.properties": + has_properties = True + try: + if not os.path.isdir(os.path.join(temp_dir, item)): + FileHelpers.move_file( + os.path.join(temp_dir, item), os.path.join(new_server_dir, item) + ) + else: + if item != "db_stats": + FileHelpers.move_dir( + os.path.join(temp_dir, item), + os.path.join(new_server_dir, item), + ) + except Exception as ex: + logger.error(f"ERROR IN ZIP IMPORT: {ex}") + if not has_properties: + logger.info( + f"No server.properties found on zip file import. " + f"Creating one with port selection of {str(port)}" + ) + with open( + os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8" + ) as file: + file.write(f"server-port={port}") + file.close() + + server_users = PermissionsServers.get_server_user_list(new_id) + for user in server_users: + self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {}) diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index ad0e46bc..61438b81 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -596,32 +596,6 @@ class Controller: temp_dir = Helpers.get_os_understandable_path(zip_path) Helpers.ensure_dir_exists(new_server_dir) Helpers.ensure_dir_exists(backup_path) - has_properties = False - # extracts archive to temp directory - for item in os.listdir(temp_dir): - if str(item) == "server.properties": - has_properties = True - try: - if not os.path.isdir(os.path.join(temp_dir, item)): - FileHelpers.move_file( - os.path.join(temp_dir, item), os.path.join(new_server_dir, item) - ) - else: - FileHelpers.move_dir( - os.path.join(temp_dir, item), os.path.join(new_server_dir, item) - ) - except Exception as ex: - logger.error(f"ERROR IN ZIP IMPORT: {ex}") - if not has_properties: - logger.info( - f"No server.properties found on zip file import. " - f"Creating one with port selection of {str(port)}" - ) - with open( - os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8" - ) as file: - file.write(f"server-port={port}") - file.close() full_jar_path = os.path.join(new_server_dir, server_jar) @@ -653,6 +627,9 @@ class Controller: port, server_type="minecraft-java", ) + self.import_helper.import_java_zip_server( + temp_dir, new_server_dir, port, new_id + ) return new_id # ********************************************************************************** From 504ffd57f6b23923cefb650a741823e82dae8bb3 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Wed, 17 Aug 2022 18:14:16 -0400 Subject: [PATCH 4/7] Working zip imports --- app/classes/shared/file_helpers.py | 8 +++++--- app/classes/shared/import_helper.py | 1 + app/classes/shared/main_controller.py | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/classes/shared/file_helpers.py b/app/classes/shared/file_helpers.py index dc2eb282..f9cde55a 100644 --- a/app/classes/shared/file_helpers.py +++ b/app/classes/shared/file_helpers.py @@ -31,9 +31,11 @@ class FileHelpers: sub.unlink() except: logger.error(f"Unable to delete file {sub}") - - # This removes the top-level folder: - path.rmdir() + try: + # This removes the top-level folder: + path.rmdir() + except: + logger.error("Unable to remove top level") return True @staticmethod diff --git a/app/classes/shared/import_helper.py b/app/classes/shared/import_helper.py index 4767a2c1..60ca0eed 100644 --- a/app/classes/shared/import_helper.py +++ b/app/classes/shared/import_helper.py @@ -106,5 +106,6 @@ class ImportHelpers: file.close() server_users = PermissionsServers.get_server_user_list(new_id) + ServersController.finish_import(new_id) for user in server_users: self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {}) diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 61438b81..0f3e3c97 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -627,6 +627,7 @@ class Controller: port, server_type="minecraft-java", ) + ServersController.set_import(new_id) self.import_helper.import_java_zip_server( temp_dir, new_server_dir, port, new_id ) From b10523ae0aa6eeb703f1256a32f50e3d8144bf47 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Wed, 17 Aug 2022 19:07:56 -0400 Subject: [PATCH 5/7] Add import status to working bedrock --- app/classes/shared/import_helper.py | 111 +++++++++++++++++++++++++- app/classes/shared/main_controller.py | 55 ++----------- app/classes/web/server_handler.py | 4 - 3 files changed, 115 insertions(+), 55 deletions(-) diff --git a/app/classes/shared/import_helper.py b/app/classes/shared/import_helper.py index 60ca0eed..446683c1 100644 --- a/app/classes/shared/import_helper.py +++ b/app/classes/shared/import_helper.py @@ -1,7 +1,6 @@ -from genericpath import isdir -import shutil import os import time +import shutil import logging import threading @@ -71,7 +70,7 @@ class ImportHelpers: target=self.import_threaded_java_zip_server, daemon=True, args=(temp_dir, new_server_dir, port, new_id), - name=f"{new_id}_import", + name=f"{new_id}_java_zip_import", ) import_thread.start() @@ -109,3 +108,109 @@ class ImportHelpers: ServersController.finish_import(new_id) for user in server_users: self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {}) + # deletes temp dir + FileHelpers.del_dirs(temp_dir) + + def import_bedrock_server( + self, server_path, new_server_dir, port, full_jar_path, new_id + ): + import_thread = threading.Thread( + target=self.import_threaded_java_zip_server, + daemon=True, + args=(server_path, new_server_dir, port, new_id), + name=f"{new_id}_bedrock_import", + ) + import_thread.start() + + def import_threaded_bedrock_server( + self, server_path, new_server_dir, port, full_jar_path, new_id + ): + for item in os.listdir(server_path): + if not item == "db_stats": + try: + if os.path.isdir(os.path.join(server_path, item)): + FileHelpers.copy_dir( + os.path.join(server_path, item), + os.path.join(new_server_dir, item), + ) + else: + FileHelpers.copy_file( + os.path.join(server_path, item), + os.path.join(new_server_dir, item), + ) + except shutil.Error as ex: + logger.error(f"Server import failed with error: {ex}") + + has_properties = False + for item in os.listdir(new_server_dir): + if str(item) == "server.properties": + has_properties = True + if not has_properties: + logger.info( + f"No server.properties found on zip file import. " + f"Creating one with port selection of {str(port)}" + ) + with open( + os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8" + ) as file: + file.write(f"server-port={port}") + file.close() + if os.name != "nt": + if Helpers.check_file_exists(full_jar_path): + os.chmod(full_jar_path, 0o2760) + ServersController.finish_import(new_id) + server_users = PermissionsServers.get_server_user_list(new_id) + for user in server_users: + self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {}) + + def import_bedrock_zip_server( + self, temp_dir, new_server_dir, full_jar_path, port, new_id + ): + import_thread = threading.Thread( + target=self.import_threaded_bedrock_zip_server, + daemon=True, + args=(temp_dir, new_server_dir, full_jar_path, port, new_id), + name=f"{new_id}_bedrock_import", + ) + import_thread.start() + + def import_threaded_bedrock_zip_server( + self, temp_dir, new_server_dir, full_jar_path, port, new_id + ): + has_properties = False + # extracts archive to temp directory + for item in os.listdir(temp_dir): + if str(item) == "server.properties": + has_properties = True + try: + if not os.path.isdir(os.path.join(temp_dir, item)): + FileHelpers.move_file( + os.path.join(temp_dir, item), os.path.join(new_server_dir, item) + ) + else: + if item != "db_stats": + FileHelpers.move_dir( + os.path.join(temp_dir, item), + os.path.join(new_server_dir, item), + ) + except Exception as ex: + logger.error(f"ERROR IN ZIP IMPORT: {ex}") + if not has_properties: + logger.info( + f"No server.properties found on zip file import. " + f"Creating one with port selection of {str(port)}" + ) + with open( + os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8" + ) as file: + file.write(f"server-port={port}") + file.close() + ServersController.finish_import(new_id) + server_users = PermissionsServers.get_server_user_list(new_id) + for user in server_users: + self.helper.websocket_helper.broadcast_user(user, "send_start_reload", {}) + if os.name != "nt": + if Helpers.check_file_exists(full_jar_path): + os.chmod(full_jar_path, 0o2760) + # deletes temp dir + FileHelpers.del_dirs(temp_dir) diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 0f3e3c97..6aaf8cb1 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -652,25 +652,6 @@ class Controller: Helpers.ensure_dir_exists(new_server_dir) Helpers.ensure_dir_exists(backup_path) server_path = Helpers.get_os_understandable_path(server_path) - try: - FileHelpers.copy_dir(server_path, new_server_dir, True) - except shutil.Error as ex: - logger.error(f"Server import failed with error: {ex}") - - has_properties = False - for item in os.listdir(new_server_dir): - if str(item) == "server.properties": - has_properties = True - if not has_properties: - logger.info( - f"No server.properties found on zip file import. " - f"Creating one with port selection of {str(port)}" - ) - with open( - os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8" - ) as file: - file.write(f"server-port={port}") - file.close() full_jar_path = os.path.join(new_server_dir, server_exe) @@ -694,9 +675,10 @@ class Controller: port, server_type="minecraft-bedrock", ) - if os.name != "nt": - if Helpers.check_file_exists(full_jar_path): - os.chmod(full_jar_path, 0o2760) + ServersController.set_import(new_id) + self.import_helper.import_bedrock_server( + server_path, new_server_dir, port, full_jar_path, new_id + ) return new_id def import_bedrock_zip_server( @@ -714,32 +696,6 @@ class Controller: temp_dir = Helpers.get_os_understandable_path(zip_path) Helpers.ensure_dir_exists(new_server_dir) Helpers.ensure_dir_exists(backup_path) - has_properties = False - # extracts archive to temp directory - for item in os.listdir(temp_dir): - if str(item) == "server.properties": - has_properties = True - try: - if not os.path.isdir(os.path.join(temp_dir, item)): - FileHelpers.move_file( - os.path.join(temp_dir, item), os.path.join(new_server_dir, item) - ) - else: - FileHelpers.move_dir( - os.path.join(temp_dir, item), os.path.join(new_server_dir, item) - ) - except Exception as ex: - logger.error(f"ERROR IN ZIP IMPORT: {ex}") - if not has_properties: - logger.info( - f"No server.properties found on zip file import. " - f"Creating one with port selection of {str(port)}" - ) - with open( - os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8" - ) as file: - file.write(f"server-port={port}") - file.close() full_jar_path = os.path.join(new_server_dir, server_exe) @@ -763,6 +719,9 @@ class Controller: port, server_type="minecraft-bedrock", ) + self.import_helper.import_bedrock_zip_server( + temp_dir, new_server_dir, full_jar_path, port, new_id + ) if os.name != "nt": if Helpers.check_file_exists(full_jar_path): os.chmod(full_jar_path, 0o2760) diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index df4ba684..7d605c2f 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -385,8 +385,6 @@ class ServerHandler(BaseHandler): new_server_id, self.get_remote_ip(), ) - # deletes temp dir - FileHelpers.del_dirs(zip_path) else: if len(server_parts) != 3: self.redirect("/panel/error?error=Invalid server data") @@ -516,8 +514,6 @@ class ServerHandler(BaseHandler): new_server_id, self.get_remote_ip(), ) - # deletes temp dir - FileHelpers.del_dirs(zip_path) else: if len(server_parts) != 2: self.redirect("/panel/error?error=Invalid server data") From 3c14d0f2ee5eae2cce47708a6be97f5dd224f888 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 27 Aug 2022 10:44:34 -0400 Subject: [PATCH 6/7] Appease the linter --- app/classes/shared/import_helper.py | 4 ++-- app/classes/shared/main_controller.py | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/classes/shared/import_helper.py b/app/classes/shared/import_helper.py index 446683c1..769ebc3a 100644 --- a/app/classes/shared/import_helper.py +++ b/app/classes/shared/import_helper.py @@ -115,9 +115,9 @@ class ImportHelpers: self, server_path, new_server_dir, port, full_jar_path, new_id ): import_thread = threading.Thread( - target=self.import_threaded_java_zip_server, + target=self.import_threaded_bedrock_server, daemon=True, - args=(server_path, new_server_dir, port, new_id), + args=(server_path, new_server_dir, port, full_jar_path, new_id), name=f"{new_id}_bedrock_import", ) import_thread.start() diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 6aaf8cb1..748062ba 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -5,7 +5,6 @@ import platform import shutil import time import logging -import threading from peewee import DoesNotExist # TZLocal is set as a hidden import on win pipeline @@ -17,7 +16,6 @@ from app.classes.models.server_permissions import EnumPermissionsServer from app.classes.shared.main_models import DatabaseShortcuts from app.classes.models.users import HelperUsers from app.classes.models.roles import HelperRoles -from app.classes.models.server_permissions import PermissionsServers from app.classes.models.management import HelpersManagement from app.classes.models.servers import HelperServers from app.classes.controllers.crafty_perms_controller import CraftyPermsController From d7244cb1008cb620236919d82ad4cbb47560b4b9 Mon Sep 17 00:00:00 2001 From: Zedifus Date: Sat, 27 Aug 2022 17:18:54 +0100 Subject: [PATCH 7/7] Update chasngelog !433 --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98e55169..ca076487 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,11 @@ # Changelog ## --- [4.0.11] - 2022/TBD ### New features -TBD +- Add server import status indicators ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/433)) ### Bug fixes TBD ### Tweaks -TBD +- Make imports threaded ([Merge Request](https://gitlab.com/crafty-controller/crafty-4/-/merge_requests/433)) ### Lang TBD