From b3df3e7b5cf93c8e02b948e17e4fada668a41422 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Mon, 20 May 2024 11:35:43 +0200 Subject: [PATCH] refactor: improve nginx config generation Signed-off-by: Dominik Willner --- kiauh/components/webui_client/__init__.py | 12 ++++++ .../webui_client}/assets/nginx_cfg | 0 kiauh/components/webui_client/client_setup.py | 32 +++++--------- kiauh/utils/fs_utils.py | 42 +++++++++++++++---- 4 files changed, 56 insertions(+), 30 deletions(-) rename kiauh/{utils => components/webui_client}/assets/nginx_cfg (100%) diff --git a/kiauh/components/webui_client/__init__.py b/kiauh/components/webui_client/__init__.py index e69de29..371c365 100644 --- a/kiauh/components/webui_client/__init__.py +++ b/kiauh/components/webui_client/__init__.py @@ -0,0 +1,12 @@ +# ======================================================================= # +# Copyright (C) 2020 - 2024 Dominik Willner # +# # +# This file is part of KIAUH - Klipper Installation And Update Helper # +# https://github.com/dw-0/kiauh # +# # +# This file may be distributed under the terms of the GNU GPLv3 license # +# ======================================================================= # + +from pathlib import Path + +MODULE_PATH = Path(__file__).resolve().parent diff --git a/kiauh/utils/assets/nginx_cfg b/kiauh/components/webui_client/assets/nginx_cfg similarity index 100% rename from kiauh/utils/assets/nginx_cfg rename to kiauh/components/webui_client/assets/nginx_cfg diff --git a/kiauh/components/webui_client/client_setup.py b/kiauh/components/webui_client/client_setup.py index cb27075..bb6e10e 100644 --- a/kiauh/components/webui_client/client_setup.py +++ b/kiauh/components/webui_client/client_setup.py @@ -12,6 +12,7 @@ from typing import List from components.klipper.klipper import Klipper from components.moonraker.moonraker import Moonraker +from components.webui_client import MODULE_PATH from components.webui_client.base_data import ( BaseWebClient, BaseWebClientConfig, @@ -34,18 +35,15 @@ from components.webui_client.client_utils import ( ) from core.instance_manager.instance_manager import InstanceManager from core.settings.kiauh_settings import KiauhSettings -from utils import NGINX_SITES_AVAILABLE, NGINX_SITES_ENABLED from utils.common import check_install_dependencies from utils.config_utils import add_config_section from utils.fs_utils import ( copy_common_vars_nginx_cfg, copy_upstream_nginx_cfg, create_nginx_cfg, - create_symlink, get_next_free_port, is_valid_port, read_ports_from_nginx_configs, - remove_file, unzip, ) from utils.input_utils import get_confirm, get_number_input @@ -54,7 +52,6 @@ from utils.sys_utils import ( cmd_sysctl_service, download_file, get_ipv4_addr, - set_nginx_permissions, ) @@ -141,7 +138,15 @@ def install_client(client: BaseWebClient) -> None: copy_upstream_nginx_cfg() copy_common_vars_nginx_cfg() - create_client_nginx_cfg(client, port) + create_nginx_cfg( + display_name=client.display_name, + cfg_name=client.name, + template_src=MODULE_PATH.joinpath("assets/nginx_cfg"), + PORT=port, + ROOT_DIR=client.client_dir, + NAME=client.name, + ) + if kl_instances: symlink_webui_nginx_log(kl_instances) cmd_sysctl_service("nginx", "restart") @@ -190,20 +195,3 @@ def update_client(client: BaseWebClient) -> None: if client.client == WebClientType.MAINSAIL: restore_mainsail_config_json() - - -def create_client_nginx_cfg(client: BaseWebClient, port: int) -> None: - display_name = client.display_name - root_dir = client.client_dir - source = NGINX_SITES_AVAILABLE.joinpath(client.name) - target = NGINX_SITES_ENABLED.joinpath(client.name) - try: - Logger.print_status(f"Creating NGINX config for {display_name} ...") - remove_file(Path("/etc/nginx/sites-enabled/default"), True) - create_nginx_cfg(client.name, port, root_dir) - create_symlink(source, target, True) - set_nginx_permissions() - Logger.print_ok(f"NGINX config for {display_name} successfully created.") - except Exception: - Logger.print_error(f"Creating NGINX config for {display_name} failed!") - raise diff --git a/kiauh/utils/fs_utils.py b/kiauh/utils/fs_utils.py index 92f461e..111294b 100644 --- a/kiauh/utils/fs_utils.py +++ b/kiauh/utils/fs_utils.py @@ -122,21 +122,23 @@ def copy_common_vars_nginx_cfg() -> None: raise -def create_nginx_cfg(name: str, port: int, root_dir: Path) -> None: +def generate_nginx_cfg_from_template(name: str, template_src: Path, **kwargs) -> None: """ - Creates an NGINX config from a template file and replaces all placeholders + Creates an NGINX config from a template file and + replaces all placeholders passed as kwargs. A placeholder must be defined + in the template file as %{placeholder}%. :param name: name of the config to create - :param port: listen port - :param root_dir: directory of the static files + :param template_src: the path to the template file :return: None """ tmp = Path.home().joinpath(f"{name}.tmp") - shutil.copy(MODULE_PATH.joinpath("assets/nginx_cfg"), tmp) + shutil.copy(template_src, tmp) with open(tmp, "r+") as f: content = f.read() - content = content.replace("%NAME%", name) - content = content.replace("%PORT%", str(port)) - content = content.replace("%ROOT_DIR%", str(root_dir)) + + for key, value in kwargs.items(): + content = content.replace(f"%{key}%", str(value)) + f.seek(0) f.write(content) f.truncate() @@ -151,6 +153,30 @@ def create_nginx_cfg(name: str, port: int, root_dir: Path) -> None: raise +def create_nginx_cfg( + display_name: str, + cfg_name: str, + template_src: Path, + **kwargs, +) -> None: + from utils.sys_utils import set_nginx_permissions + + try: + Logger.print_status(f"Creating NGINX config for {display_name} ...") + + source = NGINX_SITES_AVAILABLE.joinpath(cfg_name) + target = NGINX_SITES_ENABLED.joinpath(cfg_name) + remove_file(Path("/etc/nginx/sites-enabled/default"), True) + generate_nginx_cfg_from_template(cfg_name, template_src=template_src, **kwargs) + create_symlink(source, target, True) + set_nginx_permissions() + + Logger.print_ok(f"NGINX config for {display_name} successfully created.") + except Exception: + Logger.print_error(f"Creating NGINX config for {display_name} failed!") + raise + + def read_ports_from_nginx_configs() -> List[int]: """ Helper function to iterate over all NGINX configs and read all ports defined for listen