From 5a1353373a79aa0b9df50a0f962ec7fe10e39083 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Wed, 25 May 2022 15:54:35 +0200 Subject: [PATCH 01/15] trying something new --- app/classes/models/server_stats.py | 159 +++++++++++++---------------- app/classes/models/servers.py | 2 +- app/classes/shared/server.py | 38 +++---- 3 files changed, 91 insertions(+), 108 deletions(-) diff --git a/app/classes/models/server_stats.py b/app/classes/models/server_stats.py index 7e16f444..9d5eba39 100644 --- a/app/classes/models/server_stats.py +++ b/app/classes/models/server_stats.py @@ -66,11 +66,11 @@ class ServerStats(Model): # Servers_Stats Methods # ********************************************************************************** class HelperServerStats: - def __init__(self, database): + def __init__(self, database, server_id): self.database = database + self.server_id = server_id - @staticmethod - def init_database(server_id): + def init_database(self, server_id): try: server = HelperServers.get_server_data_by_id(server_id) db_folder = os.path.join(f"{server['path']}", "db_stats") @@ -102,10 +102,9 @@ class HelperServerStats: f"Error try to look for the db_stats files for server : {ex}" ) - @staticmethod - def select_database(server_id): + def select_database(self): try: - server = HelperServers.get_server_data_by_id(server_id) + server = HelperServers.get_server_data_by_id(self.server_id) db_file = os.path.join( f"{server['path']}", "db_stats", @@ -120,13 +119,12 @@ class HelperServerStats: f"Error try to look for the db_stats files for server : {ex}" ) - @staticmethod - def get_all_servers_stats(): + def get_all_servers_stats(self): servers = HelperServers.get_all_defined_servers() server_data = [] try: for s in servers: - HelperServerStats.select_database(s.get("server_id")) + # self.select_database(s.get("server_id")) latest = ( ServerStats.select() .where(ServerStats.server_id == s.get("server_id")) @@ -146,10 +144,9 @@ class HelperServerStats: ) return server_data - @staticmethod - def insert_server_stats(server): + def insert_server_stats(self, server): server_id = server.get("id", 0) - HelperServerStats.select_database(server_id) + # self.select_database(server_id) if server_id == 0: logger.warning("Stats saving failed with error: Server unknown (id = 0)") @@ -175,174 +172,158 @@ class HelperServerStats: } ).execute() - @staticmethod - def remove_old_stats(server_id, last_week): - HelperServerStats.select_database(server_id) + def remove_old_stats(self, last_week): + # self.select_database(self.server_id) ServerStats.delete().where(ServerStats.created < last_week).execute() - @staticmethod - def get_latest_server_stats(server_id): - HelperServerStats.select_database(server_id) + def get_latest_server_stats(self): + # self.select_database(self.server_id) return ( ServerStats.select() - .where(ServerStats.server_id == server_id) + .where(ServerStats.server_id == self.server_id) .order_by(ServerStats.created.desc()) .limit(1) ) - @staticmethod - def get_server_stats_by_id(server_id): - HelperServerStats.select_database(server_id) + def get_server_stats_by_id(self): + # self.select_database(self.server_id) stats = ( ServerStats.select() - .where(ServerStats.server_id == server_id) + .where(ServerStats.server_id == self.server_id) .order_by(ServerStats.created.desc()) .limit(1) ) return DatabaseShortcuts.return_rows(stats)[0] - @staticmethod - def server_id_exists(server_id): - HelperServerStats.select_database(server_id) - if not HelperServers.get_server_data_by_id(server_id): + def server_id_exists(self): + # self.select_database(self.server_id) + if not HelperServers.get_server_data_by_id(self.server_id): return False return True - @staticmethod - def sever_crashed(server_id): - HelperServerStats.select_database(server_id) + def sever_crashed(self): + # self.select_database(self.server_id) ServerStats.update(crashed=True).where( - ServerStats.server_id == server_id + ServerStats.server_id == self.server_id ).execute() - @staticmethod - def set_download(server_id): - HelperServerStats.select_database(server_id) + def set_download(self): + # self.select_database(self.server_id) ServerStats.update(downloading=True).where( - ServerStats.server_id == server_id + ServerStats.server_id == self.server_id ).execute() - @staticmethod - def finish_download(server_id): - HelperServerStats.select_database(server_id) + def finish_download(self): + # self.select_database(self.server_id) ServerStats.update(downloading=False).where( - ServerStats.server_id == server_id + ServerStats.server_id == self.server_id ).execute() - @staticmethod - def get_download_status(server_id): - HelperServerStats.select_database(server_id) + def get_download_status(self): + # self.select_database(self.server_id) download_status = ( - ServerStats.select().where(ServerStats.server_id == server_id).get() + ServerStats.select().where(ServerStats.server_id == self.server_id).get() ) return download_status.downloading - @staticmethod - def server_crash_reset(server_id): - if server_id is None: + def server_crash_reset(self): + if self.server_id is None: return - HelperServerStats.select_database(server_id) + # self.select_database(self.server_id) ServerStats.update(crashed=False).where( - ServerStats.server_id == server_id + ServerStats.server_id == self.server_id ).execute() - @staticmethod - def is_crashed(server_id): - HelperServerStats.select_database(server_id) - svr = ServerStats.select().where(ServerStats.server_id == server_id).get() + def is_crashed(self): + # self.select_database(self.server_id) + svr = ServerStats.select().where(ServerStats.server_id == self.server_id).get() # pylint: disable=singleton-comparison if svr.crashed == True: return True else: return False - @staticmethod - def set_update(server_id, value): - if server_id is None: + def set_update(self, value): + if self.server_id is None: return - HelperServerStats.select_database(server_id) + # self.select_database(self.server_id) try: # Checks if server even exists - ServerStats.select().where(ServerStats.server_id == server_id) + ServerStats.select().where(ServerStats.server_id == self.server_id) except Exception as ex: logger.error(f"Database entry not found! {ex}") ServerStats.update(updating=value).where( - ServerStats.server_id == server_id + ServerStats.server_id == self.server_id ).execute() - @staticmethod - def get_update_status(server_id): - HelperServerStats.select_database(server_id) + def get_update_status(self): + # self.select_database(self.server_id) update_status = ( - ServerStats.select().where(ServerStats.server_id == server_id).get() + ServerStats.select().where(ServerStats.server_id == self.server_id).get() ) return update_status.updating - @staticmethod - def set_first_run(server_id): - HelperServerStats.select_database(server_id) + def set_first_run(self): + # self.select_database(self.server_id) # Sets first run to false try: # Checks if server even exists - ServerStats.select().where(ServerStats.server_id == server_id) + ServerStats.select().where(ServerStats.server_id == self.server_id) except Exception as ex: logger.error(f"Database entry not found! {ex}") return ServerStats.update(first_run=False).where( - ServerStats.server_id == server_id + ServerStats.server_id == self.server_id ).execute() - @staticmethod - def get_first_run(server_id): - HelperServerStats.select_database(server_id) - first_run = ServerStats.select().where(ServerStats.server_id == server_id).get() + def get_first_run(self): + # self.select_database(self.server_id) + first_run = ( + ServerStats.select().where(ServerStats.server_id == self.server_id).get() + ) return first_run.first_run - @staticmethod - def get_ttl_without_player(server_id): - HelperServerStats.select_database(server_id) + def get_ttl_without_player(self): + # self.select_database(self.server_id) last_stat = ( ServerStats.select() - .where(ServerStats.server_id == server_id) + .where(ServerStats.server_id == self.server_id) .order_by(ServerStats.created.desc()) .first() ) last_stat_with_player = ( ServerStats.select() - .where(ServerStats.server_id == server_id) + .where(ServerStats.server_id == self.server_id) .where(ServerStats.online > 0) .order_by(ServerStats.created.desc()) .first() ) return last_stat.created - last_stat_with_player.created - @staticmethod - def can_stop_no_players(server_id, time_limit): - HelperServerStats.select_database(server_id) + def can_stop_no_players(self, time_limit): + # self.select_database(self.server_id) can = False - ttl_no_players = HelperServerStats.get_ttl_without_player(server_id) + ttl_no_players = self.get_ttl_without_player() if (time_limit == -1) or (ttl_no_players > time_limit): can = True return can - @staticmethod - def set_waiting_start(server_id, value): - HelperServerStats.select_database(server_id) + def set_waiting_start(self, value): + # self.select_database(self.server_id) try: # Checks if server even exists - ServerStats.select().where(ServerStats.server_id == server_id) + ServerStats.select().where(ServerStats.server_id == self.server_id) except Exception as ex: logger.error(f"Database entry not found! {ex}") ServerStats.update(waiting_start=value).where( - ServerStats.server_id == server_id + ServerStats.server_id == self.server_id ).execute() - @staticmethod - def get_waiting_start(server_id): - HelperServerStats.select_database(server_id) + def get_waiting_start(self): + # self.select_database(self.server_id) waiting_start = ( - ServerStats.select().where(ServerStats.server_id == server_id).get() + ServerStats.select().where(ServerStats.server_id == self.server_id).get() ) return waiting_start.waiting_start diff --git a/app/classes/models/servers.py b/app/classes/models/servers.py index c2c449d7..e4dd4316 100644 --- a/app/classes/models/servers.py +++ b/app/classes/models/servers.py @@ -16,7 +16,7 @@ from app.classes.models.base_model import BaseModel logger = logging.getLogger(__name__) # ********************************************************************************** -# Servers Class +# Servers Model # ********************************************************************************** class Servers(BaseModel): server_id = AutoField() diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index 7d85ad59..0992f4a1 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -111,6 +111,8 @@ class Server: self.is_crashed = False self.restart_count = 0 self.stats = stats + self.server_object = HelperServers.get_server_obj(self.server_id) + self.stats_helper = HelperServerStats(self.server_id) tz = get_localzone() self.server_scheduler = BackgroundScheduler(timezone=str(tz)) self.server_scheduler.start() @@ -119,8 +121,8 @@ class Server: ) self.is_backingup = False # Reset crash and update at initialization - HelperServerStats.server_crash_reset(self.server_id) - HelperServerStats.set_update(self.server_id, False) + self.stats_helper.server_crash_reset(self.server_id) + self.stats_helper.set_update(self.server_id, False) # ********************************************************************************** # Minecraft Server Management @@ -143,7 +145,7 @@ class Server: self.name = server_name self.settings = server_data_obj - HelperServerStats.init_database(server_id) + self.stats_helper.init_database(server_id) self.record_server_stats() # build our server run command @@ -165,7 +167,7 @@ class Server: Console.info(f"Starting server ID: {self.server_id} - {self.name}") logger.info(f"Starting server ID: {self.server_id} - {self.name}") # Sets waiting start to false since we're attempting to start the server. - HelperServerStats.set_waiting_start(self.server_id, False) + self.stats_helper.set_waiting_start(self.server_id, False) self.run_threaded_server(None) # remove the scheduled job since it's ran @@ -232,7 +234,7 @@ class Server: else: user_lang = HelperUsers.get_user_lang_by_id(user_id) - if HelperServerStats.get_download_status(self.server_id): + if self.stats_helper.get_download_status(self.server_id): if user_id: self.helper.websocket_helper.broadcast_user( user_id, @@ -405,7 +407,7 @@ class Server: ).start() self.is_crashed = False - HelperServerStats.server_crash_reset(self.server_id) + self.stats_helper.server_crash_reset(self.server_id) self.start_time = str(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")) @@ -413,7 +415,7 @@ class Server: logger.info(f"Server {self.name} running with PID {self.process.pid}") Console.info(f"Server {self.name} running with PID {self.process.pid}") self.is_crashed = False - HelperServerStats.server_crash_reset(self.server_id) + self.stats_helper.server_crash_reset(self.server_id) self.record_server_stats() check_internet_thread = threading.Thread( target=self.check_internet_thread, @@ -426,9 +428,9 @@ class Server: ) check_internet_thread.start() # Checks if this is the servers first run. - if HelperServerStats.get_first_run(self.server_id): - HelperServerStats.set_first_run(self.server_id) - loc_server_port = HelperServerStats.get_server_stats_by_id( + if self.stats_helper.get_first_run(self.server_id): + self.stats_helper.set_first_run(self.server_id) + loc_server_port = self.stats_helper.get_server_stats_by_id( self.server_id )["server_port"] # Sends port reminder message. @@ -710,7 +712,7 @@ class Server: self.server_scheduler.remove_job("c_" + str(self.server_id)) return - HelperServerStats.sever_crashed(self.server_id) + self.stats_helper.sever_crashed(self.server_id) # if we haven't tried to restart more 3 or more times if self.restart_count <= 3: @@ -734,7 +736,7 @@ class Server: self.restart_count = 0 self.is_crashed = True - HelperServerStats.sever_crashed(self.server_id) + self.stats_helper.sever_crashed(self.server_id) # cancel the watcher task self.server_scheduler.remove_job("c_" + str(self.server_id)) @@ -964,7 +966,7 @@ class Server: return [] def jar_update(self): - HelperServerStats.set_update(self.server_id, True) + self.stats_helper.set_update(self.server_id, True) update_thread = threading.Thread( target=self.a_jar_update, daemon=True, name=f"exe_update_{self.name}" ) @@ -972,7 +974,7 @@ class Server: def check_update(self): - if HelperServerStats.get_server_stats_by_id(self.server_id)["updating"]: + if self.stats_helper.get_server_stats_by_id(self.server_id)["updating"]: return True else: return False @@ -1045,11 +1047,11 @@ class Server: self.settings["executable_update_url"], current_executable ) - while HelperServerStats.get_server_stats_by_id(self.server_id)["updating"]: + while self.stats_helper.get_server_stats_by_id(self.server_id)["updating"]: if downloaded and not self.is_backingup: logger.info("Executable updated successfully. Starting Server") - HelperServerStats.set_update(self.server_id, False) + self.stats_helper.set_update(self.server_id, False) if len(self.helper.websocket_helper.clients) > 0: # There are clients self.check_update() @@ -1445,11 +1447,11 @@ class Server: def record_server_stats(self): server = self.get_servers_stats() - HelperServerStats.insert_server_stats(server) + self.stats_helper.insert_server_stats(server) # delete old data max_age = self.helper.get_setting("history_max_age") now = datetime.datetime.now() last_week = now.day - max_age - HelperServerStats.remove_old_stats(server.get("id", 0), last_week) + self.stats_helper.remove_old_stats(server.get("id", 0), last_week) From 8423cc24c1ea196912648f0150d5886487e6fa74 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Thu, 26 May 2022 14:50:20 +0200 Subject: [PATCH 02/15] I'm breaking/fixing stuff --- .../controllers/server_perms_controller.py | 8 +- app/classes/controllers/servers_controller.py | 289 ++++++++++++++++-- app/classes/models/server_stats.py | 99 ++++-- app/classes/shared/main_controller.py | 195 +----------- app/classes/shared/server.py | 43 +-- app/classes/shared/singleton.py | 7 + app/classes/shared/tasks.py | 6 +- app/classes/web/ajax_handler.py | 8 +- app/classes/web/api_handler.py | 10 +- app/classes/web/base_handler.py | 2 +- app/classes/web/panel_handler.py | 54 ++-- app/classes/web/routes/api/servers/index.py | 2 +- app/classes/web/server_handler.py | 14 +- main.py | 4 +- 14 files changed, 424 insertions(+), 317 deletions(-) create mode 100644 app/classes/shared/singleton.py diff --git a/app/classes/controllers/server_perms_controller.py b/app/classes/controllers/server_perms_controller.py index 95395fec..05fa1572 100644 --- a/app/classes/controllers/server_perms_controller.py +++ b/app/classes/controllers/server_perms_controller.py @@ -1,4 +1,5 @@ import logging +from app.classes.controllers.servers_controller import ServersController from app.classes.models.server_permissions import ( PermissionsServers, @@ -8,7 +9,9 @@ from app.classes.models.users import HelperUsers, ApiKeys from app.classes.models.roles import HelperRoles from app.classes.models.servers import HelperServers from app.classes.models.server_stats import HelperServerStats + from app.classes.shared.main_models import DatabaseShortcuts +from app.classes.shared.server import Server logger = logging.getLogger(__name__) @@ -107,7 +110,10 @@ class ServerPermsController: ) for server in authorized_servers: - latest = HelperServerStats.get_latest_server_stats(server.get("server_id")) + srv: Server = ServersController().get_server_instance_by_id( + server.get("server_id") + ) + latest = srv.stats_helper.get_latest_server_stats() server_data.append( { "server_data": server, diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index f9aa2143..8c9f142b 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -1,9 +1,23 @@ +from gettext import install +from inspect import _void import os import logging +from pydoc import Helper +import time import json import typing as t +from typing_extensions import Self from app.classes.controllers.roles_controller import RolesController + +from app.classes.shared.server import Server +from app.classes.shared.console import Console +from app.classes.shared.helpers import Helpers +from app.classes.shared.main_models import DatabaseShortcuts + +from app.classes.minecraft.server_props import ServerProps +from app.classes.minecraft.stats import Stats + from app.classes.models.servers import HelperServers from app.classes.models.server_stats import HelperServerStats from app.classes.models.users import HelperUsers, ApiKeys @@ -11,15 +25,20 @@ from app.classes.models.server_permissions import ( PermissionsServers, EnumPermissionsServer, ) -from app.classes.shared.helpers import Helpers -from app.classes.shared.main_models import DatabaseShortcuts +from app.classes.shared.singleton import Singleton logger = logging.getLogger(__name__) -class ServersController: - def __init__(self, servers_helper): - self.servers_helper = servers_helper +class ServersController(metaclass=Singleton): + servers_list: Server + + def __init__(self, helper, servers_helper, management_helper): + self.helper: Helper = helper + self.servers_helper: HelperServers = servers_helper + self.management_helper = management_helper + self.servers_list = [] + self.stats = Stats(self.helper, self) # ********************************************************************************** # Generic Servers Methods @@ -83,15 +102,18 @@ class ServersController: @staticmethod def set_download(server_id): - return HelperServerStats.set_download(server_id) + srv: Server = ServersController().get_server_instance_by_id(server_id) + return srv.stats_helper.set_download(server_id) @staticmethod def finish_download(server_id): - return HelperServerStats.finish_download(server_id) + srv: Server = ServersController().get_server_instance_by_id(server_id) + return srv.stats_helper.finish_download(server_id) @staticmethod def get_download_status(server_id): - return HelperServerStats.get_download_status(server_id) + server: Server = ServersController().get_server_instance_by_id(server_id) + return server.stats_helper.get_download_status() def remove_server(self, server_id): roles_list = PermissionsServers.get_roles_from_server(server_id) @@ -110,6 +132,105 @@ class ServersController: # ********************************************************************************** # Servers Methods # ********************************************************************************** + + def get_server_instance_by_id(self, server_id): + for server in self.servers_list: + if server["server_id"] == int(server_id): + return server["server_obj"] + return None + + def init_all_servers(self): + + servers = self.get_all_defined_servers() + + for server in servers: + server_id = server.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 Helpers.check_path_exists( + Helpers.get_os_understandable_path(server["path"]) + ): + logger.warning( + f"Unable to find server " + f"{server['server_name']} at path {server['path']}. " + f"Skipping this server" + ) + + Console.warning( + f"Unable to find server " + f"{server['server_name']} at path {server['path']}. " + f"Skipping this server" + ) + continue + + settings_file = os.path.join( + Helpers.get_os_understandable_path(server["path"]), "server.properties" + ) + + # if the properties file isn't there, let's warn + if not Helpers.check_file_exists(settings_file): + logger.error(f"Unable to find {settings_file}. Skipping this server.") + Console.error(f"Unable to find {settings_file}. Skipping this server.") + continue + + settings = ServerProps(settings_file) + + temp_server_dict = { + "server_id": server.get("server_id"), + "server_data_obj": server, + "server_obj": Server( + server.get("server_id"), + self.helper, + self.management_helper, + self.stats, + ), + "server_settings": settings.props, + } + + # setup the server, do the auto start and all that jazz + temp_server_dict["server_obj"].do_server_setup(server) + + # add this temp object to the list of init servers + self.servers_list.append(temp_server_dict) + + if server["auto_start"]: + self.servers.set_waiting_start(server["server_id"], True) + + self.refresh_server_settings(server["server_id"]) + + Console.info( + f"Loaded Server: ID {server['server_id']}" + f" | Name: {server['server_name']}" + f" | Autostart: {server['auto_start']}" + f" | Delay: {server['auto_start_delay']}" + ) + + def check_server_loaded(self, server_id_to_check: int): + + logger.info(f"Checking to see if we already registered {server_id_to_check}") + + for server in self.servers_list: + known_server = server.get("server_id") + if known_server is None: + return False + + if known_server == server_id_to_check: + logger.info( + f"skipping initialization of server {server_id_to_check} " + f"because it is already loaded" + ) + return True + + return False + + def refresh_server_settings(self, server_id: int): + server_obj = self.get_server_obj(server_id) + server_obj.reload_server_settings() + @staticmethod def get_all_defined_servers(): return HelperServers.get_all_defined_servers() @@ -141,9 +262,26 @@ class ServersController: return user_ids - @staticmethod - def get_all_servers_stats(): - return HelperServerStats.get_all_servers_stats() + def get_all_servers_stats(self): + server_data = [] + try: + for server in self.servers_list: + srv: Server = ServersController().get_server_instance_by_id( + server.get("server_id") + ) + latest = srv.stats_helper.get_latest_server_stats() + server_data.append( + { + "server_data": server["server_data_obj"], + "stats": latest, + "user_command_permission": True, + } + ) + except IndexError as ex: + logger.error( + f"Stats collection failed with error: {ex}. Was a server just created?" + ) + return server_data @staticmethod def get_authorized_servers_stats_api_key(api_key: ApiKeys): @@ -153,7 +291,8 @@ class ServersController: ) for server in authorized_servers: - latest = HelperServerStats.get_latest_server_stats(server.get("server_id")) + srv: Server = server + latest = srv.stats_helper.get_latest_server_stats() key_permissions = PermissionsServers.get_api_key_permissions_list( api_key, server.get("server_id") ) @@ -176,7 +315,8 @@ class ServersController: authorized_servers = ServersController.get_authorized_servers(user_id) for server in authorized_servers: - latest = HelperServerStats.get_latest_server_stats(server.get("server_id")) + srv: Server = server + latest = srv.stats_helper.get_latest_server_stats() # TODO user_permissions = PermissionsServers.get_user_id_permissions_list( user_id, server.get("server_id") @@ -199,16 +339,111 @@ class ServersController: def get_server_friendly_name(server_id): return HelperServers.get_server_friendly_name(server_id) + def get_server_settings(self, server_id): + for server in self.servers_list: + if int(server["server_id"]) == int(server_id): + return server["server_settings"] + + logger.warning(f"Unable to find server object for server id {server_id}") + return False + + def crash_detection(self, server_obj): + svr = self.get_server_obj(server_obj.server_id) + # start or stop crash detection depending upon user preference + # The below functions check to see if the server is running. + # They only execute if it's running. + if server_obj.crash_detection == 1: + svr.start_crash_detection() + else: + svr.stop_crash_detection() + + def get_server_obj(self, server_id: t.Union[str, int]) -> Server: + for server in self.servers_list: + if str(server["server_id"]) == str(server_id): + return server["server_obj"] + + logger.warning(f"Unable to find server object for server id {server_id}") + raise Exception(f"Unable to find server object for server id {server_id}") + + def get_server_obj_optional( + self, server_id: t.Union[str, int] + ) -> t.Optional[Server]: + for server in self.servers_list: + if str(server["server_id"]) == str(server_id): + return server["server_obj"] + + logger.warning(f"Unable to find server object for server id {server_id}") + return None + + def get_server_data(self, server_id: str): + for server in self.servers_list: + if str(server["server_id"]) == str(server_id): + return server["server_data_obj"] + + logger.warning(f"Unable to find server object for server id {server_id}") + return False + + @staticmethod + def list_defined_servers(): + servers = HelperServers.get_all_defined_servers() + return servers + + @staticmethod + def get_all_server_ids() -> t.List[int]: + return HelperServers.get_all_server_ids() + + def list_running_servers(self): + running_servers = [] + + # for each server + for servers in self.servers_list: + + # is the server running? + srv_obj = servers["server_obj"] + running = srv_obj.check_running() + # if so, let's add a dictionary to the list of running servers + if running: + running_servers.append({"id": srv_obj.server_id, "name": srv_obj.name}) + + return running_servers + + def stop_all_servers(self): + servers = self.list_running_servers() + logger.info(f"Found {len(servers)} running server(s)") + Console.info(f"Found {len(servers)} running server(s)") + + logger.info("Stopping All Servers") + Console.info("Stopping All Servers") + + for server in servers: + logger.info(f"Stopping Server ID {server['id']} - {server['name']}") + Console.info(f"Stopping Server ID {server['id']} - {server['name']}") + + self.stop_server(server["id"]) + + # let's wait 2 seconds to let everything flush out + time.sleep(2) + + logger.info("All Servers Stopped") + Console.info("All Servers Stopped") + + def stop_server(self, server_id): + # issue the stop command + svr_obj = self.get_server_obj(server_id) + svr_obj.stop_threaded_server() + # ********************************************************************************** # Servers_Stats Methods # ********************************************************************************** @staticmethod def get_server_stats_by_id(server_id): - return HelperServerStats.get_server_stats_by_id(server_id) + srv: Server = ServersController().get_server_instance_by_id(server_id) + return srv.stats_helper.get_latest_server_stats() @staticmethod def server_id_exists(server_id): - return HelperServerStats.server_id_exists(server_id) + srv: Server = ServersController().get_server_instance_by_id(server_id) + return srv.stats_helper.server_id_exists() @staticmethod def get_server_type_by_id(server_id): @@ -227,7 +462,8 @@ class ServersController: @staticmethod def is_crashed(server_id): - return HelperServerStats.is_crashed(server_id) + srv: Server = ServersController().get_server_instance_by_id(server_id) + return srv.stats_helper.is_crashed() @staticmethod def server_id_authorized_api_key(server_id: str, api_key: ApiKeys) -> bool: @@ -238,34 +474,41 @@ class ServersController: @staticmethod def set_update(server_id, value): - return HelperServerStats.set_update(server_id, value) + srv: Server = ServersController().get_server_instance_by_id(server_id) + return srv.stats_helper.set_update(value) @staticmethod def get_ttl_without_player(server_id): - return HelperServerStats.get_ttl_without_player(server_id) + srv: Server = ServersController().get_server_instance_by_id(server_id) + return srv.stats_helper.get_ttl_without_player() @staticmethod def can_stop_no_players(server_id, time_limit): - return HelperServerStats.can_stop_no_players(server_id, time_limit) + srv: Server = ServersController().get_server_instance_by_id(server_id) + return srv.stats_helper.can_stop_no_players(time_limit) @staticmethod def set_waiting_start(server_id, value): - HelperServerStats.set_waiting_start(server_id, value) + srv: Server = ServersController().get_server_instance_by_id(server_id) + srv.stats_helper.set_waiting_start(value) @staticmethod def get_waiting_start(server_id): - return HelperServerStats.get_waiting_start(server_id) + srv: Server = ServersController().get_server_instance_by_id(server_id) + return srv.stats_helper.get_waiting_start() @staticmethod def get_update_status(server_id): - return HelperServerStats.get_update_status(server_id) + srv: Server = ServersController().get_server_instance_by_id(server_id) + return srv.stats_helper.get_update_status() # ********************************************************************************** # Servers Helpers Methods # ********************************************************************************** @staticmethod def get_banned_players(server_id): - stats = HelperServerStats.get_server_stats_by_id(server_id) + srv: Server = ServersController().get_server_instance_by_id(server_id) + stats = srv.stats_helper.get_server_stats() server_path = stats["server_id"]["path"] path = os.path.join(server_path, "banned-players.json") diff --git a/app/classes/models/server_stats.py b/app/classes/models/server_stats.py index 9d5eba39..c34fea0c 100644 --- a/app/classes/models/server_stats.py +++ b/app/classes/models/server_stats.py @@ -2,6 +2,12 @@ import os import logging import datetime +import typing as t + +from peewee import DoesNotExist +from playhouse.shortcuts import model_to_dict + + from app.classes.models.servers import Servers, HelperServers from app.classes.shared.helpers import Helpers from app.classes.shared.main_models import DatabaseShortcuts @@ -27,7 +33,7 @@ except ModuleNotFoundError as e: logger = logging.getLogger(__name__) peewee_logger = logging.getLogger("peewee") peewee_logger.setLevel(logging.INFO) -database_stats_proxy = DatabaseProxy() +# database_stats_proxy = DatabaseProxy() # ********************************************************************************** @@ -59,16 +65,19 @@ class ServerStats(Model): class Meta: table_name = "server_stats" - database = database_stats_proxy + # database = database_stats_proxy # ********************************************************************************** # Servers_Stats Methods # ********************************************************************************** class HelperServerStats: - def __init__(self, database, server_id): - self.database = database - self.server_id = server_id + server_id: int + database = None + + def __init__(self, server_id): + self.server_id = int(server_id) + self.init_database(self.server_id) def init_database(self, server_id): try: @@ -78,7 +87,7 @@ class HelperServerStats: db_folder, "crafty_server_stats.sqlite", ) - database = SqliteDatabase( + self.database = SqliteDatabase( db_file, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10} ) if not os.path.exists(db_file): @@ -93,10 +102,10 @@ class HelperServerStats: f"{helper_stats.migration_dir}", "stats" ) helper_stats.db_path = db_file - database_stats_proxy.initialize(database) - migration_manager = MigrationManager(database, helper_stats) + # database_stats_proxy.initialize(self.database) + migration_manager = MigrationManager(self.database, helper_stats) migration_manager.up() # Automatically runs migrations - database_stats_proxy.initialize(database) + # database_stats_proxy.initialize(self.database) except Exception as ex: logger.warning( f"Error try to look for the db_stats files for server : {ex}" @@ -110,10 +119,10 @@ class HelperServerStats: "db_stats", "crafty_server_stats.sqlite", ) - database = SqliteDatabase( + self.database = SqliteDatabase( db_file, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10} ) - database_stats_proxy.initialize(database) + # database_stats_proxy.initialize(self.database) except Exception as ex: logger.warning( f"Error try to look for the db_stats files for server : {ex}" @@ -131,6 +140,7 @@ class HelperServerStats: .order_by(ServerStats.created.desc()) .limit(1) ) + latest._database = self.database server_data.append( { "server_data": s, @@ -170,30 +180,37 @@ class HelperServerStats: ServerStats.desc: server.get("desc", False), ServerStats.version: server.get("version", False), } - ).execute() + ).execute(self.database) def remove_old_stats(self, last_week): # self.select_database(self.server_id) - ServerStats.delete().where(ServerStats.created < last_week).execute() + ServerStats.delete().where(ServerStats.created < last_week).execute( + self.database + ) def get_latest_server_stats(self): - # self.select_database(self.server_id) - return ( + latest = ( ServerStats.select() .where(ServerStats.server_id == self.server_id) .order_by(ServerStats.created.desc()) .limit(1) + .get(self.database) ) + try: + return model_to_dict(latest) + except IndexError: + return {} - def get_server_stats_by_id(self): + def get_server_stats(self): # self.select_database(self.server_id) stats = ( ServerStats.select() .where(ServerStats.server_id == self.server_id) .order_by(ServerStats.created.desc()) .limit(1) + .get(self.database) ) - return DatabaseShortcuts.return_rows(stats)[0] + return model_to_dict(stats) def server_id_exists(self): # self.select_database(self.server_id) @@ -205,24 +222,26 @@ class HelperServerStats: # self.select_database(self.server_id) ServerStats.update(crashed=True).where( ServerStats.server_id == self.server_id - ).execute() + ).execute(self.database) def set_download(self): # self.select_database(self.server_id) ServerStats.update(downloading=True).where( ServerStats.server_id == self.server_id - ).execute() + ).execute(self.database) def finish_download(self): # self.select_database(self.server_id) ServerStats.update(downloading=False).where( ServerStats.server_id == self.server_id - ).execute() + ).execute(self.database) def get_download_status(self): # self.select_database(self.server_id) download_status = ( - ServerStats.select().where(ServerStats.server_id == self.server_id).get() + ServerStats.select() + .where(ServerStats.server_id == self.server_id) + .get(self.database) ) return download_status.downloading @@ -233,11 +252,15 @@ class HelperServerStats: # self.select_database(self.server_id) ServerStats.update(crashed=False).where( ServerStats.server_id == self.server_id - ).execute() + ).execute(self.database) def is_crashed(self): # self.select_database(self.server_id) - svr = ServerStats.select().where(ServerStats.server_id == self.server_id).get() + svr: ServerStats = ( + ServerStats.select() + .where(ServerStats.server_id == self.server_id) + .get(self.database) + ) # pylint: disable=singleton-comparison if svr.crashed == True: return True @@ -251,17 +274,21 @@ class HelperServerStats: # self.select_database(self.server_id) try: # Checks if server even exists - ServerStats.select().where(ServerStats.server_id == self.server_id) + ServerStats.select().where(ServerStats.server_id == self.server_id).execute( + self.database + ) except Exception as ex: logger.error(f"Database entry not found! {ex}") ServerStats.update(updating=value).where( ServerStats.server_id == self.server_id - ).execute() + ).execute(self.database) def get_update_status(self): # self.select_database(self.server_id) update_status = ( - ServerStats.select().where(ServerStats.server_id == self.server_id).get() + ServerStats.select() + .where(ServerStats.server_id == self.server_id) + .get(self.database) ) return update_status.updating @@ -270,18 +297,22 @@ class HelperServerStats: # Sets first run to false try: # Checks if server even exists - ServerStats.select().where(ServerStats.server_id == self.server_id) + ServerStats.select().where(ServerStats.server_id == self.server_id).execute( + self.database + ) except Exception as ex: logger.error(f"Database entry not found! {ex}") return ServerStats.update(first_run=False).where( ServerStats.server_id == self.server_id - ).execute() + ).execute(self.database) def get_first_run(self): # self.select_database(self.server_id) first_run = ( - ServerStats.select().where(ServerStats.server_id == self.server_id).get() + ServerStats.select() + .where(ServerStats.server_id == self.server_id) + .get(self.database) ) return first_run.first_run @@ -291,14 +322,14 @@ class HelperServerStats: ServerStats.select() .where(ServerStats.server_id == self.server_id) .order_by(ServerStats.created.desc()) - .first() + .first(self.database) ) last_stat_with_player = ( ServerStats.select() .where(ServerStats.server_id == self.server_id) .where(ServerStats.online > 0) .order_by(ServerStats.created.desc()) - .first() + .first(self.database) ) return last_stat.created - last_stat_with_player.created @@ -319,11 +350,13 @@ class HelperServerStats: logger.error(f"Database entry not found! {ex}") ServerStats.update(waiting_start=value).where( ServerStats.server_id == self.server_id - ).execute() + ).execute(self.database) def get_waiting_start(self): # self.select_database(self.server_id) waiting_start = ( - ServerStats.select().where(ServerStats.server_id == self.server_id).get() + ServerStats.select() + .where(ServerStats.server_id == self.server_id) + .get(self.database) ) return waiting_start.waiting_start diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index c2cf04c4..cef47ccb 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -26,11 +26,8 @@ from app.classes.models.servers import HelperServers 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.server import Server from app.classes.shared.file_helpers import FileHelpers -from app.classes.minecraft.server_props import ServerProps from app.classes.minecraft.serverjars import ServerJars -from app.classes.minecraft.stats import Stats logger = logging.getLogger(__name__) @@ -44,13 +41,13 @@ class Controller: self.servers_helper = HelperServers(database) self.management_helper = HelpersManagement(database, self.helper) self.authentication = Authentication(self.helper) - self.servers_list = [] - self.stats = Stats(self.helper, self) self.crafty_perms = CraftyPermsController() self.management = ManagementController(self.management_helper) self.roles = RolesController(self.users_helper, self.roles_helper) self.server_perms = ServerPermsController() - self.servers = ServersController(self.servers_helper) + self.servers = ServersController( + self.helper, self.servers_helper, self.management_helper + ) self.users = UsersController( self.helper, self.users_helper, self.authentication ) @@ -58,93 +55,6 @@ class Controller: self.support_scheduler = BackgroundScheduler(timezone=str(tz)) self.support_scheduler.start() - def check_server_loaded(self, server_id_to_check: int): - - logger.info(f"Checking to see if we already registered {server_id_to_check}") - - for server in self.servers_list: - known_server = server.get("server_id") - if known_server is None: - return False - - if known_server == server_id_to_check: - logger.info( - f"skipping initialization of server {server_id_to_check} " - f"because it is already loaded" - ) - return True - - return False - - def init_all_servers(self): - - servers = self.servers.get_all_defined_servers() - - for server in servers: - server_id = server.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 Helpers.check_path_exists( - Helpers.get_os_understandable_path(server["path"]) - ): - logger.warning( - f"Unable to find server " - f"{server['server_name']} at path {server['path']}. " - f"Skipping this server" - ) - - Console.warning( - f"Unable to find server " - f"{server['server_name']} at path {server['path']}. " - f"Skipping this server" - ) - continue - - settings_file = os.path.join( - Helpers.get_os_understandable_path(server["path"]), "server.properties" - ) - - # if the properties file isn't there, let's warn - if not Helpers.check_file_exists(settings_file): - logger.error(f"Unable to find {settings_file}. Skipping this server.") - Console.error(f"Unable to find {settings_file}. Skipping this server.") - continue - - settings = ServerProps(settings_file) - - temp_server_dict = { - "server_id": server.get("server_id"), - "server_data_obj": server, - "server_obj": Server(self.helper, self.management_helper, self.stats), - "server_settings": settings.props, - } - - # setup the server, do the auto start and all that jazz - temp_server_dict["server_obj"].do_server_setup(server) - - # add this temp object to the list of init servers - self.servers_list.append(temp_server_dict) - - if server["auto_start"]: - self.servers.set_waiting_start(server["server_id"], True) - - self.refresh_server_settings(server["server_id"]) - - Console.info( - f"Loaded Server: ID {server['server_id']}" - f" | Name: {server['server_name']}" - f" | Autostart: {server['auto_start']}" - f" | Delay: {server['auto_start_delay']}" - ) - - def refresh_server_settings(self, server_id: int): - server_obj = self.get_server_obj(server_id) - server_obj.reload_server_settings() - @staticmethod def check_system_user(): if HelperUsers.get_user_id_by_name("system") is not None: @@ -243,24 +153,6 @@ class Controller: False, ) - def get_server_settings(self, server_id): - for server in self.servers_list: - if int(server["server_id"]) == int(server_id): - return server["server_settings"] - - logger.warning(f"Unable to find server object for server id {server_id}") - return False - - def crash_detection(self, server_obj): - svr = self.get_server_obj(server_obj.server_id) - # start or stop crash detection depending upon user preference - # The below functions check to see if the server is running. - # They only execute if it's running. - if server_obj.crash_detection == 1: - svr.start_crash_detection() - else: - svr.stop_crash_detection() - def log_status(self, source_path, dest_path, exec_user): results = Helpers.calc_percent(source_path, dest_path) self.log_stats = results @@ -276,81 +168,6 @@ class Controller: except: return {"percent": 0, "total_files": 0} - def get_server_obj(self, server_id: t.Union[str, int]) -> Server: - for server in self.servers_list: - if str(server["server_id"]) == str(server_id): - return server["server_obj"] - - logger.warning(f"Unable to find server object for server id {server_id}") - raise Exception(f"Unable to find server object for server id {server_id}") - - def get_server_obj_optional( - self, server_id: t.Union[str, int] - ) -> t.Optional[Server]: - for server in self.servers_list: - if str(server["server_id"]) == str(server_id): - return server["server_obj"] - - logger.warning(f"Unable to find server object for server id {server_id}") - return None - - def get_server_data(self, server_id: str): - for server in self.servers_list: - if str(server["server_id"]) == str(server_id): - return server["server_data_obj"] - - logger.warning(f"Unable to find server object for server id {server_id}") - return False - - @staticmethod - def list_defined_servers(): - servers = HelperServers.get_all_defined_servers() - return servers - - @staticmethod - def get_all_server_ids() -> t.List[int]: - return HelperServers.get_all_server_ids() - - def list_running_servers(self): - running_servers = [] - - # for each server - for servers in self.servers_list: - - # is the server running? - srv_obj = servers["server_obj"] - running = srv_obj.check_running() - # if so, let's add a dictionary to the list of running servers - if running: - running_servers.append({"id": srv_obj.server_id, "name": srv_obj.name}) - - return running_servers - - def stop_all_servers(self): - servers = self.list_running_servers() - logger.info(f"Found {len(servers)} running server(s)") - Console.info(f"Found {len(servers)} running server(s)") - - logger.info("Stopping All Servers") - Console.info("Stopping All Servers") - - for server in servers: - logger.info(f"Stopping Server ID {server['id']} - {server['name']}") - Console.info(f"Stopping Server ID {server['id']} - {server['name']}") - - self.stop_server(server["id"]) - - # let's wait 2 seconds to let everything flush out - time.sleep(2) - - logger.info("All Servers Stopped") - Console.info("All Servers Stopped") - - def stop_server(self, server_id): - # issue the stop command - svr_obj = self.get_server_obj(server_id) - svr_obj.stop_threaded_server() - def create_api_server(self, data: dict): server_fs_uuid = Helpers.create_uuid() new_server_path = os.path.join(self.helper.servers_dir, server_fs_uuid) @@ -981,13 +798,13 @@ class Controller: return False # let's re-init all servers - self.init_all_servers() + self.servers.init_all_servers() return new_id def remove_server(self, server_id, files): counter = 0 - for server in self.servers_list: + for server in self.servers.servers_list: # if this is the droid... im mean server we are looking for... if str(server["server_id"]) == str(server_id): @@ -1035,7 +852,7 @@ class Controller: self.servers.remove_server(server_id) # remove the server from servers list - self.servers_list.pop(counter) + self.servers.servers_list.pop(counter) counter += 1 diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index 0992f4a1..c7375b44 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -17,7 +17,7 @@ from apscheduler.schedulers.background import BackgroundScheduler from app.classes.minecraft.stats import Stats from app.classes.minecraft.mc_ping import ping, ping_bedrock -from app.classes.models.servers import HelperServers +from app.classes.models.servers import HelperServers, Servers from app.classes.models.server_stats import HelperServerStats from app.classes.models.management import HelpersManagement from app.classes.models.users import HelperUsers @@ -93,7 +93,10 @@ class ServerOutBuf: # Minecraft Server Class # ********************************************************************************** class Server: - def __init__(self, helper, management_helper, stats): + server_object: Servers + stats_helper: HelperServerStats + + def __init__(self, server_id, helper, management_helper, stats): self.helper = helper self.management_helper = management_helper # holders for our process @@ -105,7 +108,7 @@ class Server: self.server_thread = None self.settings = None self.updating = False - self.server_id = None + self.server_id = server_id self.jar_update_url = None self.name = None self.is_crashed = False @@ -121,8 +124,8 @@ class Server: ) self.is_backingup = False # Reset crash and update at initialization - self.stats_helper.server_crash_reset(self.server_id) - self.stats_helper.set_update(self.server_id, False) + self.stats_helper.server_crash_reset() + self.stats_helper.set_update(False) # ********************************************************************************** # Minecraft Server Management @@ -167,7 +170,7 @@ class Server: Console.info(f"Starting server ID: {self.server_id} - {self.name}") logger.info(f"Starting server ID: {self.server_id} - {self.name}") # Sets waiting start to false since we're attempting to start the server. - self.stats_helper.set_waiting_start(self.server_id, False) + self.stats_helper.set_waiting_start(False) self.run_threaded_server(None) # remove the scheduled job since it's ran @@ -234,7 +237,7 @@ class Server: else: user_lang = HelperUsers.get_user_lang_by_id(user_id) - if self.stats_helper.get_download_status(self.server_id): + if self.stats_helper.get_download_status(): if user_id: self.helper.websocket_helper.broadcast_user( user_id, @@ -407,7 +410,7 @@ class Server: ).start() self.is_crashed = False - self.stats_helper.server_crash_reset(self.server_id) + self.stats_helper.server_crash_reset() self.start_time = str(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")) @@ -415,7 +418,7 @@ class Server: logger.info(f"Server {self.name} running with PID {self.process.pid}") Console.info(f"Server {self.name} running with PID {self.process.pid}") self.is_crashed = False - self.stats_helper.server_crash_reset(self.server_id) + self.stats_helper.server_crash_reset() self.record_server_stats() check_internet_thread = threading.Thread( target=self.check_internet_thread, @@ -428,11 +431,9 @@ class Server: ) check_internet_thread.start() # Checks if this is the servers first run. - if self.stats_helper.get_first_run(self.server_id): - self.stats_helper.set_first_run(self.server_id) - loc_server_port = self.stats_helper.get_server_stats_by_id( - self.server_id - )["server_port"] + if self.stats_helper.get_first_run(): + self.stats_helper.set_first_run() + loc_server_port = self.stats_helper.get_server_stats()["server_port"] # Sends port reminder message. self.helper.websocket_helper.broadcast_user( user_id, @@ -712,7 +713,7 @@ class Server: self.server_scheduler.remove_job("c_" + str(self.server_id)) return - self.stats_helper.sever_crashed(self.server_id) + self.stats_helper.sever_crashed() # if we haven't tried to restart more 3 or more times if self.restart_count <= 3: @@ -736,7 +737,7 @@ class Server: self.restart_count = 0 self.is_crashed = True - self.stats_helper.sever_crashed(self.server_id) + self.stats_helper.sever_crashed() # cancel the watcher task self.server_scheduler.remove_job("c_" + str(self.server_id)) @@ -966,7 +967,7 @@ class Server: return [] def jar_update(self): - self.stats_helper.set_update(self.server_id, True) + self.stats_helper.set_update(True) update_thread = threading.Thread( target=self.a_jar_update, daemon=True, name=f"exe_update_{self.name}" ) @@ -974,7 +975,7 @@ class Server: def check_update(self): - if self.stats_helper.get_server_stats_by_id(self.server_id)["updating"]: + if self.stats_helper.get_server_stats()["updating"]: return True else: return False @@ -1047,11 +1048,11 @@ class Server: self.settings["executable_update_url"], current_executable ) - while self.stats_helper.get_server_stats_by_id(self.server_id)["updating"]: + while self.stats_helper.get_server_stats()["updating"]: if downloaded and not self.is_backingup: logger.info("Executable updated successfully. Starting Server") - self.stats_helper.set_update(self.server_id, False) + self.stats_helper.set_update(False) if len(self.helper.websocket_helper.clients) > 0: # There are clients self.check_update() @@ -1454,4 +1455,4 @@ class Server: now = datetime.datetime.now() last_week = now.day - max_age - self.stats_helper.remove_old_stats(server.get("id", 0), last_week) + self.stats_helper.remove_old_stats(last_week) diff --git a/app/classes/shared/singleton.py b/app/classes/shared/singleton.py new file mode 100644 index 00000000..3776cb92 --- /dev/null +++ b/app/classes/shared/singleton.py @@ -0,0 +1,7 @@ +class Singleton(type): + _instances = {} + + def __call__(cls, *args, **kwargs): + if cls not in cls._instances: + cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) + return cls._instances[cls] diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index 58e1cc2d..4cd3fbc4 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -84,7 +84,7 @@ class TasksManager: commands = HelpersManagement.get_unactioned_commands() for cmd in commands: try: - svr = self.controller.get_server_obj(cmd.server_id) + svr = self.controller.servers.get_server_obj(cmd.server_id) except: logger.error( "Server value requested does not exist! " @@ -516,10 +516,10 @@ class TasksManager: ) # one for now, - self.controller.stats.record_stats() + self.controller.servers.stats.record_stats() # one for later self.scheduler.add_job( - self.controller.stats.record_stats, + self.controller.servers.stats.record_stats, "interval", seconds=stats_update_frequency, id="stats", diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index c6f16ac3..f36a7f3d 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -286,7 +286,7 @@ class AjaxHandler(BaseHandler): logger.warning("Server ID not found in send_command ajax call") Console.warning("Server ID not found in send_command ajax call") - srv_obj = self.controller.get_server_obj(server_id) + srv_obj = self.controller.servers.get_server_obj(server_id) if command == srv_obj.settings["stop_command"]: logger.info( @@ -334,7 +334,7 @@ class AjaxHandler(BaseHandler): logger.error("Server ID is none. Canceling backup!") return - server = self.controller.get_server_obj(server_id) + server = self.controller.servers.get_server_obj(server_id) self.controller.management.add_to_audit_log_raw( self.controller.users.get_user_by_id(exec_user["user_id"])["username"], exec_user["user_id"], @@ -356,7 +356,7 @@ class AjaxHandler(BaseHandler): self.redirect("/panel/error?error=Unauthorized access to Commands") return server_id = self.get_argument("id", None) - svr = self.controller.get_server_obj(server_id) + svr = self.controller.servers.get_server_obj(server_id) try: svr.kill() time.sleep(5) @@ -369,7 +369,7 @@ class AjaxHandler(BaseHandler): return elif page == "eula": server_id = self.get_argument("id", None) - svr = self.controller.get_server_obj(server_id) + svr = self.controller.servers.get_server_obj(server_id) svr.agree_eula(exec_user["user_id"]) elif page == "restore_backup": diff --git a/app/classes/web/api_handler.py b/app/classes/web/api_handler.py index 877ba6ba..df7d9e40 100644 --- a/app/classes/web/api_handler.py +++ b/app/classes/web/api_handler.py @@ -127,7 +127,7 @@ class NodeStats(ApiHandler): return # Get node stats - node_stats = self.controller.stats.get_node_stats() + node_stats = self.controller.servers.stats.get_node_stats() self.return_response(200, {"code": node_stats["node_stats"]}) @@ -161,7 +161,7 @@ class SendCommand(ApiHandler): command = self.get_argument("command", default=None, strip=True) server_id = self.get_argument("id") if command: - server = self.controller.get_server_obj(server_id) + server = self.controller.servers.get_server_obj(server_id) if server.check_running: server.send_command(command) self.return_response(200, {"run": True}) @@ -198,7 +198,7 @@ class ServerBackup(ApiHandler): self.access_denied(user) return - server = self.controller.get_server_obj(server_id) + server = self.controller.servers.get_server_obj(server_id) server.backup_server() @@ -232,7 +232,7 @@ class StartServer(ApiHandler): self.access_denied("unknown") return - server = self.controller.get_server_obj(server_id) + server = self.controller.servers.get_server_obj(server_id) if not server.check_running(): self.controller.management.send_command( @@ -270,7 +270,7 @@ class StopServer(ApiHandler): self.access_denied(user) return - server = self.controller.get_server_obj(server_id) + server = self.controller.servers.get_server_obj(server_id) if server.check_running(): self.controller.management.send_command( diff --git a/app/classes/web/base_handler.py b/app/classes/web/base_handler.py index c7aca58c..093d2a99 100644 --- a/app/classes/web/base_handler.py +++ b/app/classes/web/base_handler.py @@ -180,7 +180,7 @@ class BaseHandler(tornado.web.RequestHandler): exec_user_role = set() if superuser: - authorized_servers = self.controller.list_defined_servers() + authorized_servers = self.controller.servers.list_defined_servers() exec_user_role.add("Super User") exec_user_crafty_permissions = ( self.controller.crafty_perms.list_defined_crafty_permissions() diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 4512cf48..39a68aa0 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -42,7 +42,7 @@ class PanelHandler(BaseHandler): def get_role_servers(self) -> t.Set[int]: servers = set() - for server in self.controller.list_defined_servers(): + for server in self.controller.servers.list_defined_servers(): argument = self.get_argument(f"server_{server['server_id']}_access", "0") if argument == "0": continue @@ -191,7 +191,7 @@ class PanelHandler(BaseHandler): total_players = 0 for server in page_data["servers"]: total_players += len( - self.controller.stats.get_server_players( + self.controller.servers.stats.get_server_players( server["server_data"]["server_id"] ) ) @@ -223,7 +223,7 @@ class PanelHandler(BaseHandler): superuser = superuser and api_key.superuser if superuser: # TODO: Figure out a better solution - defined_servers = self.controller.list_defined_servers() + defined_servers = self.controller.servers.list_defined_servers() exec_user_role = {"Super User"} exec_user_crafty_permissions = ( self.controller.crafty_perms.list_defined_crafty_permissions() @@ -285,10 +285,10 @@ class PanelHandler(BaseHandler): }, "server_stats": { "total": len(defined_servers), - "running": len(self.controller.list_running_servers()), + "running": len(self.controller.servers.list_running_servers()), "stopped": ( - len(self.controller.list_defined_servers()) - - len(self.controller.list_running_servers()) + len(self.controller.servers.list_defined_servers()) + - len(self.controller.servers.list_running_servers()) ), }, "menu_servers": defined_servers, @@ -398,7 +398,7 @@ class PanelHandler(BaseHandler): "servers" ] = self.controller.servers.get_all_servers_stats() except IndexError: - self.controller.stats.record_stats() + self.controller.servers.stats.record_stats() page_data[ "servers" ] = self.controller.servers.get_all_servers_stats() @@ -408,7 +408,7 @@ class PanelHandler(BaseHandler): exec_user["user_id"] ) except IndexError: - self.controller.stats.record_stats() + self.controller.servers.stats.record_stats() user_auth = self.controller.servers.get_authorized_servers_stats( exec_user["user_id"] ) @@ -491,7 +491,7 @@ class PanelHandler(BaseHandler): "schedules", ] - server = self.controller.get_server_obj(server_id) + server = self.controller.servers.get_server_obj(server_id) # server_data isn't needed since the server_stats also pulls server data page_data["server_data"] = self.controller.servers.get_server_data_by_id( server_id @@ -509,9 +509,9 @@ class PanelHandler(BaseHandler): except Exception as e: logger.error(f"Failed to get server waiting to start: {e}") page_data["waiting_start"] = False - page_data["get_players"] = lambda: self.controller.stats.get_server_players( - server_id - ) + page_data[ + "get_players" + ] = lambda: self.controller.servers.stats.get_server_players(server_id) page_data["active_link"] = subpage page_data["permissions"] = { "Commands": EnumPermissionsServer.COMMANDS, @@ -639,10 +639,10 @@ class PanelHandler(BaseHandler): page_data[ "exclusions" ] = self.controller.management.get_excluded_backup_dirs(server_id) - page_data["backing_up"] = self.controller.get_server_obj( + page_data["backing_up"] = self.controller.servers.get_server_obj( server_id ).is_backingup - page_data["backup_stats"] = self.controller.get_server_obj( + page_data["backup_stats"] = self.controller.servers.get_server_obj( server_id ).send_backup_status() # makes it so relative path is the only thing shown @@ -652,7 +652,7 @@ class PanelHandler(BaseHandler): else: exclusions.append(file.replace(server_info["path"] + "/", "")) page_data["exclusions"] = exclusions - self.controller.refresh_server_settings(server_id) + self.controller.servers.refresh_server_settings(server_id) try: page_data["backup_list"] = server.list_backups() except: @@ -792,7 +792,7 @@ class PanelHandler(BaseHandler): page_data["roles_all"] = self.controller.roles.get_all_roles() page_data["servers"] = [] - page_data["servers_all"] = self.controller.list_defined_servers() + page_data["servers_all"] = self.controller.servers.list_defined_servers() page_data["role-servers"] = [] page_data[ "permissions_all" @@ -826,9 +826,9 @@ class PanelHandler(BaseHandler): page_data["schedules"] = HelpersManagement.get_schedules_by_server( server_id ) - page_data["get_players"] = lambda: self.controller.stats.get_server_players( - server_id - ) + page_data[ + "get_players" + ] = lambda: self.controller.servers.stats.get_server_players(server_id) page_data["active_link"] = "schedules" page_data["permissions"] = { "Commands": EnumPermissionsServer.COMMANDS, @@ -890,9 +890,9 @@ class PanelHandler(BaseHandler): if sch_id is None: self.redirect("/panel/error?error=Invalid Schedule ID") schedule = self.controller.management.get_scheduled_task_model(sch_id) - page_data["get_players"] = lambda: self.controller.stats.get_server_players( - server_id - ) + page_data[ + "get_players" + ] = lambda: self.controller.servers.stats.get_server_players(server_id) page_data["active_link"] = "schedules" page_data["permissions"] = { "Commands": EnumPermissionsServer.COMMANDS, @@ -970,7 +970,7 @@ class PanelHandler(BaseHandler): page_data["servers"] = set() page_data["role-servers"] = page_role_servers page_data["roles_all"] = self.controller.roles.get_all_roles() - page_data["servers_all"] = self.controller.list_defined_servers() + page_data["servers_all"] = self.controller.servers.list_defined_servers() page_data["superuser"] = superuser page_data[ "permissions_all" @@ -1098,7 +1098,7 @@ class PanelHandler(BaseHandler): ) return - page_data["servers_all"] = self.controller.list_defined_servers() + page_data["servers_all"] = self.controller.servers.list_defined_servers() page_data[ "permissions_all" ] = self.controller.server_perms.list_defined_permissions() @@ -1110,7 +1110,7 @@ class PanelHandler(BaseHandler): page_data["new_role"] = False role_id = self.get_argument("id", None) page_data["role"] = self.controller.roles.get_role_with_servers(role_id) - page_data["servers_all"] = self.controller.list_defined_servers() + page_data["servers_all"] = self.controller.servers.list_defined_servers() page_data[ "permissions_all" ] = self.controller.server_perms.list_defined_permissions() @@ -1257,7 +1257,7 @@ class PanelHandler(BaseHandler): "Players": EnumPermissionsServer.PLAYERS, } if superuser: - # defined_servers = self.controller.list_defined_servers() + # defined_servers = self.controller.servers.list_defined_servers() exec_user_role = {"Super User"} exec_user_crafty_permissions = ( self.controller.crafty_perms.list_defined_crafty_permissions() @@ -1354,7 +1354,7 @@ class PanelHandler(BaseHandler): self.controller.servers.update_server(server_obj) self.controller.crash_detection(server_obj) - self.controller.refresh_server_settings(server_id) + self.controller.servers.refresh_server_settings(server_id) self.controller.management.add_to_audit_log( exec_user["user_id"], diff --git a/app/classes/web/routes/api/servers/index.py b/app/classes/web/routes/api/servers/index.py index 04172386..47dc61cc 100644 --- a/app/classes/web/routes/api/servers/index.py +++ b/app/classes/web/routes/api/servers/index.py @@ -688,7 +688,7 @@ class ApiServersIndexHandler(BaseApiHandler): # Increase the server creation counter self.controller.crafty_perms.add_server_creation(user["user_id"]) - self.controller.stats.record_stats() + self.controller.servers.stats.record_stats() self.controller.management.add_to_audit_log( user["user_id"], diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index 4ab5260e..d3eab927 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -28,7 +28,7 @@ class ServerHandler(BaseHandler): superuser = superuser and api_key.superuser if superuser: - defined_servers = self.controller.list_defined_servers() + defined_servers = self.controller.servers.list_defined_servers() exec_user_role = {"Super User"} exec_user_crafty_permissions = ( self.controller.crafty_perms.list_defined_crafty_permissions() @@ -66,11 +66,11 @@ class ServerHandler(BaseHandler): "Roles_Config": EnumPermissionsCrafty.ROLES_CONFIG, }, "server_stats": { - "total": len(self.controller.list_defined_servers()), - "running": len(self.controller.list_running_servers()), + "total": len(self.controller.servers.list_defined_servers()), + "running": len(self.controller.servers.list_running_servers()), "stopped": ( - len(self.controller.list_defined_servers()) - - len(self.controller.list_running_servers()) + len(self.controller.servers.list_defined_servers()) + - len(self.controller.servers.list_running_servers()) ), }, "hosts_data": self.controller.management.get_latest_hosts_stats(), @@ -370,7 +370,7 @@ class ServerHandler(BaseHandler): new_server_id, role_id, "11111111" ) - self.controller.stats.record_stats() + self.controller.servers.stats.record_stats() self.redirect("/panel/dashboard") if page == "bedrock_step1": @@ -487,7 +487,7 @@ class ServerHandler(BaseHandler): new_server_id, role_id, "11111111" ) - self.controller.stats.record_stats() + self.controller.servers.stats.record_stats() self.redirect("/panel/dashboard") try: diff --git a/main.py b/main.py index 10179729..5c5d9d11 100644 --- a/main.py +++ b/main.py @@ -151,8 +151,8 @@ if __name__ == "__main__": # init servers logger.info("Initializing all servers defined") Console.info("Initializing all servers defined") - controller.init_all_servers() - servers = controller.list_defined_servers() + controller.servers.init_all_servers() + servers = controller.servers.list_defined_servers() def tasks_starter(): # start stats logging From 056856cf9878ee8d5f5f3b53f4cb8b12be4c5db7 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Thu, 26 May 2022 15:02:47 +0200 Subject: [PATCH 03/15] Fixing Helpers issue --- app/classes/controllers/servers_controller.py | 2 +- app/classes/shared/server.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index 8c9f142b..f3a85704 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -34,7 +34,7 @@ class ServersController(metaclass=Singleton): servers_list: Server def __init__(self, helper, servers_helper, management_helper): - self.helper: Helper = helper + self.helper: Helpers = helper self.servers_helper: HelperServers = servers_helper self.management_helper = management_helper self.servers_list = [] diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index c7375b44..b1229f72 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -94,6 +94,9 @@ class ServerOutBuf: # ********************************************************************************** class Server: server_object: Servers + helper: Helpers + management_helper: HelpersManagement + stats: Stats stats_helper: HelperServerStats def __init__(self, server_id, helper, management_helper, stats): From 2b484d5a075df489bd97f917a0c2e8efa7430a85 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Thu, 26 May 2022 15:20:21 +0200 Subject: [PATCH 04/15] fixing download jar --- app/classes/controllers/servers_controller.py | 4 +-- app/classes/shared/main_controller.py | 36 +++++++++++-------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index f3a85704..d078ecba 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -103,12 +103,12 @@ class ServersController(metaclass=Singleton): @staticmethod def set_download(server_id): srv: Server = ServersController().get_server_instance_by_id(server_id) - return srv.stats_helper.set_download(server_id) + return srv.stats_helper.set_download() @staticmethod def finish_download(server_id): srv: Server = ServersController().get_server_instance_by_id(server_id) - return srv.stats_helper.finish_download(server_id) + return srv.stats_helper.finish_download() @staticmethod def get_download_status(server_id): diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index cef47ccb..7bdeffcc 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -34,25 +34,33 @@ logger = logging.getLogger(__name__) class Controller: def __init__(self, database, helper): - self.helper = helper - self.server_jars = ServerJars(helper) - self.users_helper = HelperUsers(database, self.helper) - self.roles_helper = HelperRoles(database) - self.servers_helper = HelperServers(database) - self.management_helper = HelpersManagement(database, self.helper) - self.authentication = Authentication(self.helper) - self.crafty_perms = CraftyPermsController() - self.management = ManagementController(self.management_helper) - self.roles = RolesController(self.users_helper, self.roles_helper) - self.server_perms = ServerPermsController() - self.servers = ServersController( + self.helper: Helpers = helper + self.server_jars: ServerJars = ServerJars(helper) + self.users_helper: HelperUsers = HelperUsers(database, self.helper) + self.roles_helper: HelperRoles = HelperRoles(database) + self.servers_helper: HelperServers = HelperServers(database) + self.management_helper: HelpersManagement = HelpersManagement( + database, self.helper + ) + self.authentication: Authentication = Authentication(self.helper) + self.crafty_perms: CraftyPermsController = CraftyPermsController() + self.management: ManagementController = ManagementController( + self.management_helper + ) + self.roles: RolesController = RolesController( + self.users_helper, self.roles_helper + ) + self.server_perms: ServerPermsController = ServerPermsController() + self.servers: ServersController = ServersController( self.helper, self.servers_helper, self.management_helper ) - self.users = UsersController( + self.users: UsersController = UsersController( self.helper, self.users_helper, self.authentication ) tz = get_localzone() - self.support_scheduler = BackgroundScheduler(timezone=str(tz)) + self.support_scheduler: BackgroundScheduler = BackgroundScheduler( + timezone=str(tz) + ) self.support_scheduler.start() @staticmethod From be1930c7c7b272247d6336b1837be0b3c3141c17 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Thu, 26 May 2022 15:28:54 +0200 Subject: [PATCH 05/15] fixing server shutdown gracefull exit --- app/classes/shared/command.py | 14 +++++++++----- app/classes/shared/tasks.py | 12 +++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/classes/shared/command.py b/app/classes/shared/command.py index 2fb222cb..9fda2646 100644 --- a/app/classes/shared/command.py +++ b/app/classes/shared/command.py @@ -5,19 +5,23 @@ import threading import logging import getpass from app.classes.shared.console import Console - from app.classes.shared.import3 import Import3 +from app.classes.shared.helpers import Helpers +from app.classes.shared.tasks import TasksManager +from app.classes.shared.migration import MigrationManager +from app.classes.shared.main_controller import Controller + logger = logging.getLogger(__name__) class MainPrompt(cmd.Cmd): def __init__(self, helper, tasks_manager, migration_manager, main_controller): super().__init__() - self.helper = helper - self.tasks_manager = tasks_manager - self.migration_manager = migration_manager - self.controller = main_controller + self.helper: Helpers = helper + self.tasks_manager: TasksManager = tasks_manager + self.migration_manager: MigrationManager = migration_manager + self.controller: Controller = main_controller # overrides the default Prompt self.prompt = f"Crafty Controller v{self.helper.get_version_string()} > " diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index 4cd3fbc4..48492d2c 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -8,10 +8,12 @@ from tzlocal import get_localzone from apscheduler.events import EVENT_JOB_EXECUTED from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.triggers.cron import CronTrigger +from app.classes.controllers.users_controller import UsersController from app.classes.models.management import HelpersManagement from app.classes.models.users import HelperUsers from app.classes.shared.console import Console +from app.classes.shared.helpers import Helpers from app.classes.shared.main_controller import Controller from app.classes.web.tornado_handler import Webserver @@ -36,14 +38,14 @@ class TasksManager: controller: Controller def __init__(self, helper, controller): - self.helper = helper - self.controller = controller - self.tornado = Webserver(helper, controller, self) + self.helper: Helpers = helper + self.controller: Controller = controller + self.tornado: Webserver = Webserver(helper, controller, self) self.tz = get_localzone() self.scheduler = BackgroundScheduler(timezone=str(self.tz)) - self.users_controller = self.controller.users + self.users_controller: UsersController = self.controller.users self.webserver_thread = threading.Thread( target=self.tornado.run_tornado, daemon=True, name="tornado_thread" @@ -130,7 +132,7 @@ class TasksManager: def _main_graceful_exit(self): try: os.remove(self.helper.session_file) - self.controller.stop_all_servers() + self.controller.servers.stop_all_servers() except: logger.info("Caught error during shutdown", exc_info=True) From 6fae3951857be394df1d7ceb89eecca9dede326a Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Thu, 26 May 2022 15:43:24 +0200 Subject: [PATCH 06/15] Fixing issues for latest stats --- app/classes/controllers/server_perms_controller.py | 2 +- app/classes/controllers/servers_controller.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/classes/controllers/server_perms_controller.py b/app/classes/controllers/server_perms_controller.py index 05fa1572..ca84bbf9 100644 --- a/app/classes/controllers/server_perms_controller.py +++ b/app/classes/controllers/server_perms_controller.py @@ -117,7 +117,7 @@ class ServerPermsController: server_data.append( { "server_data": server, - "stats": DatabaseShortcuts.return_rows(latest)[0], + "stats": latest, } ) return server_data diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index a541e3e2..5cee5c8f 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -303,7 +303,7 @@ class ServersController(metaclass=Singleton): server_data.append( { "server_data": server, - "stats": DatabaseShortcuts.return_rows(latest)[0], + "stats": latest, "user_command_permission": user_command_permission, } ) From 3b4253de9be2f512741ae6350622afa5cf90e749 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Thu, 26 May 2022 23:19:59 +0200 Subject: [PATCH 07/15] Fixing get_authorized_server --- app/classes/controllers/servers_controller.py | 17 +++++++++++------ app/classes/models/server_stats.py | 9 +++------ app/classes/shared/main_models.py | 4 ++++ app/classes/web/panel_handler.py | 13 +++++++++---- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index 5cee5c8f..7495b432 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -10,6 +10,7 @@ from typing_extensions import Self from app.classes.controllers.roles_controller import RolesController +from app.classes.shared.singleton import Singleton from app.classes.shared.server import Server from app.classes.shared.console import Console from app.classes.shared.helpers import Helpers @@ -25,7 +26,6 @@ from app.classes.models.server_permissions import ( PermissionsServers, EnumPermissionsServer, ) -from app.classes.shared.singleton import Singleton logger = logging.getLogger(__name__) @@ -244,7 +244,12 @@ class ServersController(metaclass=Singleton): user.role_id ) for role in role_servers: - server_data.append(HelperServers.get_server_data_by_id(role.server_id)) + # server_data.append(HelperServers.get_server_data_by_id(role.server_id)) + server_data.append( + ServersController().get_server_instance_by_id( + role.server_id.server_id + ) + ) return server_data @@ -294,7 +299,7 @@ class ServersController(metaclass=Singleton): srv: Server = server latest = srv.stats_helper.get_latest_server_stats() key_permissions = PermissionsServers.get_api_key_permissions_list( - api_key, server.get("server_id") + api_key, server.server_id ) if EnumPermissionsServer.COMMANDS in key_permissions: user_command_permission = True @@ -302,7 +307,7 @@ class ServersController(metaclass=Singleton): user_command_permission = False server_data.append( { - "server_data": server, + "server_data": DatabaseShortcuts.get_data_obj(server.server_object), "stats": latest, "user_command_permission": user_command_permission, } @@ -319,7 +324,7 @@ class ServersController(metaclass=Singleton): latest = srv.stats_helper.get_latest_server_stats() # TODO user_permissions = PermissionsServers.get_user_id_permissions_list( - user_id, server.get("server_id") + user_id, server.server_id ) if EnumPermissionsServer.COMMANDS in user_permissions: user_command_permission = True @@ -327,7 +332,7 @@ class ServersController(metaclass=Singleton): user_command_permission = False server_data.append( { - "server_data": server, + "server_data": DatabaseShortcuts.get_data_obj(server.server_object), "stats": latest, "user_command_permission": user_command_permission, } diff --git a/app/classes/models/server_stats.py b/app/classes/models/server_stats.py index b9b425b9..6dcd6a2b 100644 --- a/app/classes/models/server_stats.py +++ b/app/classes/models/server_stats.py @@ -3,16 +3,13 @@ import logging import datetime import typing as t -from playhouse.shortcuts import model_to_dict - import typing as t from peewee import DoesNotExist -from playhouse.shortcuts import model_to_dict - from app.classes.models.servers import Servers, HelperServers from app.classes.shared.helpers import Helpers +from app.classes.shared.main_models import DatabaseShortcuts from app.classes.shared.migration import MigrationManager try: @@ -193,7 +190,7 @@ class HelperServerStats: .get(self.database) ) try: - return model_to_dict(latest) + return DatabaseShortcuts.get_data_obj(latest) except IndexError: return {} @@ -205,7 +202,7 @@ class HelperServerStats: .limit(1) .first(self.database) ) - return model_to_dict(stats) + return DatabaseShortcuts.get_data_obj(stats) def server_id_exists(self): # self.select_database(self.server_id) diff --git a/app/classes/shared/main_models.py b/app/classes/shared/main_models.py index 19699395..ae4636c2 100644 --- a/app/classes/shared/main_models.py +++ b/app/classes/shared/main_models.py @@ -58,3 +58,7 @@ class DatabaseShortcuts: def return_db_rows(model): data = [model_to_dict(row) for row in model] return data + + @staticmethod + def get_data_obj(obj): + return model_to_dict(obj) diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index aad20fbc..da4e9783 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -22,6 +22,7 @@ from app.classes.models.server_permissions import EnumPermissionsServer from app.classes.models.crafty_permissions import EnumPermissionsCrafty from app.classes.models.management import HelpersManagement from app.classes.shared.helpers import Helpers +from app.classes.shared.main_models import DatabaseShortcuts from app.classes.web.base_handler import BaseHandler logger = logging.getLogger(__name__) @@ -254,15 +255,19 @@ class PanelHandler(BaseHandler): for server_id in user_order[:]: for server in defined_servers[:]: - if str(server["server_id"]) == str(server_id): - page_servers.append(server) + if str(server.server_id) == str(server_id): + page_servers.append( + DatabaseShortcuts.get_data_obj(server.server_object) + ) user_order.remove(server_id) defined_servers.remove(server) for server in defined_servers: - server_ids.append(str(server["server_id"])) + server_ids.append(str(server.server_id)) if server not in page_servers: - page_servers.append(server) + page_servers.append( + DatabaseShortcuts.get_data_obj(server.server_object) + ) for server_id in user_order[:]: # remove IDs in list that user no longer has access to if str(server_id) not in server_ids: From 37bda32164a0a14f1151f588a9001332e62eaba1 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Mon, 30 May 2022 07:36:25 +0200 Subject: [PATCH 08/15] Fixing moved functions calls --- app/classes/controllers/servers_controller.py | 2 +- app/classes/shared/main_controller.py | 2 +- app/classes/web/ajax_handler.py | 8 ++++---- app/classes/web/panel_handler.py | 6 ++++-- app/classes/web/routes/api/roles/index.py | 2 +- app/classes/web/routes/api/servers/server/action.py | 2 +- app/classes/web/server_handler.py | 2 +- 7 files changed, 13 insertions(+), 11 deletions(-) diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index 7495b432..a111cd9b 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -198,7 +198,7 @@ class ServersController(metaclass=Singleton): self.servers_list.append(temp_server_dict) if server["auto_start"]: - self.servers.set_waiting_start(server["server_id"], True) + self.set_waiting_start(server["server_id"], True) self.refresh_server_settings(server["server_id"]) diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index e20ea8e7..363c0e55 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -821,7 +821,7 @@ class Controller: # if this is the droid... im mean server we are looking for... if str(server["server_id"]) == str(server_id): - server_data = self.get_server_data(server_id) + server_data = self.servers.get_server_data(server_id) server_name = server_data["server_name"] logger.info(f"Deleting Server: ID {server_id} | Name: {server_name} ") diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index f36a7f3d..da172971 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -394,7 +394,7 @@ class AjaxHandler(BaseHandler): server_data["server_port"], ) new_server_id = new_server - new_server = self.controller.get_server_data(new_server) + new_server = self.controller.servers.get_server_data(new_server) self.controller.rename_backup_dir( server_id, new_server_id, new_server["server_uuid"] ) @@ -412,7 +412,7 @@ class AjaxHandler(BaseHandler): server_data["server_port"], ) new_server_id = new_server - new_server = self.controller.get_server_data(new_server) + new_server = self.controller.servers.get_server_data(new_server) self.controller.rename_backup_dir( server_id, new_server_id, new_server["server_uuid"] ) @@ -522,7 +522,7 @@ class AjaxHandler(BaseHandler): f"{self.controller.servers.get_server_friendly_name(server_id)}" ) - server_data = self.controller.get_server_data(server_id) + server_data = self.controller.servers.get_server_data(server_id) server_name = server_data["server_name"] self.controller.management.add_to_audit_log( @@ -546,7 +546,7 @@ class AjaxHandler(BaseHandler): f"{self.controller.servers.get_server_friendly_name(server_id)}" ) - server_data = self.controller.get_server_data(server_id) + server_data = self.controller.servers.get_server_data(server_id) server_name = server_data["server_name"] self.controller.management.add_to_audit_log( diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index da4e9783..e26f99df 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -18,11 +18,13 @@ from tzlocal import get_localzone from croniter import croniter from app.classes.controllers.roles_controller import RolesController +from app.classes.models.servers import Servers from app.classes.models.server_permissions import EnumPermissionsServer from app.classes.models.crafty_permissions import EnumPermissionsCrafty from app.classes.models.management import HelpersManagement from app.classes.shared.helpers import Helpers from app.classes.shared.main_models import DatabaseShortcuts +from app.classes.shared.server import Server from app.classes.web.base_handler import BaseHandler logger = logging.getLogger(__name__) @@ -1318,7 +1320,7 @@ class PanelHandler(BaseHandler): if server_id is None: return - server_obj = self.controller.servers.get_server_obj(server_id) + server_obj: Servers = self.controller.servers.get_server_obj(server_id) stale_executable = server_obj.executable # Compares old jar name to page data being passed. # If they are different we replace the executable name in the @@ -1356,7 +1358,7 @@ class PanelHandler(BaseHandler): server_obj.crash_detection = crash_detection server_obj.logs_delete_after = logs_delete_after self.controller.servers.update_server(server_obj) - self.controller.crash_detection(server_obj) + self.controller.servers.crash_detection(server_obj) self.controller.servers.refresh_server_settings(server_id) diff --git a/app/classes/web/routes/api/roles/index.py b/app/classes/web/routes/api/roles/index.py index 11142ebc..2ca1baf3 100644 --- a/app/classes/web/routes/api/roles/index.py +++ b/app/classes/web/routes/api/roles/index.py @@ -104,7 +104,7 @@ class ApiRolesIndexHandler(BaseApiHandler): server_ids = ( ( {server["server_id"] for server in data["servers"]} - & set(self.controller.get_all_server_ids()) + & set(self.controller.servers.get_all_server_ids()) ) # Only allow existing servers if "servers" in data else set() diff --git a/app/classes/web/routes/api/servers/server/action.py b/app/classes/web/routes/api/servers/server/action.py index b8728b1e..14fb1789 100644 --- a/app/classes/web/routes/api/servers/server/action.py +++ b/app/classes/web/routes/api/servers/server/action.py @@ -90,7 +90,7 @@ class ApiServersServerActionHandler(BaseApiHandler): server_data.get("server_port"), ) - self.controller.init_all_servers() + self.controller.servers.init_all_servers() self.finish_json( 200, diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index d3eab927..e6bab245 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -240,7 +240,7 @@ class ServerHandler(BaseHandler): server_port, ) - self.controller.init_all_servers() + self.controller.servers.init_all_servers() return From 8f5c688b72d5b6e6ef4b5f6cf1f87808ef502f4c Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Mon, 30 May 2022 19:28:39 +0200 Subject: [PATCH 09/15] Fixing Pylint issues --- app/classes/controllers/servers_controller.py | 30 ++++++------------- app/classes/models/server_stats.py | 7 +---- app/classes/shared/main_controller.py | 3 +- app/classes/shared/tasks.py | 4 ++- app/classes/web/ajax_handler.py | 8 ++--- app/classes/web/api_handler.py | 8 ++--- app/classes/web/panel_handler.py | 10 +++++-- 7 files changed, 29 insertions(+), 41 deletions(-) diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index a111cd9b..be151e7a 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -1,12 +1,8 @@ -from gettext import install -from inspect import _void import os import logging -from pydoc import Helper import time import json import typing as t -from typing_extensions import Self from app.classes.controllers.roles_controller import RolesController @@ -20,7 +16,6 @@ from app.classes.minecraft.server_props import ServerProps from app.classes.minecraft.stats import Stats from app.classes.models.servers import HelperServers -from app.classes.models.server_stats import HelperServerStats from app.classes.models.users import HelperUsers, ApiKeys from app.classes.models.server_permissions import ( PermissionsServers, @@ -133,11 +128,13 @@ class ServersController(metaclass=Singleton): # Servers Methods # ********************************************************************************** - def get_server_instance_by_id(self, server_id): + def get_server_instance_by_id(self, server_id: t.Union[str, int]) -> Server: for server in self.servers_list: - if server["server_id"] == int(server_id): + if int(server["server_id"]) == int(server_id): return server["server_obj"] - return None + + logger.warning(f"Unable to find server object for server id {server_id}") + raise Exception(f"Unable to find server object for server id {server_id}") def init_all_servers(self): @@ -228,7 +225,7 @@ class ServersController(metaclass=Singleton): return False def refresh_server_settings(self, server_id: int): - server_obj = self.get_server_obj(server_id) + server_obj: Server = self.get_server_data_by_id(server_id) server_obj.reload_server_settings() @staticmethod @@ -362,14 +359,6 @@ class ServersController(metaclass=Singleton): else: svr.stop_crash_detection() - def get_server_obj(self, server_id: t.Union[str, int]) -> Server: - for server in self.servers_list: - if str(server["server_id"]) == str(server_id): - return server["server_obj"] - - logger.warning(f"Unable to find server object for server id {server_id}") - raise Exception(f"Unable to find server object for server id {server_id}") - def get_server_obj_optional( self, server_id: t.Union[str, int] ) -> t.Optional[Server]: @@ -401,10 +390,9 @@ class ServersController(metaclass=Singleton): running_servers = [] # for each server - for servers in self.servers_list: - + for server in self.servers_list: # is the server running? - srv_obj = servers["server_obj"] + srv_obj: Server = server["server_obj"] running = srv_obj.check_running() # if so, let's add a dictionary to the list of running servers if running: @@ -434,7 +422,7 @@ class ServersController(metaclass=Singleton): def stop_server(self, server_id): # issue the stop command - svr_obj = self.get_server_obj(server_id) + svr_obj = self.get_server_instance_by_id(server_id) svr_obj.stop_threaded_server() # ********************************************************************************** diff --git a/app/classes/models/server_stats.py b/app/classes/models/server_stats.py index 6dcd6a2b..3ac74717 100644 --- a/app/classes/models/server_stats.py +++ b/app/classes/models/server_stats.py @@ -1,11 +1,6 @@ import os import logging import datetime -import typing as t - -import typing as t - -from peewee import DoesNotExist from app.classes.models.servers import Servers, HelperServers from app.classes.shared.helpers import Helpers @@ -138,7 +133,7 @@ class HelperServerStats: server_data.append( { "server_data": server, - "stats": stats, + "stats": DatabaseShortcuts.get_data_obj(latest), "user_command_permission": True, } ) diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 363c0e55..4cdbe377 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -5,7 +5,6 @@ import shutil import time import logging import tempfile -import typing as t from peewee import DoesNotExist # TZLocal is set as a hidden import on win pipeline @@ -832,7 +831,7 @@ class Controller: running = srv_obj.check_running() if running: - self.stop_server(server_id) + self.servers.stop_server(server_id) if files: try: FileHelpers.del_dirs( diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index 48492d2c..49bf7e18 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -86,7 +86,9 @@ class TasksManager: commands = HelpersManagement.get_unactioned_commands() for cmd in commands: try: - svr = self.controller.servers.get_server_obj(cmd.server_id) + svr = self.controller.servers.get_server_instance_by_id( + cmd.server_id + ) except: logger.error( "Server value requested does not exist! " diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index da172971..492b3b92 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -286,7 +286,7 @@ class AjaxHandler(BaseHandler): logger.warning("Server ID not found in send_command ajax call") Console.warning("Server ID not found in send_command ajax call") - srv_obj = self.controller.servers.get_server_obj(server_id) + srv_obj = self.controller.servers.get_server_instance_by_id(server_id) if command == srv_obj.settings["stop_command"]: logger.info( @@ -334,7 +334,7 @@ class AjaxHandler(BaseHandler): logger.error("Server ID is none. Canceling backup!") return - server = self.controller.servers.get_server_obj(server_id) + server = self.controller.servers.get_server_instance_by_id(server_id) self.controller.management.add_to_audit_log_raw( self.controller.users.get_user_by_id(exec_user["user_id"])["username"], exec_user["user_id"], @@ -356,7 +356,7 @@ class AjaxHandler(BaseHandler): self.redirect("/panel/error?error=Unauthorized access to Commands") return server_id = self.get_argument("id", None) - svr = self.controller.servers.get_server_obj(server_id) + svr = self.controller.servers.get_server_instance_by_id(server_id) try: svr.kill() time.sleep(5) @@ -369,7 +369,7 @@ class AjaxHandler(BaseHandler): return elif page == "eula": server_id = self.get_argument("id", None) - svr = self.controller.servers.get_server_obj(server_id) + svr = self.controller.servers.get_server_instance_by_id(server_id) svr.agree_eula(exec_user["user_id"]) elif page == "restore_backup": diff --git a/app/classes/web/api_handler.py b/app/classes/web/api_handler.py index df7d9e40..5654d8eb 100644 --- a/app/classes/web/api_handler.py +++ b/app/classes/web/api_handler.py @@ -161,7 +161,7 @@ class SendCommand(ApiHandler): command = self.get_argument("command", default=None, strip=True) server_id = self.get_argument("id") if command: - server = self.controller.servers.get_server_obj(server_id) + server = self.controller.servers.get_server_instance_by_id(server_id) if server.check_running: server.send_command(command) self.return_response(200, {"run": True}) @@ -198,7 +198,7 @@ class ServerBackup(ApiHandler): self.access_denied(user) return - server = self.controller.servers.get_server_obj(server_id) + server = self.controller.servers.get_server_instance_by_id(server_id) server.backup_server() @@ -232,7 +232,7 @@ class StartServer(ApiHandler): self.access_denied("unknown") return - server = self.controller.servers.get_server_obj(server_id) + server = self.controller.servers.get_server_instance_by_id(server_id) if not server.check_running(): self.controller.management.send_command( @@ -270,7 +270,7 @@ class StopServer(ApiHandler): self.access_denied(user) return - server = self.controller.servers.get_server_obj(server_id) + server = self.controller.servers.get_server_instance_by_id(server_id) if server.check_running(): self.controller.management.send_command( diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index e26f99df..0f339e8b 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -497,7 +497,7 @@ class PanelHandler(BaseHandler): "schedules", ] - server = self.controller.servers.get_server_obj(server_id) + server = self.controller.servers.get_server_instance_by_id(server_id) # server_data isn't needed since the server_stats also pulls server data page_data["server_data"] = self.controller.servers.get_server_data_by_id( server_id @@ -645,10 +645,14 @@ class PanelHandler(BaseHandler): page_data[ "exclusions" ] = self.controller.management.get_excluded_backup_dirs(server_id) - page_data["backing_up"] = self.controller.servers.get_server_obj( + page_data[ + "backing_up" + ] = self.controller.servers.get_server_instance_by_id( server_id ).is_backingup - page_data["backup_stats"] = self.controller.servers.get_server_obj( + page_data[ + "backup_stats" + ] = self.controller.servers.get_server_instance_by_id( server_id ).send_backup_status() # makes it so relative path is the only thing shown From b0ea943992d56317531d74fe746639e0fc880268 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Mon, 30 May 2022 19:32:11 +0200 Subject: [PATCH 10/15] Fixing lasts pylint warning --- app/classes/controllers/server_perms_controller.py | 3 --- app/classes/controllers/servers_controller.py | 1 - app/classes/web/panel_handler.py | 1 - 3 files changed, 5 deletions(-) diff --git a/app/classes/controllers/server_perms_controller.py b/app/classes/controllers/server_perms_controller.py index ca84bbf9..00a124f8 100644 --- a/app/classes/controllers/server_perms_controller.py +++ b/app/classes/controllers/server_perms_controller.py @@ -8,9 +8,6 @@ from app.classes.models.server_permissions import ( from app.classes.models.users import HelperUsers, ApiKeys from app.classes.models.roles import HelperRoles from app.classes.models.servers import HelperServers -from app.classes.models.server_stats import HelperServerStats - -from app.classes.shared.main_models import DatabaseShortcuts from app.classes.shared.server import Server logger = logging.getLogger(__name__) diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index be151e7a..8978a2c5 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -241,7 +241,6 @@ class ServersController(metaclass=Singleton): user.role_id ) for role in role_servers: - # server_data.append(HelperServers.get_server_data_by_id(role.server_id)) server_data.append( ServersController().get_server_instance_by_id( role.server_id.server_id diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 0f339e8b..3d0217d2 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -24,7 +24,6 @@ from app.classes.models.crafty_permissions import EnumPermissionsCrafty from app.classes.models.management import HelpersManagement from app.classes.shared.helpers import Helpers from app.classes.shared.main_models import DatabaseShortcuts -from app.classes.shared.server import Server from app.classes.web.base_handler import BaseHandler logger = logging.getLogger(__name__) From 18bf7ebd019340cf9073f908c660645379806c8e Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Mon, 30 May 2022 19:56:21 +0200 Subject: [PATCH 11/15] Updating Argon2-cffi and psutil --- requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index d57479e9..21f035b5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ apscheduler==3.8.1 -argon2-cffi==20.1 +argon2-cffi==21.3 bleach==4.1 cached_property==1.5.2 colorama==0.4 @@ -9,7 +9,7 @@ cryptography==3.4.8 libgravatar==1.0.0 peewee==3.13 pexpect==4.8 -psutil==5.7 +psutil==5.9 pyOpenSSL==19.1.0 pyjwt==2.3 PyYAML==5.4 From f597a26e5bd1882edbd4831be2de74ce9de00d25 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Mon, 30 May 2022 22:05:36 +0200 Subject: [PATCH 12/15] Trying to fix stuff --- .../controllers/server_perms_controller.py | 4 +--- app/classes/controllers/servers_controller.py | 15 +++++++++------ app/classes/models/server_stats.py | 10 ++-------- app/classes/web/base_handler.py | 2 +- app/classes/web/panel_handler.py | 11 ++++++----- app/classes/web/server_handler.py | 9 +++++++++ main.py | 1 - 7 files changed, 28 insertions(+), 24 deletions(-) diff --git a/app/classes/controllers/server_perms_controller.py b/app/classes/controllers/server_perms_controller.py index 00a124f8..2148b16e 100644 --- a/app/classes/controllers/server_perms_controller.py +++ b/app/classes/controllers/server_perms_controller.py @@ -107,9 +107,7 @@ class ServerPermsController: ) for server in authorized_servers: - srv: Server = ServersController().get_server_instance_by_id( - server.get("server_id") - ) + srv = ServersController().get_server_instance_by_id(server.get("server_id")) latest = srv.stats_helper.get_latest_server_stats() server_data.append( { diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index 8978a2c5..c3aad5cb 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -225,7 +225,7 @@ class ServersController(metaclass=Singleton): return False def refresh_server_settings(self, server_id: int): - server_obj: Server = self.get_server_data_by_id(server_id) + server_obj = self.get_server_instance_by_id(server_id) server_obj.reload_server_settings() @staticmethod @@ -287,7 +287,7 @@ class ServersController(metaclass=Singleton): @staticmethod def get_authorized_servers_stats_api_key(api_key: ApiKeys): server_data = [] - authorized_servers = ServersController.get_authorized_servers( + authorized_servers = ServersController().get_authorized_servers( api_key.user_id # TODO: API key authorized servers? ) @@ -376,10 +376,13 @@ class ServersController(metaclass=Singleton): logger.warning(f"Unable to find server object for server id {server_id}") return False - @staticmethod - def list_defined_servers(): - servers = HelperServers.get_all_defined_servers() - return servers + def list_defined_servers(self): + defined_servers = [] + for server in self.servers_list: + defined_servers.append( + self.get_server_instance_by_id(server.get("server_id")) + ) + return defined_servers @staticmethod def get_all_server_ids() -> t.List[int]: diff --git a/app/classes/models/server_stats.py b/app/classes/models/server_stats.py index 3ac74717..98835bd8 100644 --- a/app/classes/models/server_stats.py +++ b/app/classes/models/server_stats.py @@ -123,17 +123,11 @@ class HelperServerStats: server_data = [] try: for server in servers: - latest = ( - ServerStats.select() - .where(ServerStats.server_id == server.get("server_id")) - .order_by(ServerStats.created.desc()) - .limit(1) - ) - latest._database = self.database + latest = self.get_latest_server_stats() server_data.append( { "server_data": server, - "stats": DatabaseShortcuts.get_data_obj(latest), + "stats": latest, "user_command_permission": True, } ) diff --git a/app/classes/web/base_handler.py b/app/classes/web/base_handler.py index eb5a77a5..c06e8afc 100644 --- a/app/classes/web/base_handler.py +++ b/app/classes/web/base_handler.py @@ -180,7 +180,7 @@ class BaseHandler(tornado.web.RequestHandler): exec_user_role = set() if superuser: - authorized_servers = self.controller.servers.list_defined_servers() + authorized_servers = self.controller.servers.get_all_defined_servers() exec_user_role.add("Super User") exec_user_crafty_permissions = ( self.controller.crafty_perms.list_defined_crafty_permissions() diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 3d0217d2..eebab5c4 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -41,7 +41,7 @@ class PanelHandler(BaseHandler): def get_role_servers(self) -> t.List[RolesController.RoleServerJsonType]: servers = [] - for server in self.controller.servers.list_defined_servers(): + for server in self.controller.servers.get_all_defined_servers(): argument = self.get_argument(f"server_{server['server_id']}_access", "0") if argument == "0": continue @@ -269,6 +269,7 @@ class PanelHandler(BaseHandler): page_servers.append( DatabaseShortcuts.get_data_obj(server.server_object) ) + for server_id in user_order[:]: # remove IDs in list that user no longer has access to if str(server_id) not in server_ids: @@ -801,7 +802,7 @@ class PanelHandler(BaseHandler): page_data["roles_all"] = self.controller.roles.get_all_roles() page_data["servers"] = [] - page_data["servers_all"] = self.controller.servers.list_defined_servers() + page_data["servers_all"] = self.controller.servers.get_all_defined_servers() page_data["role-servers"] = [] page_data[ "permissions_all" @@ -979,7 +980,7 @@ class PanelHandler(BaseHandler): page_data["servers"] = set() page_data["role-servers"] = page_role_servers page_data["roles_all"] = self.controller.roles.get_all_roles() - page_data["servers_all"] = self.controller.servers.list_defined_servers() + page_data["servers_all"] = self.controller.servers.get_all_defined_servers() page_data["superuser"] = superuser page_data[ "permissions_all" @@ -1107,7 +1108,7 @@ class PanelHandler(BaseHandler): ) return - page_data["servers_all"] = self.controller.servers.list_defined_servers() + page_data["servers_all"] = self.controller.servers.get_all_defined_servers() page_data[ "permissions_all" ] = self.controller.server_perms.list_defined_permissions() @@ -1119,7 +1120,7 @@ class PanelHandler(BaseHandler): page_data["new_role"] = False role_id = self.get_argument("id", None) page_data["role"] = self.controller.roles.get_role_with_servers(role_id) - page_data["servers_all"] = self.controller.servers.list_defined_servers() + page_data["servers_all"] = self.controller.servers.get_all_defined_servers() page_data[ "permissions_all" ] = self.controller.server_perms.list_defined_permissions() diff --git a/app/classes/web/server_handler.py b/app/classes/web/server_handler.py index e6bab245..7e599a66 100644 --- a/app/classes/web/server_handler.py +++ b/app/classes/web/server_handler.py @@ -10,6 +10,7 @@ import requests from app.classes.models.crafty_permissions import EnumPermissionsCrafty from app.classes.shared.helpers import Helpers from app.classes.shared.file_helpers import FileHelpers +from app.classes.shared.main_models import DatabaseShortcuts from app.classes.web.base_handler import BaseHandler logger = logging.getLogger(__name__) @@ -52,6 +53,14 @@ class ServerHandler(BaseHandler): exec_user_role.add(role["role_name"]) list_roles.append(self.controller.roles.get_role(role["role_id"])) + page_servers = [] + for server in defined_servers: + if server not in page_servers: + page_servers.append( + DatabaseShortcuts.get_data_obj(server.server_object) + ) + defined_servers = page_servers + template = "public/404.html" page_data = { diff --git a/main.py b/main.py index 5c5d9d11..89d740e3 100644 --- a/main.py +++ b/main.py @@ -152,7 +152,6 @@ if __name__ == "__main__": logger.info("Initializing all servers defined") Console.info("Initializing all servers defined") controller.servers.init_all_servers() - servers = controller.servers.list_defined_servers() def tasks_starter(): # start stats logging From 150e8e57940bec07b1e9ed7c0a5af4fe7bd755b2 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Mon, 30 May 2022 22:40:34 +0200 Subject: [PATCH 13/15] Renaming Server in shared to ServerInstance --- .../controllers/server_perms_controller.py | 1 - app/classes/controllers/servers_controller.py | 44 +++++++++---------- app/classes/shared/server.py | 2 +- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/app/classes/controllers/server_perms_controller.py b/app/classes/controllers/server_perms_controller.py index 2148b16e..f6632cd7 100644 --- a/app/classes/controllers/server_perms_controller.py +++ b/app/classes/controllers/server_perms_controller.py @@ -8,7 +8,6 @@ from app.classes.models.server_permissions import ( from app.classes.models.users import HelperUsers, ApiKeys from app.classes.models.roles import HelperRoles from app.classes.models.servers import HelperServers -from app.classes.shared.server import Server logger = logging.getLogger(__name__) diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index c3aad5cb..818b4e91 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -7,7 +7,7 @@ import typing as t from app.classes.controllers.roles_controller import RolesController from app.classes.shared.singleton import Singleton -from app.classes.shared.server import Server +from app.classes.shared.server import ServerInstance from app.classes.shared.console import Console from app.classes.shared.helpers import Helpers from app.classes.shared.main_models import DatabaseShortcuts @@ -26,7 +26,7 @@ logger = logging.getLogger(__name__) class ServersController(metaclass=Singleton): - servers_list: Server + servers_list: ServerInstance def __init__(self, helper, servers_helper, management_helper): self.helper: Helpers = helper @@ -97,17 +97,17 @@ class ServersController(metaclass=Singleton): @staticmethod def set_download(server_id): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) return srv.stats_helper.set_download() @staticmethod def finish_download(server_id): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) return srv.stats_helper.finish_download() @staticmethod def get_download_status(server_id): - server: Server = ServersController().get_server_instance_by_id(server_id) + server = ServersController().get_server_instance_by_id(server_id) return server.stats_helper.get_download_status() def remove_server(self, server_id): @@ -128,7 +128,7 @@ class ServersController(metaclass=Singleton): # Servers Methods # ********************************************************************************** - def get_server_instance_by_id(self, server_id: t.Union[str, int]) -> Server: + def get_server_instance_by_id(self, server_id: t.Union[str, int]) -> ServerInstance: for server in self.servers_list: if int(server["server_id"]) == int(server_id): return server["server_obj"] @@ -179,7 +179,7 @@ class ServersController(metaclass=Singleton): temp_server_dict = { "server_id": server.get("server_id"), "server_data_obj": server, - "server_obj": Server( + "server_obj": ServerInstance( server.get("server_id"), self.helper, self.management_helper, @@ -267,7 +267,7 @@ class ServersController(metaclass=Singleton): server_data = [] try: for server in self.servers_list: - srv: Server = ServersController().get_server_instance_by_id( + srv = ServersController().get_server_instance_by_id( server.get("server_id") ) latest = srv.stats_helper.get_latest_server_stats() @@ -292,7 +292,7 @@ class ServersController(metaclass=Singleton): ) for server in authorized_servers: - srv: Server = server + srv: ServerInstance = server latest = srv.stats_helper.get_latest_server_stats() key_permissions = PermissionsServers.get_api_key_permissions_list( api_key, server.server_id @@ -316,7 +316,7 @@ class ServersController(metaclass=Singleton): authorized_servers = ServersController.get_authorized_servers(user_id) for server in authorized_servers: - srv: Server = server + srv: ServerInstance = server latest = srv.stats_helper.get_latest_server_stats() # TODO user_permissions = PermissionsServers.get_user_id_permissions_list( @@ -360,7 +360,7 @@ class ServersController(metaclass=Singleton): def get_server_obj_optional( self, server_id: t.Union[str, int] - ) -> t.Optional[Server]: + ) -> t.Optional[ServerInstance]: for server in self.servers_list: if str(server["server_id"]) == str(server_id): return server["server_obj"] @@ -394,7 +394,7 @@ class ServersController(metaclass=Singleton): # for each server for server in self.servers_list: # is the server running? - srv_obj: Server = server["server_obj"] + srv_obj: ServerInstance = server["server_obj"] running = srv_obj.check_running() # if so, let's add a dictionary to the list of running servers if running: @@ -432,12 +432,12 @@ class ServersController(metaclass=Singleton): # ********************************************************************************** @staticmethod def get_server_stats_by_id(server_id): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) return srv.stats_helper.get_latest_server_stats() @staticmethod def server_id_exists(server_id): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) return srv.stats_helper.server_id_exists() @staticmethod @@ -457,7 +457,7 @@ class ServersController(metaclass=Singleton): @staticmethod def is_crashed(server_id): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) return srv.stats_helper.is_crashed() @staticmethod @@ -469,32 +469,32 @@ class ServersController(metaclass=Singleton): @staticmethod def set_update(server_id, value): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) return srv.stats_helper.set_update(value) @staticmethod def get_ttl_without_player(server_id): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) return srv.stats_helper.get_ttl_without_player() @staticmethod def can_stop_no_players(server_id, time_limit): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) return srv.stats_helper.can_stop_no_players(time_limit) @staticmethod def set_waiting_start(server_id, value): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) srv.stats_helper.set_waiting_start(value) @staticmethod def get_waiting_start(server_id): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) return srv.stats_helper.get_waiting_start() @staticmethod def get_update_status(server_id): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) return srv.stats_helper.get_update_status() # ********************************************************************************** @@ -502,7 +502,7 @@ class ServersController(metaclass=Singleton): # ********************************************************************************** @staticmethod def get_banned_players(server_id): - srv: Server = ServersController().get_server_instance_by_id(server_id) + srv = ServersController().get_server_instance_by_id(server_id) stats = srv.stats_helper.get_server_stats() server_path = stats["server_id"]["path"] path = os.path.join(server_path, "banned-players.json") diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index de1e7394..f2010737 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -92,7 +92,7 @@ class ServerOutBuf: # ********************************************************************************** # Minecraft Server Class # ********************************************************************************** -class Server: +class ServerInstance: server_object: Servers helper: Helpers management_helper: HelpersManagement From ec2d96741990eabb702b4e7e19a00e822fabb55d Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Mon, 30 May 2022 22:49:42 +0200 Subject: [PATCH 14/15] Fixing Commands to server --- app/classes/shared/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index 49bf7e18..3f0111b6 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -87,7 +87,7 @@ class TasksManager: for cmd in commands: try: svr = self.controller.servers.get_server_instance_by_id( - cmd.server_id + cmd.server_id.server_id ) except: logger.error( From 8e0ccd4f949fedb332ccb9c429ededf45f5ba588 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 1 Jun 2022 18:25:27 -0400 Subject: [PATCH 15/15] Fix indexing for updates made on branch --- app/classes/web/panel_handler.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index eebab5c4..3a90f5d9 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -738,8 +738,8 @@ class PanelHandler(BaseHandler): ) servers = [] for server in user_servers: - if server["server_name"] not in servers: - servers.append(server["server_name"]) + if server.name not in servers: + servers.append(server.name) new_item = {user.user_id: servers} auth_servers.update(new_item) data = {user.user_id: user_roles_list} @@ -974,7 +974,7 @@ class PanelHandler(BaseHandler): role_servers = self.controller.servers.get_authorized_servers(user_id) page_role_servers = [] for server in role_servers: - page_role_servers.append(server["server_id"]) + page_role_servers.append(server.server_id) page_data["new_user"] = False page_data["user"] = self.controller.users.get_user_by_id(user_id) page_data["servers"] = set()