From f1d11bfb0d943c737ef2c4ef77bd0bfc9bcf83ba Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Tue, 5 Jul 2022 21:35:43 -0400 Subject: [PATCH] Remove tempdir from packaging logs. --- .gitignore | 1 + app/classes/shared/file_helpers.py | 4 +-- app/classes/shared/main_controller.py | 47 ++++++++++++++++----------- app/classes/shared/tasks.py | 10 ++++++ main.py | 6 ++++ 5 files changed, 47 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 0c17d6c7..02a40213 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ venv.bak/ .idea/ /servers/ /backups/ +/temp/ /docker/servers/ /docker/backups/ session.lock diff --git a/app/classes/shared/file_helpers.py b/app/classes/shared/file_helpers.py index 620b34f8..5cd38bbf 100644 --- a/app/classes/shared/file_helpers.py +++ b/app/classes/shared/file_helpers.py @@ -101,7 +101,7 @@ class FileHelpers: ziproot = path_to_zip for file in files: try: - logger.info(f"backing up: {os.path.join(root, file)}") + logger.info(f"packaging: {os.path.join(root, file)}") if os.name == "nt": zip_file.write( os.path.join(root, file), @@ -115,7 +115,7 @@ class FileHelpers: except Exception as e: logger.warning( - f"Error backing up: {os.path.join(root, file)}!" + f"Error packaging: {os.path.join(root, file)}!" f" - Error was: {e}" ) diff --git a/app/classes/shared/main_controller.py b/app/classes/shared/main_controller.py index 692ca79d..2c64f7cd 100644 --- a/app/classes/shared/main_controller.py +++ b/app/classes/shared/main_controller.py @@ -4,7 +4,6 @@ from pathlib import Path import shutil import time import logging -import tempfile from peewee import DoesNotExist # TZLocal is set as a hidden import on win pipeline @@ -85,27 +84,38 @@ class Controller: self.users.set_prepare(exec_user["user_id"]) logger.info("Checking for previous support logs.") if exec_user["support_logs"] != "": - logger.info( - f"Found previous support log request at {exec_user['support_logs']}" - ) - if self.helper.validate_traversal( - tempfile.gettempdir(), exec_user["support_logs"] - ): - logger.debug("No transversal detected. Going for the delete.") - self.del_support_file(exec_user["support_logs"]) + if os.path.exists(exec_user["support_logs"]): + logger.info( + f"Found previous support log request at {exec_user['support_logs']}" + ) + if self.helper.validate_traversal( + os.path.join(self.project_root, "temp"), exec_user["support_logs"] + ): + logger.debug("No transversal detected. Going for the delete.") + self.del_support_file(exec_user["support_logs"]) # pausing so on screen notifications can run for user time.sleep(7) self.helper.websocket_helper.broadcast_user( exec_user["user_id"], "notification", "Preparing your support logs" ) - temp_dir = tempfile.mkdtemp() - temp_zip_storage = tempfile.mkdtemp() - full_temp = os.path.join(temp_dir, "support_logs") - os.mkdir(full_temp) + self.helper.ensure_dir_exists( + os.path.join(self.project_root, "temp", str(exec_user["user_id"])) + ) + temp_dir = os.path.join( + self.project_root, "temp", str(exec_user["user_id"]), "support_logs" + ) + + self.helper.ensure_dir_exists( + os.path.join(self.project_root, "temp", str(exec_user["user_id"]), "zip") + ) + temp_zip_storage = os.path.join( + self.project_root, "temp", str(exec_user["user_id"]), "zip" + ) + os.mkdir(temp_dir) temp_zip_storage = os.path.join(temp_zip_storage, "support_logs") - crafty_path = os.path.join(full_temp, "crafty") + crafty_path = os.path.join(temp_dir, "crafty") os.mkdir(crafty_path) - server_path = os.path.join(full_temp, "server") + server_path = os.path.join(temp_dir, "server") os.mkdir(server_path) if exec_user["superuser"]: defined_servers = self.servers.list_defined_servers() @@ -160,15 +170,14 @@ class Controller: "interval", seconds=1, id="logs_" + str(exec_user["user_id"]), - args=[full_temp, temp_zip_storage + ".zip", exec_user], + args=[temp_dir, temp_zip_storage + ".zip", exec_user], ) - FileHelpers.make_archive(temp_zip_storage, temp_dir) - + FileHelpers.make_compressed_archive(temp_zip_storage, temp_dir) if len(self.helper.websocket_helper.clients) > 0: self.helper.websocket_helper.broadcast_user( exec_user["user_id"], "support_status_update", - Helpers.calc_percent(full_temp, temp_zip_storage + ".zip"), + Helpers.calc_percent(temp_dir, temp_zip_storage + ".zip"), ) temp_zip_storage += ".zip" diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index d64adb0b..e9646fca 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -15,6 +15,7 @@ from app.classes.models.management import HelpersManagement from app.classes.models.users import HelperUsers from app.classes.controllers.users_controller import UsersController from app.classes.shared.console import Console +from app.classes.shared.file_helpers import FileHelpers from app.classes.shared.helpers import Helpers from app.classes.shared.main_controller import Controller from app.classes.web.tornado_handler import Webserver @@ -145,6 +146,15 @@ class TasksManager: self.controller.servers.stop_all_servers() except: logger.info("Caught error during shutdown", exc_info=True) + try: + temp_dir = os.path.join(self.controller.project_root, "temp") + FileHelpers.del_dirs(temp_dir) + except: + logger.info( + "Caught error during shutdown - " + "unable to delete files from Crafty Temp Dir", + exc_info=True, + ) logger.info("***** Crafty Shutting Down *****\n\n") Console.info("***** Crafty Shutting Down *****\n\n") diff --git a/main.py b/main.py index 5dfbed38..45fd67f2 100644 --- a/main.py +++ b/main.py @@ -226,6 +226,12 @@ if __name__ == "__main__": Console.info("Crafty has fully started and is now ready for use!") crafty_prompt.prompt = f"Crafty Controller v{helper.get_version_string()} > " + try: + logger.info("Removing old temp dirs") + FileHelpers.del_dirs(os.path.join(controller.project_root, "temp")) + except: + logger.info("Did not find old temp dir.") + os.mkdir(os.path.join(controller.project_root, "temp")) if not args.daemon: # Put the prompt under the cursor