feat(klipper): implement instance manager and klipper installer in python

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2023-10-26 13:44:43 +02:00
parent f45da66e9e
commit ce0daa52ae
28 changed files with 1821 additions and 69 deletions

View File

View File

@@ -0,0 +1,164 @@
# !/usr/bin/env python
# ======================================================================= #
# Copyright (C) 2020 - 2023 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 os
import pwd
import shutil
import subprocess
from pathlib import Path
from typing import List
from kiauh.instance_manager.base_instance import BaseInstance
from kiauh.utils.constants import SYSTEMD, KLIPPER_DIR, KLIPPER_ENV_DIR
from kiauh.utils.logger import Logger
from kiauh.utils.system_utils import create_directory
# noinspection PyMethodMayBeStatic
class Klipper(BaseInstance):
@classmethod
def blacklist(cls) -> List[str]:
return ["None", "mcu"]
def __init__(self, name: str):
super().__init__(name=name,
prefix="klipper",
user=pwd.getpwuid(os.getuid())[0],
data_dir_name=self._get_data_dir_from_name(name))
self.klipper_dir = KLIPPER_DIR
self.env_dir = KLIPPER_ENV_DIR
self.cfg_file = f"{self.cfg_dir}/printer.cfg"
self.log = f"{self.log_dir}/klippy.log"
self.serial = f"{self.comms_dir}/klippy.serial"
self.uds = f"{self.comms_dir}/klippy.sock"
def create(self) -> None:
Logger.print_info("Creating Klipper Instance")
module_path = os.path.dirname(os.path.abspath(__file__))
service_template_path = os.path.join(module_path, "res",
"klipper.service")
env_template_file_path = os.path.join(module_path, "res", "klipper.env")
service_file_name = self.get_service_file_name(extension=True)
service_file_target = f"{SYSTEMD}/{service_file_name}"
env_file_target = os.path.abspath(f"{self.sysd_dir}/klipper.env")
# create folder structure
dirs = [self.data_dir, self.cfg_dir, self.log_dir,
self.comms_dir, self.sysd_dir]
for _dir in dirs:
create_directory(Path(_dir))
try:
# writing the klipper service file (requires sudo!)
service_content = self._prep_service_file(service_template_path,
env_file_target)
command = ["sudo", "tee", service_file_target]
subprocess.run(command, input=service_content.encode(),
stdout=subprocess.DEVNULL, check=True)
Logger.print_ok(f"Service file created: {service_file_target}")
# writing the klipper.env file
env_file_content = self._prep_env_file(env_template_file_path)
with open(env_file_target, "w") as env_file:
env_file.write(env_file_content)
Logger.print_ok(f"Env file created: {env_file_target}")
except subprocess.CalledProcessError as e:
Logger.print_error(
f"Error creating service file {service_file_target}: {e}")
raise
except OSError as e:
Logger.print_error(
f"Error creating env file {env_file_target}: {e}")
raise
def read(self) -> None:
print("Reading Klipper Instance")
def update(self) -> None:
print("Updating Klipper Instance")
def delete(self, del_remnants: bool) -> None:
service_file = self.get_service_file_name(extension=True)
service_file_path = self._get_service_file_path()
Logger.print_info(f"Deleting Klipper Instance: {service_file}")
try:
command = ["sudo", "rm", "-f", service_file_path]
subprocess.run(command, check=True)
Logger.print_ok(f"Service file deleted: {service_file_path}")
except subprocess.CalledProcessError as e:
Logger.print_error(f"Error deleting service file: {e}")
raise
if del_remnants:
self._delete_klipper_remnants()
def _delete_klipper_remnants(self) -> None:
try:
Logger.print_info(f"Delete {self.klipper_dir} ...")
shutil.rmtree(Path(self.klipper_dir))
Logger.print_info(f"Delete {self.env_dir} ...")
shutil.rmtree(Path(self.env_dir))
except FileNotFoundError:
Logger.print_info("Cannot delete Klipper directories. Not found.")
except PermissionError as e:
Logger.print_error(f"Error deleting Klipper directories: {e}")
raise
Logger.print_ok("Directories successfully deleted.")
def get_service_file_name(self, extension=False) -> str:
name = self.prefix if self.name is None else self.prefix + '-' + self.name
return name if not extension else f"{name}.service"
def _get_service_file_path(self):
return f"{SYSTEMD}/{self.get_service_file_name(extension=True)}"
def _get_data_dir_from_name(self, name: str) -> str:
if name is None:
return "printer"
elif int(name.isdigit()):
return f"printer_{name}"
else:
return name
def _prep_service_file(self, service_template_path, env_file_path):
try:
with open(service_template_path, "r") as template_file:
template_content = template_file.read()
except FileNotFoundError:
Logger.print_error(
f"Unable to open {service_template_path} - File not found")
raise
service_content = template_content.replace("%USER%", self.user)
service_content = service_content.replace("%KLIPPER_DIR%",
self.klipper_dir)
service_content = service_content.replace("%ENV%", self.env_dir)
service_content = service_content.replace("%ENV_FILE%", env_file_path)
return service_content
def _prep_env_file(self, env_template_file_path):
try:
with open(env_template_file_path, "r") as env_file:
env_template_file_content = env_file.read()
except FileNotFoundError:
Logger.print_error(
f"Unable to open {env_template_file_path} - File not found")
raise
env_file_content = env_template_file_content.replace("%KLIPPER_DIR%",
self.klipper_dir)
env_file_content = env_file_content.replace("%CFG%", self.cfg_file)
env_file_content = env_file_content.replace("%SERIAL%", self.serial)
env_file_content = env_file_content.replace("%LOG%", self.log)
env_file_content = env_file_content.replace("%UDS%", self.uds)
return env_file_content

