mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-27 17:53:35 +05:00
feat: add mobileraker support
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
16
kiauh/components/mobileraker/__init__.py
Normal file
16
kiauh/components/mobileraker/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# ======================================================================= #
|
||||
# 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
|
||||
|
||||
MOBILERAKER_REPO = "https://github.com/Clon1998/mobileraker_companion.git"
|
||||
MOBILERAKER_DIR = Path.home().joinpath("mobileraker_companion")
|
||||
MOBILERAKER_ENV = Path.home().joinpath("mobileraker-env")
|
||||
MOBILERAKER_BACKUP_DIR = BACKUP_ROOT_DIR.joinpath("mobileraker-backups")
|
||||
224
kiauh/components/mobileraker/mobileraker.py
Normal file
224
kiauh/components/mobileraker/mobileraker.py
Normal file
@@ -0,0 +1,224 @@
|
||||
# ======================================================================= #
|
||||
# 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 pathlib import Path
|
||||
from subprocess import run, CalledProcessError
|
||||
from typing import List, Dict, Literal, Union
|
||||
|
||||
from components.klipper.klipper import Klipper
|
||||
from components.mobileraker import (
|
||||
MOBILERAKER_REPO,
|
||||
MOBILERAKER_DIR,
|
||||
MOBILERAKER_ENV,
|
||||
MOBILERAKER_BACKUP_DIR,
|
||||
)
|
||||
from components.moonraker.moonraker import Moonraker
|
||||
from core.backup_manager.backup_manager import BackupManager
|
||||
from core.instance_manager.instance_manager import InstanceManager
|
||||
from core.settings.kiauh_settings import KiauhSettings
|
||||
from utils.common import get_install_status, check_install_dependencies
|
||||
from utils.config_utils import add_config_section, remove_config_section
|
||||
from utils.constants import SYSTEMD
|
||||
from utils.fs_utils import remove_with_sudo
|
||||
from utils.git_utils import (
|
||||
git_clone_wrapper,
|
||||
git_pull_wrapper,
|
||||
get_repo_name,
|
||||
get_local_commit,
|
||||
get_remote_commit,
|
||||
)
|
||||
from utils.input_utils import get_confirm
|
||||
from utils.logger import Logger, DialogType
|
||||
from utils.sys_utils import (
|
||||
check_python_version,
|
||||
cmd_sysctl_service,
|
||||
install_python_requirements,
|
||||
cmd_sysctl_manage,
|
||||
)
|
||||
|
||||
|
||||
def install_mobileraker() -> None:
|
||||
Logger.print_status("Installing Mobileraker's companion ...")
|
||||
|
||||
if not check_python_version(3, 7):
|
||||
return
|
||||
|
||||
mr_im = InstanceManager(Moonraker)
|
||||
mr_instances = mr_im.instances
|
||||
if not mr_instances:
|
||||
warn_msg = [
|
||||
"Moonraker not found! Mobileraker's companion will not properly work "
|
||||
"without a working Moonraker installation.",
|
||||
"Mobileraker's companion's update manager configuration for Moonraker "
|
||||
"will not be added to any moonraker.conf.",
|
||||
]
|
||||
Logger.print_dialog(DialogType.WARNING, warn_msg)
|
||||
if not get_confirm(
|
||||
"Continue Mobileraker's companion installation?",
|
||||
default_choice=False,
|
||||
allow_go_back=True,
|
||||
):
|
||||
return
|
||||
|
||||
package_list = ["wget", "curl", "unzip", "dfu-util"]
|
||||
check_install_dependencies(package_list)
|
||||
|
||||
git_clone_wrapper(MOBILERAKER_REPO, MOBILERAKER_DIR)
|
||||
|
||||
try:
|
||||
script = f"{MOBILERAKER_DIR}/scripts/install.sh"
|
||||
run(script, shell=True, check=True)
|
||||
if mr_instances:
|
||||
patch_mobileraker_update_manager(mr_instances)
|
||||
mr_im.restart_all_instance()
|
||||
else:
|
||||
Logger.print_info(
|
||||
"Moonraker is not installed! Cannot add Mobileraker's companion to update manager!"
|
||||
)
|
||||
Logger.print_ok("Mobileraker's companion successfully installed!")
|
||||
except CalledProcessError as e:
|
||||
Logger.print_error(f"Error installing Mobileraker's companion:\n{e}")
|
||||
return
|
||||
|
||||
|
||||
def patch_mobileraker_update_manager(instances: List[Moonraker]) -> None:
|
||||
env_py = f"{MOBILERAKER_ENV}/bin/python"
|
||||
add_config_section(
|
||||
section="update_manager mobileraker",
|
||||
instances=instances,
|
||||
options=[
|
||||
("type", "git_repo"),
|
||||
("path", "mobileraker_companion"),
|
||||
("orgin", MOBILERAKER_REPO),
|
||||
("primary_branch", "main"),
|
||||
("managed_services", "mobileraker"),
|
||||
("env", env_py),
|
||||
("requirements", "scripts/mobileraker-requirements.txt"),
|
||||
("install_script", "scripts/install.sh"),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def update_mobileraker() -> None:
|
||||
try:
|
||||
cmd_sysctl_service("KlipperScreen", "stop")
|
||||
|
||||
if not MOBILERAKER_DIR.exists():
|
||||
Logger.print_info(
|
||||
"Mobileraker's companion does not seem to be installed! Skipping ..."
|
||||
)
|
||||
return
|
||||
|
||||
Logger.print_status("Updating Mobileraker's companion ...")
|
||||
|
||||
cmd_sysctl_service("mobileraker", "stop")
|
||||
|
||||
settings = KiauhSettings()
|
||||
if settings.get("kiauh", "backup_before_update"):
|
||||
backup_mobileraker_dir()
|
||||
|
||||
git_pull_wrapper(MOBILERAKER_REPO, MOBILERAKER_DIR)
|
||||
|
||||
requirements = MOBILERAKER_DIR.joinpath("/scripts/mobileraker-requirements.txt")
|
||||
install_python_requirements(MOBILERAKER_ENV, requirements)
|
||||
|
||||
cmd_sysctl_service("mobileraker", "start")
|
||||
|
||||
Logger.print_ok("Mobileraker's companion updated successfully.", end="\n\n")
|
||||
except CalledProcessError as e:
|
||||
Logger.print_error(f"Error updating Mobileraker's companion:\n{e}")
|
||||
return
|
||||
|
||||
|
||||
def get_mobileraker_status() -> (
|
||||
Dict[
|
||||
Literal["status", "status_code", "repo", "local", "remote"],
|
||||
Union[str, int],
|
||||
]
|
||||
):
|
||||
files = [
|
||||
MOBILERAKER_DIR,
|
||||
MOBILERAKER_ENV,
|
||||
SYSTEMD.joinpath("mobileraker.service"),
|
||||
]
|
||||
status = get_install_status(MOBILERAKER_DIR, files)
|
||||
return {
|
||||
"status": status.get("status"),
|
||||
"status_code": status.get("status_code"),
|
||||
"repo": get_repo_name(MOBILERAKER_DIR),
|
||||
"local": get_local_commit(MOBILERAKER_DIR),
|
||||
"remote": get_remote_commit(MOBILERAKER_DIR),
|
||||
}
|
||||
|
||||
|
||||
def remove_mobileraker() -> None:
|
||||
Logger.print_status("Removing Mobileraker's companion ...")
|
||||
try:
|
||||
if MOBILERAKER_DIR.exists():
|
||||
Logger.print_status("Removing Mobileraker's companion directory ...")
|
||||
shutil.rmtree(MOBILERAKER_DIR)
|
||||
Logger.print_ok("Mobileraker's companion directory successfully removed!")
|
||||
else:
|
||||
Logger.print_warn("Mobileraker's companion directory not found!")
|
||||
|
||||
if MOBILERAKER_ENV.exists():
|
||||
Logger.print_status("Removing Mobileraker's companion environment ...")
|
||||
shutil.rmtree(MOBILERAKER_ENV)
|
||||
Logger.print_ok("Mobileraker's companion environment successfully removed!")
|
||||
else:
|
||||
Logger.print_warn("Mobileraker's companion environment not found!")
|
||||
|
||||
service = SYSTEMD.joinpath("mobileraker.service")
|
||||
if service.exists():
|
||||
Logger.print_status("Removing mobileraker service ...")
|
||||
cmd_sysctl_service(service, "stop")
|
||||
cmd_sysctl_service(service, "disable")
|
||||
remove_with_sudo(service)
|
||||
cmd_sysctl_manage("deamon-reload")
|
||||
cmd_sysctl_manage("reset-failed")
|
||||
Logger.print_ok("Mobileraker's companion service successfully removed!")
|
||||
|
||||
kl_im = InstanceManager(Klipper)
|
||||
kl_instances: List[Klipper] = kl_im.instances
|
||||
for instance in kl_instances:
|
||||
logfile = instance.log_dir.joinpath("mobileraker.log")
|
||||
if logfile.exists():
|
||||
Logger.print_status(f"Removing {logfile} ...")
|
||||
Path(logfile).unlink()
|
||||
Logger.print_ok(f"{logfile} successfully removed!")
|
||||
|
||||
mr_im = InstanceManager(Moonraker)
|
||||
mr_instances: List[Moonraker] = mr_im.instances
|
||||
if mr_instances:
|
||||
Logger.print_status(
|
||||
"Removing Mobileraker's companion from update manager ..."
|
||||
)
|
||||
remove_config_section("update_manager mobileraker", mr_instances)
|
||||
Logger.print_ok(
|
||||
"Mobileraker's companion successfully removed from update manager!"
|
||||
)
|
||||
|
||||
Logger.print_ok("Mobileraker's companion successfully removed!")
|
||||
|
||||
except Exception as e:
|
||||
Logger.print_error(f"Error removing Mobileraker's companion:\n{e}")
|
||||
|
||||
|
||||
def backup_mobileraker_dir() -> None:
|
||||
bm = BackupManager()
|
||||
bm.backup_directory(
|
||||
"mobileraker_companion",
|
||||
source=MOBILERAKER_DIR,
|
||||
target=MOBILERAKER_BACKUP_DIR,
|
||||
)
|
||||
bm.backup_directory(
|
||||
"mobileraker-env",
|
||||
source=MOBILERAKER_ENV,
|
||||
target=MOBILERAKER_BACKUP_DIR,
|
||||
)
|
||||
Reference in New Issue
Block a user