From 14973c4d986117d0817dbea72c33d4f1f94e5b80 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Sun, 18 Aug 2024 19:35:43 +0200 Subject: [PATCH] refactor: tweak client setup and removal Signed-off-by: Dominik Willner --- kiauh/components/webui_client/client_setup.py | 2 +- kiauh/components/webui_client/client_utils.py | 17 ++++++++--------- kiauh/utils/fs_utils.py | 8 ++++---- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/kiauh/components/webui_client/client_setup.py b/kiauh/components/webui_client/client_setup.py index 22f6a2b..593911a 100644 --- a/kiauh/components/webui_client/client_setup.py +++ b/kiauh/components/webui_client/client_setup.py @@ -144,7 +144,7 @@ def install_client(client: BaseWebClient) -> None: ) if kl_instances: - symlink_webui_nginx_log(kl_instances) + symlink_webui_nginx_log(client, kl_instances) cmd_sysctl_service("nginx", "restart") except Exception as e: diff --git a/kiauh/components/webui_client/client_utils.py b/kiauh/components/webui_client/client_utils.py index 332721c..00fdc96 100644 --- a/kiauh/components/webui_client/client_utils.py +++ b/kiauh/components/webui_client/client_utils.py @@ -9,7 +9,6 @@ from __future__ import annotations import json -from pathlib import Path from typing import List, get_args from components.klipper.klipper import Klipper @@ -95,17 +94,19 @@ def enable_mainsail_remotemode() -> None: Logger.print_ok("Mainsails remote mode enabled!") -def symlink_webui_nginx_log(klipper_instances: List[Klipper]) -> None: +def symlink_webui_nginx_log( + client: BaseWebClient, klipper_instances: List[Klipper] +) -> None: Logger.print_status("Link NGINX logs into log directory ...") - access_log = Path("/var/log/nginx/mainsail-access.log") - error_log = Path("/var/log/nginx/mainsail-error.log") + access_log = client.nginx_access_log + error_log = client.nginx_error_log for instance in klipper_instances: - desti_access = instance.log_dir.joinpath("mainsail-access.log") + desti_access = instance.log_dir.joinpath(access_log.name) if not desti_access.exists(): desti_access.symlink_to(access_log) - desti_error = instance.log_dir.joinpath("mainsail-error.log") + desti_error = instance.log_dir.joinpath(error_log.name) if not desti_error.exists(): desti_error.symlink_to(error_log) @@ -146,9 +147,7 @@ def backup_client_data(client: BaseWebClient) -> None: bm = BackupManager() bm.backup_directory(f"{name}-{version}", src, dest) - if name == "mainsail": - c_json = MainsailData().client_dir.joinpath("config.json") - bm.backup_file(c_json, dest) + bm.backup_file(client.config_file, dest) bm.backup_file(NGINX_SITES_AVAILABLE.joinpath(name), dest) diff --git a/kiauh/utils/fs_utils.py b/kiauh/utils/fs_utils.py index d2b2dcc..397cf4d 100644 --- a/kiauh/utils/fs_utils.py +++ b/kiauh/utils/fs_utils.py @@ -80,23 +80,23 @@ def remove_file(file_path: Path, sudo=False) -> None: def run_remove_routines(file: Path) -> None: try: - if not file.exists(): + if not file.is_symlink() and not file.exists(): Logger.print_info(f"File '{file}' does not exist. Skipped ...") return if file.is_dir(): shutil.rmtree(file) - elif file.is_file(): + elif file.is_file() or file.is_symlink(): file.unlink() else: raise OSError(f"File '{file}' is neither a file nor a directory!") - Logger.print_ok("Successfully removed!") + Logger.print_ok(f"File '{file}' was successfully removed!") except OSError as e: Logger.print_error(f"Unable to delete '{file}':\n{e}") try: Logger.print_info("Trying to remove with sudo ...") remove_with_sudo(file) - Logger.print_ok("Successfully removed!") + Logger.print_ok(f"File '{file}' was successfully removed!") except CalledProcessError as e: Logger.print_error(f"Error deleting '{file}' with sudo:\n{e}") Logger.print_error("Remove this directory manually!")