Compare commits

...

5 Commits

Author SHA1 Message Date
dw-0
a96153ef34 Merge 1f2d724189 into f2691f33d3 2024-04-28 19:43:31 +02:00
dw-0
1f2d724189 feat: use dynamically created client download URL
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-04-28 19:43:36 +02:00
dw-0
1a29324e6a refactor: handle ports as ints as they are coming as ints from the KiauhSettings
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-04-28 19:42:40 +02:00
dw-0
5225e70e83 refactor: replace two seperate download url properties by only one
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-04-28 19:41:38 +02:00
dw-0
51f0713c5a refactor: print traceback of exception
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-04-28 19:40:30 +02:00
8 changed files with 49 additions and 53 deletions

View File

@@ -59,12 +59,7 @@ class BaseWebClient(ABC):
@property
@abstractmethod
def stable_url(self) -> str:
raise NotImplementedError
@property
@abstractmethod
def unstable_url(self) -> str:
def download_url(self) -> str:
raise NotImplementedError
@property

View File

@@ -55,7 +55,7 @@ def print_client_already_installed_dialog(name: str):
print_back_footer()
def print_client_port_select_dialog(name: str, port: str, ports_in_use: List[str]):
def print_client_port_select_dialog(name: str, port: int, ports_in_use: List[int]):
port = f"{COLOR_CYAN}{port}{RESET_FORMAT}"
line1 = f"Please select the port, {name} should be served on."
line2 = f"In case you need {name} to be served on a specific"

View File

@@ -104,20 +104,18 @@ def install_client(client: BaseWebClient) -> None:
install_client_cfg = get_confirm(question, allow_go_back=False)
settings = KiauhSettings()
port = settings.get(client.name, "port")
ports_in_use = read_ports_from_nginx_configs()
port: int = settings.get(client.name, "port")
ports_in_use: List[int] = read_ports_from_nginx_configs()
# check if configured port is a valid number and not in use already
valid_port = is_valid_port(port, ports_in_use)
while not valid_port:
next_port = get_next_free_port(ports_in_use)
print_client_port_select_dialog(client.display_name, next_port, ports_in_use)
port = str(
get_number_input(
f"Configure {client.display_name} for port",
min_count=int(next_port),
default=next_port,
)
port = get_number_input(
f"Configure {client.display_name} for port",
min_count=int(next_port),
default=next_port,
)
valid_port = is_valid_port(port, ports_in_use)
@@ -162,8 +160,10 @@ 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.stable_url, target, True)
Logger.print_status(
f"Downloading {client.display_name} from {client.download_url} ..."
)
download_file(client.download_url, target, True)
Logger.print_ok("Download complete!")
Logger.print_status(f"Extracting {zipfile} ...")
@@ -172,7 +172,7 @@ def download_client(client: BaseWebClient) -> None:
Logger.print_ok("OK!")
except Exception:
Logger.print_error(f"Downloading {zipfile} failed!")
Logger.print_error(f"Downloading {client.display_name} failed!")
raise

View File

