From f96649613fc6b31a0b7bf6e8a218243524a98747 Mon Sep 17 00:00:00 2001 From: Phillip Tarrant Date: Mon, 14 Sep 2020 14:05:17 -0400 Subject: [PATCH] did some refactoring on loading of servers, fixed bug where server object would be forgotten when new server added --- app/classes/shared/controller.py | 126 +++++++++++++----- app/classes/web/panel_handler.py | 14 ++ app/config/serverjars.json | 28 ++-- .../templates/panel/server_config.html | 2 +- 4 files changed, 121 insertions(+), 49 deletions(-) diff --git a/app/classes/shared/controller.py b/app/classes/shared/controller.py index e27a6de1..9044c483 100644 --- a/app/classes/shared/controller.py +++ b/app/classes/shared/controller.py @@ -21,15 +21,31 @@ class Controller: def __init__(self): self.servers_list = [] - def init_all_servers(self): + def check_server_loaded(self, server_id_to_check: int): - # if we have servers defined, let's destroy it and start over - if len(self.servers_list) > 0: - self.servers_list = [] + logger.info("Checking to see if we already registered {}".format(server_id_to_check)) + + for s in self.servers_list: + known_server = s.get('server_id') + if known_server is None: + return False + + if known_server == server_id_to_check: + logger.info('skipping initialization of server {} because it is already loaded'.format(server_id_to_check)) + return True + + return False + + def init_all_servers(self): servers = db_helper.get_all_defined_servers() for s in servers: + server_id = s.get('server_id') + + # if we have already initialized this server, let's skip it. + if self.check_server_loaded(server_id): + continue # if this server path no longer exists - let's warn and bomb out if not helper.check_path_exits(s['path']): @@ -84,6 +100,14 @@ class Controller: servers = db_helper.get_all_defined_servers() return servers + def get_server_data(self, server_id): + for s in self.servers_list: + if int(s['server_id']) == int(server_id): + return s['server_data_obj'] + + logger.warning("Unable to find server object for server id {}".format(server_id)) + return False + def list_running_servers(self): running_servers = [] @@ -104,6 +128,7 @@ class Controller: def stop_all_servers(self): servers = self.list_running_servers() + print(servers) logger.info("Found {} running server(s)".format(len(servers))) console.info("Found {} running server(s)".format(len(servers))) @@ -114,36 +139,7 @@ class Controller: logger.info("Stopping Server ID {} - {}".format(s['id'], s['name'])) console.info("Stopping Server ID {} - {}".format(s['id'], s['name'])) - # get object - svr_obj = self.get_server_obj(s['id']) - running = svr_obj.check_running(True) - - # issue the stop command - svr_obj.stop_threaded_server() - - # while it's running, we wait - x = 0 - while running: - logger.info("Server {} is still running - waiting 2s to see if it stops".format(s['name'])) - console.info("Server {} is still running - waiting 2s to see if it stops".format(s['name'])) - running = svr_obj.check_running() - - # let's keep track of how long this is going on... - x = x + 1 - - # if we have been waiting more than 120 seconds. let's just kill the pid - if x >= 60: - logger.error("Server {} is taking way too long to stop. Killing this process".format(s['name'])) - console.error("Server {} is taking way too long to stop. Killing this process".format(s['name'])) - - svr_obj.killpid(svr_obj.PID) - running = False - - # if we killed the server, let's clean up the object - if not running: - svr_obj.cleanup_server_object() - - time.sleep(2) + self.stop_server(s['id']) # let's wait 2 seconds to let everything flush out time.sleep(2) @@ -151,6 +147,39 @@ class Controller: logger.info("All Servers Stopped") console.info("All Servers Stopped") + def stop_server(self, server_id): + # get object + svr_obj = self.get_server_obj(server_id) + svr_data = self.get_server_data(server_id) + server_name = svr_data['server_name'] + + running = svr_obj.check_running(True) + + # issue the stop command + svr_obj.stop_threaded_server() + + # while it's running, we wait + x = 0 + while running: + logger.info("Server {} is still running - waiting 2s to see if it stops".format(server_name)) + console.info("Server {} is still running - waiting 2s to see if it stops".format(server_name)) + running = svr_obj.check_running() + + # let's keep track of how long this is going on... + x = x + 1 + + # if we have been waiting more than 120 seconds. let's just kill the pid + if x >= 60: + logger.error("Server {} is taking way too long to stop. Killing this process".format(server_name)) + console.error("Server {} is taking way too long to stop. Killing this process".format(server_name)) + + svr_obj.killpid(svr_obj.PID) + running = False + + # if we killed the server, let's clean up the object + if not running: + svr_obj.cleanup_server_object() + def create_jar_server(self, server: str, version: str, name: str, min_mem: int, max_mem: int, port: int): server_id = helper.create_uuid() server_dir = os.path.join(helper.servers_dir, server_id) @@ -190,8 +219,7 @@ class Controller: def import_server(self): print("todo") - 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 register_server(self, name: str, server_id: str, server_dir: str, server_command: str, server_file: str, server_log_file: str, server_stop: str): # put data in the db new_id = Servers.insert({ Servers.server_name: name, @@ -222,5 +250,31 @@ class Controller: return new_id + def remove_server(self, server_id): + counter = 0 + for s in self.servers_list: + + # if this is the droid... im mean server we are looking for... + if int(s['server_id']) == int(server_id): + server_data = self.get_server_data(server_id) + server_name = server_data['server_name'] + + logger.info("Deleting Server: ID {} | Name: {} ".format(server_id, server_name)) + console.info("Deleting Server: ID {} | Name: {} ".format(server_id, server_name)) + + srv_obj = s['server_obj'] + running = srv_obj.check_running() + + if running: + self.stop_server(server_id) + + # remove the server from the DB + Servers.delete().where(Servers.server_id == server_id).execute() + + # remove the server from servers list + self.servers_list.pop(counter) + + counter += 1 + controller = Controller() diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 3f8da2d2..e4a3460a 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -57,6 +57,20 @@ class PanelHandler(BaseHandler): elif page == 'contribute': template = "panel/contribute.html" + elif page == "remove_server": + server_id = self.get_argument('id', None) + server_data = controller.get_server_data(server_id) + server_name = server_data['server_name'] + + db_helper.add_to_audit_log(user_data['user_id'], + "Deleted server {} named {}".format(server_id, server_name), + server_id, + self.get_remote_ip()) + + controller.remove_server(server_id) + self.redirect("/panel/dashboard") + return + elif page == 'dashboard': page_data['servers'] = db_helper.get_all_servers_stats() diff --git a/app/config/serverjars.json b/app/config/serverjars.json index 04fabdb4..eceb6d7c 100644 --- a/app/config/serverjars.json +++ b/app/config/serverjars.json @@ -1,5 +1,5 @@ { - "last_refreshed": "09/10/2020, 11:57:02", + "last_refreshed": "09/14/2020, 11:31:20", "servers": { "nukkitx": [ "1.14" @@ -68,6 +68,7 @@ "1.8" ], "bukkit": [ + "1.16.3", "1.16.2", "1.16.1", "1.15.2", @@ -96,6 +97,7 @@ "1.8" ], "paper": [ + "1.16.3", "1.16.2", "1.16.1", "1.15.2", @@ -118,6 +120,7 @@ "1.8.8" ], "spigot": [ + "1.16.3", "1.16.2", "1.16.1", "1.15.2", @@ -163,30 +166,31 @@ "1.1620w20a", "1.1620w19a", "1.1620w18a", - "1.1620w17a", "1.1620w16a", - "1.1620w15a", + "1.1620w10a", + "1.16.2rc2", + "1.1620w11a", + "1.1620w09a", + "1.1620w08a", + "1.1620w07a", + "1.16.3rc1", + "1.16.2rc1", "1.1620w14a", "1.1620w14infinite", "1.1620w13b", "1.1620w13a", "1.1620w12a", - "1.1620w11a", - "1.1620w10a", - "1.1620w09a", - "1.1620w08a", - "1.1620w07a", + "1.161.16.3rc1", + "1.16rc1", + "1.1620w17a", + "1.1620w15a", "1.1620w06a", "1.16.3", - "1.16.2rc2", - "1.16.3rc1", - "1.16.2rc1", "1.16.2pre3", "1.16.2pre2", "1.16.2pre1", "1.16.2", "1.161.16.1", - "1.16rc1", "1.16.1", "1.16", "1.15pre7", diff --git a/app/frontend/templates/panel/server_config.html b/app/frontend/templates/panel/server_config.html index 7a9fbe4a..a14732fb 100644 --- a/app/frontend/templates/panel/server_config.html +++ b/app/frontend/templates/panel/server_config.html @@ -160,7 +160,7 @@ Delete Server
Please stop the server before deleting it {% else %} - Delete Server + Delete Server {% end %}