diff --git a/app/classes/shared/controller.py b/app/classes/shared/controller.py index 9044c483..4712cbed 100644 --- a/app/classes/shared/controller.py +++ b/app/classes/shared/controller.py @@ -3,6 +3,7 @@ import time import logging import sys import yaml +from distutils import dir_util from app.classes.shared.helpers import helper from app.classes.shared.console import console @@ -215,11 +216,31 @@ class Controller: new_id = self.register_server(name, server_id, server_dir, server_command, server_file, server_log_file, server_stop) return new_id - # todo - Do import server - def import_server(self): - print("todo") + @staticmethod + def verify_jar_server( server_path: str, server_jar: str): + path_check = helper.check_path_exits(server_path) + jar_check = helper.check_file_exists(os.path.join(server_path, server_jar)) + if not path_check or not jar_check: + return False + return True - def register_server(self, name: str, server_id: str, server_dir: str, server_command: str, server_file: str, server_log_file: str, server_stop: str): + def import_jar_server(self, server_name: str, server_path: str, server_jar: str, min_mem: int, max_mem: int, port: int): + server_id = helper.create_uuid() + new_server_dir = os.path.join(helper.servers_dir, server_id) + + helper.ensure_dir_exists(new_server_dir) + dir_util.copy_tree(server_path, new_server_dir) + + full_jar_path = os.path.join(new_server_dir, server_jar) + server_command = 'java -Xms{}G -Xmx{}G -jar {} nogui'.format(min_mem, max_mem, full_jar_path) + server_log_file = "{}/logs/latest.log".format(new_server_dir) + server_stop = "stop" + + new_id = self.register_server(server_name, server_id, new_server_dir, server_command, server_jar, + server_log_file, server_stop, port) + return new_id + + def register_server(self, name: str, server_id: str, server_dir: str, server_command: str, server_file: str, server_log_file: str, server_stop: str, server_port=25565): # put data in the db new_id = Servers.insert({ Servers.server_name: name, @@ -231,6 +252,7 @@ class Controller: Servers.auto_start_delay: 10, Servers.crash_detection: False, Servers.log_path: server_log_file, + Servers.server_port: server_port, Servers.stop_command: server_stop }).execute() diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index 10d0c32e..08436cad 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -84,19 +84,35 @@ class ServerHandler(BaseHandler): min_mem = bleach.clean(self.get_argument('min_memory', '')) max_mem = bleach.clean(self.get_argument('max_memory', '')) port = bleach.clean(self.get_argument('port', '')) - + import_type = bleach.clean(self.get_argument('create_type', '')) + import_server_path = bleach.clean(self.get_argument('server_path', '')) + import_server_jar = bleach.clean(self.get_argument('server_jar', '')) server_parts = server.split("|") - # todo: add server type check here and call the correct server add functions if not a jar - new_server_id = controller.create_jar_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port) + if import_type == 'import_jar': + good_path = controller.verify_jar_server(import_server_path, import_server_jar) + + if not good_path: + self.redirect("/panel/error?error=Server path or Server Jar not found!") + return False + + new_server_id = controller.import_jar_server(server_name, import_server_path,import_server_jar, min_mem, max_mem, port) + + else: + # todo: add server type check here and call the correct server add functions if not a jar + new_server_id = controller.create_jar_server(server_parts[0], server_parts[1], server_name, min_mem, max_mem, port) if new_server_id: db_helper.add_to_audit_log(user_data['user_id'], "Created server {} named {}".format(server, server_name), new_server_id, self.get_remote_ip()) - stats.record_stats() - self.redirect("/panel/dashboard") + else: + logger.error("Unable to create server") + console.error("Unable to create server") + + stats.record_stats() + self.redirect("/panel/dashboard") self.render( template, diff --git a/app/frontend/templates/panel/server_term.html b/app/frontend/templates/panel/server_term.html index a70b2718..c3fcdbad 100644 --- a/app/frontend/templates/panel/server_term.html +++ b/app/frontend/templates/panel/server_term.html @@ -68,7 +68,7 @@
+ +
+ +