refactor(Mainsail): move some functions to a mainsail utils module
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -70,7 +70,7 @@ class InstallMenu(BaseMenu):
|
||||
moonraker_setup.run_moonraker_setup(install=True)
|
||||
|
||||
def install_mainsail(self):
|
||||
mainsail_setup.run_mainsail_setup(install=True)
|
||||
mainsail_setup.run_mainsail_installation()
|
||||
|
||||
def install_fluidd(self):
|
||||
print("install_fluidd")
|
||||
|
||||
@@ -9,9 +9,7 @@
|
||||
# This file may be distributed under the terms of the GNU GPLv3 license #
|
||||
# ======================================================================= #
|
||||
|
||||
import json
|
||||
import os.path
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
@@ -25,7 +23,6 @@ from kiauh.modules.mainsail import (
|
||||
MAINSAIL_DIR,
|
||||
MAINSAIL_CONFIG_DIR,
|
||||
MAINSAIL_CONFIG_REPO_URL,
|
||||
MAINSAIL_CONFIG_JSON,
|
||||
MODULE_PATH,
|
||||
)
|
||||
from kiauh.modules.mainsail.mainsail_dialogs import (
|
||||
@@ -34,6 +31,12 @@ from kiauh.modules.mainsail.mainsail_dialogs import (
|
||||
print_install_mainsail_config_dialog,
|
||||
print_mainsail_port_select_dialog,
|
||||
)
|
||||
from kiauh.modules.mainsail.mainsail_utils import (
|
||||
restore_config_json,
|
||||
enable_mainsail_remotemode,
|
||||
backup_config_json,
|
||||
symlink_webui_nginx_log,
|
||||
)
|
||||
from kiauh.modules.moonraker.moonraker import Moonraker
|
||||
from kiauh.utils.common import check_install_dependencies
|
||||
from kiauh.utils.input_utils import get_confirm, get_number_input
|
||||
@@ -52,7 +55,7 @@ from kiauh.utils.system_utils import (
|
||||
)
|
||||
|
||||
|
||||
def run_mainsail_setup(install: bool) -> None:
|
||||
def run_mainsail_installation() -> None:
|
||||
im_mr = InstanceManager(Moonraker)
|
||||
is_moonraker_installed = len(im_mr.instances) > 0
|
||||
|
||||
@@ -177,21 +180,6 @@ def create_mainsail_nginx_cfg(port: int) -> None:
|
||||
raise
|
||||
|
||||
|
||||
def symlink_webui_nginx_log(klipper_instances: List[Klipper]) -> None:
|
||||
Logger.print_status("Link NGINX logs into log directory ...")
|
||||
access_log = Path("/var/log/nginx/mainsail-access.log")
|
||||
error_log = Path("/var/log/nginx/mainsail-error.log")
|
||||
|
||||
for instance in klipper_instances:
|
||||
desti_access = Path(instance.log_dir).joinpath("mainsail-access.log")
|
||||
if not desti_access.exists():
|
||||
desti_access.symlink_to(access_log)
|
||||
|
||||
desti_error = Path(instance.log_dir).joinpath("mainsail-error.log")
|
||||
if not desti_error.exists():
|
||||
desti_error.symlink_to(error_log)
|
||||
|
||||
|
||||
def patch_moonraker_conf(
|
||||
moonraker_instances: List[Moonraker],
|
||||
name: str,
|
||||
@@ -238,35 +226,3 @@ def patch_printer_config(klipper_instances: List[Klipper]) -> None:
|
||||
|
||||
with open(cfg_file, "a") as f:
|
||||
f.write("\n[include mainsail.cfg]")
|
||||
|
||||
|
||||
def backup_config_json() -> None:
|
||||
try:
|
||||
Logger.print_status(f"Backup '{MAINSAIL_CONFIG_JSON}' ...")
|
||||
target = os.path.join(Path.home(), "config.json.kiauh.bak")
|
||||
shutil.copy(MAINSAIL_CONFIG_JSON, target)
|
||||
except OSError:
|
||||
Logger.print_info(f"Unable to backup config.json. Skipped ...")
|
||||
|
||||
|
||||
def restore_config_json() -> None:
|
||||
try:
|
||||
Logger.print_status(f"Restore '{MAINSAIL_CONFIG_JSON}' ...")
|
||||
source = os.path.join(Path.home(), "config.json.kiauh.bak")
|
||||
shutil.copy(source, MAINSAIL_CONFIG_JSON)
|
||||
except OSError:
|
||||
Logger.print_info(f"Unable to restore config.json. Skipped ...")
|
||||
|
||||
|
||||
def enable_mainsail_remotemode() -> None:
|
||||
with open(MAINSAIL_CONFIG_JSON, "r") as f:
|
||||
config_data = json.load(f)
|
||||
|
||||
if config_data["instancesDB"] == "browser":
|
||||
return
|
||||
|
||||
Logger.print_status("Setting instance storage location to 'browser' ...")
|
||||
config_data["instancesDB"] = "browser"
|
||||
|
||||
with open(MAINSAIL_CONFIG_JSON, "w") as f:
|
||||
json.dump(config_data, f, indent=4)
|
||||
|
||||
67
kiauh/modules/mainsail/mainsail_utils.py
Normal file
67
kiauh/modules/mainsail/mainsail_utils.py
Normal file
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# ======================================================================= #
|
||||
# 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 json
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
from kiauh.modules.klipper.klipper import Klipper
|
||||
from kiauh.modules.mainsail import MAINSAIL_CONFIG_JSON
|
||||
from kiauh.utils.logger import Logger
|
||||
|
||||
|
||||
def backup_config_json() -> None:
|
||||
try:
|
||||
Logger.print_status(f"Backup '{MAINSAIL_CONFIG_JSON}' ...")
|
||||
target = os.path.join(Path.home(), "config.json.kiauh.bak")
|
||||
shutil.copy(MAINSAIL_CONFIG_JSON, target)
|
||||
except OSError:
|
||||
Logger.print_info(f"Unable to backup config.json. Skipped ...")
|
||||
|
||||
|
||||
def restore_config_json() -> None:
|
||||
try:
|
||||
Logger.print_status(f"Restore '{MAINSAIL_CONFIG_JSON}' ...")
|
||||
source = os.path.join(Path.home(), "config.json.kiauh.bak")
|
||||
shutil.copy(source, MAINSAIL_CONFIG_JSON)
|
||||
except OSError:
|
||||
Logger.print_info(f"Unable to restore config.json. Skipped ...")
|
||||
|
||||
|
||||
def enable_mainsail_remotemode() -> None:
|
||||
with open(MAINSAIL_CONFIG_JSON, "r") as f:
|
||||
config_data = json.load(f)
|
||||
|
||||
if config_data["instancesDB"] == "browser":
|
||||
return
|
||||
|
||||
Logger.print_status("Setting instance storage location to 'browser' ...")
|
||||
config_data["instancesDB"] = "browser"
|
||||
|
||||
with open(MAINSAIL_CONFIG_JSON, "w") as f:
|
||||
json.dump(config_data, f, indent=4)
|
||||
|
||||
|
||||
def symlink_webui_nginx_log(klipper_instances: List[Klipper]) -> None:
|
||||
Logger.print_status("Link NGINX logs into log directory ...")
|
||||
access_log = Path("/var/log/nginx/mainsail-access.log")
|
||||
error_log = Path("/var/log/nginx/mainsail-error.log")
|
||||
|
||||
for instance in klipper_instances:
|
||||
desti_access = Path(instance.log_dir).joinpath("mainsail-access.log")
|
||||
if not desti_access.exists():
|
||||
desti_access.symlink_to(access_log)
|
||||
|
||||
desti_error = Path(instance.log_dir).joinpath("mainsail-error.log")
|
||||
if not desti_error.exists():
|
||||
desti_error.symlink_to(error_log)
|
||||
Reference in New Issue
Block a user