refactor: improve system service removal

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-08-19 16:31:56 +02:00
parent a0076698d5
commit bbf64eec9c
4 changed files with 20 additions and 19 deletions

View File

@@ -30,6 +30,7 @@ from core.constants import SYSTEMD
from core.instance_manager.instance_manager import InstanceManager
from core.logger import DialogType, Logger
from core.settings.kiauh_settings import KiauhSettings
from core.types import ComponentStatus
from utils.common import (
check_install_dependencies,
get_install_status,
@@ -45,9 +46,8 @@ from utils.sys_utils import (
check_python_version,
cmd_sysctl_service,
install_python_requirements,
remove_service_file,
remove_system_service,
)
from core.types import ComponentStatus
def install_klipperscreen() -> None:
@@ -166,10 +166,7 @@ def remove_klipperscreen() -> None:
Logger.print_warn("KlipperScreen environment not found!")
if KLIPPERSCREEN_SERVICE_FILE.exists():
remove_service_file(
KLIPPERSCREEN_SERVICE_NAME,
KLIPPERSCREEN_SERVICE_FILE,
)
remove_system_service(KLIPPERSCREEN_SERVICE_NAME)
logfile = Path(f"/tmp/{KLIPPERSCREEN_LOG_NAME}")
if logfile.exists():

View File

@@ -29,6 +29,7 @@ from core.backup_manager.backup_manager import BackupManager
from core.instance_manager.instance_manager import InstanceManager
from core.logger import DialogType, Logger
from core.settings.kiauh_settings import KiauhSettings
from core.types import ComponentStatus
from utils.common import check_install_dependencies, get_install_status
from utils.config_utils import add_config_section, remove_config_section
from utils.git_utils import (
@@ -40,9 +41,8 @@ from utils.sys_utils import (
check_python_version,
cmd_sysctl_service,
install_python_requirements,
remove_service_file,
remove_system_service,
)
from core.types import ComponentStatus
def install_mobileraker() -> None:
@@ -161,10 +161,7 @@ def remove_mobileraker() -> None:
Logger.print_warn("Mobileraker's companion environment not found!")
if MOBILERAKER_SERVICE_FILE.exists():
remove_service_file(
MOBILERAKER_SERVICE_NAME,
MOBILERAKER_SERVICE_FILE,
)
remove_system_service(MOBILERAKER_SERVICE_NAME)
kl_im = InstanceManager(Klipper)
kl_instances: List[Klipper] = kl_im.instances

View File

@@ -55,7 +55,7 @@ def remove_with_sudo(file: Path) -> None:
cmd = ["sudo", "rm", "-rf", file.as_posix()]
run(cmd, stderr=PIPE, check=True)
except CalledProcessError as e:
Logger.print_error(f"Failed to remove file: {e}")
Logger.print_error(f"Failed to remove {file}: {e}")
raise

View File

@@ -486,21 +486,28 @@ def create_env_file(path: Path, content: str) -> None:
raise
def remove_service_file(service_name: str, service_file: Path) -> None:
def remove_system_service(service_name: str) -> None:
"""
Removes a systemd service file at the provided path with the provided name.
:param service_name: the name of the service
:param service_file: the path of the service file
Disables and removes a systemd service
:param service_name: name of the service unit file - must end with '.service'
:return: None
"""
try:
if not service_name.endswith(".service"):
raise ValueError(f"service_name '{service_name}' must end with '.service'")
file: Path = SYSTEMD.joinpath(service_name)
if not file.exists() or not file.is_file():
Logger.print_info(f"Service '{service_name}' does not exist! Skipped ...")
return
Logger.print_status(f"Removing {service_name} ...")
cmd_sysctl_service(service_name, "stop")
cmd_sysctl_service(service_name, "disable")
remove_with_sudo(service_file)
remove_with_sudo(file)
cmd_sysctl_manage("daemon-reload")
cmd_sysctl_manage("reset-failed")
Logger.print_ok(f"{service_name} successfully removed!")
except Exception as e:
Logger.print_error(f"Error removing {service_name}:\n{e}")
Logger.print_error(f"Error removing {service_name}: {e}")
raise