mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-26 01:03:35 +05:00
Compare commits
3 Commits
bbf64eec9c
...
ea991644cd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ea991644cd | ||
|
|
fe0bfc5376 | ||
|
|
fdfdf18dd2 |
@@ -31,6 +31,7 @@ from core.constants import CURRENT_USER
|
|||||||
from core.instance_manager.instance_manager import InstanceManager
|
from core.instance_manager.instance_manager import InstanceManager
|
||||||
from core.logger import DialogType, Logger
|
from core.logger import DialogType, Logger
|
||||||
from core.settings.kiauh_settings import KiauhSettings
|
from core.settings.kiauh_settings import KiauhSettings
|
||||||
|
from core.types import ComponentStatus
|
||||||
from utils.common import (
|
from utils.common import (
|
||||||
check_install_dependencies,
|
check_install_dependencies,
|
||||||
get_install_status,
|
get_install_status,
|
||||||
@@ -44,7 +45,6 @@ from utils.sys_utils import (
|
|||||||
cmd_sysctl_service,
|
cmd_sysctl_service,
|
||||||
parse_packages_from_file,
|
parse_packages_from_file,
|
||||||
)
|
)
|
||||||
from core.types import ComponentStatus
|
|
||||||
|
|
||||||
|
|
||||||
def install_crowsnest() -> None:
|
def install_crowsnest() -> None:
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from subprocess import CalledProcessError, run
|
from subprocess import CalledProcessError
|
||||||
|
|
||||||
from components.klipper import (
|
from components.klipper import (
|
||||||
KLIPPER_CFG_NAME,
|
KLIPPER_CFG_NAME,
|
||||||
@@ -33,7 +33,6 @@ class Klipper(BaseInstance):
|
|||||||
klipper_dir: Path = KLIPPER_DIR
|
klipper_dir: Path = KLIPPER_DIR
|
||||||
env_dir: Path = KLIPPER_ENV_DIR
|
env_dir: Path = KLIPPER_ENV_DIR
|
||||||
cfg_file: Path | None = None
|
cfg_file: Path | None = None
|
||||||
log: Path | None = None
|
|
||||||
serial: Path | None = None
|
serial: Path | None = None
|
||||||
uds: Path | None = None
|
uds: Path | None = None
|
||||||
|
|
||||||
@@ -42,8 +41,8 @@ class Klipper(BaseInstance):
|
|||||||
|
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
super().__post_init__()
|
super().__post_init__()
|
||||||
|
self.log_file_name = KLIPPER_LOG_NAME
|
||||||
self.cfg_file = self.cfg_dir.joinpath(KLIPPER_CFG_NAME)
|
self.cfg_file = self.cfg_dir.joinpath(KLIPPER_CFG_NAME)
|
||||||
self.log = self.log_dir.joinpath(KLIPPER_LOG_NAME)
|
|
||||||
self.serial = self.comms_dir.joinpath(KLIPPER_SERIAL_NAME)
|
self.serial = self.comms_dir.joinpath(KLIPPER_SERIAL_NAME)
|
||||||
self.uds = self.comms_dir.joinpath(KLIPPER_UDS_NAME)
|
self.uds = self.comms_dir.joinpath(KLIPPER_UDS_NAME)
|
||||||
|
|
||||||
@@ -56,7 +55,7 @@ class Klipper(BaseInstance):
|
|||||||
self.create_folders()
|
self.create_folders()
|
||||||
|
|
||||||
create_service_file(
|
create_service_file(
|
||||||
name=self.get_service_file_name(extension=True),
|
name=self.service_file_path.name,
|
||||||
content=self._prep_service_file_content(),
|
content=self._prep_service_file_content(),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -72,21 +71,6 @@ class Klipper(BaseInstance):
|
|||||||
Logger.print_error(f"Error creating env file: {e}")
|
Logger.print_error(f"Error creating env file: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def delete(self) -> None:
|
|
||||||
service_file = self.get_service_file_name(extension=True)
|
|
||||||
service_file_path = self.get_service_file_path()
|
|
||||||
|
|
||||||
Logger.print_status(f"Removing Klipper Instance: {service_file}")
|
|
||||||
|
|
||||||
try:
|
|
||||||
command = ["sudo", "rm", "-f", service_file_path]
|
|
||||||
run(command, check=True)
|
|
||||||
self.delete_logfiles(KLIPPER_LOG_NAME)
|
|
||||||
Logger.print_ok("Instance successfully removed!")
|
|
||||||
except CalledProcessError as e:
|
|
||||||
Logger.print_error(f"Error removing instance: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
def _prep_service_file_content(self) -> str:
|
def _prep_service_file_content(self) -> str:
|
||||||
template = KLIPPER_SERVICE_TEMPLATE
|
template = KLIPPER_SERVICE_TEMPLATE
|
||||||
|
|
||||||
@@ -138,7 +122,7 @@ class Klipper(BaseInstance):
|
|||||||
)
|
)
|
||||||
env_file_content = env_file_content.replace(
|
env_file_content = env_file_content.replace(
|
||||||
"%LOG%",
|
"%LOG%",
|
||||||
self.log.as_posix() if self.log else "",
|
self.log_dir.joinpath(self.log_file_name).as_posix(),
|
||||||
)
|
)
|
||||||
env_file_content = env_file_content.replace(
|
env_file_content = env_file_content.replace(
|
||||||
"%UDS%",
|
"%UDS%",
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ def print_instance_overview(
|
|||||||
|
|
||||||
for i, s in enumerate(instances):
|
for i, s in enumerate(instances):
|
||||||
if display_type is DisplayType.SERVICE_NAME:
|
if display_type is DisplayType.SERVICE_NAME:
|
||||||
name = s.get_service_file_name()
|
name = s.service_file_path.stem
|
||||||
else:
|
else:
|
||||||
name = s.data_dir
|
name = s.data_dir
|
||||||
line = f"{COLOR_CYAN}{f'{i + start_index})' if show_index else '●'} {name}{RESET_FORMAT}"
|
line = f"{COLOR_CYAN}{f'{i + start_index})' if show_index else '●'} {name}{RESET_FORMAT}"
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ from core.instance_manager.instance_manager import InstanceManager
|
|||||||
from core.logger import Logger
|
from core.logger import Logger
|
||||||
from utils.fs_utils import run_remove_routines
|
from utils.fs_utils import run_remove_routines
|
||||||
from utils.input_utils import get_selection_input
|
from utils.input_utils import get_selection_input
|
||||||
from utils.sys_utils import cmd_sysctl_manage
|
|
||||||
|
|
||||||
|
|
||||||
def run_klipper_removal(
|
def run_klipper_removal(
|
||||||
@@ -25,17 +24,17 @@ def run_klipper_removal(
|
|||||||
remove_dir: bool,
|
remove_dir: bool,
|
||||||
remove_env: bool,
|
remove_env: bool,
|
||||||
) -> None:
|
) -> None:
|
||||||
im = InstanceManager(Klipper)
|
klipper_instances = InstanceManager(Klipper).instances
|
||||||
|
|
||||||
if remove_service:
|
if remove_service:
|
||||||
Logger.print_status("Removing Klipper instances ...")
|
Logger.print_status("Removing Klipper instances ...")
|
||||||
if im.instances:
|
if klipper_instances:
|
||||||
instances_to_remove = select_instances_to_remove(im.instances)
|
instances_to_remove = select_instances_to_remove(klipper_instances)
|
||||||
remove_instances(im, instances_to_remove)
|
remove_instances(instances_to_remove)
|
||||||
else:
|
else:
|
||||||
Logger.print_info("No Klipper Services installed! Skipped ...")
|
Logger.print_info("No Klipper Services installed! Skipped ...")
|
||||||
|
|
||||||
if (remove_dir or remove_env) and im.instances:
|
if (remove_dir or remove_env) and klipper_instances:
|
||||||
Logger.print_info("There are still other Klipper services installed:")
|
Logger.print_info("There are still other Klipper services installed:")
|
||||||
Logger.print_info(f"● '{KLIPPER_DIR}' was not removed.", prefix=False)
|
Logger.print_info(f"● '{KLIPPER_DIR}' was not removed.", prefix=False)
|
||||||
Logger.print_info(f"● '{KLIPPER_ENV_DIR}' was not removed.", prefix=False)
|
Logger.print_info(f"● '{KLIPPER_ENV_DIR}' was not removed.", prefix=False)
|
||||||
@@ -74,20 +73,14 @@ def select_instances_to_remove(instances: List[Klipper]) -> List[Klipper] | None
|
|||||||
|
|
||||||
|
|
||||||
def remove_instances(
|
def remove_instances(
|
||||||
instance_manager: InstanceManager,
|
|
||||||
instance_list: List[Klipper] | None,
|
instance_list: List[Klipper] | None,
|
||||||
) -> None:
|
) -> None:
|
||||||
if not instance_list:
|
if not instance_list:
|
||||||
return
|
return
|
||||||
|
|
||||||
for instance in instance_list:
|
for instance in instance_list:
|
||||||
Logger.print_status(f"Removing instance {instance.get_service_file_name()} ...")
|
Logger.print_status(f"Removing instance {instance.service_file_path.stem} ...")
|
||||||
instance_manager.current_instance = instance
|
instance.remove()
|
||||||
instance_manager.stop_instance()
|
|
||||||
instance_manager.disable_instance()
|
|
||||||
instance_manager.delete_instance()
|
|
||||||
|
|
||||||
cmd_sysctl_manage("daemon-reload")
|
|
||||||
|
|
||||||
|
|
||||||
def delete_klipper_logs(instances: List[Klipper]) -> None:
|
def delete_klipper_logs(instances: List[Klipper]) -> None:
|
||||||
|
|||||||
@@ -231,7 +231,7 @@ def display_moonraker_info(moonraker_list: List[Moonraker]) -> bool:
|
|||||||
DialogType.INFO,
|
DialogType.INFO,
|
||||||
[
|
[
|
||||||
"Existing Moonraker instances detected:",
|
"Existing Moonraker instances detected:",
|
||||||
*[f"● {m.get_service_file_name()}" for m in moonraker_list],
|
*[f"● {m.service_file_path.stem}" for m in moonraker_list],
|
||||||
"\n\n",
|
"\n\n",
|
||||||
"The following Klipper instances will be installed:",
|
"The following Klipper instances will be installed:",
|
||||||
*[f"● klipper-{m.suffix}" for m in moonraker_list],
|
*[f"● klipper-{m.suffix}" for m in moonraker_list],
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from subprocess import CalledProcessError, run
|
from subprocess import CalledProcessError
|
||||||
|
|
||||||
from components.moonraker import (
|
from components.moonraker import (
|
||||||
MOONRAKER_CFG_NAME,
|
MOONRAKER_CFG_NAME,
|
||||||
@@ -38,19 +38,18 @@ class Moonraker(BaseInstance):
|
|||||||
backup_dir: Path | None = None
|
backup_dir: Path | None = None
|
||||||
certs_dir: Path | None = None
|
certs_dir: Path | None = None
|
||||||
db_dir: Path | None = None
|
db_dir: Path | None = None
|
||||||
log: Path | None = None
|
|
||||||
|
|
||||||
def __init__(self, suffix: str = ""):
|
def __init__(self, suffix: str = ""):
|
||||||
super().__init__(suffix=suffix)
|
super().__init__(suffix=suffix)
|
||||||
|
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
super().__post_init__()
|
super().__post_init__()
|
||||||
|
self.log_file_name = MOONRAKER_LOG_NAME
|
||||||
self.cfg_file = self.cfg_dir.joinpath(MOONRAKER_CFG_NAME)
|
self.cfg_file = self.cfg_dir.joinpath(MOONRAKER_CFG_NAME)
|
||||||
self.port = self._get_port()
|
self.port = self._get_port()
|
||||||
self.backup_dir = self.data_dir.joinpath("backup")
|
self.backup_dir = self.data_dir.joinpath("backup")
|
||||||
self.certs_dir = self.data_dir.joinpath("certs")
|
self.certs_dir = self.data_dir.joinpath("certs")
|
||||||
self.db_dir = self.data_dir.joinpath("database")
|
self.db_dir = self.data_dir.joinpath("database")
|
||||||
self.log = self.log_dir.joinpath(MOONRAKER_LOG_NAME)
|
|
||||||
|
|
||||||
def create(self, create_example_cfg: bool = False) -> None:
|
def create(self, create_example_cfg: bool = False) -> None:
|
||||||
from utils.sys_utils import create_env_file, create_service_file
|
from utils.sys_utils import create_env_file, create_service_file
|
||||||
@@ -60,7 +59,7 @@ class Moonraker(BaseInstance):
|
|||||||
try:
|
try:
|
||||||
self.create_folders([self.backup_dir, self.certs_dir, self.db_dir])
|
self.create_folders([self.backup_dir, self.certs_dir, self.db_dir])
|
||||||
create_service_file(
|
create_service_file(
|
||||||
name=self.get_service_file_name(extension=True),
|
name=self.service_file_path.name,
|
||||||
content=self._prep_service_file_content(),
|
content=self._prep_service_file_content(),
|
||||||
)
|
)
|
||||||
create_env_file(
|
create_env_file(
|
||||||
@@ -75,21 +74,6 @@ class Moonraker(BaseInstance):
|
|||||||
Logger.print_error(f"Error creating env file: {e}")
|
Logger.print_error(f"Error creating env file: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def delete(self) -> None:
|
|
||||||
service_file = self.get_service_file_name(extension=True)
|
|
||||||
service_file_path = self.get_service_file_path()
|
|
||||||
|
|
||||||
Logger.print_status(f"Removing Moonraker Instance: {service_file}")
|
|
||||||
|
|
||||||
try:
|
|
||||||
command = ["sudo", "rm", "-f", service_file_path]
|
|
||||||
run(command, check=True)
|
|
||||||
self.delete_logfiles(MOONRAKER_LOG_NAME)
|
|
||||||
Logger.print_ok("Instance successfully removed!")
|
|
||||||
except CalledProcessError as e:
|
|
||||||
Logger.print_error(f"Error removing instance: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
def _prep_service_file_content(self) -> str:
|
def _prep_service_file_content(self) -> str:
|
||||||
template = MOONRAKER_SERVICE_TEMPLATE
|
template = MOONRAKER_SERVICE_TEMPLATE
|
||||||
|
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ def print_moonraker_overview(
|
|||||||
dialog += "║ ║\n"
|
dialog += "║ ║\n"
|
||||||
|
|
||||||
instance_map = {
|
instance_map = {
|
||||||
k.get_service_file_name(): (
|
k.service_file_path.stem: (
|
||||||
k.get_service_file_name().replace("klipper", "moonraker")
|
k.service_file_path.stem.replace("klipper", "moonraker")
|
||||||
if k.suffix in [m.suffix for m in moonraker_instances]
|
if k.suffix in [m.suffix for m in moonraker_instances]
|
||||||
else ""
|
else ""
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ from core.instance_manager.instance_manager import InstanceManager
|
|||||||
from core.logger import Logger
|
from core.logger import Logger
|
||||||
from utils.fs_utils import run_remove_routines
|
from utils.fs_utils import run_remove_routines
|
||||||
from utils.input_utils import get_selection_input
|
from utils.input_utils import get_selection_input
|
||||||
from utils.sys_utils import cmd_sysctl_manage
|
|
||||||
|
|
||||||
|
|
||||||
def run_moonraker_removal(
|
def run_moonraker_removal(
|
||||||
@@ -27,17 +26,17 @@ def run_moonraker_removal(
|
|||||||
remove_env: bool,
|
remove_env: bool,
|
||||||
remove_polkit: bool,
|
remove_polkit: bool,
|
||||||
) -> None:
|
) -> None:
|
||||||
im = InstanceManager(Moonraker)
|
moonraker_instances = InstanceManager(Moonraker).instances
|
||||||
|
|
||||||
if remove_service:
|
if remove_service:
|
||||||
Logger.print_status("Removing Moonraker instances ...")
|
Logger.print_status("Removing Moonraker instances ...")
|
||||||
if im.instances:
|
if moonraker_instances:
|
||||||
instances_to_remove = select_instances_to_remove(im.instances)
|
instances_to_remove = select_instances_to_remove(moonraker_instances)
|
||||||
remove_instances(im, instances_to_remove)
|
remove_instances(instances_to_remove)
|
||||||
else:
|
else:
|
||||||
Logger.print_info("No Moonraker Services installed! Skipped ...")
|
Logger.print_info("No Moonraker Services installed! Skipped ...")
|
||||||
|
|
||||||
if (remove_polkit or remove_dir or remove_env) and im.instances:
|
if (remove_polkit or remove_dir or remove_env) and moonraker_instances:
|
||||||
Logger.print_info("There are still other Moonraker services installed")
|
Logger.print_info("There are still other Moonraker services installed")
|
||||||
Logger.print_info(
|
Logger.print_info(
|
||||||
"● Moonraker PolicyKit rules were not removed.", prefix=False
|
"● Moonraker PolicyKit rules were not removed.", prefix=False
|
||||||
@@ -84,20 +83,14 @@ def select_instances_to_remove(
|
|||||||
|
|
||||||
|
|
||||||
def remove_instances(
|
def remove_instances(
|
||||||
instance_manager: InstanceManager,
|
|
||||||
instance_list: List[Moonraker] | None,
|
instance_list: List[Moonraker] | None,
|
||||||
) -> None:
|
) -> None:
|
||||||
if not instance_list:
|
if not instance_list:
|
||||||
Logger.print_info("No Moonraker instances found. Skipped ...")
|
Logger.print_info("No Moonraker instances found. Skipped ...")
|
||||||
return
|
return
|
||||||
for instance in instance_list:
|
for instance in instance_list:
|
||||||
Logger.print_status(f"Removing instance {instance.get_service_file_name()} ...")
|
Logger.print_status(f"Removing instance {instance.service_file_path.stem} ...")
|
||||||
instance_manager.current_instance = instance
|
instance.remove()
|
||||||
instance_manager.stop_instance()
|
|
||||||
instance_manager.disable_instance()
|
|
||||||
instance_manager.delete_instance()
|
|
||||||
|
|
||||||
cmd_sysctl_manage("daemon-reload")
|
|
||||||
|
|
||||||
|
|
||||||
def remove_polkit_rules() -> None:
|
def remove_polkit_rules() -> None:
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ from components.moonraker.moonraker_dialogs import print_moonraker_overview
|
|||||||
from components.moonraker.moonraker_utils import (
|
from components.moonraker.moonraker_utils import (
|
||||||
backup_moonraker_dir,
|
backup_moonraker_dir,
|
||||||
create_example_moonraker_conf,
|
create_example_moonraker_conf,
|
||||||
|
moonraker_factory,
|
||||||
)
|
)
|
||||||
from components.webui_client.client_utils import (
|
from components.webui_client.client_utils import (
|
||||||
enable_mainsail_remotemode,
|
enable_mainsail_remotemode,
|
||||||
@@ -50,6 +51,7 @@ from utils.input_utils import (
|
|||||||
from utils.sys_utils import (
|
from utils.sys_utils import (
|
||||||
check_python_version,
|
check_python_version,
|
||||||
cmd_sysctl_manage,
|
cmd_sysctl_manage,
|
||||||
|
cmd_sysctl_service,
|
||||||
create_python_venv,
|
create_python_venv,
|
||||||
install_python_requirements,
|
install_python_requirements,
|
||||||
parse_packages_from_file,
|
parse_packages_from_file,
|
||||||
@@ -61,14 +63,12 @@ def install_moonraker() -> None:
|
|||||||
return
|
return
|
||||||
|
|
||||||
klipper_list: List[Klipper] = InstanceManager(Klipper).instances
|
klipper_list: List[Klipper] = InstanceManager(Klipper).instances
|
||||||
mr_im = InstanceManager(Moonraker)
|
moonraker_list: List[Moonraker] = InstanceManager(Moonraker).instances
|
||||||
moonraker_list: List[Moonraker] = mr_im.instances
|
instances: List[Moonraker] = []
|
||||||
|
|
||||||
instance_names = []
|
|
||||||
selected_option: str | Klipper
|
selected_option: str | Klipper
|
||||||
|
|
||||||
if len(klipper_list) == 0:
|
if len(klipper_list) == 1:
|
||||||
instance_names.append(klipper_list[0].suffix)
|
instances.append(moonraker_factory(klipper_list[0]))
|
||||||
else:
|
else:
|
||||||
print_moonraker_overview(
|
print_moonraker_overview(
|
||||||
klipper_list,
|
klipper_list,
|
||||||
@@ -87,12 +87,12 @@ def install_moonraker() -> None:
|
|||||||
return
|
return
|
||||||
|
|
||||||
if selected_option == "a":
|
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:
|
else:
|
||||||
klipper_instance: Klipper | None = options.get(selected_option)
|
klipper_instance: Klipper | None = options.get(selected_option)
|
||||||
if klipper_instance is None:
|
if klipper_instance is None:
|
||||||
raise Exception("Error selecting instance!")
|
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?")
|
create_example_cfg = get_confirm("Create example moonraker.conf?")
|
||||||
|
|
||||||
@@ -102,26 +102,23 @@ def install_moonraker() -> None:
|
|||||||
install_moonraker_polkit()
|
install_moonraker_polkit()
|
||||||
|
|
||||||
used_ports_map = {m.suffix: m.port for m in moonraker_list}
|
used_ports_map = {m.suffix: m.port for m in moonraker_list}
|
||||||
for name in instance_names:
|
for instance in instances:
|
||||||
current_instance = Moonraker(suffix=name)
|
instance.create()
|
||||||
|
cmd_sysctl_service(instance.service_file_path.name, "enable")
|
||||||
mr_im.current_instance = current_instance
|
|
||||||
mr_im.create_instance()
|
|
||||||
mr_im.enable_instance()
|
|
||||||
|
|
||||||
if create_example_cfg:
|
if create_example_cfg:
|
||||||
# if a webclient and/or it's config is installed, patch
|
# if a webclient and/or it's config is installed, patch
|
||||||
# its update section to the config
|
# its update section to the config
|
||||||
clients = get_existing_clients()
|
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")
|
cmd_sysctl_manage("daemon-reload")
|
||||||
|
|
||||||
# if mainsail is installed, and we installed
|
# if mainsail is installed, and we installed
|
||||||
# multiple moonraker instances, we enable mainsails remote mode
|
# 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()
|
enable_mainsail_remotemode()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -10,9 +10,11 @@
|
|||||||
import shutil
|
import shutil
|
||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
|
from components.klipper.klipper import Klipper
|
||||||
from components.moonraker import (
|
from components.moonraker import (
|
||||||
MODULE_PATH,
|
MODULE_PATH,
|
||||||
MOONRAKER_BACKUP_DIR,
|
MOONRAKER_BACKUP_DIR,
|
||||||
|
MOONRAKER_CFG_NAME,
|
||||||
MOONRAKER_DB_BACKUP_DIR,
|
MOONRAKER_DB_BACKUP_DIR,
|
||||||
MOONRAKER_DEFAULT_PORT,
|
MOONRAKER_DEFAULT_PORT,
|
||||||
MOONRAKER_DIR,
|
MOONRAKER_DIR,
|
||||||
@@ -26,11 +28,30 @@ from core.logger import Logger
|
|||||||
from core.submodules.simple_config_parser.src.simple_config_parser.simple_config_parser import (
|
from core.submodules.simple_config_parser.src.simple_config_parser.simple_config_parser import (
|
||||||
SimpleConfigParser,
|
SimpleConfigParser,
|
||||||
)
|
)
|
||||||
|
from core.types import ComponentStatus
|
||||||
from utils.common import get_install_status
|
from utils.common import get_install_status
|
||||||
from utils.sys_utils import (
|
from utils.sys_utils import (
|
||||||
get_ipv4_addr,
|
get_ipv4_addr,
|
||||||
)
|
)
|
||||||
from core.types import ComponentStatus
|
|
||||||
|
|
||||||
|
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:
|
def get_moonraker_status() -> ComponentStatus:
|
||||||
|
|||||||
@@ -31,10 +31,10 @@ from core.logger import Logger
|
|||||||
class Octoeverywhere(BaseInstance):
|
class Octoeverywhere(BaseInstance):
|
||||||
dir: Path = OE_DIR
|
dir: Path = OE_DIR
|
||||||
env_dir: Path = OE_ENV_DIR
|
env_dir: Path = OE_ENV_DIR
|
||||||
|
log_file_name = OE_LOG_NAME
|
||||||
store_dir: Path = OE_STORE_DIR
|
store_dir: Path = OE_STORE_DIR
|
||||||
cfg_file: Path | None = None
|
cfg_file: Path | None = None
|
||||||
sys_cfg_file: Path | None = None
|
sys_cfg_file: Path | None = None
|
||||||
log: Path | None = None
|
|
||||||
|
|
||||||
def __init__(self, suffix: str = ""):
|
def __init__(self, suffix: str = ""):
|
||||||
super().__init__(suffix=suffix)
|
super().__init__(suffix=suffix)
|
||||||
@@ -43,7 +43,6 @@ class Octoeverywhere(BaseInstance):
|
|||||||
super().__post_init__()
|
super().__post_init__()
|
||||||
self.cfg_file = self.cfg_dir.joinpath(OE_CFG_NAME)
|
self.cfg_file = self.cfg_dir.joinpath(OE_CFG_NAME)
|
||||||
self.sys_cfg_file = self.cfg_dir.joinpath(OE_SYS_CFG_NAME)
|
self.sys_cfg_file = self.cfg_dir.joinpath(OE_SYS_CFG_NAME)
|
||||||
self.log = self.log_dir.joinpath(OE_LOG_NAME)
|
|
||||||
|
|
||||||
def create(self) -> None:
|
def create(self) -> None:
|
||||||
Logger.print_status("Creating OctoEverywhere for Klipper Instance ...")
|
Logger.print_status("Creating OctoEverywhere for Klipper Instance ...")
|
||||||
@@ -64,20 +63,3 @@ class Octoeverywhere(BaseInstance):
|
|||||||
except CalledProcessError as e:
|
except CalledProcessError as e:
|
||||||
Logger.print_error(f"Error updating OctoEverywhere for Klipper: {e}")
|
Logger.print_error(f"Error updating OctoEverywhere for Klipper: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def delete(self) -> None:
|
|
||||||
service_file: str = self.get_service_file_name(extension=True)
|
|
||||||
service_file_path: Path = self.get_service_file_path()
|
|
||||||
|
|
||||||
Logger.print_status(
|
|
||||||
f"Deleting OctoEverywhere for Klipper Instance: {service_file}"
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
command = ["sudo", "rm", "-f", service_file_path.as_posix()]
|
|
||||||
run(command, check=True)
|
|
||||||
self.delete_logfiles(OE_LOG_NAME)
|
|
||||||
Logger.print_ok(f"Service file deleted: {service_file_path}")
|
|
||||||
except CalledProcessError as e:
|
|
||||||
Logger.print_error(f"Error deleting service file: {e}")
|
|
||||||
raise
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ from components.octoeverywhere import (
|
|||||||
from components.octoeverywhere.octoeverywhere import Octoeverywhere
|
from components.octoeverywhere.octoeverywhere import Octoeverywhere
|
||||||
from core.instance_manager.instance_manager import InstanceManager
|
from core.instance_manager.instance_manager import InstanceManager
|
||||||
from core.logger import DialogType, Logger
|
from core.logger import DialogType, Logger
|
||||||
|
from core.types import ComponentStatus
|
||||||
from utils.common import (
|
from utils.common import (
|
||||||
check_install_dependencies,
|
check_install_dependencies,
|
||||||
get_install_status,
|
get_install_status,
|
||||||
@@ -35,11 +36,9 @@ from utils.fs_utils import run_remove_routines
|
|||||||
from utils.git_utils import git_clone_wrapper
|
from utils.git_utils import git_clone_wrapper
|
||||||
from utils.input_utils import get_confirm
|
from utils.input_utils import get_confirm
|
||||||
from utils.sys_utils import (
|
from utils.sys_utils import (
|
||||||
cmd_sysctl_manage,
|
|
||||||
install_python_requirements,
|
install_python_requirements,
|
||||||
parse_packages_from_file,
|
parse_packages_from_file,
|
||||||
)
|
)
|
||||||
from core.types import ComponentStatus
|
|
||||||
|
|
||||||
|
|
||||||
def get_octoeverywhere_status() -> ComponentStatus:
|
def get_octoeverywhere_status() -> ComponentStatus:
|
||||||
@@ -142,7 +141,7 @@ def remove_octoeverywhere() -> None:
|
|||||||
ob_instances: List[Octoeverywhere] = ob_im.instances
|
ob_instances: List[Octoeverywhere] = ob_im.instances
|
||||||
|
|
||||||
try:
|
try:
|
||||||
remove_oe_instances(ob_im, ob_instances)
|
remove_oe_instances(ob_instances)
|
||||||
remove_oe_dir()
|
remove_oe_dir()
|
||||||
remove_oe_env()
|
remove_oe_env()
|
||||||
remove_config_section(f"include {OE_SYS_CFG_NAME}", mr_instances)
|
remove_config_section(f"include {OE_SYS_CFG_NAME}", mr_instances)
|
||||||
@@ -173,7 +172,6 @@ def install_oe_dependencies() -> None:
|
|||||||
|
|
||||||
|
|
||||||
def remove_oe_instances(
|
def remove_oe_instances(
|
||||||
instance_manager: InstanceManager,
|
|
||||||
instance_list: List[Octoeverywhere],
|
instance_list: List[Octoeverywhere],
|
||||||
) -> None:
|
) -> None:
|
||||||
if not instance_list:
|
if not instance_list:
|
||||||
@@ -181,13 +179,8 @@ def remove_oe_instances(
|
|||||||
return
|
return
|
||||||
|
|
||||||
for instance in instance_list:
|
for instance in instance_list:
|
||||||
Logger.print_status(f"Removing instance {instance.get_service_file_name()} ...")
|
Logger.print_status(f"Removing instance {instance.service_file_path.stem} ...")
|
||||||
instance_manager.current_instance = instance
|
instance.remove()
|
||||||
instance_manager.stop_instance()
|
|
||||||
instance_manager.disable_instance()
|
|
||||||
instance_manager.delete_instance()
|
|
||||||
|
|
||||||
cmd_sysctl_manage("daemon-reload")
|
|
||||||
|
|
||||||
|
|
||||||
def remove_oe_dir() -> None:
|
def remove_oe_dir() -> None:
|
||||||
|
|||||||
@@ -23,19 +23,24 @@ from core.logger import Logger
|
|||||||
class BaseInstance(ABC):
|
class BaseInstance(ABC):
|
||||||
suffix: str
|
suffix: str
|
||||||
user: str = field(default=CURRENT_USER, init=False)
|
user: str = field(default=CURRENT_USER, init=False)
|
||||||
|
service_file_path: Path | None = None
|
||||||
|
is_legacy_instance: bool = False
|
||||||
data_dir: Path | None = None
|
data_dir: Path | None = None
|
||||||
data_dir_name: str = ""
|
data_dir_name: str = ""
|
||||||
is_legacy_instance: bool = False
|
|
||||||
cfg_dir: Path | None = None
|
cfg_dir: Path | None = None
|
||||||
log_dir: Path | None = None
|
sysd_dir: Path | None = None # NOT to be confused with /etc/systemd/system
|
||||||
comms_dir: Path | None = None
|
comms_dir: Path | None = None
|
||||||
sysd_dir: Path | None = None
|
|
||||||
gcodes_dir: Path | None = None
|
gcodes_dir: Path | None = None
|
||||||
|
log_dir: Path | None = None
|
||||||
|
log_file_name: str = ""
|
||||||
|
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
|
self._set_service_file_path()
|
||||||
self._set_data_dir()
|
self._set_data_dir()
|
||||||
self._set_is_legacy_instance()
|
|
||||||
if self.data_dir is not None:
|
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.cfg_dir = self.data_dir.joinpath("config")
|
||||||
self.log_dir = self.data_dir.joinpath("logs")
|
self.log_dir = self.data_dir.joinpath("logs")
|
||||||
self.comms_dir = self.data_dir.joinpath("comms")
|
self.comms_dir = self.data_dir.joinpath("comms")
|
||||||
@@ -50,9 +55,28 @@ class BaseInstance(ABC):
|
|||||||
def create(self) -> None:
|
def create(self) -> None:
|
||||||
raise NotImplementedError("Subclasses must implement the create method")
|
raise NotImplementedError("Subclasses must implement the create method")
|
||||||
|
|
||||||
@abstractmethod
|
def remove(self) -> None:
|
||||||
def delete(self) -> None:
|
from utils.fs_utils import run_remove_routines
|
||||||
raise NotImplementedError("Subclasses must implement the delete method")
|
from utils.sys_utils import remove_system_service
|
||||||
|
|
||||||
|
try:
|
||||||
|
# remove the service file
|
||||||
|
if self.service_file_path is not None:
|
||||||
|
remove_system_service(self.service_file_path.name)
|
||||||
|
|
||||||
|
# then remove all the log files
|
||||||
|
if not self.log_file_name or not self.log_dir or not self.log_dir.exists():
|
||||||
|
return
|
||||||
|
|
||||||
|
files = self.log_dir.iterdir()
|
||||||
|
logs = [f for f in files if f.name.startswith(self.log_file_name)]
|
||||||
|
for log in logs:
|
||||||
|
Logger.print_status(f"Remove '{log}'")
|
||||||
|
run_remove_routines(log)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
Logger.print_error(f"Error removing service: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
def create_folders(self, add_dirs: List[Path] | None = None) -> None:
|
def create_folders(self, add_dirs: List[Path] | None = None) -> None:
|
||||||
dirs: List[Path | None] = [
|
dirs: List[Path | None] = [
|
||||||
@@ -72,22 +96,7 @@ class BaseInstance(ABC):
|
|||||||
continue
|
continue
|
||||||
_dir.mkdir(exist_ok=True)
|
_dir.mkdir(exist_ok=True)
|
||||||
|
|
||||||
# todo: refactor into a set method and access the value by accessing the property
|
def remove_logfiles(self, log_name: str) -> None:
|
||||||
def get_service_file_name(self, extension: bool = False) -> str:
|
|
||||||
from utils.common import convert_camelcase_to_kebabcase
|
|
||||||
|
|
||||||
name: str = convert_camelcase_to_kebabcase(self.__class__.__name__)
|
|
||||||
if self.suffix != "":
|
|
||||||
name += f"-{self.suffix}"
|
|
||||||
|
|
||||||
return name if not extension else f"{name}.service"
|
|
||||||
|
|
||||||
# todo: refactor into a set method and access the value by accessing the property
|
|
||||||
def get_service_file_path(self) -> Path:
|
|
||||||
path: Path = SYSTEMD.joinpath(self.get_service_file_name(extension=True))
|
|
||||||
return path
|
|
||||||
|
|
||||||
def delete_logfiles(self, log_name: str) -> None:
|
|
||||||
from utils.fs_utils import run_remove_routines
|
from utils.fs_utils import run_remove_routines
|
||||||
|
|
||||||
if not self.log_dir or not self.log_dir.exists():
|
if not self.log_dir or not self.log_dir.exists():
|
||||||
@@ -105,20 +114,27 @@ class BaseInstance(ABC):
|
|||||||
else:
|
else:
|
||||||
self.data_dir = Path.home().joinpath(f"printer_{self.suffix}_data")
|
self.data_dir = Path.home().joinpath(f"printer_{self.suffix}_data")
|
||||||
|
|
||||||
if self.get_service_file_path().exists():
|
if self.service_file_path and self.service_file_path.exists():
|
||||||
with open(self.get_service_file_path(), "r") as service_file:
|
with open(self.service_file_path, "r") as service_file:
|
||||||
service_content = service_file.read()
|
lines = service_file.readlines()
|
||||||
pattern = re.compile("^EnvironmentFile=(.+)(/systemd/.+\.env)")
|
for line in lines:
|
||||||
match = re.search(pattern, service_content)
|
pattern = r"^EnvironmentFile=(.+)(/systemd/.+\.env)"
|
||||||
if match:
|
match = re.search(pattern, line)
|
||||||
self.data_dir = Path(match.group(1))
|
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
|
||||||
|
|
||||||
|
name: str = convert_camelcase_to_kebabcase(self.__class__.__name__)
|
||||||
|
if self.suffix != "":
|
||||||
|
name += f"-{self.suffix}"
|
||||||
|
|
||||||
|
self.service_file_path = SYSTEMD.joinpath(f"{name}.service")
|
||||||
|
|
||||||
def _set_is_legacy_instance(self) -> None:
|
def _set_is_legacy_instance(self) -> None:
|
||||||
if (
|
legacy_pattern = r"^(?!printer)(.+)_data"
|
||||||
self.suffix != ""
|
match = re.search(legacy_pattern, self.data_dir_name)
|
||||||
and not self.data_dir_name.startswith("printer_")
|
if match and self.suffix != "":
|
||||||
and not self.data_dir_name.endswith("_data")
|
|
||||||
):
|
|
||||||
self.is_legacy_instance = True
|
self.is_legacy_instance = True
|
||||||
else:
|
|
||||||
self.is_legacy_instance = False
|
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ class InstanceManager:
|
|||||||
self._current_instance = value
|
self._current_instance = value
|
||||||
if value is not None:
|
if value is not None:
|
||||||
self.instance_suffix = value.suffix
|
self.instance_suffix = value.suffix
|
||||||
self.instance_service = value.get_service_file_name()
|
self.instance_service = value.service_file_path.stem
|
||||||
self.instance_service_path = value.get_service_file_path()
|
self.instance_service_path = value.service_file_path
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def instance_suffix(self) -> str | None:
|
def instance_suffix(self) -> str | None:
|
||||||
@@ -98,16 +98,6 @@ class InstanceManager:
|
|||||||
else:
|
else:
|
||||||
raise ValueError("current_instance cannot be None")
|
raise ValueError("current_instance cannot be None")
|
||||||
|
|
||||||
def delete_instance(self) -> None:
|
|
||||||
if self.current_instance is not None:
|
|
||||||
try:
|
|
||||||
self.current_instance.delete()
|
|
||||||
except (OSError, subprocess.CalledProcessError) as e:
|
|
||||||
Logger.print_error(f"Removing instance failed: {e}")
|
|
||||||
raise
|
|
||||||
else:
|
|
||||||
raise ValueError("current_instance cannot be None")
|
|
||||||
|
|
||||||
def enable_instance(self) -> None:
|
def enable_instance(self) -> None:
|
||||||
try:
|
try:
|
||||||
cmd_sysctl_service(self.instance_service_full, "enable")
|
cmd_sysctl_service(self.instance_service_full, "enable")
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ from extensions.obico import (
|
|||||||
class MoonrakerObico(BaseInstance):
|
class MoonrakerObico(BaseInstance):
|
||||||
dir: Path = OBICO_DIR
|
dir: Path = OBICO_DIR
|
||||||
env_dir: Path = OBICO_ENV_DIR
|
env_dir: Path = OBICO_ENV_DIR
|
||||||
|
log_file_name = OBICO_LOG_NAME
|
||||||
cfg_file: Path | None = None
|
cfg_file: Path | None = None
|
||||||
log: Path | None = None
|
|
||||||
is_linked: bool = False
|
is_linked: bool = False
|
||||||
|
|
||||||
def __init__(self, suffix: str = ""):
|
def __init__(self, suffix: str = ""):
|
||||||
@@ -44,7 +44,6 @@ class MoonrakerObico(BaseInstance):
|
|||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
super().__post_init__()
|
super().__post_init__()
|
||||||
self.cfg_file = self.cfg_dir.joinpath(OBICO_CFG_NAME)
|
self.cfg_file = self.cfg_dir.joinpath(OBICO_CFG_NAME)
|
||||||
self.log = self.log_dir.joinpath(OBICO_LOG_NAME)
|
|
||||||
self.is_linked: bool = self._check_link_status()
|
self.is_linked: bool = self._check_link_status()
|
||||||
|
|
||||||
def create(self) -> None:
|
def create(self) -> None:
|
||||||
@@ -55,7 +54,7 @@ class MoonrakerObico(BaseInstance):
|
|||||||
try:
|
try:
|
||||||
self.create_folders()
|
self.create_folders()
|
||||||
create_service_file(
|
create_service_file(
|
||||||
name=self.get_service_file_name(extension=True),
|
name=self.service_file_path.name,
|
||||||
content=self._prep_service_file_content(),
|
content=self._prep_service_file_content(),
|
||||||
)
|
)
|
||||||
create_env_file(
|
create_env_file(
|
||||||
@@ -70,21 +69,6 @@ class MoonrakerObico(BaseInstance):
|
|||||||
Logger.print_error(f"Error creating env file: {e}")
|
Logger.print_error(f"Error creating env file: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def delete(self) -> None:
|
|
||||||
service_file: str = self.get_service_file_name(extension=True)
|
|
||||||
service_file_path: Path = self.get_service_file_path()
|
|
||||||
|
|
||||||
Logger.print_status(f"Deleting Obico for Klipper Instance: {service_file}")
|
|
||||||
|
|
||||||
try:
|
|
||||||
command = ["sudo", "rm", "-f", service_file_path.as_posix()]
|
|
||||||
run(command, check=True)
|
|
||||||
self.delete_logfiles(OBICO_LOG_NAME)
|
|
||||||
Logger.print_ok(f"Service file deleted: {service_file_path}")
|
|
||||||
except CalledProcessError as e:
|
|
||||||
Logger.print_error(f"Error deleting service file: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
def link(self) -> None:
|
def link(self) -> None:
|
||||||
Logger.print_status(
|
Logger.print_status(
|
||||||
f"Linking instance for printer {self.data_dir_name} to the Obico server ..."
|
f"Linking instance for printer {self.data_dir_name} to the Obico server ..."
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ class ObicoExtension(BaseExtension):
|
|||||||
ob_instances: List[MoonrakerObico] = ob_im.instances
|
ob_instances: List[MoonrakerObico] = ob_im.instances
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._remove_obico_instances(ob_im, ob_instances)
|
self._remove_obico_instances(ob_instances)
|
||||||
self._remove_obico_dir()
|
self._remove_obico_dir()
|
||||||
self._remove_obico_env()
|
self._remove_obico_env()
|
||||||
remove_config_section(f"include {OBICO_MACROS_CFG_NAME}", kl_instances)
|
remove_config_section(f"include {OBICO_MACROS_CFG_NAME}", kl_instances)
|
||||||
@@ -288,7 +288,11 @@ class ObicoExtension(BaseExtension):
|
|||||||
scp.read(obico.cfg_file)
|
scp.read(obico.cfg_file)
|
||||||
scp.set("server", "url", self.server_url)
|
scp.set("server", "url", self.server_url)
|
||||||
scp.set("moonraker", "port", str(moonraker.port))
|
scp.set("moonraker", "port", str(moonraker.port))
|
||||||
scp.set("logging", "path", obico.log.as_posix())
|
scp.set(
|
||||||
|
"logging",
|
||||||
|
"path",
|
||||||
|
obico.log_dir.joinpath(obico.log_file_name).as_posix(),
|
||||||
|
)
|
||||||
scp.write(obico.cfg_file)
|
scp.write(obico.cfg_file)
|
||||||
|
|
||||||
def _patch_printer_cfg(self, klipper: List[Klipper]) -> None:
|
def _patch_printer_cfg(self, klipper: List[Klipper]) -> None:
|
||||||
@@ -336,7 +340,6 @@ class ObicoExtension(BaseExtension):
|
|||||||
|
|
||||||
def _remove_obico_instances(
|
def _remove_obico_instances(
|
||||||
self,
|
self,
|
||||||
instance_manager: InstanceManager,
|
|
||||||
instance_list: List[MoonrakerObico],
|
instance_list: List[MoonrakerObico],
|
||||||
) -> None:
|
) -> None:
|
||||||
if not instance_list:
|
if not instance_list:
|
||||||
@@ -345,14 +348,9 @@ class ObicoExtension(BaseExtension):
|
|||||||
|
|
||||||
for instance in instance_list:
|
for instance in instance_list:
|
||||||
Logger.print_status(
|
Logger.print_status(
|
||||||
f"Removing instance {instance.get_service_file_name()} ..."
|
f"Removing instance {instance.service_file_path.stem} ..."
|
||||||
)
|
)
|
||||||
instance_manager.current_instance = instance
|
instance.remove()
|
||||||
instance_manager.stop_instance()
|
|
||||||
instance_manager.disable_instance()
|
|
||||||
instance_manager.delete_instance()
|
|
||||||
|
|
||||||
cmd_sysctl_manage("daemon-reload")
|
|
||||||
|
|
||||||
def _remove_obico_dir(self) -> None:
|
def _remove_obico_dir(self) -> None:
|
||||||
Logger.print_status("Removing Obico for Klipper directory ...")
|
Logger.print_status("Removing Obico for Klipper directory ...")
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from subprocess import CalledProcessError, run
|
from subprocess import CalledProcessError
|
||||||
|
|
||||||
from core.instance_manager.base_instance import BaseInstance
|
from core.instance_manager.base_instance import BaseInstance
|
||||||
from core.logger import Logger
|
from core.logger import Logger
|
||||||
@@ -30,8 +30,8 @@ from extensions.telegram_bot import (
|
|||||||
class MoonrakerTelegramBot(BaseInstance):
|
class MoonrakerTelegramBot(BaseInstance):
|
||||||
bot_dir: Path = TG_BOT_DIR
|
bot_dir: Path = TG_BOT_DIR
|
||||||
env_dir: Path = TG_BOT_ENV
|
env_dir: Path = TG_BOT_ENV
|
||||||
|
log_file_name = TG_BOT_LOG_NAME
|
||||||
cfg_file: Path | None = None
|
cfg_file: Path | None = None
|
||||||
log: Path | None = None
|
|
||||||
|
|
||||||
def __init__(self, suffix: str = ""):
|
def __init__(self, suffix: str = ""):
|
||||||
super().__init__(suffix=suffix)
|
super().__init__(suffix=suffix)
|
||||||
@@ -39,7 +39,6 @@ class MoonrakerTelegramBot(BaseInstance):
|
|||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
super().__post_init__()
|
super().__post_init__()
|
||||||
self.cfg_file = self.cfg_dir.joinpath(TG_BOT_CFG_NAME)
|
self.cfg_file = self.cfg_dir.joinpath(TG_BOT_CFG_NAME)
|
||||||
self.log = self.log_dir.joinpath(TG_BOT_LOG_NAME)
|
|
||||||
|
|
||||||
def create(self) -> None:
|
def create(self) -> None:
|
||||||
from utils.sys_utils import create_env_file, create_service_file
|
from utils.sys_utils import create_env_file, create_service_file
|
||||||
@@ -49,7 +48,7 @@ class MoonrakerTelegramBot(BaseInstance):
|
|||||||
try:
|
try:
|
||||||
self.create_folders()
|
self.create_folders()
|
||||||
create_service_file(
|
create_service_file(
|
||||||
name=self.get_service_file_name(extension=True),
|
name=self.service_file_path.name,
|
||||||
content=self._prep_service_file_content(),
|
content=self._prep_service_file_content(),
|
||||||
)
|
)
|
||||||
create_env_file(
|
create_env_file(
|
||||||
@@ -64,20 +63,6 @@ class MoonrakerTelegramBot(BaseInstance):
|
|||||||
Logger.print_error(f"Error creating env file: {e}")
|
Logger.print_error(f"Error creating env file: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def delete(self) -> None:
|
|
||||||
service_file: str = self.get_service_file_name(extension=True)
|
|
||||||
service_file_path: Path = self.get_service_file_path()
|
|
||||||
|
|
||||||
Logger.print_status(f"Deleting Moonraker Telegram Bot Instance: {service_file}")
|
|
||||||
|
|
||||||
try:
|
|
||||||
command = ["sudo", "rm", "-f", service_file_path.as_posix()]
|
|
||||||
run(command, check=True)
|
|
||||||
Logger.print_ok(f"Service file deleted: {service_file_path}")
|
|
||||||
except CalledProcessError as e:
|
|
||||||
Logger.print_error(f"Error deleting service file: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
def _prep_service_file_content(self) -> str:
|
def _prep_service_file_content(self) -> str:
|
||||||
template = TG_BOT_SERVICE_TEMPLATE
|
template = TG_BOT_SERVICE_TEMPLATE
|
||||||
|
|
||||||
@@ -126,6 +111,6 @@ class MoonrakerTelegramBot(BaseInstance):
|
|||||||
)
|
)
|
||||||
env_file_content = env_file_content.replace(
|
env_file_content = env_file_content.replace(
|
||||||
"%LOG%",
|
"%LOG%",
|
||||||
self.log.as_posix() if self.log else "",
|
self.log_dir.joinpath(self.log_file_name).as_posix(),
|
||||||
)
|
)
|
||||||
return env_file_content
|
return env_file_content
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ class TelegramBotExtension(BaseExtension):
|
|||||||
tb_instances: List[MoonrakerTelegramBot] = tb_im.instances
|
tb_instances: List[MoonrakerTelegramBot] = tb_im.instances
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._remove_bot_instances(tb_im, tb_instances)
|
self._remove_bot_instances(tb_instances)
|
||||||
self._remove_bot_dir()
|
self._remove_bot_dir()
|
||||||
self._remove_bot_env()
|
self._remove_bot_env()
|
||||||
remove_config_section("update_manager moonraker-telegram-bot", mr_instances)
|
remove_config_section("update_manager moonraker-telegram-bot", mr_instances)
|
||||||
@@ -181,19 +181,13 @@ class TelegramBotExtension(BaseExtension):
|
|||||||
|
|
||||||
def _remove_bot_instances(
|
def _remove_bot_instances(
|
||||||
self,
|
self,
|
||||||
instance_manager: InstanceManager,
|
|
||||||
instance_list: List[MoonrakerTelegramBot],
|
instance_list: List[MoonrakerTelegramBot],
|
||||||
) -> None:
|
) -> None:
|
||||||
for instance in instance_list:
|
for instance in instance_list:
|
||||||
Logger.print_status(
|
Logger.print_status(
|
||||||
f"Removing instance {instance.get_service_file_name()} ..."
|
f"Removing instance {instance.service_file_path.stem} ..."
|
||||||
)
|
)
|
||||||
instance_manager.current_instance = instance
|
instance.remove()
|
||||||
instance_manager.stop_instance()
|
|
||||||
instance_manager.disable_instance()
|
|
||||||
instance_manager.delete_instance()
|
|
||||||
|
|
||||||
cmd_sysctl_manage("daemon-reload")
|
|
||||||
|
|
||||||
def _remove_bot_dir(self) -> None:
|
def _remove_bot_dir(self) -> None:
|
||||||
if not TG_BOT_DIR.exists():
|
if not TG_BOT_DIR.exists():
|
||||||
|
|||||||
Reference in New Issue
Block a user