View File

@@ -0,0 +1,236 @@
# !/usr/bin/env python
# ======================================================================= #
# Copyright (C) 2020 - 2023 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 os
import re
import subprocess
from pathlib import Path
from typing import Optional, List, Union
from kiauh.instance_manager.instance_manager import InstanceManager
from kiauh.modules.klipper.klipper import Klipper
from kiauh.modules.klipper.klipper_utils import print_instance_overview
from kiauh.utils.constants import KLIPPER_DIR, KLIPPER_ENV_DIR
from kiauh.utils.input_utils import get_user_confirm, get_user_number_input, \
get_user_string_input, get_user_selection_input
from kiauh.utils.logger import Logger
from kiauh.utils.system_utils import parse_packages_from_file, \
clone_repo, create_python_venv, \
install_python_requirements, update_system_package_lists, \
install_system_packages
def run_klipper_setup(install: bool) -> None:
instance_manager = InstanceManager(Klipper)
instance_list = instance_manager.get_instances()
instances_installed = len(instance_list)
is_klipper_installed = check_klipper_installation(instance_manager)
if not install and not is_klipper_installed:
Logger.print_warn("Klipper not installed!")
return
if install:
add_additional = handle_existing_instances(instance_manager)
if is_klipper_installed and not add_additional:
Logger.print_info("Exiting Klipper setup ...")
return
install_klipper(instance_manager)
if not install:
if instances_installed == 1:
remove_single_instance(instance_manager)
else:
remove_multi_instance(instance_manager)
def check_klipper_installation(instance_manager: InstanceManager) -> bool:
instance_list = instance_manager.get_instances()
instances_installed = len(instance_list)
if instances_installed < 1:
return False
return True
def handle_existing_instances(instance_manager: InstanceManager) -> bool:
instance_list = instance_manager.get_instances()
instance_count = len(instance_list)
if instance_count > 0:
print_instance_overview(instance_list)
if not get_user_confirm("Add new instances?"):
return False
return True
def install_klipper(instance_manager: InstanceManager) -> None:
instance_list = instance_manager.get_instances()
if_adding = " additional" if len(instance_list) > 0 else ""
install_count = get_user_number_input(
f"Number of{if_adding} Klipper instances to set up",
1, default=1)
instance_names = set_instance_names(instance_list, install_count)
if len(instance_list) < 1:
setup_klipper_prerequesites()
for name in instance_names:
current_instance = Klipper(name=name)
instance_manager.set_current_instance(current_instance)
instance_manager.create_instance()
instance_manager.enable_instance()
instance_manager.start_instance()
instance_manager.reload_daemon()
# step 4: check/handle conflicting packages/services
# step 5: check for required group membership
def setup_klipper_prerequesites() -> None:
# clone klipper TODO: read branch and url from json to allow forks
url = "https://github.com/Klipper3D/klipper"
branch = "master"
clone_repo(Path(KLIPPER_DIR), url, branch)
# install klipper dependencies and create python virtualenv
install_klipper_packages(Path(KLIPPER_DIR))
create_python_venv(Path(KLIPPER_ENV_DIR))
klipper_py_req = Path(f"{KLIPPER_DIR}/scripts/klippy-requirements.txt")
install_python_requirements(Path(KLIPPER_ENV_DIR), klipper_py_req)
def install_klipper_packages(klipper_dir: Path) -> None:
script = f"{klipper_dir}/scripts/install-debian.sh"
packages = parse_packages_from_file(script)
packages = [pkg.replace("python-dev", "python3-dev") for pkg in packages]
# Add dfu-util for octopi-images
packages.append("dfu-util")
# Add dbus requirement for DietPi distro
if os.path.exists("/boot/dietpi/.version"):
packages.append("dbus")
update_system_package_lists(silent=False)
install_system_packages(packages)
def set_instance_names(instance_list, install_count: int) -> List[
Union[str, None]]:
instance_count = len(instance_list)
# default single instance install
if instance_count == 0 and install_count == 1:
return [None]
# new multi instance install
elif ((instance_count == 0 and install_count > 1)
# or convert single instance install to multi instance install
or (instance_count == 1 and install_count >= 1)):
if get_user_confirm("Assign custom names?", False):
return assign_custom_names(instance_count, install_count, None)
else:
_range = range(1, install_count + 1)
return [str(i) for i in _range]
# existing multi instance install
elif instance_count > 1:
if has_custom_names(instance_list):
return assign_custom_names(instance_count, install_count,
instance_list)
else:
start = get_highest_index(instance_list) + 1
_range = range(start, start + install_count)
return [str(i) for i in _range]
def has_custom_names(instance_list: List[Klipper]) -> bool:
pattern = re.compile("^\d+$")
for instance in instance_list:
if not pattern.match(instance.name):
return True
return False
def assign_custom_names(instance_count: int, install_count: int,
instance_list: Optional[List[Klipper]]) -> List[str]:
instance_names = []
exclude = Klipper.blacklist()
# if an instance_list is provided, exclude all existing instance names
if instance_list is not None:
for instance in instance_list:
exclude.append(instance.name)
for i in range(instance_count + install_count):
question = f"Enter name for instance {i + 1}"
name = get_user_string_input(question, exclude=exclude)
instance_names.append(name)
exclude.append(name)
return instance_names
def get_highest_index(instance_list: List[Klipper]) -> int:
indices = [int(instance.name.split('-')[-1]) for instance in instance_list]
return max(indices)
def remove_single_instance(instance_manager: InstanceManager) -> None:
instance_list = instance_manager.get_instances()
try:
instance_manager.set_current_instance(instance_list[0])
instance_manager.stop_instance()
instance_manager.disable_instance()
instance_manager.delete_instance(del_remnants=True)
instance_manager.reload_daemon()
except (OSError, subprocess.CalledProcessError):
Logger.print_error("Removing instance failed!")
return
def remove_multi_instance(instance_manager: InstanceManager) -> None:
instance_list = instance_manager.get_instances()
print_instance_overview(instance_list, show_index=True,
show_select_all=True)
options = [str(i) for i in range(len(instance_list))]
options.extend(["a", "A", "b", "B"])
selection = get_user_selection_input(
"Select Klipper instance to remove", options)
print(selection)
if selection == "b".lower():
return
elif selection == "a".lower():
Logger.print_info("Removing all Klipper instances ...")
for instance in instance_list:
instance_manager.set_current_instance(instance)
instance_manager.stop_instance()
instance_manager.disable_instance()
instance_manager.delete_instance(del_remnants=True)
else:
instance = instance_list[int(selection)]
Logger.print_info(
f"Removing Klipper instance: {instance.get_service_file_name()}")
instance_manager.set_current_instance(instance)
instance_manager.stop_instance()
instance_manager.disable_instance()
instance_manager.delete_instance(del_remnants=False)
instance_manager.reload_daemon()

