feat: KIAUH v6 - full rewrite of KIAUH in Python (#428)

This commit is contained in:
dw-0
2024-08-31 19:16:52 +02:00
committed by GitHub
parent 8547942986
commit 0ee0fa3325
159 changed files with 13461 additions and 54 deletions

View File

@@ -0,0 +1,34 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from pathlib import Path
MODULE_PATH = Path(__file__).resolve().parent
# repo
OBICO_REPO = "https://github.com/TheSpaghettiDetective/moonraker-obico.git"
# names
OBICO_SERVICE_NAME = "moonraker-obico.service"
OBICO_ENV_FILE_NAME = "moonraker-obico.env"
OBICO_CFG_NAME = "moonraker-obico.cfg"
OBICO_CFG_SAMPLE_NAME = "moonraker-obico.cfg.sample"
OBICO_LOG_NAME = "moonraker-obico.log"
OBICO_UPDATE_CFG_NAME = "moonraker-obico-update.cfg"
OBICO_UPDATE_CFG_SAMPLE_NAME = "moonraker-obico-update.cfg.sample"
OBICO_MACROS_CFG_NAME = "moonraker_obico_macros.cfg"
# directories
OBICO_DIR = Path.home().joinpath("moonraker-obico")
OBICO_ENV_DIR = Path.home().joinpath("moonraker-obico-env")
# files
OBICO_SERVICE_TEMPLATE = MODULE_PATH.joinpath(f"assets/{OBICO_SERVICE_NAME}")
OBICO_ENV_FILE_TEMPLATE = MODULE_PATH.joinpath(f"assets/{OBICO_ENV_FILE_NAME}")
OBICO_LINK_SCRIPT = OBICO_DIR.joinpath("scripts/link.sh")
OBICO_REQ_FILE = OBICO_DIR.joinpath("requirements.txt")

View File

@@ -0,0 +1 @@
OBICO_ARGS="-m moonraker_obico.app -c %CFG%"

View File

@@ -0,0 +1,16 @@
#Systemd service file for moonraker-obico
[Unit]
Description=Moonraker-Obico
After=network-online.target moonraker.service
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
User=%USER%
WorkingDirectory=%OBICO_DIR%
EnvironmentFile=%ENV_FILE%
ExecStart=%ENV%/bin/python3 $OBICO_ARGS
Restart=always
RestartSec=5

View File

@@ -0,0 +1,16 @@
{
"metadata": {
"index": 6,
"module": "moonraker_obico_extension",
"maintained_by": "Obico",
"display_name": "Obico for Klipper",
"description": [
"Open source 3D Printing cloud and AI",
"- AI-Powered Failure Detection",
"- Free Remote Monitoring and Access",
"- 25FPS High-Def Webcam Streaming",
"- Free 4.9-Star Mobile App"
],
"updates": true
}
}

View File

@@ -0,0 +1,145 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
from __future__ import annotations
from dataclasses import dataclass, field
from pathlib import Path
from subprocess import CalledProcessError, run
from components.moonraker.moonraker import Moonraker
from core.constants import CURRENT_USER
from core.instance_manager.base_instance import BaseInstance
from core.logger import Logger
from core.submodules.simple_config_parser.src.simple_config_parser.simple_config_parser import (
SimpleConfigParser,
)
from extensions.obico import (
OBICO_CFG_NAME,
OBICO_DIR,
OBICO_ENV_DIR,
OBICO_ENV_FILE_NAME,
OBICO_ENV_FILE_TEMPLATE,
OBICO_LINK_SCRIPT,
OBICO_LOG_NAME,
OBICO_SERVICE_TEMPLATE,
)
from utils.fs_utils import create_folders
from utils.sys_utils import get_service_file_path
# noinspection PyMethodMayBeStatic
@dataclass(repr=True)
class MoonrakerObico:
suffix: str
base: BaseInstance = field(init=False, repr=False)
service_file_path: Path = field(init=False)
log_file_name: str = OBICO_LOG_NAME
dir: Path = OBICO_DIR
env_dir: Path = OBICO_ENV_DIR
data_dir: Path = field(init=False)
cfg_file: Path = field(init=False)
is_linked: bool = False
def __post_init__(self):
self.base: BaseInstance = BaseInstance(Moonraker, self.suffix)
self.base.log_file_name = self.log_file_name
self.service_file_path: Path = get_service_file_path(
MoonrakerObico, self.suffix
)
self.data_dir: Path = self.base.data_dir
self.cfg_file = self.base.cfg_dir.joinpath(OBICO_CFG_NAME)
self.is_linked: bool = self._check_link_status()
def create(self) -> None:
from utils.sys_utils import create_env_file, create_service_file
Logger.print_status("Creating new Obico for Klipper Instance ...")
try:
create_folders(self.base.base_folders)
create_service_file(
name=self.service_file_path.name,
content=self._prep_service_file_content(),
)
create_env_file(
path=self.base.sysd_dir.joinpath(OBICO_ENV_FILE_NAME),
content=self._prep_env_file_content(),
)
except CalledProcessError as e:
Logger.print_error(f"Error creating instance: {e}")
raise
except OSError as e:
Logger.print_error(f"Error creating env file: {e}")
raise
def link(self) -> None:
Logger.print_status(
f"Linking instance for printer {self.data_dir.name} to the Obico server ..."
)
try:
cmd = [f"{OBICO_LINK_SCRIPT} -q -c {self.cfg_file}"]
if self.suffix:
cmd.append(f"-n {self.suffix}")
run(cmd, check=True, shell=True)
except CalledProcessError as e:
Logger.print_error(f"Error during Obico linking: {e}")
raise
def _prep_service_file_content(self) -> str:
template = OBICO_SERVICE_TEMPLATE
try:
with open(template, "r") as template_file:
template_content = template_file.read()
except FileNotFoundError:
Logger.print_error(f"Unable to open {template} - File not found")
raise
service_content = template_content.replace(
"%USER%",
CURRENT_USER,
)
service_content = service_content.replace(
"%OBICO_DIR%",
self.dir.as_posix(),
)
service_content = service_content.replace(
"%ENV%",
self.env_dir.as_posix(),
)
service_content = service_content.replace(
"%ENV_FILE%",
self.base.sysd_dir.joinpath(OBICO_ENV_FILE_NAME).as_posix(),
)
return service_content
def _prep_env_file_content(self) -> str:
template = OBICO_ENV_FILE_TEMPLATE
try:
with open(template, "r") as env_file:
env_template_file_content = env_file.read()
except FileNotFoundError:
Logger.print_error(f"Unable to open {template} - File not found")
raise
env_file_content = env_template_file_content.replace(
"%CFG%",
f"{self.base.cfg_dir}/{self.cfg_file}",
)
return env_file_content
def _check_link_status(self) -> bool:
if not self.cfg_file or not self.cfg_file.exists():
return False
scp = SimpleConfigParser()
scp.read(self.cfg_file)
return scp.get("server", "auth_token", None) is not None

View File

@@ -0,0 +1,367 @@
# ======================================================================= #
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/dw-0/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= #
import shutil
from typing import List
from components.klipper.klipper import Klipper
from components.moonraker.moonraker import Moonraker
from core.instance_manager.instance_manager import InstanceManager
from core.logger import DialogType, Logger
from core.submodules.simple_config_parser.src.simple_config_parser.simple_config_parser import (
SimpleConfigParser,
)
from extensions.base_extension import BaseExtension
from extensions.obico import (
OBICO_CFG_SAMPLE_NAME,
OBICO_DIR,
OBICO_ENV_DIR,
OBICO_MACROS_CFG_NAME,
OBICO_REPO,
OBICO_REQ_FILE,
OBICO_UPDATE_CFG_NAME,
OBICO_UPDATE_CFG_SAMPLE_NAME,
)
from extensions.obico.moonraker_obico import (
MoonrakerObico,
)
from utils.common import check_install_dependencies, moonraker_exists
from utils.config_utils import (
add_config_section,
remove_config_section,
)
from utils.fs_utils import run_remove_routines
from utils.git_utils import git_clone_wrapper, git_pull_wrapper
from utils.input_utils import get_confirm, get_selection_input, get_string_input
from utils.instance_utils import get_instances
from utils.sys_utils import (
cmd_sysctl_manage,
cmd_sysctl_service,
create_python_venv,
install_python_requirements,
parse_packages_from_file,
)
# noinspection PyMethodMayBeStatic
class ObicoExtension(BaseExtension):
server_url: str
def install_extension(self, **kwargs) -> None:
Logger.print_status("Installing Obico for Klipper ...")
# check if moonraker is installed. if not, notify the user and exit
if not moonraker_exists():
return
# if obico is already installed, ask if the user wants to repair an
# incomplete installation or link to the obico server
force_clone = False
obico_instances: List[MoonrakerObico] = get_instances(MoonrakerObico)
if obico_instances:
self._print_is_already_installed()
options = ["l", "r", "b"]
action = get_selection_input("Perform action", option_list=options)
if action.lower() == "b":
Logger.print_info("Exiting Obico for Klipper installation ...")
return
elif action.lower() == "l":
unlinked_instances: List[MoonrakerObico] = [
obico for obico in obico_instances if not obico.is_linked
]
self._link_obico_instances(unlinked_instances)
return
else:
Logger.print_status("Re-Installing Obico for Klipper ...")
force_clone = True
# let the user confirm installation
kl_instances: List[Klipper] = get_instances(Klipper)
mr_instances: List[Moonraker] = get_instances(Moonraker)
self._print_moonraker_instances(mr_instances)
if not get_confirm(
"Continue Obico for Klipper installation?",
default_choice=True,
allow_go_back=True,
):
return
try:
git_clone_wrapper(OBICO_REPO, OBICO_DIR, force=force_clone)
self._install_dependencies()
# ask the user for the obico server url
self._get_server_url()
# create obico instances
for moonraker in mr_instances:
instance = MoonrakerObico(suffix=moonraker.suffix)
instance.create()
cmd_sysctl_service(instance.service_file_path.name, "enable")
# create obico config
self._create_obico_cfg(instance, moonraker)
# create obico macros
self._create_obico_macros_cfg(moonraker)
# create obico update manager
self._create_obico_update_manager_cfg(moonraker)
cmd_sysctl_service(instance.service_file_path.name, "start")
cmd_sysctl_manage("daemon-reload")
# add to klippers config
self._patch_printer_cfg(kl_instances)
InstanceManager.restart_all(kl_instances)
# add to moonraker update manager
self._patch_moonraker_conf(mr_instances)
InstanceManager.restart_all(mr_instances)
# check linking of / ask for linking instances
self._check_and_opt_link_instances()
Logger.print_dialog(
DialogType.SUCCESS,
["Obico for Klipper successfully installed!"],
center_content=True,
)
except Exception as e:
Logger.print_error(f"Error during Obico for Klipper installation:\n{e}")
def update_extension(self, **kwargs) -> None:
Logger.print_status("Updating Obico for Klipper ...")
try:
instances = get_instances(MoonrakerObico)
InstanceManager.stop_all(instances)
git_pull_wrapper(OBICO_REPO, OBICO_DIR)
self._install_dependencies()
InstanceManager.start_all(instances)
Logger.print_ok("Obico for Klipper successfully updated!")
except Exception as e:
Logger.print_error(f"Error during Obico for Klipper update:\n{e}")
def remove_extension(self, **kwargs) -> None:
Logger.print_status("Removing Obico for Klipper ...")
kl_instances: List[Klipper] = get_instances(Klipper)
mr_instances: List[Moonraker] = get_instances(Moonraker)
ob_instances: List[MoonrakerObico] = get_instances(MoonrakerObico)
try:
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)
remove_config_section(f"include {OBICO_UPDATE_CFG_NAME}", mr_instances)
Logger.print_dialog(
DialogType.SUCCESS,
["Obico for Klipper successfully removed!"],
center_content=True,
)
except Exception as e:
Logger.print_error(f"Error during Obico for Klipper removal:\n{e}")
def _obico_server_url_prompt(self) -> None:
Logger.print_dialog(
DialogType.CUSTOM,
custom_title="Obico Server URL",
content=[
"You can use a self-hosted Obico Server or the Obico Cloud. "
"For more information, please visit:",
"https://obico.io.",
"\n\n",
"For the Obico Cloud, leave it as the default:",
"https://app.obico.io.",
"\n\n",
"For self-hosted server, specify:",
"http://server_ip:port",
"For instance, 'http://192.168.0.5:3334'.",
],
)
def _print_moonraker_instances(self, mr_instances: List[Moonraker]) -> None:
mr_names = [f"{moonraker.data_dir.name}" for moonraker in mr_instances]
if len(mr_names) > 1:
Logger.print_dialog(
DialogType.INFO,
[
"The following Moonraker instances were found:",
*mr_names,
"\n\n",
"The setup will apply the same names to Obico!",
],
)
def _print_is_already_installed(self) -> None:
Logger.print_dialog(
DialogType.INFO,
[
"Obico is already installed!",
"It is safe to run the installer again to link your "
"printer or repair any issues.",
"\n\n",
"You can perform the following actions:",
"L) Link printer to the Obico server",
"R) Repair installation",
],
)
def _get_server_url(self) -> None:
self._obico_server_url_prompt()
pattern = r"^(http|https)://[a-zA-Z0-9./?=_%:-]*$"
self.server_url = get_string_input(
"Obico Server URL",
regex=pattern,
default="https://app.obico.io",
)
def _install_dependencies(self) -> None:
# install dependencies
script = OBICO_DIR.joinpath("install.sh")
package_list = parse_packages_from_file(script)
check_install_dependencies({*package_list})
# create virtualenv
if create_python_venv(OBICO_ENV_DIR):
install_python_requirements(OBICO_ENV_DIR, OBICO_REQ_FILE)
def _create_obico_macros_cfg(self, moonraker: Moonraker) -> None:
macros_cfg = OBICO_DIR.joinpath(f"include_cfgs/{OBICO_MACROS_CFG_NAME}")
macros_target = moonraker.base.cfg_dir.joinpath(OBICO_MACROS_CFG_NAME)
if not macros_target.exists():
shutil.copy(macros_cfg, macros_target)
else:
Logger.print_info(
f"Obico's '{OBICO_MACROS_CFG_NAME}' in {moonraker.base.cfg_dir} already exists! Skipped ..."
)
def _create_obico_update_manager_cfg(self, moonraker: Moonraker) -> None:
update_cfg = OBICO_DIR.joinpath(OBICO_UPDATE_CFG_SAMPLE_NAME)
update_cfg_target = moonraker.base.cfg_dir.joinpath(OBICO_UPDATE_CFG_NAME)
if not update_cfg_target.exists():
shutil.copy(update_cfg, update_cfg_target)
else:
Logger.print_info(
f"Obico's '{OBICO_UPDATE_CFG_NAME}' in {moonraker.base.cfg_dir} already exists! Skipped ..."
)
def _create_obico_cfg(
self, current_instance: MoonrakerObico, moonraker: Moonraker
) -> None:
cfg_template = OBICO_DIR.joinpath(OBICO_CFG_SAMPLE_NAME)
cfg_target_file = current_instance.cfg_file
if not cfg_template.exists():
Logger.print_error(
f"Obico config template file {cfg_target_file} does not exist!"
)
return
if not cfg_target_file.exists():
shutil.copy(cfg_template, cfg_target_file)
self._patch_obico_cfg(moonraker, current_instance)
else:
Logger.print_info(
f"Obico config in {current_instance.base.cfg_dir} already exists! Skipped ..."
)
def _patch_obico_cfg(self, moonraker: Moonraker, obico: MoonrakerObico) -> None:
scp = SimpleConfigParser()
scp.read(obico.cfg_file)
scp.set("server", "url", self.server_url)
scp.set("moonraker", "port", str(moonraker.port))
scp.set(
"logging",
"path",
obico.base.log_dir.joinpath(obico.log_file_name).as_posix(),
)
scp.write(obico.cfg_file)
def _patch_printer_cfg(self, klipper: List[Klipper]) -> None:
add_config_section(
section=f"include {OBICO_MACROS_CFG_NAME}", instances=klipper
)
def _patch_moonraker_conf(self, instances: List[Moonraker]) -> None:
add_config_section(
section=f"include {OBICO_UPDATE_CFG_NAME}", instances=instances
)
def _link_obico_instances(self, unlinked_instances) -> None:
for obico in unlinked_instances:
obico.link()
def _check_and_opt_link_instances(self) -> None:
Logger.print_status("Checking link status of Obico instances ...")
ob_instances: List[MoonrakerObico] = get_instances(MoonrakerObico)
unlinked_instances: List[MoonrakerObico] = [
obico for obico in ob_instances if not obico.is_linked
]
if unlinked_instances:
Logger.print_dialog(
DialogType.INFO,
[
"The Obico instances for the following printers are not "
"linked to the server:",
*[f"{obico.data_dir.name}" for obico in unlinked_instances],
"\n\n",
"It will take only 10 seconds to link the printer to the Obico server.",
"For more information visit:",
"https://www.obico.io/docs/user-guides/klipper-setup/",
"\n\n",
"If you don't want to link the printer now, you can restart the "
"linking process later by running this installer again.",
],
)
if not get_confirm("Do you want to link the printers now?"):
Logger.print_info("Linking to Obico server skipped ...")
return
self._link_obico_instances(unlinked_instances)
def _remove_obico_instances(
self,
instance_list: List[MoonrakerObico],
) -> None:
if not instance_list:
Logger.print_info("No Obico instances found. Skipped ...")
return
for instance in instance_list:
Logger.print_status(
f"Removing instance {instance.service_file_path.stem} ..."
)
InstanceManager.remove(instance)
def _remove_obico_dir(self) -> None:
Logger.print_status("Removing Obico for Klipper directory ...")
if not OBICO_DIR.exists():
Logger.print_info(f"'{OBICO_DIR}' does not exist. Skipped ...")
return
run_remove_routines(OBICO_DIR)
def _remove_obico_env(self) -> None:
Logger.print_status("Removing Obico for Klipper environment ...")
if not OBICO_ENV_DIR.exists():
Logger.print_info(f"'{OBICO_ENV_DIR}' does not exist. Skipped ...")
return
run_remove_routines(OBICO_ENV_DIR)