refactor: improve nginx config generation
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -0,0 +1,12 @@
|
|||||||
|
# ======================================================================= #
|
||||||
|
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
|
||||||
|
# #
|
||||||
|
# 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
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from typing import List
|
|||||||
|
|
||||||
from components.klipper.klipper import Klipper
|
from components.klipper.klipper import Klipper
|
||||||
from components.moonraker.moonraker import Moonraker
|
from components.moonraker.moonraker import Moonraker
|
||||||
|
from components.webui_client import MODULE_PATH
|
||||||
from components.webui_client.base_data import (
|
from components.webui_client.base_data import (
|
||||||
BaseWebClient,
|
BaseWebClient,
|
||||||
BaseWebClientConfig,
|
BaseWebClientConfig,
|
||||||
@@ -34,18 +35,15 @@ from components.webui_client.client_utils import (
|
|||||||
)
|
)
|
||||||
from core.instance_manager.instance_manager import InstanceManager
|
from core.instance_manager.instance_manager import InstanceManager
|
||||||
from core.settings.kiauh_settings import KiauhSettings
|
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.common import check_install_dependencies
|
||||||
from utils.config_utils import add_config_section
|
from utils.config_utils import add_config_section
|
||||||
from utils.fs_utils import (
|
from utils.fs_utils import (
|
||||||
copy_common_vars_nginx_cfg,
|
copy_common_vars_nginx_cfg,
|
||||||
copy_upstream_nginx_cfg,
|
copy_upstream_nginx_cfg,
|
||||||
create_nginx_cfg,
|
create_nginx_cfg,
|
||||||
create_symlink,
|
|
||||||
get_next_free_port,
|
get_next_free_port,
|
||||||
is_valid_port,
|
is_valid_port,
|
||||||
read_ports_from_nginx_configs,
|
read_ports_from_nginx_configs,
|
||||||
remove_file,
|
|
||||||
unzip,
|
unzip,
|
||||||
)
|
)
|
||||||
from utils.input_utils import get_confirm, get_number_input
|
from utils.input_utils import get_confirm, get_number_input
|
||||||
@@ -54,7 +52,6 @@ from utils.sys_utils import (
|
|||||||
cmd_sysctl_service,
|
cmd_sysctl_service,
|
||||||
download_file,
|
download_file,
|
||||||
get_ipv4_addr,
|
get_ipv4_addr,
|
||||||
set_nginx_permissions,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -141,7 +138,15 @@ def install_client(client: BaseWebClient) -> None:
|
|||||||
|
|
||||||
copy_upstream_nginx_cfg()
|
copy_upstream_nginx_cfg()
|
||||||
copy_common_vars_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:
|
if kl_instances:
|
||||||
symlink_webui_nginx_log(kl_instances)
|
symlink_webui_nginx_log(kl_instances)
|
||||||
cmd_sysctl_service("nginx", "restart")
|
cmd_sysctl_service("nginx", "restart")
|
||||||
@@ -190,20 +195,3 @@ def update_client(client: BaseWebClient) -> None:
|
|||||||
|
|
||||||
if client.client == WebClientType.MAINSAIL:
|
if client.client == WebClientType.MAINSAIL:
|
||||||
restore_mainsail_config_json()
|
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
|
|
||||||
|
|||||||
@@ -122,21 +122,23 @@ def copy_common_vars_nginx_cfg() -> None:
|
|||||||
raise
|
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 name: name of the config to create
|
||||||
:param port: listen port
|
:param template_src: the path to the template file
|
||||||
:param root_dir: directory of the static files
|
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
tmp = Path.home().joinpath(f"{name}.tmp")
|
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:
|
with open(tmp, "r+") as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
content = content.replace("%NAME%", name)
|
|
||||||
content = content.replace("%PORT%", str(port))
|
for key, value in kwargs.items():
|
||||||
content = content.replace("%ROOT_DIR%", str(root_dir))
|
content = content.replace(f"%{key}%", str(value))
|
||||||
|
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
f.write(content)
|
f.write(content)
|
||||||
f.truncate()
|
f.truncate()
|
||||||
@@ -151,6 +153,30 @@ def create_nginx_cfg(name: str, port: int, root_dir: Path) -> None:
|
|||||||
raise
|
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]:
|
def read_ports_from_nginx_configs() -> List[int]:
|
||||||
"""
|
"""
|
||||||
Helper function to iterate over all NGINX configs and read all ports defined for listen
|
Helper function to iterate over all NGINX configs and read all ports defined for listen
|
||||||
|
|||||||
Reference in New Issue
Block a user