Compare commits

..

3 Commits

Author SHA1 Message Date
dw-0
ea991644cd refactor: create moonraker instances based on klipper instances
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-08-20 20:22:01 +02:00
dw-0
fe0bfc5376 refactor: create a remove base method and use it in all subclasses
replace get_service_file_path with service_file_path and remove get_service_file_name

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-08-19 21:14:19 +02:00
dw-0
fdfdf18dd2 chore: sort imports
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
2024-08-19 21:07:06 +02:00
18 changed files with 139 additions and 225 deletions

View File

@@ -31,6 +31,7 @@ from core.constants import CURRENT_USER
from core.instance_manager.instance_manager import InstanceManager
from core.logger import DialogType, Logger
from core.settings.kiauh_settings import KiauhSettings
from core.types import ComponentStatus
from utils.common import (
check_install_dependencies,
get_install_status,
@@ -44,7 +45,6 @@ from utils.sys_utils import (
cmd_sysctl_service,
parse_packages_from_file,
)
from core.types import ComponentStatus
def install_crowsnest() -> None:

View File

@@ -10,7 +10,7 @@ from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from subprocess import CalledProcessError, run
from subprocess import CalledProcessError
from components.klipper import (
KLIPPER_CFG_NAME,
@@ -33,7 +33,6 @@ class Klipper(BaseInstance):
klipper_dir: Path = KLIPPER_DIR
env_dir: Path = KLIPPER_ENV_DIR
cfg_file: Path | None = None
log: Path | None = None
serial: Path | None = None
uds: Path | None = None
@@ -42,8 +41,8 @@ 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.log = self.log_dir.joinpath(KLIPPER_LOG_NAME)
self.serial = self.comms_dir.joinpath(KLIPPER_SERIAL_NAME)
self.uds = self.comms_dir.joinpath(KLIPPER_UDS_NAME)
@@ -56,7 +55,7 @@ class Klipper(BaseInstance):
self.create_folders()
create_service_file(
name=self.get_service_file_name(extension=True),
name=self.service_file_path.name,
content=self._prep_service_file_content(),
)
@@ -72,21 +71,6 @@ class Klipper(BaseInstance):
Logger.print_error(f"Error creating env file: {e}")
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:
template = KLIPPER_SERVICE_TEMPLATE
@@ -138,7 +122,7 @@ class Klipper(BaseInstance):
)
env_file_content = env_file_content.replace(
"%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(
"%UDS%",

View File

@@ -53,7 +53,7 @@ def print_instance_overview(
for i, s in enumerate(instances):
if display_type is DisplayType.SERVICE_NAME:
name = s.get_service_file_name()
name = s.service_file_path.stem
else:
name = s.data_dir
line = f"{COLOR_CYAN}{f'{i + start_index})' if show_index else ''} {name}{RESET_FORMAT}"

View File

@@ -17,7 +17,6 @@ from core.instance_manager.instance_manager import InstanceManager
from core.logger import Logger
from utils.fs_utils import run_remove_routines
from utils.input_utils import get_selection_input
from utils.sys_utils import cmd_sysctl_manage
def run_klipper_removal(
@@ -25,17 +24,17 @@ def run_klipper_removal(
remove_dir: bool,
remove_env: bool,
) -> None:
im = InstanceManager(Klipper)
klipper_instances = InstanceManager(Klipper).instances
if remove_service:
Logger.print_status("Removing Klipper instances ...")
if im.instances:
instances_to_remove = select_instances_to_remove(im.instances)
remove_instances(im, instances_to_remove)
if klipper_instances:
instances_to_remove = select_instances_to_remove(klipper_instances)
remove_instances(instances_to_remove)
else:
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(f"'{KLIPPER_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(
instance_manager: InstanceManager,
instance_list: List[Klipper] | None,
) -> None:
if not instance_list:
return
for instance in instance_list:
Logger.print_status(f"Removing instance {instance.get_service_file_name()} ...")
instance_manager.current_instance = instance
instance_manager.stop_instance()
instance_manager.disable_instance()
instance_manager.delete_instance()
cmd_sysctl_manage("daemon-reload")
Logger.print_status(f"Removing instance {instance.service_file_path.stem} ...")
instance.remove()
def delete_klipper_logs(instances: List[Klipper]) -> None:

View File

@@ -231,7 +231,7 @@ def display_moonraker_info(moonraker_list: List[Moonraker]) -> bool:
DialogType.INFO,
[
"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",
"The following Klipper instances will be installed:",
*[f"● klipper-{m.suffix}" for m in moonraker_list],

View File

@@ -10,7 +10,7 @@ from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from subprocess import CalledProcessError, run
from subprocess import CalledProcessError
from components.moonraker import (
MOONRAKER_CFG_NAME,
@@ -38,19 +38,18 @@ class Moonraker(BaseInstance):
backup_dir: Path | None = None
certs_dir: Path | None = None
db_dir: Path | None = None
log: Path | None = None
def __init__(self, suffix: str = ""):
super().__init__(suffix=suffix)
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")
self.certs_dir = self.data_dir.joinpath("certs")
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:
from utils.sys_utils import create_env_file, create_service_file
@@ -60,7 +59,7 @@ class Moonraker(BaseInstance):
try:
self.create_folders([self.backup_dir, self.certs_dir, self.db_dir])
create_service_file(
name=self.get_service_file_name(extension=True),
name=self.service_file_path.name,
content=self._prep_service_file_content(),
)
create_env_file(
@@ -75,21 +74,6 @@ class Moonraker(BaseInstance):
Logger.print_error(f"Error creating env file: {e}")
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:
template = MOONRAKER_SERVICE_TEMPLATE

View File

@@ -37,8 +37,8 @@ def print_moonraker_overview(
dialog += "║ ║\n"
instance_map = {
k.get_service_file_name(): (
k.get_service_file_name().replace("klipper", "moonraker")
k.service_file_path.stem: (
k.service_file_path.stem.replace("klipper", "moonraker")
if k.suffix in [m.suffix for m in moonraker_instances]
else ""
)

View File

@@ -18,7 +18,6 @@ from core.instance_manager.instance_manager import InstanceManager
from core.logger import Logger
from utils.fs_utils import run_remove_routines
from utils.input_utils import get_selection_input
from utils.sys_utils import cmd_sysctl_manage
def run_moonraker_removal(
@@ -27,17 +26,17 @@ def run_moonraker_removal(
remove_env: bool,
remove_polkit: bool,
) -> None:
im = InstanceManager(Moonraker)
moonraker_instances = InstanceManager(Moonraker).instances
if remove_service:
Logger.print_status("Removing Moonraker instances ...")
if im.instances:
instances_to_remove = select_instances_to_remove(im.instances)
remove_instances(im, instances_to_remove)
if moonraker_instances:
instances_to_remove = select_instances_to_remove(moonraker_instances)
remove_instances(instances_to_remove)
else:
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(
"● Moonraker PolicyKit rules were not removed.", prefix=False
@@ -84,20 +83,14 @@ def select_instances_to_remove(
def remove_instances(
instance_manager: InstanceManager,
instance_list: List[Moonraker] | None,
) -> None:
if not instance_list:
Logger.print_info("No Moonraker instances found. Skipped ...")
return
for instance in instance_list:
Logger.print_status(f"Removing instance {instance.get_service_file_name()} ...")
instance_manager.current_instance = instance
instance_manager.stop_instance()
instance_manager.disable_instance()
instance_manager.delete_instance()
cmd_sysctl_manage("daemon-reload")
Logger.print_status(f"Removing instance {instance.service_file_path.stem} ...")
instance.remove()
def remove_polkit_rules() -> None:

View File

@@ -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:

View File

@@ -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,
@@ -26,11 +28,30 @@ from core.logger import Logger
from core.submodules.simple_config_parser.src.simple_config_parser.simple_config_parser import (
SimpleConfigParser,
)
from core.types import ComponentStatus
from utils.common import get_install_status
from utils.sys_utils import (
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:

View File

@@ -31,10 +31,10 @@ from core.logger import Logger
class Octoeverywhere(BaseInstance):
dir: Path = OE_DIR
env_dir: Path = OE_ENV_DIR
log_file_name = OE_LOG_NAME
store_dir: Path = OE_STORE_DIR
cfg_file: Path | None = None
sys_cfg_file: Path | None = None
log: Path | None = None
def __init__(self, suffix: str = ""):
super().__init__(suffix=suffix)
@@ -43,7 +43,6 @@ class Octoeverywhere(BaseInstance):
super().__post_init__()
self.cfg_file = self.cfg_dir.joinpath(OE_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:
Logger.print_status("Creating OctoEverywhere for Klipper Instance ...")
@@ -64,20 +63,3 @@ class Octoeverywhere(BaseInstance):
except CalledProcessError as e:
Logger.print_error(f"Error updating OctoEverywhere for Klipper: {e}")
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

View File

@@ -23,6 +23,7 @@ from components.octoeverywhere import (
from components.octoeverywhere.octoeverywhere import Octoeverywhere
from core.instance_manager.instance_manager import InstanceManager
from core.logger import DialogType, Logger
from core.types import ComponentStatus
from utils.common import (
check_install_dependencies,
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.input_utils import get_confirm
from utils.sys_utils import (
cmd_sysctl_manage,
install_python_requirements,
parse_packages_from_file,
)
from core.types import ComponentStatus
def get_octoeverywhere_status() -> ComponentStatus:
@@ -142,7 +141,7 @@ def remove_octoeverywhere() -> None:
ob_instances: List[Octoeverywhere] = ob_im.instances
try:
remove_oe_instances(ob_im, ob_instances)
remove_oe_instances(ob_instances)
remove_oe_dir()
remove_oe_env()
remove_config_section(f"include {OE_SYS_CFG_NAME}", mr_instances)
@@ -173,7 +172,6 @@ def install_oe_dependencies() -> None:
def remove_oe_instances(
instance_manager: InstanceManager,
instance_list: List[Octoeverywhere],
) -> None:
if not instance_list:
@@ -181,13 +179,8 @@ def remove_oe_instances(
return
for instance in instance_list:
Logger.print_status(f"Removing instance {instance.get_service_file_name()} ...")
instance_manager.current_instance = instance
instance_manager.stop_instance()
instance_manager.disable_instance()
instance_manager.delete_instance()
cmd_sysctl_manage("daemon-reload")
Logger.print_status(f"Removing instance {instance.service_file_path.stem} ...")
instance.remove()
def remove_oe_dir() -> None:

View File

@@ -23,19 +23,24 @@ from core.logger import Logger
class BaseInstance(ABC):
suffix: str
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_name: str = ""
is_legacy_instance: bool = False
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
sysd_dir: Path | None = None
gcodes_dir: Path | None = None
log_dir: Path | None = None
log_file_name: str = ""
def __post_init__(self) -> None:
self._set_service_file_path()
self._set_data_dir()
self._set_is_legacy_instance()
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")
@@ -50,9 +55,28 @@ class BaseInstance(ABC):
def create(self) -> None:
raise NotImplementedError("Subclasses must implement the create method")
@abstractmethod
def delete(self) -> None:
raise NotImplementedError("Subclasses must implement the delete method")
def remove(self) -> None:
from utils.fs_utils import run_remove_routines
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:
dirs: List[Path | None] = [
@@ -72,22 +96,7 @@ class BaseInstance(ABC):
continue
_dir.mkdir(exist_ok=True)
# todo: refactor into a set method and access the value by accessing the property
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:
def remove_logfiles(self, log_name: str) -> None:
from utils.fs_utils import run_remove_routines
if not self.log_dir or not self.log_dir.exists():
@@ -105,20 +114,27 @@ class BaseInstance(ABC):
else:
self.data_dir = Path.home().joinpath(f"printer_{self.suffix}_data")
if self.get_service_file_path().exists():
with open(self.get_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))
if self.service_file_path and self.service_file_path.exists():
with open(self.service_file_path, "r") as service_file:
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
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:
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

View File

@@ -49,8 +49,8 @@ class InstanceManager:
self._current_instance = value
if value is not None:
self.instance_suffix = value.suffix
self.instance_service = value.get_service_file_name()
self.instance_service_path = value.get_service_file_path()
self.instance_service = value.service_file_path.stem
self.instance_service_path = value.service_file_path
@property
def instance_suffix(self) -> str | None:
@@ -98,16 +98,6 @@ class InstanceManager:
else:
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:
try:
cmd_sysctl_service(self.instance_service_full, "enable")

View File

@@ -34,8 +34,8 @@ from extensions.obico import (
class MoonrakerObico(BaseInstance):
dir: Path = OBICO_DIR
env_dir: Path = OBICO_ENV_DIR
log_file_name = OBICO_LOG_NAME
cfg_file: Path | None = None
log: Path | None = None
is_linked: bool = False
def __init__(self, suffix: str = ""):
@@ -44,7 +44,6 @@ class MoonrakerObico(BaseInstance):
def __post_init__(self):
super().__post_init__()
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()
def create(self) -> None:
@@ -55,7 +54,7 @@ class MoonrakerObico(BaseInstance):
try:
self.create_folders()
create_service_file(
name=self.get_service_file_name(extension=True),
name=self.service_file_path.name,
content=self._prep_service_file_content(),
)
create_env_file(
@@ -70,21 +69,6 @@ class MoonrakerObico(BaseInstance):
Logger.print_error(f"Error creating env file: {e}")
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:
Logger.print_status(
f"Linking instance for printer {self.data_dir_name} to the Obico server ..."

View File

@@ -165,7 +165,7 @@ class ObicoExtension(BaseExtension):
ob_instances: List[MoonrakerObico] = ob_im.instances
try:
self._remove_obico_instances(ob_im, ob_instances)
self._remove_obico_instances(ob_instances)
self._remove_obico_dir()
self._remove_obico_env()
remove_config_section(f"include {OBICO_MACROS_CFG_NAME}", kl_instances)
@@ -288,7 +288,11 @@ class ObicoExtension(BaseExtension):
scp.read(obico.cfg_file)
scp.set("server", "url", self.server_url)
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)
def _patch_printer_cfg(self, klipper: List[Klipper]) -> None:
@@ -336,7 +340,6 @@ class ObicoExtension(BaseExtension):
def _remove_obico_instances(
self,
instance_manager: InstanceManager,
instance_list: List[MoonrakerObico],
) -> None:
if not instance_list:
@@ -345,14 +348,9 @@ class ObicoExtension(BaseExtension):
for instance in instance_list:
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_manager.stop_instance()
instance_manager.disable_instance()
instance_manager.delete_instance()
cmd_sysctl_manage("daemon-reload")
instance.remove()
def _remove_obico_dir(self) -> None:
Logger.print_status("Removing Obico for Klipper directory ...")

View File

@@ -10,7 +10,7 @@ from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from subprocess import CalledProcessError, run
from subprocess import CalledProcessError
from core.instance_manager.base_instance import BaseInstance
from core.logger import Logger
@@ -30,8 +30,8 @@ from extensions.telegram_bot import (
class MoonrakerTelegramBot(BaseInstance):
bot_dir: Path = TG_BOT_DIR
env_dir: Path = TG_BOT_ENV
log_file_name = TG_BOT_LOG_NAME
cfg_file: Path | None = None
log: Path | None = None
def __init__(self, suffix: str = ""):
super().__init__(suffix=suffix)
@@ -39,7 +39,6 @@ class MoonrakerTelegramBot(BaseInstance):
def __post_init__(self):
super().__post_init__()
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:
from utils.sys_utils import create_env_file, create_service_file
@@ -49,7 +48,7 @@ class MoonrakerTelegramBot(BaseInstance):
try:
self.create_folders()
create_service_file(
name=self.get_service_file_name(extension=True),
name=self.service_file_path.name,
content=self._prep_service_file_content(),
)
create_env_file(
@@ -64,20 +63,6 @@ class MoonrakerTelegramBot(BaseInstance):
Logger.print_error(f"Error creating env file: {e}")
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:
template = TG_BOT_SERVICE_TEMPLATE
@@ -126,6 +111,6 @@ class MoonrakerTelegramBot(BaseInstance):
)
env_file_content = env_file_content.replace(
"%LOG%",
self.log.as_posix() if self.log else "",
self.log_dir.joinpath(self.log_file_name).as_posix(),
)
return env_file_content

View File

@@ -144,7 +144,7 @@ class TelegramBotExtension(BaseExtension):
tb_instances: List[MoonrakerTelegramBot] = tb_im.instances
try:
self._remove_bot_instances(tb_im, tb_instances)
self._remove_bot_instances(tb_instances)
self._remove_bot_dir()
self._remove_bot_env()
remove_config_section("update_manager moonraker-telegram-bot", mr_instances)
@@ -181,19 +181,13 @@ class TelegramBotExtension(BaseExtension):
def _remove_bot_instances(
self,
instance_manager: InstanceManager,
instance_list: List[MoonrakerTelegramBot],
) -> None:
for instance in instance_list:
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_manager.stop_instance()
instance_manager.disable_instance()
instance_manager.delete_instance()
cmd_sysctl_manage("daemon-reload")
instance.remove()
def _remove_bot_dir(self) -> None:
if not TG_BOT_DIR.exists():