Remove tempdir from packaging logs.

This commit is contained in:
amcmanu3 2022-07-05 21:35:43 -04:00
parent 50523cead3
commit f1d11bfb0d
5 changed files with 47 additions and 21 deletions

1
.gitignore vendored
View File

@ -20,6 +20,7 @@ venv.bak/
.idea/
/servers/
/backups/
/temp/
/docker/servers/
/docker/backups/
session.lock

View File

@ -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}"
)

View File

@ -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"

View File

@ -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")

View File

@ -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