mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-21 14:53:35 +05:00
feat: KIAUH v6 - full rewrite of KIAUH in Python (#428)
This commit is contained in:
45
kiauh/components/moonraker/__init__.py
Normal file
45
kiauh/components/moonraker/__init__.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# ======================================================================= #
|
||||
# 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
|
||||
|
||||
from core.backup_manager import BACKUP_ROOT_DIR
|
||||
|
||||
MODULE_PATH = Path(__file__).resolve().parent
|
||||
|
||||
# names
|
||||
MOONRAKER_CFG_NAME = "moonraker.conf"
|
||||
MOONRAKER_LOG_NAME = "moonraker.log"
|
||||
MOONRAKER_SERVICE_NAME = "moonraker.service"
|
||||
MOONRAKER_DEFAULT_PORT = 7125
|
||||
MOONRAKER_ENV_FILE_NAME = "moonraker.env"
|
||||
|
||||
# directories
|
||||
MOONRAKER_DIR = Path.home().joinpath("moonraker")
|
||||
MOONRAKER_ENV_DIR = Path.home().joinpath("moonraker-env")
|
||||
MOONRAKER_BACKUP_DIR = BACKUP_ROOT_DIR.joinpath("moonraker-backups")
|
||||
MOONRAKER_DB_BACKUP_DIR = BACKUP_ROOT_DIR.joinpath("moonraker-db-backups")
|
||||
|
||||
# files
|
||||
MOONRAKER_INSTALL_SCRIPT = MOONRAKER_DIR.joinpath("scripts/install-moonraker.sh")
|
||||
MOONRAKER_REQ_FILE = MOONRAKER_DIR.joinpath("scripts/moonraker-requirements.txt")
|
||||
MOONRAKER_SPEEDUPS_REQ_FILE = MOONRAKER_DIR.joinpath("scripts/moonraker-speedups.txt")
|
||||
MOONRAKER_DEPS_JSON_FILE = MOONRAKER_DIR.joinpath("scripts/system-dependencies.json")
|
||||
# introduced due to
|
||||
# https://github.com/Arksine/moonraker/issues/349
|
||||
# https://github.com/Arksine/moonraker/pull/346
|
||||
POLKIT_LEGACY_FILE = Path("/etc/polkit-1/localauthority/50-local.d/10-moonraker.pkla")
|
||||
POLKIT_FILE = Path("/etc/polkit-1/rules.d/moonraker.rules")
|
||||
POLKIT_USR_FILE = Path("/usr/share/polkit-1/rules.d/moonraker.rules")
|
||||
POLKIT_SCRIPT = MOONRAKER_DIR.joinpath("scripts/set-policykit-rules.sh")
|
||||
MOONRAKER_SERVICE_TEMPLATE = MODULE_PATH.joinpath(f"assets/{MOONRAKER_SERVICE_NAME}")
|
||||
MOONRAKER_ENV_FILE_TEMPLATE = MODULE_PATH.joinpath(f"assets/{MOONRAKER_ENV_FILE_NAME}")
|
||||
|
||||
|
||||
EXIT_MOONRAKER_SETUP = "Exiting Moonraker setup ..."
|
||||
29
kiauh/components/moonraker/assets/moonraker.conf
Normal file
29
kiauh/components/moonraker/assets/moonraker.conf
Normal file
@@ -0,0 +1,29 @@
|
||||
[server]
|
||||
host: 0.0.0.0
|
||||
port: %PORT%
|
||||
klippy_uds_address: %UDS%
|
||||
|
||||
[authorization]
|
||||
trusted_clients:
|
||||
10.0.0.0/8
|
||||
127.0.0.0/8
|
||||
169.254.0.0/16
|
||||
172.16.0.0/12
|
||||
192.168.0.0/16
|
||||
FE80::/10
|
||||
::1/128
|
||||
cors_domains:
|
||||
*.lan
|
||||
*.local
|
||||
*://localhost
|
||||
*://localhost:*
|
||||
*://my.mainsail.xyz
|
||||
*://app.fluidd.xyz
|
||||
|
||||
[octoprint_compat]
|
||||
|
||||
[history]
|
||||
|
||||
[update_manager]
|
||||
channel: dev
|
||||
refresh_interval: 168
|
||||
1
kiauh/components/moonraker/assets/moonraker.env
Normal file
1
kiauh/components/moonraker/assets/moonraker.env
Normal file
@@ -0,0 +1 @@
|
||||
MOONRAKER_ARGS="%MOONRAKER_DIR%/moonraker/moonraker.py -d %PRINTER_DATA%"
|
||||
19
kiauh/components/moonraker/assets/moonraker.service
Normal file
19
kiauh/components/moonraker/assets/moonraker.service
Normal file
@@ -0,0 +1,19 @@
|
||||
[Unit]
|
||||
Description=API Server for Klipper SV1
|
||||
Documentation=https://moonraker.readthedocs.io/
|
||||
Requires=network-online.target
|
||||
After=network-online.target
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=%USER%
|
||||
SupplementaryGroups=moonraker-admin
|
||||
RemainAfterExit=yes
|
||||
WorkingDirectory=%MOONRAKER_DIR%
|
||||
EnvironmentFile=%ENV_FILE%
|
||||
ExecStart=%ENV%/bin/python $MOONRAKER_ARGS
|
||||
Restart=always
|
||||
RestartSec=10
|
||||
0
kiauh/components/moonraker/menus/__init__.py
Normal file
0
kiauh/components/moonraker/menus/__init__.py
Normal file
128
kiauh/components/moonraker/menus/moonraker_remove_menu.py
Normal file
128
kiauh/components/moonraker/menus/moonraker_remove_menu.py
Normal file
@@ -0,0 +1,128 @@
|
||||
# ======================================================================= #
|
||||
# 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
|
||||
|
||||
import textwrap
|
||||
from typing import Type
|
||||
|
||||
from components.moonraker import moonraker_remove
|
||||
from core.constants import COLOR_CYAN, COLOR_RED, RESET_FORMAT
|
||||
from core.menus import Option
|
||||
from core.menus.base_menu import BaseMenu
|
||||
|
||||
|
||||
# noinspection PyUnusedLocal
|
||||
class MoonrakerRemoveMenu(BaseMenu):
|
||||
def __init__(self, previous_menu: Type[BaseMenu] | None = None):
|
||||
super().__init__()
|
||||
self.previous_menu: Type[BaseMenu] | None = previous_menu
|
||||
self.remove_moonraker_service = False
|
||||
self.remove_moonraker_dir = False
|
||||
self.remove_moonraker_env = False
|
||||
self.remove_moonraker_polkit = False
|
||||
self.selection_state = False
|
||||
|
||||
def set_previous_menu(self, previous_menu: Type[BaseMenu] | None) -> None:
|
||||
from core.menus.remove_menu import RemoveMenu
|
||||
|
||||
self.previous_menu = previous_menu if previous_menu is not None else RemoveMenu
|
||||
|
||||
def set_options(self) -> None:
|
||||
self.options = {
|
||||
"a": Option(method=self.toggle_all),
|
||||
"1": Option(method=self.toggle_remove_moonraker_service),
|
||||
"2": Option(method=self.toggle_remove_moonraker_dir),
|
||||
"3": Option(method=self.toggle_remove_moonraker_env),
|
||||
"4": Option(method=self.toggle_remove_moonraker_polkit),
|
||||
"c": Option(method=self.run_removal_process),
|
||||
}
|
||||
|
||||
def print_menu(self) -> None:
|
||||
header = " [ Remove Moonraker ] "
|
||||
color = COLOR_RED
|
||||
count = 62 - len(color) - len(RESET_FORMAT)
|
||||
checked = f"[{COLOR_CYAN}x{RESET_FORMAT}]"
|
||||
unchecked = "[ ]"
|
||||
o1 = checked if self.remove_moonraker_service else unchecked
|
||||
o2 = checked if self.remove_moonraker_dir else unchecked
|
||||
o3 = checked if self.remove_moonraker_env else unchecked
|
||||
o4 = checked if self.remove_moonraker_polkit else unchecked
|
||||
menu = textwrap.dedent(
|
||||
f"""
|
||||
╔═══════════════════════════════════════════════════════╗
|
||||
║ {color}{header:~^{count}}{RESET_FORMAT} ║
|
||||
╟───────────────────────────────────────────────────────╢
|
||||
║ Enter a number and hit enter to select / deselect ║
|
||||
║ the specific option for removal. ║
|
||||
╟───────────────────────────────────────────────────────╢
|
||||
║ a) {self._get_selection_state_str():37} ║
|
||||
╟───────────────────────────────────────────────────────╢
|
||||
║ 1) {o1} Remove Service ║
|
||||
║ 2) {o2} Remove Local Repository ║
|
||||
║ 3) {o3} Remove Python Environment ║
|
||||
║ 4) {o4} Remove Policy Kit Rules ║
|
||||
╟───────────────────────────────────────────────────────╢
|
||||
║ C) Continue ║
|
||||
╟───────────────────────────────────────────────────────╢
|
||||
"""
|
||||
)[1:]
|
||||
print(menu, end="")
|
||||
|
||||
def toggle_all(self, **kwargs) -> None:
|
||||
self.selection_state = not self.selection_state
|
||||
self.remove_moonraker_service = self.selection_state
|
||||
self.remove_moonraker_dir = self.selection_state
|
||||
self.remove_moonraker_env = self.selection_state
|
||||
self.remove_moonraker_polkit = self.selection_state
|
||||
|
||||
def toggle_remove_moonraker_service(self, **kwargs) -> None:
|
||||
self.remove_moonraker_service = not self.remove_moonraker_service
|
||||
|
||||
def toggle_remove_moonraker_dir(self, **kwargs) -> None:
|
||||
self.remove_moonraker_dir = not self.remove_moonraker_dir
|
||||
|
||||
def toggle_remove_moonraker_env(self, **kwargs) -> None:
|
||||
self.remove_moonraker_env = not self.remove_moonraker_env
|
||||
|
||||
def toggle_remove_moonraker_polkit(self, **kwargs) -> None:
|
||||
self.remove_moonraker_polkit = not self.remove_moonraker_polkit
|
||||
|
||||
def run_removal_process(self, **kwargs) -> None:
|
||||
if (
|
||||
not self.remove_moonraker_service
|
||||
and not self.remove_moonraker_dir
|
||||
and not self.remove_moonraker_env
|
||||
and not self.remove_moonraker_polkit
|
||||
):
|
||||
error = f"{COLOR_RED}Nothing selected! Select options to remove first.{RESET_FORMAT}"
|
||||
print(error)
|
||||
return
|
||||
|
||||
moonraker_remove.run_moonraker_removal(
|
||||
self.remove_moonraker_service,
|
||||
self.remove_moonraker_dir,
|
||||
self.remove_moonraker_env,
|
||||
self.remove_moonraker_polkit,
|
||||
)
|
||||
|
||||
self.remove_moonraker_service = False
|
||||
self.remove_moonraker_dir = False
|
||||
self.remove_moonraker_env = False
|
||||
self.remove_moonraker_polkit = False
|
||||
|
||||
self._go_back()
|
||||
|
||||
def _get_selection_state_str(self) -> str:
|
||||
return (
|
||||
"Select everything" if not self.selection_state else "Deselect everything"
|
||||
)
|
||||
|
||||
def _go_back(self, **kwargs) -> None:
|
||||
if self.previous_menu is not None:
|
||||
self.previous_menu().run()
|
||||
144
kiauh/components/moonraker/moonraker.py
Normal file
144
kiauh/components/moonraker/moonraker.py
Normal file
@@ -0,0 +1,144 @@
|
||||
# ======================================================================= #
|
||||
# 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
|
||||
|
||||
from components.klipper.klipper import Klipper
|
||||
from components.moonraker import (
|
||||
MOONRAKER_CFG_NAME,
|
||||
MOONRAKER_DIR,
|
||||
MOONRAKER_ENV_DIR,
|
||||
MOONRAKER_ENV_FILE_NAME,
|
||||
MOONRAKER_ENV_FILE_TEMPLATE,
|
||||
MOONRAKER_LOG_NAME,
|
||||
MOONRAKER_SERVICE_TEMPLATE,
|
||||
)
|
||||
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 utils.fs_utils import create_folders
|
||||
from utils.sys_utils import get_service_file_path
|
||||
|
||||
|
||||
# noinspection PyMethodMayBeStatic
|
||||
@dataclass
|
||||
class Moonraker:
|
||||
suffix: str
|
||||
base: BaseInstance = field(init=False, repr=False)
|
||||
service_file_path: Path = field(init=False)
|
||||
log_file_name: str = MOONRAKER_LOG_NAME
|
||||
moonraker_dir: Path = MOONRAKER_DIR
|
||||
env_dir: Path = MOONRAKER_ENV_DIR
|
||||
data_dir: Path = field(init=False)
|
||||
cfg_file: Path = field(init=False)
|
||||
backup_dir: Path = field(init=False)
|
||||
certs_dir: Path = field(init=False)
|
||||
db_dir: Path = field(init=False)
|
||||
port: int | None = field(init=False)
|
||||
|
||||
def __post_init__(self):
|
||||
self.base: BaseInstance = BaseInstance(Klipper, self.suffix)
|
||||
self.base.log_file_name = self.log_file_name
|
||||
|
||||
self.service_file_path: Path = get_service_file_path(Moonraker, self.suffix)
|
||||
self.data_dir: Path = self.base.data_dir
|
||||
self.cfg_file: Path = self.base.cfg_dir.joinpath(MOONRAKER_CFG_NAME)
|
||||
self.backup_dir: Path = self.base.data_dir.joinpath("backup")
|
||||
self.certs_dir: Path = self.base.data_dir.joinpath("certs")
|
||||
self.db_dir: Path = self.base.data_dir.joinpath("database")
|
||||
self.port: int | None = self._get_port()
|
||||
|
||||
def create(self) -> None:
|
||||
from utils.sys_utils import create_env_file, create_service_file
|
||||
|
||||
Logger.print_status("Creating new Moonraker 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(MOONRAKER_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 _prep_service_file_content(self) -> str:
|
||||
template = MOONRAKER_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(
|
||||
"%MOONRAKER_DIR%",
|
||||
self.moonraker_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(MOONRAKER_ENV_FILE_NAME).as_posix(),
|
||||
)
|
||||
return service_content
|
||||
|
||||
def _prep_env_file_content(self) -> str:
|
||||
template = MOONRAKER_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(
|
||||
"%MOONRAKER_DIR%",
|
||||
self.moonraker_dir.as_posix(),
|
||||
)
|
||||
env_file_content = env_file_content.replace(
|
||||
"%PRINTER_DATA%",
|
||||
self.base.data_dir.as_posix(),
|
||||
)
|
||||
|
||||
return env_file_content
|
||||
|
||||
def _get_port(self) -> int | None:
|
||||
if not self.cfg_file or not self.cfg_file.is_file():
|
||||
return None
|
||||
|
||||
scp = SimpleConfigParser()
|
||||
scp.read(self.cfg_file)
|
||||
port: int | None = scp.getint("server", "port", fallback=None)
|
||||
|
||||
return port
|
||||
71
kiauh/components/moonraker/moonraker_dialogs.py
Normal file
71
kiauh/components/moonraker/moonraker_dialogs.py
Normal file
@@ -0,0 +1,71 @@
|
||||
# ======================================================================= #
|
||||
# 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 textwrap
|
||||
from typing import List
|
||||
|
||||
from components.klipper.klipper import Klipper
|
||||
from components.moonraker.moonraker import Moonraker
|
||||
from core.constants import COLOR_CYAN, COLOR_GREEN, COLOR_YELLOW, RESET_FORMAT
|
||||
from core.menus.base_menu import print_back_footer
|
||||
|
||||
|
||||
def print_moonraker_overview(
|
||||
klipper_instances: List[Klipper],
|
||||
moonraker_instances: List[Moonraker],
|
||||
show_index=False,
|
||||
show_select_all=False,
|
||||
):
|
||||
headline = f"{COLOR_GREEN}The following instances were found:{RESET_FORMAT}"
|
||||
dialog = textwrap.dedent(
|
||||
f"""
|
||||
╔═══════════════════════════════════════════════════════╗
|
||||
║{headline:^64}║
|
||||
╟───────────────────────────────────────────────────────╢
|
||||
"""
|
||||
)[1:]
|
||||
|
||||
if show_select_all:
|
||||
select_all = f"{COLOR_YELLOW}a) Select all{RESET_FORMAT}"
|
||||
dialog += f"║ {select_all:<63}║\n"
|
||||
dialog += "║ ║\n"
|
||||
|
||||
instance_map = {
|
||||
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 ""
|
||||
)
|
||||
for k in klipper_instances
|
||||
}
|
||||
|
||||
for i, k in enumerate(instance_map):
|
||||
mr_name = instance_map.get(k)
|
||||
m = f"<-> {mr_name}" if mr_name != "" else ""
|
||||
line = f"{COLOR_CYAN}{f'{i+1})' if show_index else '●'} {k} {m} {RESET_FORMAT}"
|
||||
dialog += f"║ {line:<63}║\n"
|
||||
|
||||
warn_l1 = f"{COLOR_YELLOW}PLEASE NOTE: {RESET_FORMAT}"
|
||||
warn_l2 = f"{COLOR_YELLOW}If you select an instance with an existing Moonraker{RESET_FORMAT}"
|
||||
warn_l3 = f"{COLOR_YELLOW}instance, that Moonraker instance will be re-created!{RESET_FORMAT}"
|
||||
warning = textwrap.dedent(
|
||||
f"""
|
||||
║ ║
|
||||
╟───────────────────────────────────────────────────────╢
|
||||
║ {warn_l1:<63}║
|
||||
║ {warn_l2:<63}║
|
||||
║ {warn_l3:<63}║
|
||||
╟───────────────────────────────────────────────────────╢
|
||||
"""
|
||||
)[1:]
|
||||
|
||||
dialog += warning
|
||||
|
||||
print(dialog, end="")
|
||||
print_back_footer()
|
||||
124
kiauh/components/moonraker/moonraker_remove.py
Normal file
124
kiauh/components/moonraker/moonraker_remove.py
Normal file
@@ -0,0 +1,124 @@
|
||||
# ======================================================================= #
|
||||
# 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 subprocess import DEVNULL, PIPE, CalledProcessError, run
|
||||
from typing import List
|
||||
|
||||
from components.klipper.klipper_dialogs import print_instance_overview
|
||||
from components.moonraker import MOONRAKER_DIR, MOONRAKER_ENV_DIR
|
||||
from components.moonraker.moonraker import Moonraker
|
||||
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.instance_utils import get_instances
|
||||
from utils.sys_utils import unit_file_exists
|
||||
|
||||
|
||||
def run_moonraker_removal(
|
||||
remove_service: bool,
|
||||
remove_dir: bool,
|
||||
remove_env: bool,
|
||||
remove_polkit: bool,
|
||||
) -> None:
|
||||
instances = get_instances(Moonraker)
|
||||
|
||||
if remove_service:
|
||||
Logger.print_status("Removing Moonraker instances ...")
|
||||
if instances:
|
||||
instances_to_remove = select_instances_to_remove(instances)
|
||||
remove_instances(instances_to_remove)
|
||||
else:
|
||||
Logger.print_info("No Moonraker Services installed! Skipped ...")
|
||||
|
||||
delete_remaining: bool = remove_polkit or remove_dir or remove_env
|
||||
if delete_remaining and unit_file_exists("moonraker", suffix="service"):
|
||||
Logger.print_info("There are still other Moonraker services installed")
|
||||
Logger.print_info(
|
||||
"● Moonraker PolicyKit rules were not removed.", prefix=False
|
||||
)
|
||||
Logger.print_info(f"● '{MOONRAKER_DIR}' was not removed.", prefix=False)
|
||||
Logger.print_info(f"● '{MOONRAKER_ENV_DIR}' was not removed.", prefix=False)
|
||||
else:
|
||||
if remove_polkit:
|
||||
Logger.print_status("Removing all Moonraker policykit rules ...")
|
||||
remove_polkit_rules()
|
||||
if remove_dir:
|
||||
Logger.print_status("Removing Moonraker local repository ...")
|
||||
run_remove_routines(MOONRAKER_DIR)
|
||||
if remove_env:
|
||||
Logger.print_status("Removing Moonraker Python environment ...")
|
||||
run_remove_routines(MOONRAKER_ENV_DIR)
|
||||
|
||||
|
||||
def select_instances_to_remove(
|
||||
instances: List[Moonraker],
|
||||
) -> List[Moonraker] | None:
|
||||
start_index = 1
|
||||
options = [str(i + start_index) for i in range(len(instances))]
|
||||
options.extend(["a", "b"])
|
||||
instance_map = {options[i]: instances[i] for i in range(len(instances))}
|
||||
|
||||
print_instance_overview(
|
||||
instances,
|
||||
start_index=start_index,
|
||||
show_index=True,
|
||||
show_select_all=True,
|
||||
)
|
||||
selection = get_selection_input("Select Moonraker instance to remove", options)
|
||||
|
||||
instances_to_remove = []
|
||||
if selection == "b":
|
||||
return None
|
||||
elif selection == "a":
|
||||
instances_to_remove.extend(instances)
|
||||
else:
|
||||
instances_to_remove.append(instance_map[selection])
|
||||
|
||||
return instances_to_remove
|
||||
|
||||
|
||||
def remove_instances(
|
||||
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.service_file_path.stem} ...")
|
||||
InstanceManager.remove(instance)
|
||||
|
||||
|
||||
def remove_polkit_rules() -> None:
|
||||
if not MOONRAKER_DIR.exists():
|
||||
log = "Cannot remove policykit rules. Moonraker directory not found."
|
||||
Logger.print_warn(log)
|
||||
return
|
||||
|
||||
try:
|
||||
cmd = [f"{MOONRAKER_DIR}/scripts/set-policykit-rules.sh", "--clear"]
|
||||
run(cmd, stderr=PIPE, stdout=DEVNULL, check=True)
|
||||
except CalledProcessError as e:
|
||||
Logger.print_error(f"Error while removing policykit rules: {e}")
|
||||
|
||||
Logger.print_ok("Policykit rules successfully removed!")
|
||||
|
||||
|
||||
def delete_moonraker_logs(instances: List[Moonraker]) -> None:
|
||||
all_logfiles = []
|
||||
for instance in instances:
|
||||
all_logfiles = list(instance.base.log_dir.glob("moonraker.log*"))
|
||||
if not all_logfiles:
|
||||
Logger.print_info("No Moonraker logs found. Skipped ...")
|
||||
return
|
||||
|
||||
for log in all_logfiles:
|
||||
Logger.print_status(f"Remove '{log}'")
|
||||
run_remove_routines(log)
|
||||
219
kiauh/components/moonraker/moonraker_setup.py
Normal file
219
kiauh/components/moonraker/moonraker_setup.py
Normal file
@@ -0,0 +1,219 @@
|
||||
# ======================================================================= #
|
||||
# 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
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
from typing import List
|
||||
|
||||
from components.klipper.klipper import Klipper
|
||||
from components.moonraker import (
|
||||
EXIT_MOONRAKER_SETUP,
|
||||
MOONRAKER_DEPS_JSON_FILE,
|
||||
MOONRAKER_DIR,
|
||||
MOONRAKER_ENV_DIR,
|
||||
MOONRAKER_INSTALL_SCRIPT,
|
||||
MOONRAKER_REQ_FILE,
|
||||
MOONRAKER_SPEEDUPS_REQ_FILE,
|
||||
POLKIT_FILE,
|
||||
POLKIT_LEGACY_FILE,
|
||||
POLKIT_SCRIPT,
|
||||
POLKIT_USR_FILE,
|
||||
)
|
||||
from components.moonraker.moonraker import Moonraker
|
||||
from components.moonraker.moonraker_dialogs import print_moonraker_overview
|
||||
from components.moonraker.moonraker_utils import (
|
||||
backup_moonraker_dir,
|
||||
create_example_moonraker_conf,
|
||||
)
|
||||
from components.webui_client.client_utils import (
|
||||
enable_mainsail_remotemode,
|
||||
get_existing_clients,
|
||||
)
|
||||
from components.webui_client.mainsail_data import MainsailData
|
||||
from core.instance_manager.instance_manager import InstanceManager
|
||||
from core.logger import Logger
|
||||
from core.settings.kiauh_settings import KiauhSettings
|
||||
from utils.common import check_install_dependencies
|
||||
from utils.fs_utils import check_file_exist
|
||||
from utils.git_utils import git_clone_wrapper, git_pull_wrapper
|
||||
from utils.input_utils import (
|
||||
get_confirm,
|
||||
get_selection_input,
|
||||
)
|
||||
from utils.instance_utils import get_instances
|
||||
from utils.sys_utils import (
|
||||
check_python_version,
|
||||
cmd_sysctl_manage,
|
||||
cmd_sysctl_service,
|
||||
create_python_venv,
|
||||
install_python_requirements,
|
||||
parse_packages_from_file,
|
||||
)
|
||||
|
||||
|
||||
def install_moonraker() -> None:
|
||||
klipper_list: List[Klipper] = get_instances(Klipper)
|
||||
|
||||
if not check_moonraker_install_requirements(klipper_list):
|
||||
return
|
||||
|
||||
moonraker_list: List[Moonraker] = get_instances(Moonraker)
|
||||
instances: List[Moonraker] = []
|
||||
selected_option: str | Klipper
|
||||
|
||||
if len(klipper_list) == 1:
|
||||
instances.append(Moonraker(klipper_list[0].suffix))
|
||||
else:
|
||||
print_moonraker_overview(
|
||||
klipper_list,
|
||||
moonraker_list,
|
||||
show_index=True,
|
||||
show_select_all=True,
|
||||
)
|
||||
options = {str(i + 1): k for i, k in enumerate(klipper_list)}
|
||||
additional_options = {"a": None, "b": None}
|
||||
options = {**options, **additional_options}
|
||||
question = "Select Klipper instance to setup Moonraker for"
|
||||
selected_option = get_selection_input(question, options)
|
||||
|
||||
if selected_option == "b":
|
||||
Logger.print_status(EXIT_MOONRAKER_SETUP)
|
||||
return
|
||||
|
||||
if selected_option == "a":
|
||||
instances.extend([Moonraker(k.suffix) for k in klipper_list])
|
||||
else:
|
||||
klipper_instance: Klipper | None = options.get(selected_option)
|
||||
if klipper_instance is None:
|
||||
raise Exception("Error selecting instance!")
|
||||
instances.append(Moonraker(klipper_instance.suffix))
|
||||
|
||||
create_example_cfg = get_confirm("Create example moonraker.conf?")
|
||||
|
||||
try:
|
||||
check_install_dependencies()
|
||||
setup_moonraker_prerequesites()
|
||||
install_moonraker_polkit()
|
||||
|
||||
used_ports_map = {m.suffix: m.port for m in moonraker_list}
|
||||
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(instance, used_ports_map, clients)
|
||||
|
||||
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(moonraker_list) > 1:
|
||||
enable_mainsail_remotemode()
|
||||
|
||||
except Exception as e:
|
||||
Logger.print_error(f"Error while installing Moonraker: {e}")
|
||||
return
|
||||
|
||||
|
||||
def check_moonraker_install_requirements(klipper_list: List[Klipper]) -> bool:
|
||||
def check_klipper_instances() -> bool:
|
||||
if len(klipper_list) >= 1:
|
||||
return True
|
||||
|
||||
Logger.print_warn("Klipper not installed!")
|
||||
Logger.print_warn("Moonraker cannot be installed! Install Klipper first.")
|
||||
return False
|
||||
|
||||
return check_python_version(3, 7) and check_klipper_instances()
|
||||
|
||||
|
||||
def setup_moonraker_prerequesites() -> None:
|
||||
settings = KiauhSettings()
|
||||
repo = settings.moonraker.repo_url
|
||||
branch = settings.moonraker.branch
|
||||
|
||||
git_clone_wrapper(repo, MOONRAKER_DIR, branch)
|
||||
|
||||
# install moonraker dependencies and create python virtualenv
|
||||
install_moonraker_packages()
|
||||
if create_python_venv(MOONRAKER_ENV_DIR):
|
||||
install_python_requirements(MOONRAKER_ENV_DIR, MOONRAKER_REQ_FILE)
|
||||
install_python_requirements(MOONRAKER_ENV_DIR, MOONRAKER_SPEEDUPS_REQ_FILE)
|
||||
|
||||
|
||||
def install_moonraker_packages() -> None:
|
||||
moonraker_deps = []
|
||||
|
||||
if MOONRAKER_DEPS_JSON_FILE.exists():
|
||||
with open(MOONRAKER_DEPS_JSON_FILE, "r") as deps:
|
||||
moonraker_deps = json.load(deps).get("debian", [])
|
||||
elif MOONRAKER_INSTALL_SCRIPT.exists():
|
||||
moonraker_deps = parse_packages_from_file(MOONRAKER_INSTALL_SCRIPT)
|
||||
|
||||
if not moonraker_deps:
|
||||
raise ValueError("Error reading Moonraker dependencies!")
|
||||
|
||||
check_install_dependencies({*moonraker_deps})
|
||||
|
||||
|
||||
def install_moonraker_polkit() -> None:
|
||||
Logger.print_status("Installing Moonraker policykit rules ...")
|
||||
|
||||
legacy_file_exists = check_file_exist(POLKIT_LEGACY_FILE, True)
|
||||
polkit_file_exists = check_file_exist(POLKIT_FILE, True)
|
||||
usr_file_exists = check_file_exist(POLKIT_USR_FILE, True)
|
||||
|
||||
if legacy_file_exists or (polkit_file_exists and usr_file_exists):
|
||||
Logger.print_info("Moonraker policykit rules are already installed.")
|
||||
return
|
||||
|
||||
try:
|
||||
command = [POLKIT_SCRIPT, "--disable-systemctl"]
|
||||
result = subprocess.run(
|
||||
command,
|
||||
stderr=subprocess.PIPE,
|
||||
stdout=subprocess.DEVNULL,
|
||||
text=True,
|
||||
)
|
||||
if result.returncode != 0 or result.stderr:
|
||||
Logger.print_error(f"{result.stderr}", False)
|
||||
Logger.print_error("Installing Moonraker policykit rules failed!")
|
||||
return
|
||||
|
||||
Logger.print_ok("Moonraker policykit rules successfully installed!")
|
||||
except subprocess.CalledProcessError as e:
|
||||
log = f"Error while installing Moonraker policykit rules: {e.stderr.decode()}"
|
||||
Logger.print_error(log)
|
||||
|
||||
|
||||
def update_moonraker() -> None:
|
||||
if not get_confirm("Update Moonraker now?"):
|
||||
return
|
||||
|
||||
settings = KiauhSettings()
|
||||
if settings.kiauh.backup_before_update:
|
||||
backup_moonraker_dir()
|
||||
|
||||
instances = get_instances(Moonraker)
|
||||
InstanceManager.stop_all(instances)
|
||||
|
||||
git_pull_wrapper(repo=settings.moonraker.repo_url, target_dir=MOONRAKER_DIR)
|
||||
|
||||
# install possible new system packages
|
||||
install_moonraker_packages()
|
||||
# install possible new python dependencies
|
||||
install_python_requirements(MOONRAKER_ENV_DIR, MOONRAKER_REQ_FILE)
|
||||
|
||||
InstanceManager.start_all(instances)
|
||||
145
kiauh/components/moonraker/moonraker_utils.py
Normal file
145
kiauh/components/moonraker/moonraker_utils.py
Normal 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 #
|
||||
# ======================================================================= #
|
||||
|
||||
import shutil
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from components.moonraker import (
|
||||
MODULE_PATH,
|
||||
MOONRAKER_BACKUP_DIR,
|
||||
MOONRAKER_DB_BACKUP_DIR,
|
||||
MOONRAKER_DEFAULT_PORT,
|
||||
MOONRAKER_DIR,
|
||||
MOONRAKER_ENV_DIR,
|
||||
)
|
||||
from components.moonraker.moonraker import Moonraker
|
||||
from components.webui_client.base_data import BaseWebClient
|
||||
from core.backup_manager.backup_manager import BackupManager
|
||||
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.instance_utils import get_instances
|
||||
from utils.sys_utils import (
|
||||
get_ipv4_addr,
|
||||
)
|
||||
|
||||
|
||||
def get_moonraker_status() -> ComponentStatus:
|
||||
return get_install_status(MOONRAKER_DIR, MOONRAKER_ENV_DIR, Moonraker)
|
||||
|
||||
|
||||
def create_example_moonraker_conf(
|
||||
instance: Moonraker,
|
||||
ports_map: Dict[str, int],
|
||||
clients: Optional[List[BaseWebClient]] = None,
|
||||
) -> None:
|
||||
Logger.print_status(f"Creating example moonraker.conf in '{instance.base.cfg_dir}'")
|
||||
if instance.cfg_file.is_file():
|
||||
Logger.print_info(f"'{instance.cfg_file}' already exists.")
|
||||
return
|
||||
|
||||
source = MODULE_PATH.joinpath("assets/moonraker.conf")
|
||||
target = instance.cfg_file
|
||||
try:
|
||||
shutil.copy(source, target)
|
||||
except OSError as e:
|
||||
Logger.print_error(f"Unable to create example moonraker.conf:\n{e}")
|
||||
return
|
||||
|
||||
ports = [
|
||||
ports_map.get(instance)
|
||||
for instance in ports_map
|
||||
if ports_map.get(instance) is not None
|
||||
]
|
||||
if ports_map.get(instance.suffix) is None:
|
||||
# this could be improved to not increment the max value of the ports list and assign it as the port
|
||||
# as it can lead to situation where the port for e.g. instance moonraker-2 becomes 7128 if the port
|
||||
# of moonraker-1 is 7125 and moonraker-3 is 7127 and there are moonraker.conf files for moonraker-1
|
||||
# and moonraker-3 already. though, there does not seem to be a very reliable way of always assigning
|
||||
# the correct port to each instance and the user will likely be required to correct the value manually.
|
||||
port = max(ports) + 1 if ports else MOONRAKER_DEFAULT_PORT
|
||||
else:
|
||||
port = ports_map.get(instance.suffix)
|
||||
|
||||
ports_map[instance.suffix] = port
|
||||
|
||||
ip = get_ipv4_addr().split(".")[:2]
|
||||
ip.extend(["0", "0/16"])
|
||||
uds = instance.base.comms_dir.joinpath("klippy.sock")
|
||||
|
||||
scp = SimpleConfigParser()
|
||||
scp.read(target)
|
||||
trusted_clients: List[str] = [
|
||||
".".join(ip),
|
||||
*scp.get("authorization", "trusted_clients"),
|
||||
]
|
||||
|
||||
scp.set("server", "port", str(port))
|
||||
scp.set("server", "klippy_uds_address", str(uds))
|
||||
scp.set(
|
||||
"authorization",
|
||||
"trusted_clients",
|
||||
"\n".join(trusted_clients),
|
||||
True,
|
||||
)
|
||||
|
||||
# add existing client and client configs in the update section
|
||||
if clients is not None and len(clients) > 0:
|
||||
for c in clients:
|
||||
# client part
|
||||
c_section = f"update_manager {c.name}"
|
||||
c_options = [
|
||||
("type", "web"),
|
||||
("channel", "stable"),
|
||||
("repo", c.repo_path),
|
||||
("path", c.client_dir),
|
||||
]
|
||||
scp.add_section(section=c_section)
|
||||
for option in c_options:
|
||||
scp.set(c_section, option[0], option[1])
|
||||
|
||||
# client config part
|
||||
c_config = c.client_config
|
||||
if c_config.config_dir.exists():
|
||||
c_config_section = f"update_manager {c_config.name}"
|
||||
c_config_options = [
|
||||
("type", "git_repo"),
|
||||
("primary_branch", "master"),
|
||||
("path", c_config.config_dir),
|
||||
("origin", c_config.repo_url),
|
||||
("managed_services", "klipper"),
|
||||
]
|
||||
scp.add_section(section=c_config_section)
|
||||
for option in c_config_options:
|
||||
scp.set(c_config_section, option[0], option[1])
|
||||
|
||||
scp.write(target)
|
||||
Logger.print_ok(f"Example moonraker.conf created in '{instance.base.cfg_dir}'")
|
||||
|
||||
|
||||
def backup_moonraker_dir() -> None:
|
||||
bm = BackupManager()
|
||||
bm.backup_directory("moonraker", source=MOONRAKER_DIR, target=MOONRAKER_BACKUP_DIR)
|
||||
bm.backup_directory(
|
||||
"moonraker-env", source=MOONRAKER_ENV_DIR, target=MOONRAKER_BACKUP_DIR
|
||||
)
|
||||
|
||||
|
||||
def backup_moonraker_db_dir() -> None:
|
||||
instances: List[Moonraker] = get_instances(Moonraker)
|
||||
bm = BackupManager()
|
||||
|
||||
for instance in instances:
|
||||
name = f"database-{instance.data_dir.name}"
|
||||
bm.backup_directory(
|
||||
name, source=instance.db_dir, target=MOONRAKER_DB_BACKUP_DIR
|
||||
)
|
||||
Reference in New Issue
Block a user