View File

@@ -0,0 +1,39 @@
#!/usr/bin/env python
# ======================================================================= #
# Copyright (C) 2020 - 2023 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 typing import List
from kiauh.instance_manager.base_instance import BaseInstance
from kiauh.menus.base_menu import print_back_footer
from kiauh.utils.constants import COLOR_GREEN, COLOR_CYAN, COLOR_YELLOW, \
RESET_FORMAT
def print_instance_overview(instances: List[BaseInstance], show_index=False,
show_select_all=False):
headline = f"{COLOR_GREEN}The following Klipper instances were found:{RESET_FORMAT}"
print("/=======================================================\\")
print(f"|{'{:^64}'.format(headline)}|")
print("|-------------------------------------------------------|")
if show_select_all:
select_all = f" {COLOR_YELLOW}a) Select all{RESET_FORMAT}"
print(f"|{'{:64}'.format(select_all)}|")
print("| |")
for i, s in enumerate(instances):
index = f"{i})" if show_index else ""
instance = s.get_service_file_name()
line = f"{'{:53}'.format(f'{index} {instance}')}"
print(f"| {COLOR_CYAN}{line}{RESET_FORMAT}|")
print_back_footer()

View File

@@ -0,0 +1 @@
KLIPPER_ARGS="%KLIPPER_DIR%/klippy/klippy.py %CFG% -I %SERIAL% -l %LOG% -a %UDS%"

View File

@@ -0,0 +1,18 @@
[Unit]
Description=Klipper 3D Printer Firmware SV1
Documentation=https://www.klipper3d.org/
After=network-online.target
Wants=udev.target
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
User=%USER%
RemainAfterExit=yes
WorkingDirectory=%KLIPPER_DIR%
EnvironmentFile=%ENV_FILE%
ExecStart=%ENV%/bin/python $KLIPPER_ARGS
Restart=always
RestartSec=10