From aa7f47a4f446046127b32c055071c140bdefcbad Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Wed, 1 Nov 2023 18:44:56 +0100 Subject: [PATCH 01/13] Reworking id to use uuid --- app/classes/controllers/servers_controller.py | 6 +- app/classes/models/management.py | 10 +- app/classes/models/server_stats.py | 2 +- app/classes/models/servers.py | 47 +++---- app/classes/shared/command.py | 13 +- app/classes/shared/main_controller.py | 10 +- app/classes/shared/migrate_uuid.py | 116 ++++++++++++++++++ app/classes/shared/migration.py | 15 +++ app/classes/web/panel_handler.py | 4 +- app/classes/web/routes/api/api_handlers.py | 36 +++--- app/classes/web/routes/api/servers/index.py | 4 +- .../web/routes/api/servers/server/action.py | 37 +++--- .../servers/server/backups/backup/index.py | 2 +- .../web/routes/api/servers/server/index.py | 2 +- .../web/routes/metrics/metrics_handlers.py | 2 +- .../panel/server_admin_controls.html | 2 +- .../templates/panel/server_backup.html | 4 +- .../templates/panel/server_config.html | 2 +- .../templates/panel/server_files.html | 2 +- app/frontend/templates/panel/server_logs.html | 2 +- .../templates/panel/server_metrics.html | 2 +- .../templates/panel/server_schedule_edit.html | 10 +- .../templates/panel/server_schedules.html | 2 +- app/frontend/templates/panel/server_term.html | 2 +- .../templates/panel/server_webhook_edit.html | 14 +-- .../templates/panel/server_webhooks.html | 2 +- .../20231022_rework_servers_uuid.py | 103 ++++++++++++++++ main.py | 4 +- 28 files changed, 354 insertions(+), 103 deletions(-) create mode 100644 app/classes/shared/migrate_uuid.py create mode 100644 app/migrations/20231022_rework_servers_uuid.py diff --git a/app/classes/controllers/servers_controller.py b/app/classes/controllers/servers_controller.py index c0bae7b0..f36ce73f 100644 --- a/app/classes/controllers/servers_controller.py +++ b/app/classes/controllers/servers_controller.py @@ -77,8 +77,8 @@ class ServersController(metaclass=Singleton): PeeweeException: If the server already exists """ return HelperServers.create_server( - name, server_uuid, + name, server_dir, backup_path, server_command, @@ -158,9 +158,9 @@ class ServersController(metaclass=Singleton): # Servers Methods # ********************************************************************************** - def get_server_instance_by_id(self, server_id: t.Union[str, int]) -> ServerInstance: + def get_server_instance_by_id(self, server_id: t.Union[str, str]) -> ServerInstance: for server in self.servers_list: - if int(server["server_id"]) == int(server_id): + if server["server_id"] == server_id: return server["server_obj"] logger.warning(f"Unable to find server object for server id {server_id}") diff --git a/app/classes/models/management.py b/app/classes/models/management.py index e86e3209..ffe207c2 100644 --- a/app/classes/models/management.py +++ b/app/classes/models/management.py @@ -31,9 +31,9 @@ class AuditLog(BaseModel): user_name = CharField(default="") user_id = IntegerField(default=0, index=True) source_ip = CharField(default="127.0.0.1") - server_id = IntegerField( - default=None, index=True - ) # When auditing global events, use server ID 0 + server_id = ForeignKeyField( + Servers, backref="audit_server", null=True + ) # When auditing global events, use server ID null log_msg = TextField(default="") class Meta: @@ -79,7 +79,7 @@ class HostStats(BaseModel): # ********************************************************************************** class Webhooks(BaseModel): id = AutoField() - server_id = IntegerField(null=True) + server_id = ForeignKeyField(Servers, backref="webhook_server", null=True) name = CharField(default="Custom Webhook", max_length=64) url = CharField(default="") webhook_type = CharField(default="Custom") @@ -337,7 +337,7 @@ class HelpersManagement: @staticmethod def delete_scheduled_task_by_server(server_id): - Schedules.delete().where(Schedules.server_id == int(server_id)).execute() + Schedules.delete().where(Schedules.server_id == server_id).execute() @staticmethod def get_scheduled_task(schedule_id): diff --git a/app/classes/models/server_stats.py b/app/classes/models/server_stats.py index 8473ed12..64258c8c 100644 --- a/app/classes/models/server_stats.py +++ b/app/classes/models/server_stats.py @@ -71,7 +71,7 @@ class HelperServerStats: database = None def __init__(self, server_id): - self.server_id = int(server_id) + self.server_id = server_id self.init_database(self.server_id) def init_database(self, server_id): diff --git a/app/classes/models/servers.py b/app/classes/models/servers.py index a83fd0a2..d7061e09 100644 --- a/app/classes/models/servers.py +++ b/app/classes/models/servers.py @@ -3,15 +3,18 @@ import datetime import typing as t from peewee import ( CharField, - AutoField, + # AutoField, DateTimeField, BooleanField, IntegerField, + ForeignKeyField, ) from playhouse.shortcuts import model_to_dict from app.classes.shared.main_models import DatabaseShortcuts from app.classes.models.base_model import BaseModel +from app.classes.models.users import Users +from app.classes.shared.helpers import Helpers logger = logging.getLogger(__name__) @@ -20,7 +23,7 @@ logger = logging.getLogger(__name__) # Servers Model # ********************************************************************************** class Servers(BaseModel): - server_id = AutoField() + server_id = CharField(primary_key=True, default=Helpers.create_uuid()) created = DateTimeField(default=datetime.datetime.now) server_uuid = CharField(default="", index=True) server_name = CharField(default="Server", index=True) @@ -40,6 +43,7 @@ class Servers(BaseModel): type = CharField(default="minecraft-java") show_status = BooleanField(default=1) created_by = IntegerField(default=-100) + # created_by = ForeignKeyField(Users, backref="creator_server", null=True) shutdown_timeout = IntegerField(default=60) ignored_exits = CharField(default="0") @@ -59,8 +63,8 @@ class HelperServers: # ********************************************************************************** @staticmethod def create_server( + server_id: str, name: str, - server_uuid: str, server_dir: str, backup_path: str, server_command: str, @@ -94,25 +98,24 @@ class HelperServers: Raises: PeeweeException: If the server already exists """ - return Servers.insert( - { - Servers.server_name: name, - Servers.server_uuid: server_uuid, - Servers.path: server_dir, - Servers.executable: server_file, - Servers.execution_command: server_command, - Servers.auto_start: False, - Servers.auto_start_delay: 10, - Servers.crash_detection: False, - Servers.log_path: server_log_file, - Servers.server_port: server_port, - Servers.server_ip: server_host, - Servers.stop_command: server_stop, - Servers.backup_path: backup_path, - Servers.type: server_type, - Servers.created_by: created_by, - } - ).execute() + return Servers.create( + server_id=server_id, + server_uuid=server_id, + server_name=name, + path=server_dir, + executable=server_file, + execution_command=server_command, + auto_start=False, + auto_start_delay=10, + crash_detection=False, + log_path=server_log_file, + server_port=server_port, + server_ip=server_host, + stop_command=server_stop, + backup_path=backup_path, + type=server_type, + created_by=created_by, + ).server_id @staticmethod def get_server_obj(server_id): diff --git a/app/classes/shared/command.py b/app/classes/shared/command.py index 155fe083..c8bd5a8d 100644 --- a/app/classes/shared/command.py +++ b/app/classes/shared/command.py @@ -6,6 +6,7 @@ import logging import getpass from app.classes.shared.console import Console from app.classes.shared.import3 import Import3 +from app.classes.shared.migrate_uuid import MigrateUUID from app.classes.shared.helpers import Helpers from app.classes.shared.tasks import TasksManager @@ -18,7 +19,13 @@ logger = logging.getLogger(__name__) class MainPrompt(cmd.Cmd): def __init__( - self, helper, tasks_manager, migration_manager, main_controller, import3 + self, + helper, + tasks_manager, + migration_manager, + main_controller, + import3, + migrate_uuid, ): super().__init__() self.helper: Helpers = helper @@ -26,6 +33,7 @@ class MainPrompt(cmd.Cmd): self.migration_manager: MigrationManager = migration_manager self.controller: Controller = main_controller self.import3: Import3 = import3 + self.migrate_uuid: MigrateUUID = migrate_uuid # overrides the default Prompt self.prompt = "" @@ -114,6 +122,9 @@ class MainPrompt(cmd.Cmd): def do_import3(self, _line): self.import3.start_import() + def do_migrate_uuid(self, _line): + self.migrate_uuid.start_migrate() + def universal_exit(self): logger.info("Stopping all server daemons / threads") Console.info( diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 23586696..aca0bd45 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -175,7 +175,7 @@ class Controller: try: os.mkdir(final_path) except FileExistsError: - final_path += "_" + server["server_uuid"] + final_path += "_" + server["server_id"] os.mkdir(final_path) try: FileHelpers.copy_file( @@ -560,11 +560,11 @@ class Controller: # and add the user to it if he's not a superuser if len(captured_roles) == 0: if not exec_user["superuser"]: - new_server_uuid = self.servers.get_server_data_by_id(new_server_id).get( - "server_uuid" + new_server_id = self.servers.get_server_data_by_id(new_server_id).get( + "server_id" ) role_id = self.roles.add_role( - f"Creator of Server with uuid={new_server_uuid}", + f"Creator of Server with id={new_server_id}", exec_user["user_id"], ) self.server_perms.add_role_server(new_server_id, role_id, "11111111") @@ -1023,7 +1023,7 @@ class Controller: for server in servers: server_path = server.get("path") new_local_server_path = os.path.join( - new_server_path, server.get("server_uuid") + new_server_path, server.get("server_id") ) if os.path.isdir(server_path): WebSocketManager().broadcast_page( diff --git a/app/classes/shared/migrate_uuid.py b/app/classes/shared/migrate_uuid.py new file mode 100644 index 00000000..82aa76d2 --- /dev/null +++ b/app/classes/shared/migrate_uuid.py @@ -0,0 +1,116 @@ +import logging + +from app.classes.shared.console import Console + +from app.classes.shared.main_controller import Controller +from app.classes.models.servers import Servers +from app.classes.models.management import ( + AuditLog, + Webhooks, + Schedules, + Backups, +) +from app.classes.models.server_permissions import RoleServers + +logger = logging.getLogger(__name__) + + +class MigrateUUID: + def __init__(self, helper, controller: Controller): + self.helper = helper + self.controller = controller + + def start_migrate(self): + success = self.update_backreferences_tables() + if success: + self.migrate_servers() + + def update_backreferences_tables(self): + try: + # Changes on Audit Log Table + for audit_log in AuditLog.select(): + old_server_id = audit_log.server_id_id + if old_server_id == "0" or old_server_id is None: + server_uuid = None + else: + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + AuditLog.update(server_id=server_uuid).where( + AuditLog.audit_id == audit_log.audit_id + ).execute() + + # Changes on Webhooks Log Table + for webhook in Webhooks.select(): + old_server_id = webhook.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + Webhooks.update(server_id=server_uuid).where( + Webhooks.id == webhook.id + ).execute() + + # Changes on Schedules Log Table + for schedule in Schedules.select(): + old_server_id = schedule.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + Schedules.update(server_id=server_uuid).where( + Schedules.schedule_id == schedule.schedule_id + ).execute() + + # Changes on Backups Log Table + for backup in Backups.select(): + old_server_id = backup.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + Backups.update(server_id=server_uuid).where( + Backups.server_id == old_server_id + ).execute() + + # Changes on RoleServers Log Table + for role_servers in RoleServers.select(): + old_server_id = role_servers.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + RoleServers.update(server_id=server_uuid).where( + RoleServers.role_id == role_servers.id + and RoleServers.server_id == old_server_id + ).execute() + + except Exception as ex: + logger.error("Error while migrating Data from Int to UUID") + logger.error(ex) + Console.error("Error while migrating Data from Int to UUID") + Console.error(ex) + return False + return True + + def migrate_servers(self): + try: + # Migrating servers from the old id type to the new one + for server in Servers.select(): + Servers.update(server_id=server.server_uuid).where( + Servers.server_id == server.server_id + ).execute() + + except Exception as ex: + logger.error("Error while migrating Data from Int to UUID") + logger.error(ex) + Console.error("Error while migrating Data from Int to UUID") + Console.error(ex) + return False + return True diff --git a/app/classes/shared/migration.py b/app/classes/shared/migration.py index c31542a2..1adea4c6 100644 --- a/app/classes/shared/migration.py +++ b/app/classes/shared/migration.py @@ -200,6 +200,21 @@ class Migrator(object): ) return model + @get_model + def alter_column_type( + self, + model: peewee.Model, + column_name: str, + field: peewee.Field, + ) -> peewee.Model: + """ + Alter field data type in database. + """ + self.operations.append( + self.migrator.alter_column_type(model._meta.table_name, column_name, field) + ) + return model + @get_model def rename_table(self, model: peewee.Model, new_name: str) -> peewee.Model: """ diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index e1d21f03..eb540ded 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -153,7 +153,7 @@ class PanelHandler(BaseHandler): self.redirect("/panel/error?error=Invalid Server ID") return None for server in self.controller.servers.failed_servers: - if int(server_id) == server["server_id"]: + if server_id == server["server_id"]: self.failed_server = True return server_id # Does this server exist? @@ -518,7 +518,7 @@ class PanelHandler(BaseHandler): "server_id": { "server_id": server_id, "server_name": server_temp_obj["server_name"], - "server_uuid": server_temp_obj["server_uuid"], + "server_uuid": server_temp_obj["server_id"], "path": server_temp_obj["path"], "log_path": server_temp_obj["log_path"], "executable": server_temp_obj["executable"], diff --git a/app/classes/web/routes/api/api_handlers.py b/app/classes/web/routes/api/api_handlers.py index 706c346f..7c0ba192 100644 --- a/app/classes/web/routes/api/api_handlers.py +++ b/app/classes/web/routes/api/api_handlers.py @@ -202,92 +202,92 @@ def api_handlers(handler_args): handler_args, ), ( - r"/api/v2/servers/([0-9]+)/?", + r"/api/v2/servers/([a-z0-9-]+)/?", ApiServersServerIndexHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/backups/?", + r"/api/v2/servers/([a-z0-9-]+)/backups/?", ApiServersServerBackupsIndexHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/backups/backup/?", + r"/api/v2/servers/([a-z0-9-]+)/backups/backup/?", ApiServersServerBackupsBackupIndexHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/files/?", + r"/api/v2/servers/([a-z0-9-]+)/files/?", ApiServersServerFilesIndexHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/files/create/?", + r"/api/v2/servers/([a-z0-9-]+)/files/create/?", ApiServersServerFilesCreateHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/files/zip/?", + r"/api/v2/servers/([a-z0-9-]+)/files/zip/?", ApiServersServerFilesZipHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/tasks/?", + r"/api/v2/servers/([a-z0-9-]+)/tasks/?", ApiServersServerTasksIndexHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/tasks/([0-9]+)/?", + r"/api/v2/servers/([a-z0-9-]+)/tasks/([0-9]+)/?", ApiServersServerTasksTaskIndexHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/tasks/([0-9]+)/children/?", + r"/api/v2/servers/([a-z0-9-]+)/tasks/([0-9]+)/children/?", ApiServersServerTasksTaskChildrenHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/stats/?", + r"/api/v2/servers/([a-z0-9-]+)/stats/?", ApiServersServerStatsHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/history/?", + r"/api/v2/servers/([a-z0-9-]+)/history/?", ApiServersServerHistoryHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/webhook/([0-9]+)/?", + r"/api/v2/servers/([a-z0-9-]+)/webhook/([0-9]+)/?", ApiServersServerWebhooksManagementIndexHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/webhook/?", + r"/api/v2/servers/([a-z0-9-]+)/webhook/?", ApiServersServerWebhooksIndexHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/action/([a-z_]+)/?", + r"/api/v2/servers/([a-z0-9-]+)/action/([a-z_]+)/?", ApiServersServerActionHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/logs/?", + r"/api/v2/servers/([a-z0-9-]+)/logs/?", ApiServersServerLogsHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/users/?", + r"/api/v2/servers/([a-z0-9-]+)/users/?", ApiServersServerUsersHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/public/?", + r"/api/v2/servers/([a-z0-9-]+)/public/?", ApiServersServerPublicHandler, handler_args, ), ( - r"/api/v2/servers/([0-9]+)/stdin/?", + r"/api/v2/servers/([a-z0-9-]+)/stdin/?", ApiServersServerStdinHandler, handler_args, ), diff --git a/app/classes/web/routes/api/servers/index.py b/app/classes/web/routes/api/servers/index.py index 31dbd4c6..8c4c1669 100644 --- a/app/classes/web/routes/api/servers/index.py +++ b/app/classes/web/routes/api/servers/index.py @@ -734,7 +734,7 @@ class ApiServersIndexHandler(BaseApiHandler): ( f"created server {data['name']}" f" (ID: {new_server_id})" - f" (UUID: {new_server_uuid})" + f" (UUID: {new_server_id})" ), server_id=new_server_id, source_ip=self.get_remote_ip(), @@ -746,7 +746,7 @@ class ApiServersIndexHandler(BaseApiHandler): "status": "ok", "data": { "new_server_id": str(new_server_id), - "new_server_uuid": new_server_uuid, + "new_server_uuid": new_server_id, }, }, ) diff --git a/app/classes/web/routes/api/servers/server/action.py b/app/classes/web/routes/api/servers/server/action.py index 153b889d..9f98c553 100644 --- a/app/classes/web/routes/api/servers/server/action.py +++ b/app/classes/web/routes/api/servers/server/action.py @@ -3,7 +3,6 @@ import os from app.classes.models.server_permissions import EnumPermissionsServer from app.classes.models.servers import Servers from app.classes.shared.file_helpers import FileHelpers -from app.classes.shared.helpers import Helpers from app.classes.web.base_api_handler import BaseApiHandler @@ -60,10 +59,20 @@ class ApiServersServerActionHandler(BaseApiHandler): name_counter += 1 new_server_name = server_data.get("server_name") + f" (Copy {name_counter})" - new_server_uuid = Helpers.create_uuid() - while os.path.exists(os.path.join(self.helper.servers_dir, new_server_uuid)): - new_server_uuid = Helpers.create_uuid() - new_server_path = os.path.join(self.helper.servers_dir, new_server_uuid) + new_server_id = self.controller.servers.create_server( + new_server_name, + None, + "", + None, + server_data.get("executable"), + None, + server_data.get("stop_command"), + server_data.get("type"), + user_id, + server_data.get("server_port"), + ) + + new_server_path = os.path.join(self.helper.servers_dir, new_server_id) self.controller.management.add_to_audit_log( user_id, @@ -81,19 +90,11 @@ class ApiServersServerActionHandler(BaseApiHandler): self.helper.get_os_understandable_path(server_data.get("log_path")) ) - new_server_id = self.controller.servers.create_server( - new_server_name, - new_server_uuid, - new_server_path, - "", - new_server_command, - server_data.get("executable"), - new_server_log_file, - server_data.get("stop_command"), - server_data.get("type"), - user_id, - server_data.get("server_port"), - ) + server: Servers = self.controller.servers.get_server_obj(new_server_id) + server.path = new_server_path + server.log_path = new_server_log_file + server.execution_command = new_server_command + self.controller.servers.update_server(server) self.controller.servers.init_all_servers() diff --git a/app/classes/web/routes/api/servers/server/backups/backup/index.py b/app/classes/web/routes/api/servers/server/backups/backup/index.py index b92e1e9f..41ae74b0 100644 --- a/app/classes/web/routes/api/servers/server/backups/backup/index.py +++ b/app/classes/web/routes/api/servers/server/backups/backup/index.py @@ -145,7 +145,7 @@ class ApiServersServerBackupsBackupIndexHandler(BaseApiHandler): new_server_id = 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"] + server_id, new_server_id, new_server["server_id"] ) # preserve current schedules for schedule in self.controller.management.get_schedules_by_server( diff --git a/app/classes/web/routes/api/servers/server/index.py b/app/classes/web/routes/api/servers/server/index.py index afe02a0b..98b545bf 100644 --- a/app/classes/web/routes/api/servers/server/index.py +++ b/app/classes/web/routes/api/servers/server/index.py @@ -174,7 +174,7 @@ class ApiServersServerIndexHandler(BaseApiHandler): self.tasks_manager.remove_all_server_tasks(server_id) failed = False for item in self.controller.servers.failed_servers[:]: - if item["server_id"] == int(server_id): + if item["server_id"] == server_id: self.controller.servers.failed_servers.remove(item) failed = True diff --git a/app/classes/web/routes/metrics/metrics_handlers.py b/app/classes/web/routes/metrics/metrics_handlers.py index fa43a909..7fb1e31d 100644 --- a/app/classes/web/routes/metrics/metrics_handlers.py +++ b/app/classes/web/routes/metrics/metrics_handlers.py @@ -17,7 +17,7 @@ def metrics_handlers(handler_args): handler_args, ), ( - r"/metrics/servers/([0-9]+)/?", + r"/metrics/servers/([a-z0-9-]+)/?", ApiOpenMetricsServersHandler, handler_args, ), diff --git a/app/frontend/templates/panel/server_admin_controls.html b/app/frontend/templates/panel/server_admin_controls.html index c021f310..b136a650 100644 --- a/app/frontend/templates/panel/server_admin_controls.html +++ b/app/frontend/templates/panel/server_admin_controls.html @@ -17,7 +17,7 @@ {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
- UUID: {{ data['server_stats']['server_id']['server_uuid'] }} + UUID: {{ data['server_stats']['server_id']['server_id'] }} diff --git a/app/frontend/templates/panel/server_backup.html b/app/frontend/templates/panel/server_backup.html index d79e710e..27b45a8a 100644 --- a/app/frontend/templates/panel/server_backup.html +++ b/app/frontend/templates/panel/server_backup.html @@ -17,7 +17,7 @@ {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
- UUID: {{ data['server_stats']['server_id']['server_uuid'] }} + UUID: {{ data['server_stats']['server_id']['server_id'] }} @@ -453,7 +453,7 @@ }); } }); - + try { if ($('#backup_path').val() == '') { console.log('true') diff --git a/app/frontend/templates/panel/server_config.html b/app/frontend/templates/panel/server_config.html index 46f7b4dc..4d38e41f 100644 --- a/app/frontend/templates/panel/server_config.html +++ b/app/frontend/templates/panel/server_config.html @@ -17,7 +17,7 @@ {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
- UUID: {{ data['server_stats']['server_id']['server_uuid'] }} + UUID: {{ data['server_stats']['server_id']['server_id'] }} diff --git a/app/frontend/templates/panel/server_files.html b/app/frontend/templates/panel/server_files.html index ce188d2a..2d99c6bb 100644 --- a/app/frontend/templates/panel/server_files.html +++ b/app/frontend/templates/panel/server_files.html @@ -17,7 +17,7 @@ {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
- UUID: {{ data['server_stats']['server_id']['server_uuid'] }} + UUID: {{ data['server_stats']['server_id']['server_id'] }} diff --git a/app/frontend/templates/panel/server_logs.html b/app/frontend/templates/panel/server_logs.html index 9a4033a1..bc56816a 100644 --- a/app/frontend/templates/panel/server_logs.html +++ b/app/frontend/templates/panel/server_logs.html @@ -17,7 +17,7 @@ {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
- UUID: {{ data['server_stats']['server_id']['server_uuid'] }} + UUID: {{ data['server_stats']['server_id']['server_id'] }} diff --git a/app/frontend/templates/panel/server_metrics.html b/app/frontend/templates/panel/server_metrics.html index 5067e709..be7712e5 100644 --- a/app/frontend/templates/panel/server_metrics.html +++ b/app/frontend/templates/panel/server_metrics.html @@ -17,7 +17,7 @@ {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
- UUID: {{ data['server_stats']['server_id']['server_uuid'] }} + UUID: {{ data['server_stats']['server_id']['server_id'] }} diff --git a/app/frontend/templates/panel/server_schedule_edit.html b/app/frontend/templates/panel/server_schedule_edit.html index fac8242a..7b116f7f 100644 --- a/app/frontend/templates/panel/server_schedule_edit.html +++ b/app/frontend/templates/panel/server_schedule_edit.html @@ -17,7 +17,7 @@ {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
- UUID: {{ data['server_stats']['server_id']['server_uuid'] }} + UUID: {{ data['server_stats']['server_id']['server_id'] }} @@ -235,7 +235,7 @@ if (key != "start_time" && key != "cron_string" && key != "interval_type") { if (typeof value == "boolean") { return value - } + } console.log(key) if (key === "interval" && value === ""){ return 0; @@ -298,7 +298,7 @@ title: responseData.status, message: responseData.error }); - } + } }); $("#schedule_form").on("submit", async function (e) { @@ -332,7 +332,7 @@ method: 'PATCH', headers: { 'X-XSRFToken': token, - "Content-Type": "application/json", + "Content-Type": "application/json", }, body: formDataJsonString, }); @@ -345,7 +345,7 @@ title: responseData.error, message: responseData.error_data }); - } + } }); }); diff --git a/app/frontend/templates/panel/server_schedules.html b/app/frontend/templates/panel/server_schedules.html index 85bc077d..1fcb30df 100644 --- a/app/frontend/templates/panel/server_schedules.html +++ b/app/frontend/templates/panel/server_schedules.html @@ -17,7 +17,7 @@ {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
- UUID: {{ data['server_stats']['server_id']['server_uuid'] }} + UUID: {{ data['server_stats']['server_id']['server_id'] }} diff --git a/app/frontend/templates/panel/server_term.html b/app/frontend/templates/panel/server_term.html index bb63fbe7..0272ca4f 100644 --- a/app/frontend/templates/panel/server_term.html +++ b/app/frontend/templates/panel/server_term.html @@ -17,7 +17,7 @@ {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
- UUID: {{ data['server_stats']['server_id']['server_uuid'] }} + UUID: {{ data['server_stats']['server_id']['server_id'] }} diff --git a/app/frontend/templates/panel/server_webhook_edit.html b/app/frontend/templates/panel/server_webhook_edit.html index e7087771..57b483dc 100644 --- a/app/frontend/templates/panel/server_webhook_edit.html +++ b/app/frontend/templates/panel/server_webhook_edit.html @@ -17,7 +17,7 @@ {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
- UUID: {{ data['server_stats']['server_id']['server_uuid'] }} + UUID: {{ data['server_stats']['server_id']['server_id'] }} @@ -159,7 +159,7 @@ if (key != "start_time" && key != "cron_string" && key != "interval_type") { if (typeof value == "boolean") { return value - } + } console.log(key) if (key === "interval" && value === ""){ return 0; @@ -219,7 +219,7 @@ title: responseData.status, message: responseData.error }); - } + } }); $("#webhook_form").on("submit", async function (e) { @@ -247,7 +247,7 @@ method: 'PATCH', headers: { 'X-XSRFToken': token, - "Content-Type": "application/json", + "Content-Type": "application/json", }, body: formDataJsonString, }); @@ -260,15 +260,15 @@ title: responseData.status, message: responseData.error }); - } + } }); }); - + function hexToDiscordInt(hexColor) { // Remove the hash at the start if it's there const sanitizedHex = hexColor.startsWith('#') ? hexColor.slice(1) : hexColor; - + // Convert the hex to an integer return parseInt(sanitizedHex, 16); } diff --git a/app/frontend/templates/panel/server_webhooks.html b/app/frontend/templates/panel/server_webhooks.html index 25a61257..432b0148 100644 --- a/app/frontend/templates/panel/server_webhooks.html +++ b/app/frontend/templates/panel/server_webhooks.html @@ -17,7 +17,7 @@ {{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{ data['server_stats']['server_id']['server_name'] }}
- UUID: {{ data['server_stats']['server_id']['server_uuid'] }} + UUID: {{ data['server_stats']['server_id']['server_id'] }} diff --git a/app/migrations/20231022_rework_servers_uuid.py b/app/migrations/20231022_rework_servers_uuid.py new file mode 100644 index 00000000..2cf1614e --- /dev/null +++ b/app/migrations/20231022_rework_servers_uuid.py @@ -0,0 +1,103 @@ +import datetime +import uuid +import peewee + +from app.classes.shared.migration import Migrator +from app.classes.models.management import AuditLog +from app.classes.models.management import Webhooks + + +def migrate(migrator: Migrator, database, **kwargs): + """ + Write your migrations here. + """ + db = database + + # ********************************************************************************** + # Servers New Model from Old (easier to migrate without dunmping Database) + # ********************************************************************************** + class Servers(peewee.Model): + server_id = peewee.CharField(primary_key=True, default=str(uuid.uuid4())) + created = peewee.DateTimeField(default=datetime.datetime.now) + server_uuid = peewee.CharField(default="", index=True) + server_name = peewee.CharField(default="Server", index=True) + path = peewee.CharField(default="") + backup_path = peewee.CharField(default="") + executable = peewee.CharField(default="") + log_path = peewee.CharField(default="") + execution_command = peewee.CharField(default="") + auto_start = peewee.BooleanField(default=0) + auto_start_delay = peewee.IntegerField(default=10) + crash_detection = peewee.BooleanField(default=0) + stop_command = peewee.CharField(default="stop") + executable_update_url = peewee.CharField(default="") + server_ip = peewee.CharField(default="127.0.0.1") + server_port = peewee.IntegerField(default=25565) + logs_delete_after = peewee.IntegerField(default=0) + type = peewee.CharField(default="minecraft-java") + show_status = peewee.BooleanField(default=1) + created_by = peewee.IntegerField(default=-100) + shutdown_timeout = peewee.IntegerField(default=60) + ignored_exits = peewee.CharField(default="0") + + class Meta: + table_name = "servers" + database = db + + # Changes on Server Table + migrator.alter_column_type( + Servers, + "server_id", + peewee.CharField(primary_key=True, default=str(uuid.uuid4())), + ) + + # Changes on Audit Log Table + migrator.alter_column_type( + AuditLog, + "server_id", + peewee.ForeignKeyField( + Servers, + backref="audit_server", + null=True, + field=peewee.CharField(primary_key=True, default=str(uuid.uuid4())), + ), + ) + # Changes on Webhook Table + migrator.alter_column_type( + Webhooks, + "server_id", + peewee.ForeignKeyField( + Servers, + backref="webhook_server", + null=True, + field=peewee.CharField(primary_key=True, default=str(uuid.uuid4())), + ), + ) + + +def rollback(migrator: Migrator, database, **kwargs): + """ + Write your rollback migrations here. + """ + db = database + + # Changes on Server Table + migrator.alter_column_type( + "servers", + "server_id", + peewee.AutoField(), + ) + + # Changes on Audit Log Table + migrator.alter_column_type( + AuditLog, + "server_id", + peewee.IntegerField(default=None, index=True), + ) + + # Changes on Webhook Table + migrator.alter_column_type( + Webhooks, + "server_id", + peewee.IntegerField(null=True), + ) diff --git a/main.py b/main.py index 143dfb4f..3379af5b 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ from packaging import version as pkg_version from app.classes.shared.file_helpers import FileHelpers from app.classes.shared.import3 import Import3 +from app.classes.shared.migrate_uuid import MigrateUUID from app.classes.shared.console import Console from app.classes.shared.helpers import Helpers from app.classes.models.users import HelperUsers @@ -174,6 +175,7 @@ if __name__ == "__main__": Console.info("Remote change complete.") import3 = Import3(helper, controller) + migrate_uuid = MigrateUUID(helper, controller) tasks_manager = TasksManager(helper, controller, file_helper) tasks_manager.start_webserver() @@ -259,7 +261,7 @@ if __name__ == "__main__": controller.clear_support_status() crafty_prompt = MainPrompt( - helper, tasks_manager, migration_manager, controller, import3 + helper, tasks_manager, migration_manager, controller, import3, migrate_uuid ) controller_setup_thread = Thread(target=controller_setup, name="controller_setup") From 586b2038b64a740cd5e3c1977655b7048f69035f Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Fri, 10 Nov 2023 23:16:04 +0100 Subject: [PATCH 02/13] Fixing warnings --- app/classes/models/servers.py | 5 ++--- app/classes/shared/main_controller.py | 2 +- app/classes/web/routes/api/servers/index.py | 4 +--- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/classes/models/servers.py b/app/classes/models/servers.py index d7061e09..28cd8998 100644 --- a/app/classes/models/servers.py +++ b/app/classes/models/servers.py @@ -3,17 +3,16 @@ import datetime import typing as t from peewee import ( CharField, - # AutoField, DateTimeField, BooleanField, IntegerField, - ForeignKeyField, ) from playhouse.shortcuts import model_to_dict from app.classes.shared.main_models import DatabaseShortcuts from app.classes.models.base_model import BaseModel -from app.classes.models.users import Users + +# from app.classes.models.users import Users from app.classes.shared.helpers import Helpers logger = logging.getLogger(__name__) diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index aca0bd45..6f85c25e 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -575,7 +575,7 @@ class Controller: role_id = role self.server_perms.add_role_server(new_server_id, role_id, "11111111") - return new_server_id, server_fs_uuid + return new_server_id @staticmethod def verify_jar_server(server_path: str, server_jar: str): diff --git a/app/classes/web/routes/api/servers/index.py b/app/classes/web/routes/api/servers/index.py index 8c4c1669..3c14f604 100644 --- a/app/classes/web/routes/api/servers/index.py +++ b/app/classes/web/routes/api/servers/index.py @@ -723,9 +723,7 @@ class ApiServersIndexHandler(BaseApiHandler): 405, {"status": "error", "error": "DATA CONSTRAINT FAILED"} ) return - new_server_id, new_server_uuid = self.controller.create_api_server( - data, user["user_id"] - ) + new_server_id = self.controller.create_api_server(data, user["user_id"]) self.controller.servers.stats.record_stats() From ad6e53a5d1c974f3b4bfc4e107aacd43fb45535a Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Fri, 16 Feb 2024 19:29:14 +0100 Subject: [PATCH 03/13] Adding all migration steps to a migration.py file --- app/classes/shared/migrate_uuid.py | 116 ------------------ .../20231022_rework_servers_uuid.py | 103 +++++++++++++++- 2 files changed, 101 insertions(+), 118 deletions(-) delete mode 100644 app/classes/shared/migrate_uuid.py diff --git a/app/classes/shared/migrate_uuid.py b/app/classes/shared/migrate_uuid.py deleted file mode 100644 index 82aa76d2..00000000 --- a/app/classes/shared/migrate_uuid.py +++ /dev/null @@ -1,116 +0,0 @@ -import logging - -from app.classes.shared.console import Console - -from app.classes.shared.main_controller import Controller -from app.classes.models.servers import Servers -from app.classes.models.management import ( - AuditLog, - Webhooks, - Schedules, - Backups, -) -from app.classes.models.server_permissions import RoleServers - -logger = logging.getLogger(__name__) - - -class MigrateUUID: - def __init__(self, helper, controller: Controller): - self.helper = helper - self.controller = controller - - def start_migrate(self): - success = self.update_backreferences_tables() - if success: - self.migrate_servers() - - def update_backreferences_tables(self): - try: - # Changes on Audit Log Table - for audit_log in AuditLog.select(): - old_server_id = audit_log.server_id_id - if old_server_id == "0" or old_server_id is None: - server_uuid = None - else: - try: - server = Servers.get_by_id(old_server_id) - server_uuid = server.server_uuid - except: - server_uuid = old_server_id - AuditLog.update(server_id=server_uuid).where( - AuditLog.audit_id == audit_log.audit_id - ).execute() - - # Changes on Webhooks Log Table - for webhook in Webhooks.select(): - old_server_id = webhook.server_id_id - try: - server = Servers.get_by_id(old_server_id) - server_uuid = server.server_uuid - except: - server_uuid = old_server_id - Webhooks.update(server_id=server_uuid).where( - Webhooks.id == webhook.id - ).execute() - - # Changes on Schedules Log Table - for schedule in Schedules.select(): - old_server_id = schedule.server_id_id - try: - server = Servers.get_by_id(old_server_id) - server_uuid = server.server_uuid - except: - server_uuid = old_server_id - Schedules.update(server_id=server_uuid).where( - Schedules.schedule_id == schedule.schedule_id - ).execute() - - # Changes on Backups Log Table - for backup in Backups.select(): - old_server_id = backup.server_id_id - try: - server = Servers.get_by_id(old_server_id) - server_uuid = server.server_uuid - except: - server_uuid = old_server_id - Backups.update(server_id=server_uuid).where( - Backups.server_id == old_server_id - ).execute() - - # Changes on RoleServers Log Table - for role_servers in RoleServers.select(): - old_server_id = role_servers.server_id_id - try: - server = Servers.get_by_id(old_server_id) - server_uuid = server.server_uuid - except: - server_uuid = old_server_id - RoleServers.update(server_id=server_uuid).where( - RoleServers.role_id == role_servers.id - and RoleServers.server_id == old_server_id - ).execute() - - except Exception as ex: - logger.error("Error while migrating Data from Int to UUID") - logger.error(ex) - Console.error("Error while migrating Data from Int to UUID") - Console.error(ex) - return False - return True - - def migrate_servers(self): - try: - # Migrating servers from the old id type to the new one - for server in Servers.select(): - Servers.update(server_id=server.server_uuid).where( - Servers.server_id == server.server_id - ).execute() - - except Exception as ex: - logger.error("Error while migrating Data from Int to UUID") - logger.error(ex) - Console.error("Error while migrating Data from Int to UUID") - Console.error(ex) - return False - return True diff --git a/app/migrations/20231022_rework_servers_uuid.py b/app/migrations/20231022_rework_servers_uuid.py index 2cf1614e..3d09d902 100644 --- a/app/migrations/20231022_rework_servers_uuid.py +++ b/app/migrations/20231022_rework_servers_uuid.py @@ -1,10 +1,19 @@ import datetime import uuid import peewee +import logging +from app.classes.shared.console import Console from app.classes.shared.migration import Migrator -from app.classes.models.management import AuditLog -from app.classes.models.management import Webhooks +from app.classes.models.management import ( + AuditLog, + Webhooks, + Schedules, + Backups, +) +from app.classes.models.server_permissions import RoleServers + +logger = logging.getLogger(__name__) def migrate(migrator: Migrator, database, **kwargs): @@ -74,6 +83,96 @@ def migrate(migrator: Migrator, database, **kwargs): ), ) + try: + # Changes on Audit Log Table + for audit_log in AuditLog.select(): + old_server_id = audit_log.server_id_id + if old_server_id == "0" or old_server_id is None: + server_uuid = None + else: + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + AuditLog.update(server_id=server_uuid).where( + AuditLog.audit_id == audit_log.audit_id + ).execute() + + # Changes on Webhooks Log Table + for webhook in Webhooks.select(): + old_server_id = webhook.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + Webhooks.update(server_id=server_uuid).where( + Webhooks.id == webhook.id + ).execute() + + # Changes on Schedules Log Table + for schedule in Schedules.select(): + old_server_id = schedule.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + Schedules.update(server_id=server_uuid).where( + Schedules.schedule_id == schedule.schedule_id + ).execute() + + # Changes on Backups Log Table + for backup in Backups.select(): + old_server_id = backup.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + Backups.update(server_id=server_uuid).where( + Backups.server_id == old_server_id + ).execute() + + # Changes on RoleServers Log Table + for role_servers in RoleServers.select(): + old_server_id = role_servers.server_id_id + try: + server = Servers.get_by_id(old_server_id) + server_uuid = server.server_uuid + except: + server_uuid = old_server_id + RoleServers.update(server_id=server_uuid).where( + RoleServers.role_id == role_servers.id + and RoleServers.server_id == old_server_id + ).execute() + except Exception as ex: + logger.error("Error while migrating Data from Int to UUID (Foreign Keys)") + logger.error(ex) + Console.error("Error while migrating Data from Int to UUID (Foreign Keys)") + Console.error(ex) + return False + + try: + # Migrating servers from the old id type to the new one + for server in Servers.select(): + Servers.update(server_id=server.server_uuid).where( + Servers.server_id == server.server_id + ).execute() + + except Exception as ex: + logger.error("Error while migrating Data from Int to UUID (Primary Keys)") + logger.error(ex) + Console.error("Error while migrating Data from Int to UUID (Primary Keys)") + Console.error(ex) + return False + + # Changes on Server Table + migrator.drop_columns("servers", ["server_uuid"]) + + return True + def rollback(migrator: Migrator, database, **kwargs): """ From ccec2a3400ca7ce27b5fd1b602ba8a7491064b6e Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Fri, 16 Feb 2024 19:32:23 +0100 Subject: [PATCH 04/13] rename migration file to have a more recent one --- ...022_rework_servers_uuid.py => 20240216_rework_servers_uuid.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/migrations/{20231022_rework_servers_uuid.py => 20240216_rework_servers_uuid.py} (100%) diff --git a/app/migrations/20231022_rework_servers_uuid.py b/app/migrations/20240216_rework_servers_uuid.py similarity index 100% rename from app/migrations/20231022_rework_servers_uuid.py rename to app/migrations/20240216_rework_servers_uuid.py From bb3901f7835653653d265e020607e35f9f281d25 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Fri, 16 Feb 2024 19:45:08 +0100 Subject: [PATCH 05/13] remove forgoten use of migration_uuid --- main.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/main.py b/main.py index dcbf27e5..ebaf7806 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,6 @@ from packaging import version as pkg_version from app.classes.shared.file_helpers import FileHelpers from app.classes.shared.import3 import Import3 -from app.classes.shared.migrate_uuid import MigrateUUID from app.classes.shared.console import Console from app.classes.shared.helpers import Helpers from app.classes.models.users import HelperUsers @@ -384,7 +383,6 @@ if __name__ == "__main__": controller.set_project_root(APPLICATION_PATH) tasks_manager = TasksManager(helper, controller, file_helper) import3 = Import3(helper, controller) - migrate_uuid = MigrateUUID(helper, controller) # Check to see if client config.json version is different than the # Master config.json in helpers.py @@ -417,7 +415,7 @@ if __name__ == "__main__": # start the Crafty console. crafty_prompt = MainPrompt( - helper, tasks_manager, migration_manager, controller, import3, migrate_uuid + helper, tasks_manager, migration_manager, controller, import3 ) # set up all controllers From 875c9058b03a40821ff22fb4c7ef6ea2f0c9b063 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Fri, 16 Feb 2024 19:46:22 +0100 Subject: [PATCH 06/13] there were still use -_- --- app/classes/shared/command.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/classes/shared/command.py b/app/classes/shared/command.py index c8bd5a8d..ab7a494a 100644 --- a/app/classes/shared/command.py +++ b/app/classes/shared/command.py @@ -6,7 +6,6 @@ import logging import getpass from app.classes.shared.console import Console from app.classes.shared.import3 import Import3 -from app.classes.shared.migrate_uuid import MigrateUUID from app.classes.shared.helpers import Helpers from app.classes.shared.tasks import TasksManager @@ -25,7 +24,6 @@ class MainPrompt(cmd.Cmd): migration_manager, main_controller, import3, - migrate_uuid, ): super().__init__() self.helper: Helpers = helper @@ -33,7 +31,6 @@ class MainPrompt(cmd.Cmd): self.migration_manager: MigrationManager = migration_manager self.controller: Controller = main_controller self.import3: Import3 = import3 - self.migrate_uuid: MigrateUUID = migrate_uuid # overrides the default Prompt self.prompt = "" @@ -122,9 +119,6 @@ class MainPrompt(cmd.Cmd): def do_import3(self, _line): self.import3.start_import() - def do_migrate_uuid(self, _line): - self.migrate_uuid.start_migrate() - def universal_exit(self): logger.info("Stopping all server daemons / threads") Console.info( From 90e0777b9b986473551c512bcd753365adcbe552 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Fri, 16 Feb 2024 20:04:12 +0100 Subject: [PATCH 07/13] try to revert back to old migrate history if failed --- app/classes/models/servers.py | 2 +- .../20240216_rework_servers_uuid.py | 78 ++++++++++++------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/app/classes/models/servers.py b/app/classes/models/servers.py index 92270d91..8802236a 100644 --- a/app/classes/models/servers.py +++ b/app/classes/models/servers.py @@ -24,7 +24,7 @@ logger = logging.getLogger(__name__) class Servers(BaseModel): server_id = CharField(primary_key=True, default=Helpers.create_uuid()) created = DateTimeField(default=datetime.datetime.now) - server_uuid = CharField(default="", index=True) + # server_uuid = CharField(default="", index=True) server_name = CharField(default="Server", index=True) path = CharField(default="") backup_path = CharField(default="") diff --git a/app/migrations/20240216_rework_servers_uuid.py b/app/migrations/20240216_rework_servers_uuid.py index 3d09d902..30ec45a2 100644 --- a/app/migrations/20240216_rework_servers_uuid.py +++ b/app/migrations/20240216_rework_servers_uuid.py @@ -4,7 +4,7 @@ import peewee import logging from app.classes.shared.console import Console -from app.classes.shared.migration import Migrator +from app.classes.shared.migration import Migrator, MigrateHistory from app.classes.models.management import ( AuditLog, Webhooks, @@ -53,35 +53,47 @@ def migrate(migrator: Migrator, database, **kwargs): table_name = "servers" database = db - # Changes on Server Table - migrator.alter_column_type( - Servers, - "server_id", - peewee.CharField(primary_key=True, default=str(uuid.uuid4())), - ) + try: + # Changes on Server Table + migrator.alter_column_type( + Servers, + "server_id", + peewee.CharField(primary_key=True, default=str(uuid.uuid4())), + ) - # Changes on Audit Log Table - migrator.alter_column_type( - AuditLog, - "server_id", - peewee.ForeignKeyField( - Servers, - backref="audit_server", - null=True, - field=peewee.CharField(primary_key=True, default=str(uuid.uuid4())), - ), - ) - # Changes on Webhook Table - migrator.alter_column_type( - Webhooks, - "server_id", - peewee.ForeignKeyField( - Servers, - backref="webhook_server", - null=True, - field=peewee.CharField(primary_key=True, default=str(uuid.uuid4())), - ), - ) + # Changes on Audit Log Table + migrator.alter_column_type( + AuditLog, + "server_id", + peewee.ForeignKeyField( + Servers, + backref="audit_server", + null=True, + field=peewee.CharField(primary_key=True, default=str(uuid.uuid4())), + ), + ) + # Changes on Webhook Table + migrator.alter_column_type( + Webhooks, + "server_id", + peewee.ForeignKeyField( + Servers, + backref="webhook_server", + null=True, + field=peewee.CharField(primary_key=True, default=str(uuid.uuid4())), + ), + ) + + except Exception as ex: + logger.error("Error while migrating Data from Int to UUID (Type Change)") + logger.error(ex) + Console.error("Error while migrating Data from Int to UUID (Type Change)") + Console.error(ex) + last_migration = ( + MigrateHistory.select().order_by(MigrateHistory.id.desc()).get() + ) + last_migration.delete() + return False try: # Changes on Audit Log Table @@ -152,6 +164,10 @@ def migrate(migrator: Migrator, database, **kwargs): logger.error(ex) Console.error("Error while migrating Data from Int to UUID (Foreign Keys)") Console.error(ex) + last_migration = ( + MigrateHistory.select().order_by(MigrateHistory.id.desc()).get() + ) + last_migration.delete() return False try: @@ -166,6 +182,10 @@ def migrate(migrator: Migrator, database, **kwargs): logger.error(ex) Console.error("Error while migrating Data from Int to UUID (Primary Keys)") Console.error(ex) + last_migration = ( + MigrateHistory.select().order_by(MigrateHistory.id.desc()).get() + ) + last_migration.delete() return False # Changes on Server Table From 12627cec8e20ec1d424e5690be74a822a57268f5 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Fri, 16 Feb 2024 20:21:38 +0100 Subject: [PATCH 08/13] Fixed the why it was not working --- .../20240216_rework_servers_uuid.py | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/app/migrations/20240216_rework_servers_uuid.py b/app/migrations/20240216_rework_servers_uuid.py index 30ec45a2..b4c2dee0 100644 --- a/app/migrations/20240216_rework_servers_uuid.py +++ b/app/migrations/20240216_rework_servers_uuid.py @@ -54,6 +54,9 @@ def migrate(migrator: Migrator, database, **kwargs): database = db try: + logger.error("Migrating Data from Int to UUID (Type Change)") + Console.error("Migrating Data from Int to UUID (Type Change)") + # Changes on Server Table migrator.alter_column_type( Servers, @@ -84,18 +87,20 @@ def migrate(migrator: Migrator, database, **kwargs): ), ) + migrator.run() + except Exception as ex: logger.error("Error while migrating Data from Int to UUID (Type Change)") logger.error(ex) Console.error("Error while migrating Data from Int to UUID (Type Change)") Console.error(ex) - last_migration = ( - MigrateHistory.select().order_by(MigrateHistory.id.desc()).get() - ) + last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) last_migration.delete() return False try: + logger.error("Migrating Data from Int to UUID (Foreign Keys)") + Console.error("Migrating Data from Int to UUID (Foreign Keys)") # Changes on Audit Log Table for audit_log in AuditLog.select(): old_server_id = audit_log.server_id_id @@ -164,13 +169,13 @@ def migrate(migrator: Migrator, database, **kwargs): logger.error(ex) Console.error("Error while migrating Data from Int to UUID (Foreign Keys)") Console.error(ex) - last_migration = ( - MigrateHistory.select().order_by(MigrateHistory.id.desc()).get() - ) + last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) last_migration.delete() return False try: + logger.error("Migrating Data from Int to UUID (Primary Keys)") + Console.error("Migrating Data from Int to UUID (Primary Keys)") # Migrating servers from the old id type to the new one for server in Servers.select(): Servers.update(server_id=server.server_uuid).where( @@ -182,14 +187,13 @@ def migrate(migrator: Migrator, database, **kwargs): logger.error(ex) Console.error("Error while migrating Data from Int to UUID (Primary Keys)") Console.error(ex) - last_migration = ( - MigrateHistory.select().order_by(MigrateHistory.id.desc()).get() - ) + last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) last_migration.delete() return False # Changes on Server Table migrator.drop_columns("servers", ["server_uuid"]) + migrator.run() return True From b30d7b627cec94488cb15d75ab2b99f0b159fb60 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Fri, 16 Feb 2024 20:23:23 +0100 Subject: [PATCH 09/13] removing uuid from server model --- app/classes/models/servers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/classes/models/servers.py b/app/classes/models/servers.py index 8802236a..13d9096a 100644 --- a/app/classes/models/servers.py +++ b/app/classes/models/servers.py @@ -24,7 +24,6 @@ logger = logging.getLogger(__name__) class Servers(BaseModel): server_id = CharField(primary_key=True, default=Helpers.create_uuid()) created = DateTimeField(default=datetime.datetime.now) - # server_uuid = CharField(default="", index=True) server_name = CharField(default="Server", index=True) path = CharField(default="") backup_path = CharField(default="") From 3529c9f7fead3a3409912c313635f39946f4f12d Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Fri, 16 Feb 2024 20:27:30 +0100 Subject: [PATCH 10/13] Fixing loog messages --- .../20240216_rework_servers_uuid.py | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/app/migrations/20240216_rework_servers_uuid.py b/app/migrations/20240216_rework_servers_uuid.py index b4c2dee0..ccfce915 100644 --- a/app/migrations/20240216_rework_servers_uuid.py +++ b/app/migrations/20240216_rework_servers_uuid.py @@ -54,8 +54,8 @@ def migrate(migrator: Migrator, database, **kwargs): database = db try: - logger.error("Migrating Data from Int to UUID (Type Change)") - Console.error("Migrating Data from Int to UUID (Type Change)") + logger.info("Migrating Data from Int to UUID (Type Change)") + Console.info("Migrating Data from Int to UUID (Type Change)") # Changes on Server Table migrator.alter_column_type( @@ -89,6 +89,9 @@ def migrate(migrator: Migrator, database, **kwargs): migrator.run() + logger.info("Migrating Data from Int to UUID (Type Change) : SUCCESS") + Console.info("Migrating Data from Int to UUID (Type Change) : SUCCESS") + except Exception as ex: logger.error("Error while migrating Data from Int to UUID (Type Change)") logger.error(ex) @@ -164,6 +167,10 @@ def migrate(migrator: Migrator, database, **kwargs): RoleServers.role_id == role_servers.id and RoleServers.server_id == old_server_id ).execute() + + logger.error("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") + Console.error("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") + except Exception as ex: logger.error("Error while migrating Data from Int to UUID (Foreign Keys)") logger.error(ex) @@ -182,6 +189,9 @@ def migrate(migrator: Migrator, database, **kwargs): Servers.server_id == server.server_id ).execute() + logger.error("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") + Console.error("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") + except Exception as ex: logger.error("Error while migrating Data from Int to UUID (Primary Keys)") logger.error(ex) @@ -192,8 +202,16 @@ def migrate(migrator: Migrator, database, **kwargs): return False # Changes on Server Table + logger.error("Migrating Data from Int to UUID (Removing UUID Field from Servers)") + Console.error("Migrating Data from Int to UUID (Removing UUID Field from Servers)") migrator.drop_columns("servers", ["server_uuid"]) migrator.run() + logger.error( + "Migrating Data from Int to UUID (Removing UUID Field from Servers) : SUCCESS" + ) + Console.error( + "Migrating Data from Int to UUID (Removing UUID Field from Servers) : SUCCESS" + ) return True From 3bd5de5bf602f6a05fbffeac4513bd887382a1b1 Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Fri, 16 Feb 2024 20:36:22 +0100 Subject: [PATCH 11/13] update cryptography in requirements.txt too fix vulnerability --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 7b1adcfc..13e46226 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ argon2-cffi==23.1.0 cached_property==1.5.2 colorama==0.4.6 croniter==1.4.1 -cryptography==41.0.7 +cryptography==42.0.0 libgravatar==1.0.4 nh3==0.2.14 packaging==23.2 From c20c6b35b8cbfbe574128385c4dda4a639e2ecfb Mon Sep 17 00:00:00 2001 From: Zedifus Date: Sun, 3 Mar 2024 01:36:53 +0000 Subject: [PATCH 12/13] Re-tag release to 4.3.0 BREAKING: Contains db changes w/ no avail rollback --- CHANGELOG.md | 2 +- README.md | 2 +- app/config/version.json | 4 ++-- sonar-project.properties | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abd77b2a..aee7e09d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # Changelog -## --- [4.2.4] - 2023/TBD +## --- [4.3.0] - 2023/TBD ### New features TBD ### Refactor diff --git a/README.md b/README.md index b1b401d7..75da23a7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![Crafty Logo](app/frontend/static/assets/images/logo_long.svg)](https://craftycontrol.com) -# Crafty Controller 4.2.4 +# Crafty Controller 4.3.0 > Python based Control Panel for your Minecraft Server ## What is Crafty Controller? diff --git a/app/config/version.json b/app/config/version.json index 3c001e77..db68adb0 100644 --- a/app/config/version.json +++ b/app/config/version.json @@ -1,5 +1,5 @@ { "major": 4, - "minor": 2, - "sub": 4 + "minor": 3, + "sub": 0 } diff --git a/sonar-project.properties b/sonar-project.properties index 635324ef..bf2d9c5c 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -3,7 +3,7 @@ sonar.organization=crafty-controller # This is the name and version displayed in the SonarCloud UI. sonar.projectName=Crafty 4 -sonar.projectVersion=4.2.4 +sonar.projectVersion=4.3.0 sonar.python.version=3.9, 3.10, 3.11 sonar.exclusions=app/migrations/**, app/frontend/static/assets/vendors/** From 2c289133f5e83f95547409b76e46cf608e97bcae Mon Sep 17 00:00:00 2001 From: Silversthorn Date: Sun, 3 Mar 2024 10:00:18 +0100 Subject: [PATCH 13/13] Fixing logging errors to info to display properly logs Remove return true/false values as the function shouldn't return values --- .../20240216_rework_servers_uuid.py | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/app/migrations/20240216_rework_servers_uuid.py b/app/migrations/20240216_rework_servers_uuid.py index ccfce915..c741a84c 100644 --- a/app/migrations/20240216_rework_servers_uuid.py +++ b/app/migrations/20240216_rework_servers_uuid.py @@ -99,11 +99,11 @@ def migrate(migrator: Migrator, database, **kwargs): Console.error(ex) last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) last_migration.delete() - return False + return try: - logger.error("Migrating Data from Int to UUID (Foreign Keys)") - Console.error("Migrating Data from Int to UUID (Foreign Keys)") + logger.info("Migrating Data from Int to UUID (Foreign Keys)") + Console.info("Migrating Data from Int to UUID (Foreign Keys)") # Changes on Audit Log Table for audit_log in AuditLog.select(): old_server_id = audit_log.server_id_id @@ -168,8 +168,8 @@ def migrate(migrator: Migrator, database, **kwargs): and RoleServers.server_id == old_server_id ).execute() - logger.error("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") - Console.error("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") + logger.info("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") + Console.info("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS") except Exception as ex: logger.error("Error while migrating Data from Int to UUID (Foreign Keys)") @@ -178,19 +178,19 @@ def migrate(migrator: Migrator, database, **kwargs): Console.error(ex) last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) last_migration.delete() - return False + return try: - logger.error("Migrating Data from Int to UUID (Primary Keys)") - Console.error("Migrating Data from Int to UUID (Primary Keys)") + logger.info("Migrating Data from Int to UUID (Primary Keys)") + Console.info("Migrating Data from Int to UUID (Primary Keys)") # Migrating servers from the old id type to the new one for server in Servers.select(): Servers.update(server_id=server.server_uuid).where( Servers.server_id == server.server_id ).execute() - logger.error("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") - Console.error("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") + logger.info("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") + Console.info("Migrating Data from Int to UUID (Primary Keys) : SUCCESS") except Exception as ex: logger.error("Error while migrating Data from Int to UUID (Primary Keys)") @@ -199,21 +199,21 @@ def migrate(migrator: Migrator, database, **kwargs): Console.error(ex) last_migration = MigrateHistory.get_by_id(MigrateHistory.select().count()) last_migration.delete() - return False + return # Changes on Server Table - logger.error("Migrating Data from Int to UUID (Removing UUID Field from Servers)") - Console.error("Migrating Data from Int to UUID (Removing UUID Field from Servers)") + logger.info("Migrating Data from Int to UUID (Removing UUID Field from Servers)") + Console.info("Migrating Data from Int to UUID (Removing UUID Field from Servers)") migrator.drop_columns("servers", ["server_uuid"]) migrator.run() - logger.error( + logger.info( "Migrating Data from Int to UUID (Removing UUID Field from Servers) : SUCCESS" ) - Console.error( + Console.info( "Migrating Data from Int to UUID (Removing UUID Field from Servers) : SUCCESS" ) - return True + return def rollback(migrator: Migrator, database, **kwargs):