From 0ce0d2142c36ce24df596f645dbd5ead5b699855 Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Thu, 12 Dec 2024 15:42:59 +0000 Subject: [PATCH] refactor(services): remove systemctl call duplication --- selfprivacy_api/services/__init__.py | 7 ++++ .../services/mailserver/__init__.py | 19 ++++------- .../services/prometheus/__init__.py | 16 ++++----- selfprivacy_api/services/service.py | 33 +++++++++++++------ .../services/test_service/__init__.py | 7 ++++ 5 files changed, 49 insertions(+), 33 deletions(-) diff --git a/selfprivacy_api/services/__init__.py b/selfprivacy_api/services/__init__.py index 25e7c21..a2d60fe 100644 --- a/selfprivacy_api/services/__init__.py +++ b/selfprivacy_api/services/__init__.py @@ -112,6 +112,13 @@ class ServiceManager(Service): """Return service description.""" return "Enables communication between the SelfPrivacy app and the server." + @staticmethod + def get_units() -> List[str]: + """ + List of all units associated with this service. + """ + return [] + @staticmethod def get_svg_icon() -> str: """Read SVG icon from file and return it as base64 encoded string.""" diff --git a/selfprivacy_api/services/mailserver/__init__.py b/selfprivacy_api/services/mailserver/__init__.py index 05ce56a..03b4359 100644 --- a/selfprivacy_api/services/mailserver/__init__.py +++ b/selfprivacy_api/services/mailserver/__init__.py @@ -27,6 +27,10 @@ class MailServer(Service): def get_description() -> str: return "E-Mail for company and family." + @staticmethod + def get_units() -> List[str]: + return ["dovecot2.service", "postfix.service"] + @staticmethod def get_svg_icon() -> str: return base64.b64encode(MAILSERVER_ICON.encode("utf-8")).decode("utf-8") @@ -75,19 +79,8 @@ class MailServer(Service): raise NotImplementedError("disable is not implemented for MailServer") @staticmethod - def stop(): - subprocess.run(["systemctl", "stop", "dovecot2.service"], check=False) - subprocess.run(["systemctl", "stop", "postfix.service"], check=False) - - @staticmethod - def start(): - subprocess.run(["systemctl", "start", "dovecot2.service"], check=False) - subprocess.run(["systemctl", "start", "postfix.service"], check=False) - - @staticmethod - def restart(): - subprocess.run(["systemctl", "restart", "dovecot2.service"], check=False) - subprocess.run(["systemctl", "restart", "postfix.service"], check=False) + def get_logs(): + return "" @staticmethod def get_folders() -> List[str]: diff --git a/selfprivacy_api/services/prometheus/__init__.py b/selfprivacy_api/services/prometheus/__init__.py index 8b4bf4c..b3a919a 100644 --- a/selfprivacy_api/services/prometheus/__init__.py +++ b/selfprivacy_api/services/prometheus/__init__.py @@ -30,6 +30,10 @@ class Prometheus(Service): def get_svg_icon() -> str: return base64.b64encode(PROMETHEUS_ICON.encode("utf-8")).decode("utf-8") + @staticmethod + def get_units() -> List[str]: + return ["prometheus.service"] + @staticmethod def get_url() -> Optional[str]: """Return service url.""" @@ -64,16 +68,8 @@ class Prometheus(Service): return get_service_status("prometheus.service") @staticmethod - def stop(): - subprocess.run(["systemctl", "stop", "prometheus.service"]) - - @staticmethod - def start(): - subprocess.run(["systemctl", "start", "prometheus.service"]) - - @staticmethod - def restart(): - subprocess.run(["systemctl", "restart", "prometheus.service"]) + def get_logs(): + return "" @staticmethod def get_owned_folders() -> List[OwnedPath]: diff --git a/selfprivacy_api/services/service.py b/selfprivacy_api/services/service.py index ca180f3..15ddcdb 100644 --- a/selfprivacy_api/services/service.py +++ b/selfprivacy_api/services/service.py @@ -4,6 +4,8 @@ from abc import ABC, abstractmethod import logging from typing import List, Optional from os.path import exists +from subprocess import run +from selfprivacy_api.utils.root_interface import call_root_function from selfprivacy_api import utils from selfprivacy_api.services.config_item import ServiceConfigItem @@ -110,6 +112,13 @@ class Service(ABC): """ return cls.get_id() + @staticmethod + @abstractmethod + def get_units() -> List[str]: + """ + List of all units associated with this service. + """ + @classmethod def get_group(cls) -> Optional[str]: """ @@ -228,23 +237,27 @@ class Service(ABC): """Disable the service. Usually this means disabling systemd unit.""" cls._set_enable(False) - @staticmethod - @abstractmethod - def stop(): + @classmethod + def stop(cls): """Stop the service. Usually this means stopping systemd unit.""" - pass + for unit in cls.get_units(): + run(["systemctl", "stop", unit], check=False) + # TODO: use root separation daemon: + # call_root_function(["systemctl", "stop", unit]) @staticmethod - @abstractmethod - def start(): + def start(cls): """Start the service. Usually this means starting systemd unit.""" - pass + for unit in cls.get_units(): + run(["systemctl", "start", unit], check=False) + # call_root_function(["systemctl", "start", unit]) @staticmethod - @abstractmethod - def restart(): + def restart(cls): """Restart the service. Usually this means restarting systemd unit.""" - pass + for unit in cls.get_units(): + run(["systemctl", "restart", unit], check=False) + # call_root_function(["systemctl", "restart", unit]) @classmethod def get_configuration(cls): diff --git a/selfprivacy_api/services/test_service/__init__.py b/selfprivacy_api/services/test_service/__init__.py index 8f0e50d..519eb2e 100644 --- a/selfprivacy_api/services/test_service/__init__.py +++ b/selfprivacy_api/services/test_service/__init__.py @@ -59,6 +59,13 @@ class DummyService(Service): """Return service description.""" return "A small service used for test purposes. Does nothing." + @staticmethod + def get_units() -> List[str]: + """ + List of all units associated with this service. + """ + return [] + @staticmethod def get_svg_icon() -> str: """Read SVG icon from file and return it as base64 encoded string."""