From 64ea337e7e7aca0ba3ede9c67a8807a6f07b5fdf Mon Sep 17 00:00:00 2001 From: dw-0 Date: Tue, 2 Jul 2024 22:07:52 +0200 Subject: [PATCH] refactor: create service removal helper function Signed-off-by: Dominik Willner --- kiauh/utils/sys_utils.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/kiauh/utils/sys_utils.py b/kiauh/utils/sys_utils.py index 707f364..477a5d2 100644 --- a/kiauh/utils/sys_utils.py +++ b/kiauh/utils/sys_utils.py @@ -22,7 +22,7 @@ from subprocess import DEVNULL, PIPE, CalledProcessError, Popen, run from typing import List, Literal from utils.constants import SYSTEMD -from utils.fs_utils import check_file_exist +from utils.fs_utils import check_file_exist, remove_with_sudo from utils.input_utils import get_confirm from utils.logger import Logger @@ -438,3 +438,23 @@ def create_env_file(path: Path, content: str) -> None: except OSError as e: Logger.print_error(f"Error creating env file: {e}") raise + + +def remove_service_file(service_name: str, service_file: Path) -> 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 + :return: None + """ + try: + Logger.print_status(f"Removing {service_name} ...") + cmd_sysctl_service(service_name, "stop") + cmd_sysctl_service(service_name, "disable") + remove_with_sudo(service_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}") + raise