mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-26 09:13:35 +05:00
refactor: full refactor of how webclient data is handled
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -11,12 +11,13 @@ from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
from components.klipper.klipper import Klipper
|
||||
from components.webui_client import (
|
||||
ClientName,
|
||||
ClientData,
|
||||
)
|
||||
|
||||
from components.moonraker.moonraker import Moonraker
|
||||
from components.webui_client.base_data import (
|
||||
WebClientType,
|
||||
BaseWebClient,
|
||||
BaseWebClientConfig,
|
||||
)
|
||||
from components.webui_client.client_config.client_config_setup import (
|
||||
install_client_config,
|
||||
)
|
||||
@@ -30,7 +31,6 @@ from components.webui_client.client_utils import (
|
||||
restore_mainsail_config_json,
|
||||
enable_mainsail_remotemode,
|
||||
symlink_webui_nginx_log,
|
||||
load_client_data,
|
||||
config_for_other_client_exist,
|
||||
)
|
||||
from core.config_manager.config_manager import ConfigManager
|
||||
@@ -60,17 +60,13 @@ from utils.system_utils import (
|
||||
)
|
||||
|
||||
|
||||
def install_client(client_name: ClientName) -> None:
|
||||
client: ClientData = load_client_data(client_name)
|
||||
d_name = client.get("display_name")
|
||||
|
||||
def install_client(client: BaseWebClient) -> None:
|
||||
if client is None:
|
||||
Logger.print_error("Missing parameter client_name!")
|
||||
return
|
||||
raise ValueError("Missing parameter client_data!")
|
||||
|
||||
if client.get("dir").exists():
|
||||
if client.client_dir.exists():
|
||||
Logger.print_info(
|
||||
f"{client.get('display_name')} seems to be already installed! Skipped ..."
|
||||
f"{client.display_name} seems to be already installed! Skipped ..."
|
||||
)
|
||||
return
|
||||
|
||||
@@ -81,31 +77,35 @@ def install_client(client_name: ClientName) -> None:
|
||||
if not mr_instances:
|
||||
print_moonraker_not_found_dialog()
|
||||
if not get_confirm(
|
||||
f"Continue {d_name} installation?",
|
||||
f"Continue {client.display_name} installation?",
|
||||
allow_go_back=True,
|
||||
):
|
||||
return
|
||||
|
||||
# if moonraker is not installed or multiple instances
|
||||
# are installed we enable mainsails remote mode
|
||||
if client.get("remote_mode") and not mr_instances or len(mr_instances) > 1:
|
||||
if (
|
||||
client.client == WebClientType.MAINSAIL
|
||||
and not mr_instances
|
||||
or len(mr_instances) > 1
|
||||
):
|
||||
enable_remotemode = True
|
||||
|
||||
kl_im = InstanceManager(Klipper)
|
||||
kl_instances = kl_im.instances
|
||||
install_client_cfg = False
|
||||
client_config = client.get("client_config")
|
||||
client_config: BaseWebClientConfig = client.client_config
|
||||
if (
|
||||
kl_instances
|
||||
and not client_config.get("dir").exists()
|
||||
and not config_for_other_client_exist(client_to_ignore=client.get("name"))
|
||||
and not client_config.config_dir.exists()
|
||||
and not config_for_other_client_exist(client_to_ignore=client.client)
|
||||
):
|
||||
print_install_client_config_dialog(client)
|
||||
question = f"Download the recommended {client_config.get('display_name')}?"
|
||||
question = f"Download the recommended {client_config.display_name}?"
|
||||
install_client_cfg = get_confirm(question, allow_go_back=False)
|
||||
|
||||
cm = ConfigManager(cfg_file=KIAUH_CFG)
|
||||
default_port = cm.get_value(client.get("name"), "port")
|
||||
default_port = cm.get_value(client.name, "port")
|
||||
client_port = default_port if default_port and default_port.isdigit() else "80"
|
||||
ports_in_use = read_ports_from_nginx_configs()
|
||||
|
||||
@@ -113,10 +113,10 @@ def install_client(client_name: ClientName) -> None:
|
||||
valid_port = is_valid_port(client_port, ports_in_use)
|
||||
while not valid_port:
|
||||
next_port = get_next_free_port(ports_in_use)
|
||||
print_client_port_select_dialog(d_name, next_port, ports_in_use)
|
||||
print_client_port_select_dialog(client.display_name, next_port, ports_in_use)
|
||||
client_port = str(
|
||||
get_number_input(
|
||||
f"Configure {d_name} for port",
|
||||
f"Configure {client.display_name} for port",
|
||||
min_count=int(next_port),
|
||||
default=next_port,
|
||||
)
|
||||
@@ -127,22 +127,22 @@ def install_client(client_name: ClientName) -> None:
|
||||
|
||||
try:
|
||||
download_client(client)
|
||||
if enable_remotemode and client.get("name") == "mainsail":
|
||||
if enable_remotemode and client.client == WebClientType.MAINSAIL:
|
||||
enable_mainsail_remotemode()
|
||||
if mr_instances:
|
||||
add_config_section(
|
||||
section=f"update_manager {client.get('name')}",
|
||||
section=f"update_manager {client.name}",
|
||||
instances=mr_instances,
|
||||
options=[
|
||||
("type", "web"),
|
||||
("channel", "stable"),
|
||||
("repo", client.get("mr_conf_repo")),
|
||||
("path", client.get("mr_conf_path")),
|
||||
("repo", str(client.repo_path)),
|
||||
("path", str(client.client_dir)),
|
||||
],
|
||||
)
|
||||
mr_im.restart_all_instance()
|
||||
if install_client_cfg and kl_instances:
|
||||
install_client_config(client.get("name"))
|
||||
install_client_config(client)
|
||||
|
||||
copy_upstream_nginx_cfg()
|
||||
copy_common_vars_nginx_cfg()
|
||||
@@ -152,24 +152,24 @@ def install_client(client_name: ClientName) -> None:
|
||||
control_systemd_service("nginx", "restart")
|
||||
|
||||
except Exception as e:
|
||||
Logger.print_error(f"{d_name} installation failed!\n{e}")
|
||||
Logger.print_error(f"{client.display_name} installation failed!\n{e}")
|
||||
return
|
||||
|
||||
log = f"Open {d_name} now on: http://{get_ipv4_addr()}:{client_port}"
|
||||
Logger.print_ok(f"{d_name} installation complete!", start="\n")
|
||||
log = f"Open {client.display_name} now on: http://{get_ipv4_addr()}:{client_port}"
|
||||
Logger.print_ok(f"{client.display_name} installation complete!", start="\n")
|
||||
Logger.print_ok(log, prefix=False, end="\n\n")
|
||||
|
||||
|
||||
def download_client(client: ClientData) -> None:
|
||||
zipfile = f"{client.get('name').lower()}.zip"
|
||||
def download_client(client: BaseWebClient) -> None:
|
||||
zipfile = f"{client.name.lower()}.zip"
|
||||
target = Path().home().joinpath(zipfile)
|
||||
try:
|
||||
Logger.print_status(f"Downloading {zipfile} ...")
|
||||
download_file(client.get("url"), target, True)
|
||||
download_file(client.stable_url, target, True)
|
||||
Logger.print_ok("Download complete!")
|
||||
|
||||
Logger.print_status(f"Extracting {zipfile} ...")
|
||||
unzip(target, client.get("dir"))
|
||||
unzip(target, client.client_dir)
|
||||
target.unlink(missing_ok=True)
|
||||
Logger.print_ok("OK!")
|
||||
|
||||
@@ -178,29 +178,35 @@ def download_client(client: ClientData) -> None:
|
||||
raise
|
||||
|
||||
|
||||
def update_client(client: ClientData) -> None:
|
||||
Logger.print_status(f"Updating {client.get('display_name')} ...")
|
||||
if client.get("name") == "mainsail":
|
||||
def update_client(client: BaseWebClient) -> None:
|
||||
Logger.print_status(f"Updating {client.display_name} ...")
|
||||
if not client.client_dir.exists():
|
||||
Logger.print_info(
|
||||
f"Unable to update {client.display_name}. Directory does not exist! Skipping ..."
|
||||
)
|
||||
return
|
||||
|
||||
if client.client == WebClientType.MAINSAIL:
|
||||
backup_mainsail_config_json(is_temp=True)
|
||||
|
||||
download_client(client)
|
||||
|
||||
if client.get("name") == "mainsail":
|
||||
if client.client == WebClientType.MAINSAIL:
|
||||
restore_mainsail_config_json()
|
||||
|
||||
|
||||
def create_client_nginx_cfg(client: ClientData, port: int) -> None:
|
||||
d_name = client.get("display_name")
|
||||
root_dir = client.get("dir")
|
||||
source = NGINX_SITES_AVAILABLE.joinpath(client.get("name"))
|
||||
target = NGINX_SITES_ENABLED.joinpath(client.get("name"))
|
||||
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 {d_name} ...")
|
||||
Logger.print_status(f"Creating NGINX config for {display_name} ...")
|
||||
remove_file(Path("/etc/nginx/sites-enabled/default"), True)
|
||||
create_nginx_cfg(client.get("name"), port, root_dir)
|
||||
create_nginx_cfg(client.name, port, root_dir)
|
||||
create_symlink(source, target, True)
|
||||
set_nginx_permissions()
|
||||
Logger.print_ok(f"NGINX config for {d_name} successfully created.")
|
||||
Logger.print_ok(f"NGINX config for {display_name} successfully created.")
|
||||
except Exception:
|
||||
Logger.print_error(f"Creating NGINX config for {d_name} failed!")
|
||||
Logger.print_error(f"Creating NGINX config for {display_name} failed!")
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user