mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-25 00:33:37 +05:00
refactor: move config related helper methods into own util module
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -16,7 +16,8 @@ from components.klipper.klipper import Klipper
|
|||||||
from components.moonraker.moonraker import Moonraker
|
from components.moonraker.moonraker import Moonraker
|
||||||
from components.webui_client.base_data import BaseWebClientConfig
|
from components.webui_client.base_data import BaseWebClientConfig
|
||||||
from core.instance_manager.instance_manager import InstanceManager
|
from core.instance_manager.instance_manager import InstanceManager
|
||||||
from utils.filesystem_utils import remove_file, remove_config_section
|
from utils.config_utils import remove_config_section
|
||||||
|
from utils.filesystem_utils import remove_file
|
||||||
from utils.logger import Logger
|
from utils.logger import Logger
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,11 +26,8 @@ from components.webui_client.client_utils import (
|
|||||||
|
|
||||||
from core.instance_manager.instance_manager import InstanceManager
|
from core.instance_manager.instance_manager import InstanceManager
|
||||||
from utils.common import backup_printer_config_dir
|
from utils.common import backup_printer_config_dir
|
||||||
from utils.filesystem_utils import (
|
from utils.config_utils import add_config_section, add_config_section_at_top
|
||||||
create_symlink,
|
from utils.filesystem_utils import create_symlink
|
||||||
add_config_section,
|
|
||||||
add_config_section_at_top,
|
|
||||||
)
|
|
||||||
from utils.git_utils import git_clone_wrapper, git_pull_wrapper
|
from utils.git_utils import git_clone_wrapper, git_pull_wrapper
|
||||||
from utils.input_utils import get_confirm
|
from utils.input_utils import get_confirm
|
||||||
from utils.logger import Logger
|
from utils.logger import Logger
|
||||||
|
|||||||
@@ -23,10 +23,10 @@ from components.webui_client.client_config.client_config_remove import (
|
|||||||
from components.webui_client.client_utils import backup_mainsail_config_json
|
from components.webui_client.client_utils import backup_mainsail_config_json
|
||||||
|
|
||||||
from core.instance_manager.instance_manager import InstanceManager
|
from core.instance_manager.instance_manager import InstanceManager
|
||||||
|
from utils.config_utils import remove_config_section
|
||||||
from utils.filesystem_utils import (
|
from utils.filesystem_utils import (
|
||||||
remove_nginx_config,
|
remove_nginx_config,
|
||||||
remove_nginx_logs,
|
remove_nginx_logs,
|
||||||
remove_config_section,
|
|
||||||
)
|
)
|
||||||
from utils.logger import Logger
|
from utils.logger import Logger
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ 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 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.filesystem_utils import (
|
from utils.filesystem_utils import (
|
||||||
unzip,
|
unzip,
|
||||||
copy_upstream_nginx_cfg,
|
copy_upstream_nginx_cfg,
|
||||||
@@ -44,7 +45,6 @@ from utils.filesystem_utils import (
|
|||||||
create_nginx_cfg,
|
create_nginx_cfg,
|
||||||
create_symlink,
|
create_symlink,
|
||||||
remove_file,
|
remove_file,
|
||||||
add_config_section,
|
|
||||||
read_ports_from_nginx_configs,
|
read_ports_from_nginx_configs,
|
||||||
is_valid_port,
|
is_valid_port,
|
||||||
get_next_free_port,
|
get_next_free_port,
|
||||||
|
|||||||
83
kiauh/utils/config_utils.py
Normal file
83
kiauh/utils/config_utils.py
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
# ======================================================================= #
|
||||||
|
# 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 #
|
||||||
|
# ======================================================================= #
|
||||||
|
import tempfile
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import List, TypeVar, Tuple, Optional
|
||||||
|
|
||||||
|
from components.klipper.klipper import Klipper
|
||||||
|
from components.moonraker.moonraker import Moonraker
|
||||||
|
from core.config_manager.config_manager import ConfigManager
|
||||||
|
from utils.logger import Logger
|
||||||
|
|
||||||
|
B = TypeVar("B", Klipper, Moonraker)
|
||||||
|
ConfigOption = Tuple[str, str]
|
||||||
|
|
||||||
|
|
||||||
|
def add_config_section(
|
||||||
|
section: str,
|
||||||
|
instances: List[B],
|
||||||
|
options: Optional[List[ConfigOption]] = None,
|
||||||
|
) -> None:
|
||||||
|
for instance in instances:
|
||||||
|
cfg_file = instance.cfg_file
|
||||||
|
Logger.print_status(f"Add section '[{section}]' to '{cfg_file}' ...")
|
||||||
|
|
||||||
|
if not Path(cfg_file).exists():
|
||||||
|
Logger.print_warn(f"'{cfg_file}' not found!")
|
||||||
|
continue
|
||||||
|
|
||||||
|
cm = ConfigManager(cfg_file)
|
||||||
|
if cm.config.has_section(section):
|
||||||
|
Logger.print_info("Section already exist. Skipped ...")
|
||||||
|
continue
|
||||||
|
|
||||||
|
cm.config.add_section(section)
|
||||||
|
|
||||||
|
if options is not None:
|
||||||
|
for option in options:
|
||||||
|
cm.config.set(section, option[0], option[1])
|
||||||
|
|
||||||
|
cm.write_config()
|
||||||
|
|
||||||
|
|
||||||
|
def add_config_section_at_top(section: str, instances: List[B]):
|
||||||
|
for instance in instances:
|
||||||
|
tmp_cfg = tempfile.NamedTemporaryFile(mode="w", delete=False)
|
||||||
|
tmp_cfg_path = Path(tmp_cfg.name)
|
||||||
|
cmt = ConfigManager(tmp_cfg_path)
|
||||||
|
cmt.config.add_section(section)
|
||||||
|
cmt.write_config()
|
||||||
|
tmp_cfg.close()
|
||||||
|
|
||||||
|
cfg_file = instance.cfg_file
|
||||||
|
with open(cfg_file, "r") as org:
|
||||||
|
org_content = org.readlines()
|
||||||
|
with open(tmp_cfg_path, "a") as tmp:
|
||||||
|
tmp.writelines(org_content)
|
||||||
|
|
||||||
|
cfg_file.unlink()
|
||||||
|
tmp_cfg_path.rename(cfg_file)
|
||||||
|
|
||||||
|
|
||||||
|
def remove_config_section(section: str, instances: List[B]) -> None:
|
||||||
|
for instance in instances:
|
||||||
|
cfg_file = instance.cfg_file
|
||||||
|
Logger.print_status(f"Remove section '[{section}]' from '{cfg_file}' ...")
|
||||||
|
|
||||||
|
if not Path(cfg_file).exists():
|
||||||
|
Logger.print_warn(f"'{cfg_file}' not found!")
|
||||||
|
continue
|
||||||
|
|
||||||
|
cm = ConfigManager(cfg_file)
|
||||||
|
if not cm.config.has_section(section):
|
||||||
|
Logger.print_info("Section does not exist. Skipped ...")
|
||||||
|
continue
|
||||||
|
|
||||||
|
cm.config.remove_section(section)
|
||||||
|
cm.write_config()
|
||||||
@@ -12,15 +12,12 @@
|
|||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from zipfile import ZipFile
|
from zipfile import ZipFile
|
||||||
|
|
||||||
from typing import List, TypeVar, Tuple, Optional
|
from typing import List
|
||||||
|
|
||||||
from components.klipper.klipper import Klipper
|
from components.klipper.klipper import Klipper
|
||||||
from components.moonraker.moonraker import Moonraker
|
|
||||||
from core.config_manager.config_manager import ConfigManager
|
|
||||||
from core.instance_manager.instance_manager import InstanceManager
|
from core.instance_manager.instance_manager import InstanceManager
|
||||||
from utils import (
|
from utils import (
|
||||||
NGINX_SITES_AVAILABLE,
|
NGINX_SITES_AVAILABLE,
|
||||||
@@ -31,10 +28,6 @@ from utils import (
|
|||||||
from utils.logger import Logger
|
from utils.logger import Logger
|
||||||
|
|
||||||
|
|
||||||
B = TypeVar("B", Klipper, Moonraker)
|
|
||||||
ConfigOption = Tuple[str, str]
|
|
||||||
|
|
||||||
|
|
||||||
def check_file_exist(file_path: Path, sudo=False) -> bool:
|
def check_file_exist(file_path: Path, sudo=False) -> bool:
|
||||||
"""
|
"""
|
||||||
Helper function for checking the existence of a file |
|
Helper function for checking the existence of a file |
|
||||||
@@ -183,98 +176,6 @@ def get_next_free_port(ports_in_use: List[int]) -> int:
|
|||||||
return min(valid_ports - used_ports)
|
return min(valid_ports - used_ports)
|
||||||
|
|
||||||
|
|
||||||
def add_config_section(
|
|
||||||
section: str,
|
|
||||||
instances: List[B],
|
|
||||||
options: Optional[List[ConfigOption]] = None,
|
|
||||||
) -> None:
|
|
||||||
for instance in instances:
|
|
||||||
cfg_file = instance.cfg_file
|
|
||||||
Logger.print_status(f"Add section '[{section}]' to '{cfg_file}' ...")
|
|
||||||
|
|
||||||
if not Path(cfg_file).exists():
|
|
||||||
Logger.print_warn(f"'{cfg_file}' not found!")
|
|
||||||
continue
|
|
||||||
|
|
||||||
cm = ConfigManager(cfg_file)
|
|
||||||
if cm.config.has_section(section):
|
|
||||||
Logger.print_info("Section already exist. Skipped ...")
|
|
||||||
continue
|
|
||||||
|
|
||||||
cm.config.add_section(section)
|
|
||||||
|
|
||||||
if options is not None:
|
|
||||||
for option in options:
|
|
||||||
cm.config.set(section, option[0], option[1])
|
|
||||||
|
|
||||||
cm.write_config()
|
|
||||||
|
|
||||||
|
|
||||||
def add_config_section_at_top(section: str, instances: List[B]):
|
|
||||||
for instance in instances:
|
|
||||||
tmp_cfg = tempfile.NamedTemporaryFile(mode="w", delete=False)
|
|
||||||
tmp_cfg_path = Path(tmp_cfg.name)
|
|
||||||
cmt = ConfigManager(tmp_cfg_path)
|
|
||||||
cmt.config.add_section(section)
|
|
||||||
cmt.write_config()
|
|
||||||
tmp_cfg.close()
|
|
||||||
|
|
||||||
cfg_file = instance.cfg_file
|
|
||||||
with open(cfg_file, "r") as org:
|
|
||||||
org_content = org.readlines()
|
|
||||||
with open(tmp_cfg_path, "a") as tmp:
|
|
||||||
tmp.writelines(org_content)
|
|
||||||
|
|
||||||
cfg_file.unlink()
|
|
||||||
tmp_cfg_path.rename(cfg_file)
|
|
||||||
|
|
||||||
|
|
||||||
def remove_config_section(section: str, instances: List[B]) -> None:
|
|
||||||
for instance in instances:
|
|
||||||
cfg_file = instance.cfg_file
|
|
||||||
Logger.print_status(f"Remove section '[{section}]' from '{cfg_file}' ...")
|
|
||||||
|
|
||||||
if not Path(cfg_file).exists():
|
|
||||||
Logger.print_warn(f"'{cfg_file}' not found!")
|
|
||||||
continue
|
|
||||||
|
|
||||||
cm = ConfigManager(cfg_file)
|
|
||||||
if not cm.config.has_section(section):
|
|
||||||
Logger.print_info("Section does not exist. Skipped ...")
|
|
||||||
continue
|
|
||||||
|
|
||||||
cm.config.remove_section(section)
|
|
||||||
cm.write_config()
|
|
||||||
|
|
||||||
|
|
||||||
def patch_moonraker_conf(
|
|
||||||
moonraker_instances: List[Moonraker],
|
|
||||||
name: str,
|
|
||||||
section_name: str,
|
|
||||||
template_file: str,
|
|
||||||
) -> None:
|
|
||||||
for instance in moonraker_instances:
|
|
||||||
cfg_file = instance.cfg_file
|
|
||||||
Logger.print_status(f"Add {name} update section to '{cfg_file}' ...")
|
|
||||||
|
|
||||||
if not Path(cfg_file).exists():
|
|
||||||
Logger.print_warn(f"'{cfg_file}' not found!")
|
|
||||||
return
|
|
||||||
|
|
||||||
cm = ConfigManager(cfg_file)
|
|
||||||
if cm.config.has_section(section_name):
|
|
||||||
Logger.print_info("Section already exist. Skipped ...")
|
|
||||||
return
|
|
||||||
|
|
||||||
template = MODULE_PATH.joinpath("assets", template_file)
|
|
||||||
with open(template, "r") as t:
|
|
||||||
template_content = "\n"
|
|
||||||
template_content += t.read()
|
|
||||||
|
|
||||||
with open(cfg_file, "a") as f:
|
|
||||||
f.write(template_content)
|
|
||||||
|
|
||||||
|
|
||||||
def remove_nginx_config(name: str) -> None:
|
def remove_nginx_config(name: str) -> None:
|
||||||
Logger.print_status(f"Removing NGINX config for {name.capitalize()} ...")
|
Logger.print_status(f"Removing NGINX config for {name.capitalize()} ...")
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user