diff --git a/kiauh/modules/mainsail/mainsail_remove.py b/kiauh/modules/mainsail/mainsail_remove.py index 525ad3f..d09fdb6 100644 --- a/kiauh/modules/mainsail/mainsail_remove.py +++ b/kiauh/modules/mainsail/mainsail_remove.py @@ -41,7 +41,8 @@ def run_mainsail_removal( if remove_mr_updater_section: remove_updater_section("update_manager mainsail") if remove_ms_config: - remove_ms_config_dir() + remove_mainsail_cfg_dir() + remove_mainsail_cfg_symlink() if remove_mr_updater_section: remove_updater_section("update_manager mainsail-config") if remove_msc_printer_cfg_include: @@ -63,8 +64,8 @@ def remove_mainsail_dir() -> None: def remove_nginx_config() -> None: Logger.print_status("Removing Mainsails NGINX config ...") try: - remove_file(Path(NGINX_SITES_AVAILABLE).joinpath("mainsail"), True) - remove_file(Path(NGINX_SITES_ENABLED).joinpath("mainsail"), True) + remove_file(Path(NGINX_SITES_AVAILABLE, "mainsail"), True) + remove_file(Path(NGINX_SITES_ENABLED, "mainsail"), True) except subprocess.CalledProcessError as e: log = f"Unable to remove Mainsail NGINX config:\n{e.stderr.decode()}" @@ -82,8 +83,8 @@ def remove_nginx_logs() -> None: return for instance in im.instances: - remove_file(Path(instance.log_dir).joinpath("mainsail-access.log")) - remove_file(Path(instance.log_dir).joinpath("mainsail-error.log")) + remove_file(Path(instance.log_dir, "mainsail-access.log")) + remove_file(Path(instance.log_dir, "mainsail-error.log")) except (OSError, subprocess.CalledProcessError) as e: Logger.print_error(f"Unable to NGINX logs:\n{e}") @@ -114,7 +115,7 @@ def remove_updater_section(name: str) -> None: cm.write_config() -def remove_ms_config_dir() -> None: +def remove_mainsail_cfg_dir() -> None: Logger.print_status("Removing mainsail-config ...") if not Path(MAINSAIL_CONFIG_DIR).exists(): Logger.print_info(f"'{MAINSAIL_CONFIG_DIR}' does not exist. Skipping ...") @@ -126,6 +127,17 @@ def remove_ms_config_dir() -> None: Logger.print_error(f"Unable to delete '{MAINSAIL_CONFIG_DIR}':\n{e}") +def remove_mainsail_cfg_symlink() -> None: + Logger.print_status("Removing mainsail.cfg symlinks ...") + im = InstanceManager(Moonraker) + for instance in im.instances: + Logger.print_status(f"Removing symlink from '{instance.cfg_dir}' ...") + try: + remove_file(Path(instance.cfg_dir, "mainsail.cfg")) + except subprocess.CalledProcessError: + Logger.print_error("Failed to remove symlink!") + + def remove_printer_cfg_include() -> None: Logger.print_status("Remove mainsail-config include from printer.cfg ...") im = InstanceManager(Klipper) diff --git a/kiauh/modules/mainsail/mainsail_setup.py b/kiauh/modules/mainsail/mainsail_setup.py index 0970251..5558137 100644 --- a/kiauh/modules/mainsail/mainsail_setup.py +++ b/kiauh/modules/mainsail/mainsail_setup.py @@ -42,8 +42,8 @@ from kiauh.modules.moonraker.moonraker import Moonraker from kiauh.utils.common import check_install_dependencies from kiauh.utils.filesystem_utils import ( unzip, - create_upstream_nginx_cfg, - create_common_vars_nginx_cfg, + copy_upstream_nginx_cfg, + copy_common_vars_nginx_cfg, delete_default_nginx_cfg, create_nginx_cfg, enable_nginx_cfg, @@ -130,8 +130,8 @@ def run_mainsail_installation() -> None: patch_printer_config(im_kl.instances) im_kl.restart_all_instance() - create_upstream_nginx_cfg() - create_common_vars_nginx_cfg() + copy_upstream_nginx_cfg() + copy_common_vars_nginx_cfg() create_mainsail_nginx_cfg(mainsail_port) if is_klipper_installed: symlink_webui_nginx_log(im_kl.instances) diff --git a/kiauh/modules/moonraker/moonraker_setup.py b/kiauh/modules/moonraker/moonraker_setup.py index 4f5a2a3..cde127e 100644 --- a/kiauh/modules/moonraker/moonraker_setup.py +++ b/kiauh/modules/moonraker/moonraker_setup.py @@ -181,9 +181,9 @@ def install_moonraker_packages(moonraker_dir: Path) -> None: def install_moonraker_polkit() -> None: Logger.print_status("Installing Moonraker policykit rules ...") - legacy_file_exists = check_file_exist(Path(POLKIT_LEGACY_FILE)) - polkit_file_exists = check_file_exist(Path(POLKIT_FILE)) - usr_file_exists = check_file_exist(Path(POLKIT_USR_FILE)) + legacy_file_exists = check_file_exist(Path(POLKIT_LEGACY_FILE), True) + polkit_file_exists = check_file_exist(Path(POLKIT_FILE), True) + usr_file_exists = check_file_exist(Path(POLKIT_USR_FILE), True) if legacy_file_exists or (polkit_file_exists and usr_file_exists): Logger.print_info("Moonraker policykit rules are already installed.") diff --git a/kiauh/utils/filesystem_utils.py b/kiauh/utils/filesystem_utils.py index ad4e868..721290c 100644 --- a/kiauh/utils/filesystem_utils.py +++ b/kiauh/utils/filesystem_utils.py @@ -23,19 +23,25 @@ from kiauh.utils import ( from kiauh.utils.logger import Logger -def check_file_exist(file_path: Path) -> bool: +def check_file_exist(file_path: Path, sudo=False) -> bool: """ - Helper function for checking the existence of a file where - elevated permissions are required | + Helper function for checking the existence of a file | :param file_path: the absolute path of the file to check - :return: True if file exists, otherwise False + :param sudo: use sudo if required + :return: True, if file exists, otherwise False """ - try: - command = ["sudo", "find", file_path] - subprocess.check_output(command, stderr=subprocess.DEVNULL) - return True - except subprocess.CalledProcessError: - return False + if sudo: + try: + command = ["sudo", "find", file_path] + subprocess.check_output(command, stderr=subprocess.DEVNULL) + return True + except subprocess.CalledProcessError: + return False + else: + if Path(file_path).exists(): + return True + else: + return False def create_directory(_dir: Path) -> None: @@ -66,8 +72,8 @@ def create_symlink(source: Path, target: Path, sudo=False) -> None: def remove_file(file_path: Path, sudo=False) -> None: try: - command = f"{'sudo ' if sudo else ''}rm -f {file_path}" - subprocess.run(command, stderr=subprocess.PIPE, check=True, shell=True) + cmd = f"{'sudo ' if sudo else ''}rm -f {file_path}" + subprocess.run(cmd, stderr=subprocess.PIPE, check=True, shell=True) except subprocess.CalledProcessError as e: log = f"Cannot remove file {file_path}: {e.stderr.decode()}" Logger.print_error(log) @@ -85,7 +91,7 @@ def unzip(file: str, target_dir: str) -> None: _zip.extractall(target_dir) -def create_upstream_nginx_cfg() -> None: +def copy_upstream_nginx_cfg() -> None: """ Creates an upstream.conf in /etc/nginx/conf.d :return: None @@ -101,7 +107,7 @@ def create_upstream_nginx_cfg() -> None: raise -def create_common_vars_nginx_cfg() -> None: +def copy_common_vars_nginx_cfg() -> None: """ Creates a common_vars.conf in /etc/nginx/conf.d :return: None @@ -152,7 +158,7 @@ def delete_default_nginx_cfg() -> None: :return: None """ default_cfg = Path("/etc/nginx/sites-enabled/default") - if not check_file_exist(default_cfg): + if not check_file_exist(default_cfg, True): return try: @@ -172,7 +178,7 @@ def enable_nginx_cfg(name: str) -> None: """ source = os.path.join(NGINX_SITES_AVAILABLE, name) target = os.path.join(NGINX_SITES_ENABLED, name) - if check_file_exist(Path(target)): + if check_file_exist(Path(target), True): return try: diff --git a/kiauh/utils/system_utils.py b/kiauh/utils/system_utils.py index 4925707..0b07655 100644 --- a/kiauh/utils/system_utils.py +++ b/kiauh/utils/system_utils.py @@ -19,15 +19,7 @@ import urllib.error import urllib.request from pathlib import Path from typing import List, Literal -from zipfile import ZipFile -from kiauh.utils import ( - NGINX_CONFD, - MODULE_PATH, - NGINX_SITES_AVAILABLE, - NGINX_SITES_ENABLED, -) -from kiauh.utils.filesystem_utils import check_file_exist from kiauh.utils.input_utils import get_confirm from kiauh.utils.logger import Logger