feat: add utils function to check for a specific service instance

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-05-25 21:32:59 +02:00
parent df45c5955e
commit d414be609a

View File

@@ -8,6 +8,7 @@
# ======================================================================= # # ======================================================================= #
import os import os
import re
import select import select
import shutil import shutil
import socket import socket
@@ -20,6 +21,7 @@ from pathlib import Path
from subprocess import DEVNULL, PIPE, CalledProcessError, Popen, run from subprocess import DEVNULL, PIPE, CalledProcessError, Popen, run
from typing import List, Literal 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
from utils.input_utils import get_confirm from utils.input_utils import get_confirm
from utils.logger import Logger from utils.logger import Logger
@@ -73,7 +75,6 @@ def parse_packages_from_file(source_file: Path) -> List[str]:
""" """
packages = [] packages = []
print("Reading dependencies...")
with open(source_file, "r") as file: with open(source_file, "r") as file:
for line in file: for line in file:
line = line.strip() line = line.strip()
@@ -365,6 +366,23 @@ def cmd_sysctl_manage(action: SysCtlManageAction) -> None:
raise raise
def service_instance_exists(name: str, exclude: List[str] = None) -> bool:
"""
Checks if a systemd service instance exists.
:param name: the service name
:param exclude: List of strings of service names to exclude
:return: True if the service exists, False otherwise
"""
exclude = exclude or []
pattern = re.compile(f"^{name}(-[0-9a-zA-Z]+)?.service$")
service_list = [
Path(SYSTEMD, service)
for service in SYSTEMD.iterdir()
if pattern.search(service.name) and not any(s in service.name for s in exclude)
]
return any(service_list)
def log_process(process: Popen) -> None: def log_process(process: Popen) -> None:
""" """
Helper method to print stdout of a process in near realtime to the console. Helper method to print stdout of a process in near realtime to the console.