refactor: implement constants for klipper

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-07-01 20:42:22 +02:00
parent 92ed67ddd2
commit 2391f491bb
4 changed files with 75 additions and 61 deletions

View File

@@ -13,15 +13,21 @@ 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")
MOONRAKER_REQUIREMENTS_TXT = MOONRAKER_DIR.joinpath(
"scripts/moonraker-requirements.txt"
)
DEFAULT_MOONRAKER_PORT = 7125
# files
MOONRAKER_REQ_FILE = MOONRAKER_DIR.joinpath("scripts/moonraker-requirements.txt")
# introduced due to
# https://github.com/Arksine/moonraker/issues/349
# https://github.com/Arksine/moonraker/pull/346
@@ -29,5 +35,8 @@ POLKIT_LEGACY_FILE = Path("/etc/polkit-1/localauthority/50-local.d/10-moonraker.
POLKIT_FILE = Path("/etc/polkit-1/rules.d/moonraker.rules")
POLKIT_USR_FILE = Path("/usr/share/polkit-1/rules.d/moonraker.rules")
POLKIT_SCRIPT = Path.home().joinpath("moonraker/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 ..."

View File

@@ -12,7 +12,15 @@ from pathlib import Path
from subprocess import DEVNULL, CalledProcessError, run
from typing import List
from components.moonraker import MODULE_PATH, MOONRAKER_DIR, MOONRAKER_ENV_DIR
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.instance_manager.base_instance import BaseInstance
from core.submodules.simple_config_parser.src.simple_config_parser.simple_config_parser import (
SimpleConfigParser,
@@ -31,13 +39,13 @@ class Moonraker(BaseInstance):
super().__init__(instance_type=self, suffix=suffix)
self.moonraker_dir: Path = MOONRAKER_DIR
self.env_dir: Path = MOONRAKER_ENV_DIR
self.cfg_file = self.cfg_dir.joinpath("moonraker.conf")
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._comms_dir = self.data_dir.joinpath("comms")
self.log = self.log_dir.joinpath("moonraker.log")
self.log = self.log_dir.joinpath(MOONRAKER_LOG_NAME)
@property
def db_dir(self) -> Path:
@@ -50,21 +58,10 @@ class Moonraker(BaseInstance):
def create(self, create_example_cfg: bool = False) -> None:
Logger.print_status("Creating new Moonraker Instance ...")
service_template_path = MODULE_PATH.joinpath("assets/moonraker.service")
service_file_name = self.get_service_file_name(extension=True)
service_file_target = SYSTEMD.joinpath(service_file_name)
env_template_file_path = MODULE_PATH.joinpath("assets/moonraker.env")
env_file_target = self.sysd_dir.joinpath("moonraker.env")
try:
self.create_folders([self.backup_dir, self.certs_dir, self._db_dir])
self._write_service_file(
service_template_path,
service_file_target,
env_file_target,
)
self._write_env_file(env_template_file_path, env_file_target)
self._write_service_file()
self._write_env_file()
except CalledProcessError as e:
Logger.print_error(f"Error creating instance: {e}")
@@ -88,15 +85,10 @@ class Moonraker(BaseInstance):
Logger.print_error(f"Error removing instance: {e}")
raise
def _write_service_file(
self,
service_template_path: Path,
service_file_target: Path,
env_file_target: Path,
) -> None:
service_content = self._prep_service_file(
service_template_path, env_file_target
)
def _write_service_file(self) -> None:
service_file_name = self.get_service_file_name(extension=True)
service_file_target = SYSTEMD.joinpath(service_file_name)
service_content = self._prep_service_file()
command = ["sudo", "tee", service_file_target]
run(
command,
@@ -106,48 +98,61 @@ class Moonraker(BaseInstance):
)
Logger.print_ok(f"Service file created: {service_file_target}")
def _write_env_file(
self, env_template_file_path: Path, env_file_target: Path
) -> None:
env_file_content = self._prep_env_file(env_template_file_path)
def _write_env_file(self) -> None:
env_file_content = self._prep_env_file()
env_file_target = self.sysd_dir.joinpath(MOONRAKER_ENV_FILE_NAME)
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}")
def _prep_service_file(
self, service_template_path: Path, env_file_path: Path
) -> str:
def _prep_service_file(self) -> str:
template = MOONRAKER_SERVICE_TEMPLATE
try:
with open(service_template_path, "r") as template_file:
with open(template, "r") as template_file:
template_content = template_file.read()
except FileNotFoundError:
Logger.print_error(
f"Unable to open {service_template_path} - File not found"
)
Logger.print_error(f"Unable to open {template} - File not found")
raise
service_content = template_content.replace("%USER%", self.user)
service_content = service_content.replace(
"%MOONRAKER_DIR%", str(self.moonraker_dir)
service_content = template_content.replace(
"%USER%",
self.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.sysd_dir.joinpath(MOONRAKER_ENV_FILE_NAME).as_posix(),
)
service_content = service_content.replace("%ENV%", str(self.env_dir))
service_content = service_content.replace("%ENV_FILE%", str(env_file_path))
return service_content
def _prep_env_file(self, env_template_file_path: Path) -> str:
def _prep_env_file(self) -> str:
template = MOONRAKER_ENV_FILE_TEMPLATE
try:
with open(env_template_file_path, "r") as env_file:
with open(template, "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"
)
Logger.print_error(f"Unable to open {template} - File not found")
raise
env_file_content = env_template_file_content.replace(
"%MOONRAKER_DIR%", str(self.moonraker_dir)
"%MOONRAKER_DIR%",
self.moonraker_dir.as_posix(),
)
env_file_content = env_file_content.replace(
"%PRINTER_DATA%", str(self.data_dir)
"%PRINTER_DATA%",
self.data_dir.as_posix(),
)
return env_file_content
def _get_port(self) -> int | None:
@@ -163,6 +168,8 @@ class Moonraker(BaseInstance):
def _delete_logfiles(self) -> None:
from utils.fs_utils import run_remove_routines
for log in list(self.log_dir.glob("moonraker.log*")):
files = self.log_dir.iterdir()
logs = [f for f in files if f.name.startswith(MOONRAKER_LOG_NAME)]
for log in logs:
Logger.print_status(f"Remove '{log}'")
run_remove_routines(log)

View File

@@ -15,7 +15,7 @@ from components.moonraker import (
EXIT_MOONRAKER_SETUP,
MOONRAKER_DIR,
MOONRAKER_ENV_DIR,
MOONRAKER_REQUIREMENTS_TXT,
MOONRAKER_REQ_FILE,
POLKIT_FILE,
POLKIT_LEGACY_FILE,
POLKIT_SCRIPT,
@@ -145,7 +145,7 @@ def setup_moonraker_prerequesites() -> None:
# install moonraker dependencies and create python virtualenv
install_moonraker_packages(MOONRAKER_DIR)
create_python_venv(MOONRAKER_ENV_DIR)
install_python_requirements(MOONRAKER_ENV_DIR, MOONRAKER_REQUIREMENTS_TXT)
install_python_requirements(MOONRAKER_ENV_DIR, MOONRAKER_REQ_FILE)
def install_moonraker_packages(moonraker_dir: Path) -> None:
@@ -206,13 +206,11 @@ def update_moonraker() -> None:
instance_manager = InstanceManager(Moonraker)
instance_manager.stop_all_instance()
git_pull_wrapper(
repo=settings.moonraker.repo_url, target_dir=MOONRAKER_DIR
)
git_pull_wrapper(repo=settings.moonraker.repo_url, target_dir=MOONRAKER_DIR)
# install possible new system packages
install_moonraker_packages(MOONRAKER_DIR)
# install possible new python dependencies
install_python_requirements(MOONRAKER_ENV_DIR, MOONRAKER_REQUIREMENTS_TXT)
install_python_requirements(MOONRAKER_ENV_DIR, MOONRAKER_REQ_FILE)
instance_manager.start_all_instance()

View File

@@ -11,10 +11,10 @@ import shutil
from typing import Dict, List, Optional
from components.moonraker import (
DEFAULT_MOONRAKER_PORT,
MODULE_PATH,
MOONRAKER_BACKUP_DIR,
MOONRAKER_DB_BACKUP_DIR,
MOONRAKER_DEFAULT_PORT,
MOONRAKER_DIR,
MOONRAKER_ENV_DIR,
)
@@ -68,7 +68,7 @@ def create_example_moonraker_conf(
# 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 DEFAULT_MOONRAKER_PORT
port = max(ports) + 1 if ports else MOONRAKER_DEFAULT_PORT
else:
port = ports_map.get(instance.suffix)