mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-24 00:03:42 +05:00
refactor(Mainsail): refactor methods for removing and checking files
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -41,7 +41,8 @@ def run_mainsail_removal(
|
|||||||
if remove_mr_updater_section:
|
if remove_mr_updater_section:
|
||||||
remove_updater_section("update_manager mainsail")
|
remove_updater_section("update_manager mainsail")
|
||||||
if remove_ms_config:
|
if remove_ms_config:
|
||||||
remove_ms_config_dir()
|
remove_mainsail_cfg_dir()
|
||||||
|
remove_mainsail_cfg_symlink()
|
||||||
if remove_mr_updater_section:
|
if remove_mr_updater_section:
|
||||||
remove_updater_section("update_manager mainsail-config")
|
remove_updater_section("update_manager mainsail-config")
|
||||||
if remove_msc_printer_cfg_include:
|
if remove_msc_printer_cfg_include:
|
||||||
@@ -63,8 +64,8 @@ def remove_mainsail_dir() -> None:
|
|||||||
def remove_nginx_config() -> None:
|
def remove_nginx_config() -> None:
|
||||||
Logger.print_status("Removing Mainsails NGINX config ...")
|
Logger.print_status("Removing Mainsails NGINX config ...")
|
||||||
try:
|
try:
|
||||||
remove_file(Path(NGINX_SITES_AVAILABLE).joinpath("mainsail"), True)
|
remove_file(Path(NGINX_SITES_AVAILABLE, "mainsail"), True)
|
||||||
remove_file(Path(NGINX_SITES_ENABLED).joinpath("mainsail"), True)
|
remove_file(Path(NGINX_SITES_ENABLED, "mainsail"), True)
|
||||||
|
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
log = f"Unable to remove Mainsail NGINX config:\n{e.stderr.decode()}"
|
log = f"Unable to remove Mainsail NGINX config:\n{e.stderr.decode()}"
|
||||||
@@ -82,8 +83,8 @@ def remove_nginx_logs() -> None:
|
|||||||
return
|
return
|
||||||
|
|
||||||
for instance in im.instances:
|
for instance in im.instances:
|
||||||
remove_file(Path(instance.log_dir).joinpath("mainsail-access.log"))
|
remove_file(Path(instance.log_dir, "mainsail-access.log"))
|
||||||
remove_file(Path(instance.log_dir).joinpath("mainsail-error.log"))
|
remove_file(Path(instance.log_dir, "mainsail-error.log"))
|
||||||
|
|
||||||
except (OSError, subprocess.CalledProcessError) as e:
|
except (OSError, subprocess.CalledProcessError) as e:
|
||||||
Logger.print_error(f"Unable to NGINX logs:\n{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()
|
cm.write_config()
|
||||||
|
|
||||||
|
|
||||||
def remove_ms_config_dir() -> None:
|
def remove_mainsail_cfg_dir() -> None:
|
||||||
Logger.print_status("Removing mainsail-config ...")
|
Logger.print_status("Removing mainsail-config ...")
|
||||||
if not Path(MAINSAIL_CONFIG_DIR).exists():
|
if not Path(MAINSAIL_CONFIG_DIR).exists():
|
||||||
Logger.print_info(f"'{MAINSAIL_CONFIG_DIR}' does not exist. Skipping ...")
|
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}")
|
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:
|
def remove_printer_cfg_include() -> None:
|
||||||
Logger.print_status("Remove mainsail-config include from printer.cfg ...")
|
Logger.print_status("Remove mainsail-config include from printer.cfg ...")
|
||||||
im = InstanceManager(Klipper)
|
im = InstanceManager(Klipper)
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ from kiauh.modules.moonraker.moonraker import Moonraker
|
|||||||
from kiauh.utils.common import check_install_dependencies
|
from kiauh.utils.common import check_install_dependencies
|
||||||
from kiauh.utils.filesystem_utils import (
|
from kiauh.utils.filesystem_utils import (
|
||||||
unzip,
|
unzip,
|
||||||
create_upstream_nginx_cfg,
|
copy_upstream_nginx_cfg,
|
||||||
create_common_vars_nginx_cfg,
|
copy_common_vars_nginx_cfg,
|
||||||
delete_default_nginx_cfg,
|
delete_default_nginx_cfg,
|
||||||
create_nginx_cfg,
|
create_nginx_cfg,
|
||||||
enable_nginx_cfg,
|
enable_nginx_cfg,
|
||||||
@@ -130,8 +130,8 @@ def run_mainsail_installation() -> None:
|
|||||||
patch_printer_config(im_kl.instances)
|
patch_printer_config(im_kl.instances)
|
||||||
im_kl.restart_all_instance()
|
im_kl.restart_all_instance()
|
||||||
|
|
||||||
create_upstream_nginx_cfg()
|
copy_upstream_nginx_cfg()
|
||||||
create_common_vars_nginx_cfg()
|
copy_common_vars_nginx_cfg()
|
||||||
create_mainsail_nginx_cfg(mainsail_port)
|
create_mainsail_nginx_cfg(mainsail_port)
|
||||||
if is_klipper_installed:
|
if is_klipper_installed:
|
||||||
symlink_webui_nginx_log(im_kl.instances)
|
symlink_webui_nginx_log(im_kl.instances)
|
||||||
|
|||||||
@@ -181,9 +181,9 @@ def install_moonraker_packages(moonraker_dir: Path) -> None:
|
|||||||
def install_moonraker_polkit() -> None:
|
def install_moonraker_polkit() -> None:
|
||||||
Logger.print_status("Installing Moonraker policykit rules ...")
|
Logger.print_status("Installing Moonraker policykit rules ...")
|
||||||
|
|
||||||
legacy_file_exists = check_file_exist(Path(POLKIT_LEGACY_FILE))
|
legacy_file_exists = check_file_exist(Path(POLKIT_LEGACY_FILE), True)
|
||||||
polkit_file_exists = check_file_exist(Path(POLKIT_FILE))
|
polkit_file_exists = check_file_exist(Path(POLKIT_FILE), True)
|
||||||
usr_file_exists = check_file_exist(Path(POLKIT_USR_FILE))
|
usr_file_exists = check_file_exist(Path(POLKIT_USR_FILE), True)
|
||||||
|
|
||||||
if legacy_file_exists or (polkit_file_exists and usr_file_exists):
|
if legacy_file_exists or (polkit_file_exists and usr_file_exists):
|
||||||
Logger.print_info("Moonraker policykit rules are already installed.")
|
Logger.print_info("Moonraker policykit rules are already installed.")
|
||||||
|
|||||||
@@ -23,19 +23,25 @@ from kiauh.utils import (
|
|||||||
from kiauh.utils.logger import Logger
|
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
|
Helper function for checking the existence of a file |
|
||||||
elevated permissions are required |
|
|
||||||
:param file_path: the absolute path of the file to check
|
: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:
|
if sudo:
|
||||||
command = ["sudo", "find", file_path]
|
try:
|
||||||
subprocess.check_output(command, stderr=subprocess.DEVNULL)
|
command = ["sudo", "find", file_path]
|
||||||
return True
|
subprocess.check_output(command, stderr=subprocess.DEVNULL)
|
||||||
except subprocess.CalledProcessError:
|
return True
|
||||||
return False
|
except subprocess.CalledProcessError:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
if Path(file_path).exists():
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def create_directory(_dir: Path) -> None:
|
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:
|
def remove_file(file_path: Path, sudo=False) -> None:
|
||||||
try:
|
try:
|
||||||
command = f"{'sudo ' if sudo else ''}rm -f {file_path}"
|
cmd = f"{'sudo ' if sudo else ''}rm -f {file_path}"
|
||||||
subprocess.run(command, stderr=subprocess.PIPE, check=True, shell=True)
|
subprocess.run(cmd, stderr=subprocess.PIPE, check=True, shell=True)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
log = f"Cannot remove file {file_path}: {e.stderr.decode()}"
|
log = f"Cannot remove file {file_path}: {e.stderr.decode()}"
|
||||||
Logger.print_error(log)
|
Logger.print_error(log)
|
||||||
@@ -85,7 +91,7 @@ def unzip(file: str, target_dir: str) -> None:
|
|||||||
_zip.extractall(target_dir)
|
_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
|
Creates an upstream.conf in /etc/nginx/conf.d
|
||||||
:return: None
|
:return: None
|
||||||
@@ -101,7 +107,7 @@ def create_upstream_nginx_cfg() -> None:
|
|||||||
raise
|
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
|
Creates a common_vars.conf in /etc/nginx/conf.d
|
||||||
:return: None
|
:return: None
|
||||||
@@ -152,7 +158,7 @@ def delete_default_nginx_cfg() -> None:
|
|||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
default_cfg = Path("/etc/nginx/sites-enabled/default")
|
default_cfg = Path("/etc/nginx/sites-enabled/default")
|
||||||
if not check_file_exist(default_cfg):
|
if not check_file_exist(default_cfg, True):
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -172,7 +178,7 @@ def enable_nginx_cfg(name: str) -> None:
|
|||||||
"""
|
"""
|
||||||
source = os.path.join(NGINX_SITES_AVAILABLE, name)
|
source = os.path.join(NGINX_SITES_AVAILABLE, name)
|
||||||
target = os.path.join(NGINX_SITES_ENABLED, name)
|
target = os.path.join(NGINX_SITES_ENABLED, name)
|
||||||
if check_file_exist(Path(target)):
|
if check_file_exist(Path(target), True):
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -19,15 +19,7 @@ import urllib.error
|
|||||||
import urllib.request
|
import urllib.request
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Literal
|
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.input_utils import get_confirm
|
||||||
from kiauh.utils.logger import Logger
|
from kiauh.utils.logger import Logger
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user