@@ -22,10 +22,11 @@ from components.webui_client.base_data import (
from components.webui_client.mainsail_data import MainsailData
from core.backup_manager.backup_manager import BackupManager
from core.repo_manager.repo_manager import RepoManager
from core.settings.kiauh_settings import KiauhSettings
from utils import NGINX_SITES_AVAILABLE, NGINX_CONFD
from utils.common import get_install_status_webui
from utils.constants import COLOR_CYAN, RESET_FORMAT, COLOR_YELLOW
from utils.git_utils import get_latest_tag
from utils.git_utils import get_latest_tag, get_latest_unstable_tag
from utils.logger import Logger
@@ -201,3 +202,20 @@ def config_for_other_client_exist(client_to_ignore: WebClientType) -> bool:
clients = clients - {client_to_ignore.value}
return True if len(clients) > 0 else False
def get_download_url(base_url: str, client: BaseWebClient) -> str:
settings = KiauhSettings()
use_unstable = settings.get(client.name, "unstable_releases")
stable_url = f"{base_url}/latest/download/{client.name}.zip"
if not use_unstable:
return stable_url
try:
unstable_tag = get_latest_unstable_tag(client.repo_path)
if unstable_tag == "":
raise Exception
return f"{base_url}/download/{unstable_tag}/{client.name}.zip"
except Exception:
return stable_url

View File

@@ -18,8 +18,8 @@ from components.webui_client.base_data import (
WebClientType,
BaseWebClient,
)
from components.webui_client.client_utils import get_download_url
from core.backup_manager import BACKUP_ROOT_DIR
from utils.git_utils import get_latest_unstable_tag
@dataclass(frozen=True)
@@ -46,19 +46,8 @@ class FluiddData(BaseWebClient):
repo_path: str = "fluidd-core/fluidd"
@property
def stable_url(self) -> str:
return f"{self.BASE_DL_URL}/latest/download/fluidd.zip"
@property
def unstable_url(self) -> str:
try:
unstable_tag = get_latest_unstable_tag(self.repo_path)
if unstable_tag != "":
return f"{self.BASE_DL_URL}/download/{unstable_tag}/fluidd.zip"
else:
raise Exception
except Exception:
return self.stable_url
def download_url(self) -> str:
return get_download_url(self.BASE_DL_URL, self)
@property
def client_config(self) -> BaseWebClientConfig:

View File

@@ -19,7 +19,6 @@ from components.webui_client.base_data import (
BaseWebClient,
)
from core.backup_manager import BACKUP_ROOT_DIR
from utils.git_utils import get_latest_unstable_tag
@dataclass(frozen=True)
@@ -46,19 +45,10 @@ class MainsailData(BaseWebClient):
repo_path: str = "mainsail-crew/mainsail"
@property
def stable_url(self) -> str:
return f"{self.BASE_DL_URL}/latest/download/mainsail.zip"
def download_url(self) -> str:
from components.webui_client.client_utils import get_download_url
@property
def unstable_url(self) -> str:
try:
unstable_tag = get_latest_unstable_tag(self.repo_path)
if unstable_tag != "":
return f"{self.BASE_DL_URL}/download/{unstable_tag}/mainsail.zip"
else:
raise Exception
except Exception:
return self.stable_url
return get_download_url(self.BASE_DL_URL, self)
@property
def client_config(self) -> BaseWebClientConfig:

View File

@@ -12,6 +12,7 @@ from __future__ import annotations
import subprocess
import sys
import textwrap
import traceback
from abc import abstractmethod
from typing import Type, Dict, Optional
@@ -213,4 +214,6 @@ class BaseMenu(metaclass=PostInitCaller):
option.method(opt_index=option.opt_index, opt_data=option.opt_data)
self.run()
except Exception as e:
Logger.print_error(f"An unexpected error occured:\n{e}")
Logger.print_error(
f"An unexpected error occured:\n{e}\n{traceback.format_exc()}"
)

View File

@@ -149,7 +149,7 @@ def create_nginx_cfg(name: str, port: int, root_dir: Path) -> None:
raise
def read_ports_from_nginx_configs() -> List[str]:
def read_ports_from_nginx_configs() -> List[int]:
"""
Helper function to iterate over all NGINX configs and read all ports defined for listen
:return: A sorted list of listen ports
@@ -168,18 +168,19 @@ def read_ports_from_nginx_configs() -> List[str]:
if line.startswith("listen") and line.split()[-1] not in port_list:
port_list.append(line.split()[-1])
return sorted(port_list, key=lambda x: int(x))
ports_to_ints_list = [int(port) for port in port_list]
return sorted(ports_to_ints_list, key=lambda x: int(x))
def is_valid_port(port: str, ports_in_use: List[str]) -> bool:
return port.isdigit() and port not in ports_in_use
def is_valid_port(port: int, ports_in_use: List[int]) -> bool:
return port not in ports_in_use
def get_next_free_port(ports_in_use: List[str]) -> str:
def get_next_free_port(ports_in_use: List[int]) -> int:
valid_ports = set(range(80, 7125))
used_ports = set(map(int, ports_in_use))
return str(min(valid_ports - used_ports))
return min(valid_ports - used_ports)
def add_config_section(