refactor(Mainsail): remove specific methods and replace by generic ones

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2023-12-22 22:54:29 +01:00
parent edd5f5c6fd
commit 3d5e83d5ab
2 changed files with 7 additions and 44 deletions

View File

@@ -39,15 +39,15 @@ from kiauh.modules.mainsail.mainsail_utils import (
symlink_webui_nginx_log,
)
from kiauh.modules.moonraker.moonraker import Moonraker
from kiauh.utils import NGINX_SITES_AVAILABLE, NGINX_SITES_ENABLED
from kiauh.utils.common import check_install_dependencies
from kiauh.utils.filesystem_utils import (
unzip,
copy_upstream_nginx_cfg,
copy_common_vars_nginx_cfg,
delete_default_nginx_cfg,
create_nginx_cfg,
enable_nginx_cfg,
create_symlink,
remove_file,
)
from kiauh.utils.input_utils import get_confirm, get_number_input
from kiauh.utils.logger import Logger
@@ -184,12 +184,14 @@ def create_mainsail_cfg_symlink(klipper_instances: List[Klipper]) -> None:
def create_mainsail_nginx_cfg(port: int) -> None:
root_dir = MAINSAIL_DIR
source = Path(NGINX_SITES_AVAILABLE, "mainsail")
target = Path(NGINX_SITES_ENABLED, "mainsail")
try:
Logger.print_status("Creating NGINX config for Mainsail ...")
root_dir = MAINSAIL_DIR
delete_default_nginx_cfg()
remove_file(Path("/etc/nginx/sites-enabled/default"), True)
create_nginx_cfg("mainsail", port, root_dir)
enable_nginx_cfg("mainsail")
create_symlink(source, target, True)
set_nginx_permissions()
Logger.print_ok("NGINX config for Mainsail successfully created.")
except Exception:

View File

@@ -16,7 +16,6 @@ from zipfile import ZipFile
from kiauh.utils import (
NGINX_SITES_AVAILABLE,
NGINX_SITES_ENABLED,
MODULE_PATH,
NGINX_CONFD,
)
@@ -150,41 +149,3 @@ def create_nginx_cfg(name: str, port: int, root_dir: str) -> None:
log = f"Unable to create '{target}': {e.stderr.decode()}"
Logger.print_error(log)
raise
def delete_default_nginx_cfg() -> None:
"""
Deletes a default NGINX config
:return: None
"""
default_cfg = Path("/etc/nginx/sites-enabled/default")
if not check_file_exist(default_cfg, True):
return
try:
command = ["sudo", "rm", default_cfg]
subprocess.run(command, stderr=subprocess.PIPE, check=True)
except subprocess.CalledProcessError as e:
log = f"Unable to delete '{default_cfg}': {e.stderr.decode()}"
Logger.print_error(log)
raise
def enable_nginx_cfg(name: str) -> None:
"""
Helper method to enable an NGINX config |
:param name: name of the config to enable
:return: None
"""
source = os.path.join(NGINX_SITES_AVAILABLE, name)
target = os.path.join(NGINX_SITES_ENABLED, name)
if check_file_exist(Path(target), True):
return
try:
command = ["sudo", "ln", "-s", source, target]
subprocess.run(command, stderr=subprocess.PIPE, check=True)
except subprocess.CalledProcessError as e:
log = f"Unable to create symlink: {e.stderr.decode()}"
Logger.print_error(log)
raise