mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-26 01:03:35 +05:00
refactor: create moonraker instances based on klipper instances
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -32,7 +32,6 @@ from core.logger import Logger
|
||||
class Klipper(BaseInstance):
|
||||
klipper_dir: Path = KLIPPER_DIR
|
||||
env_dir: Path = KLIPPER_ENV_DIR
|
||||
log_file_name = KLIPPER_LOG_NAME
|
||||
cfg_file: Path | None = None
|
||||
serial: Path | None = None
|
||||
uds: Path | None = None
|
||||
@@ -42,6 +41,7 @@ class Klipper(BaseInstance):
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
super().__post_init__()
|
||||
self.log_file_name = KLIPPER_LOG_NAME
|
||||
self.cfg_file = self.cfg_dir.joinpath(KLIPPER_CFG_NAME)
|
||||
self.serial = self.comms_dir.joinpath(KLIPPER_SERIAL_NAME)
|
||||
self.uds = self.comms_dir.joinpath(KLIPPER_UDS_NAME)
|
||||
|
||||
@@ -33,7 +33,6 @@ from core.submodules.simple_config_parser.src.simple_config_parser.simple_config
|
||||
class Moonraker(BaseInstance):
|
||||
moonraker_dir: Path = MOONRAKER_DIR
|
||||
env_dir: Path = MOONRAKER_ENV_DIR
|
||||
log_file_name = MOONRAKER_LOG_NAME
|
||||
cfg_file: Path | None = None
|
||||
port: int | None = None
|
||||
backup_dir: Path | None = None
|
||||
@@ -45,6 +44,7 @@ class Moonraker(BaseInstance):
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
super().__post_init__()
|
||||
self.log_file_name = MOONRAKER_LOG_NAME
|
||||
self.cfg_file = self.cfg_dir.joinpath(MOONRAKER_CFG_NAME)
|
||||
self.port = self._get_port()
|
||||
self.backup_dir = self.data_dir.joinpath("backup")
|
||||
|
||||
@@ -31,6 +31,7 @@ from components.moonraker.moonraker_dialogs import print_moonraker_overview
|
||||
from components.moonraker.moonraker_utils import (
|
||||
backup_moonraker_dir,
|
||||
create_example_moonraker_conf,
|
||||
moonraker_factory,
|
||||
)
|
||||
from components.webui_client.client_utils import (
|
||||
enable_mainsail_remotemode,
|
||||
@@ -50,6 +51,7 @@ from utils.input_utils import (
|
||||
from utils.sys_utils import (
|
||||
check_python_version,
|
||||
cmd_sysctl_manage,
|
||||
cmd_sysctl_service,
|
||||
create_python_venv,
|
||||
install_python_requirements,
|
||||
parse_packages_from_file,
|
||||
@@ -61,14 +63,12 @@ def install_moonraker() -> None:
|
||||
return
|
||||
|
||||
klipper_list: List[Klipper] = InstanceManager(Klipper).instances
|
||||
mr_im = InstanceManager(Moonraker)
|
||||
moonraker_list: List[Moonraker] = mr_im.instances
|
||||
|
||||
instance_names = []
|
||||
moonraker_list: List[Moonraker] = InstanceManager(Moonraker).instances
|
||||
instances: List[Moonraker] = []
|
||||
selected_option: str | Klipper
|
||||
|
||||
if len(klipper_list) == 0:
|
||||
instance_names.append(klipper_list[0].suffix)
|
||||
if len(klipper_list) == 1:
|
||||
instances.append(moonraker_factory(klipper_list[0]))
|
||||
else:
|
||||
print_moonraker_overview(
|
||||
klipper_list,
|
||||
@@ -87,12 +87,12 @@ def install_moonraker() -> None:
|
||||
return
|
||||
|
||||
if selected_option == "a":
|
||||
instance_names.extend([k.suffix for k in klipper_list])
|
||||
instances.extend([moonraker_factory(k) for k in klipper_list])
|
||||
else:
|
||||
klipper_instance: Klipper | None = options.get(selected_option)
|
||||
if klipper_instance is None:
|
||||
raise Exception("Error selecting instance!")
|
||||
instance_names.append(klipper_instance.suffix)
|
||||
instances.append(moonraker_factory(klipper_instance))
|
||||
|
||||
create_example_cfg = get_confirm("Create example moonraker.conf?")
|
||||
|
||||
@@ -102,26 +102,23 @@ def install_moonraker() -> None:
|
||||
install_moonraker_polkit()
|
||||
|
||||
used_ports_map = {m.suffix: m.port for m in moonraker_list}
|
||||
for name in instance_names:
|
||||
current_instance = Moonraker(suffix=name)
|
||||
|
||||
mr_im.current_instance = current_instance
|
||||
mr_im.create_instance()
|
||||
mr_im.enable_instance()
|
||||
for instance in instances:
|
||||
instance.create()
|
||||
cmd_sysctl_service(instance.service_file_path.name, "enable")
|
||||
|
||||
if create_example_cfg:
|
||||
# if a webclient and/or it's config is installed, patch
|
||||
# its update section to the config
|
||||
clients = get_existing_clients()
|
||||
create_example_moonraker_conf(current_instance, used_ports_map, clients)
|
||||
create_example_moonraker_conf(instance, used_ports_map, clients)
|
||||
|
||||
mr_im.start_instance()
|
||||
cmd_sysctl_service(instance.service_file_path.name, "start")
|
||||
|
||||
cmd_sysctl_manage("daemon-reload")
|
||||
|
||||
# if mainsail is installed, and we installed
|
||||
# multiple moonraker instances, we enable mainsails remote mode
|
||||
if MainsailData().client_dir.exists() and len(mr_im.instances) > 1:
|
||||
if MainsailData().client_dir.exists() and len(moonraker_list) > 1:
|
||||
enable_mainsail_remotemode()
|
||||
|
||||
except Exception as e:
|
||||
|
||||
@@ -10,9 +10,11 @@
|
||||
import shutil
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from components.klipper.klipper import Klipper
|
||||
from components.moonraker import (
|
||||
MODULE_PATH,
|
||||
MOONRAKER_BACKUP_DIR,
|
||||
MOONRAKER_CFG_NAME,
|
||||
MOONRAKER_DB_BACKUP_DIR,
|
||||
MOONRAKER_DEFAULT_PORT,
|
||||
MOONRAKER_DIR,
|
||||
@@ -33,6 +35,25 @@ from utils.sys_utils import (
|
||||
)
|
||||
|
||||
|
||||
def moonraker_factory(klipper_instance: Klipper) -> Moonraker:
|
||||
"""Create a new Moonraker instance from a Klipper instance."""
|
||||
|
||||
instance: Moonraker = Moonraker(suffix=klipper_instance.suffix)
|
||||
instance.is_legacy_instance = klipper_instance.is_legacy_instance
|
||||
instance.data_dir = klipper_instance.data_dir
|
||||
instance.data_dir_name = klipper_instance.data_dir_name
|
||||
instance.cfg_dir = klipper_instance.cfg_dir
|
||||
instance.cfg_file = instance.cfg_dir.joinpath(MOONRAKER_CFG_NAME)
|
||||
instance.log_dir = klipper_instance.log_dir
|
||||
instance.sysd_dir = klipper_instance.sysd_dir
|
||||
instance.comms_dir = klipper_instance.comms_dir
|
||||
instance.gcodes_dir = klipper_instance.gcodes_dir
|
||||
instance.db_dir = instance.data_dir.joinpath("database")
|
||||
instance.backup_dir = instance.data_dir.joinpath("backup")
|
||||
instance.certs_dir = instance.data_dir.joinpath("certs")
|
||||
return instance
|
||||
|
||||
|
||||
def get_moonraker_status() -> ComponentStatus:
|
||||
return get_install_status(MOONRAKER_DIR, MOONRAKER_ENV_DIR, Moonraker)
|
||||
|
||||
|
||||
@@ -35,10 +35,12 @@ class BaseInstance(ABC):
|
||||
log_file_name: str = ""
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
self._set_data_dir()
|
||||
self._set_is_legacy_instance()
|
||||
self._set_service_file_path()
|
||||
self._set_data_dir()
|
||||
|
||||
if self.data_dir is not None:
|
||||
self.data_dir_name = self.data_dir.name
|
||||
self._set_is_legacy_instance()
|
||||
self.cfg_dir = self.data_dir.joinpath("config")
|
||||
self.log_dir = self.data_dir.joinpath("logs")
|
||||
self.comms_dir = self.data_dir.joinpath("comms")
|
||||
@@ -112,13 +114,15 @@ class BaseInstance(ABC):
|
||||
else:
|
||||
self.data_dir = Path.home().joinpath(f"printer_{self.suffix}_data")
|
||||
|
||||
if self.service_file_path is not None and self.service_file_path.exists():
|
||||
if self.service_file_path and self.service_file_path.exists():
|
||||
with open(self.service_file_path, "r") as service_file:
|
||||
service_content = service_file.read()
|
||||
pattern = re.compile("^EnvironmentFile=(.+)(/systemd/.+\.env)")
|
||||
match = re.search(pattern, service_content)
|
||||
if match:
|
||||
self.data_dir = Path(match.group(1))
|
||||
lines = service_file.readlines()
|
||||
for line in lines:
|
||||
pattern = r"^EnvironmentFile=(.+)(/systemd/.+\.env)"
|
||||
match = re.search(pattern, line)
|
||||
if match:
|
||||
self.data_dir = Path(match.group(1))
|
||||
break
|
||||
|
||||
def _set_service_file_path(self) -> None:
|
||||
from utils.common import convert_camelcase_to_kebabcase
|
||||
@@ -130,11 +134,7 @@ class BaseInstance(ABC):
|
||||
self.service_file_path = SYSTEMD.joinpath(f"{name}.service")
|
||||
|
||||
def _set_is_legacy_instance(self) -> None:
|
||||
if (
|
||||
self.suffix != ""
|
||||
and not self.data_dir_name.startswith("printer_")
|
||||
and not self.data_dir_name.endswith("_data")
|
||||
):
|
||||
legacy_pattern = r"^(?!printer)(.+)_data"
|
||||
match = re.search(legacy_pattern, self.data_dir_name)
|
||||
if match and self.suffix != "":
|
||||
self.is_legacy_instance = True
|
||||
else:
|
||||
self.is_legacy_instance = False
|
||||
|
||||
Reference in New Issue
Block a user