refactor(services): remove systemctl call duplication

This commit is contained in:
Houkime 2024-12-12 15:42:59 +00:00
parent f21e59f99f
commit 0ce0d2142c
5 changed files with 49 additions and 33 deletions

View file

@ -112,6 +112,13 @@ class ServiceManager(Service):
"""Return service description.""" """Return service description."""
return "Enables communication between the SelfPrivacy app and the server." 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 @staticmethod
def get_svg_icon() -> str: def get_svg_icon() -> str:
"""Read SVG icon from file and return it as base64 encoded string.""" """Read SVG icon from file and return it as base64 encoded string."""

View file

@ -27,6 +27,10 @@ class MailServer(Service):
def get_description() -> str: def get_description() -> str:
return "E-Mail for company and family." return "E-Mail for company and family."
@staticmethod
def get_units() -> List[str]:
return ["dovecot2.service", "postfix.service"]
@staticmethod @staticmethod
def get_svg_icon() -> str: def get_svg_icon() -> str:
return base64.b64encode(MAILSERVER_ICON.encode("utf-8")).decode("utf-8") 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") raise NotImplementedError("disable is not implemented for MailServer")
@staticmethod @staticmethod
def stop(): def get_logs():
subprocess.run(["systemctl", "stop", "dovecot2.service"], check=False) return ""
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)
@staticmethod @staticmethod
def get_folders() -> List[str]: def get_folders() -> List[str]:

View file

@ -30,6 +30,10 @@ class Prometheus(Service):
def get_svg_icon() -> str: def get_svg_icon() -> str:
return base64.b64encode(PROMETHEUS_ICON.encode("utf-8")).decode("utf-8") return base64.b64encode(PROMETHEUS_ICON.encode("utf-8")).decode("utf-8")
@staticmethod
def get_units() -> List[str]:
return ["prometheus.service"]
@staticmethod @staticmethod
def get_url() -> Optional[str]: def get_url() -> Optional[str]:
"""Return service url.""" """Return service url."""
@ -64,16 +68,8 @@ class Prometheus(Service):
return get_service_status("prometheus.service") return get_service_status("prometheus.service")
@staticmethod @staticmethod
def stop(): def get_logs():
subprocess.run(["systemctl", "stop", "prometheus.service"]) return ""
@staticmethod
def start():
subprocess.run(["systemctl", "start", "prometheus.service"])
@staticmethod
def restart():
subprocess.run(["systemctl", "restart", "prometheus.service"])
@staticmethod @staticmethod
def get_owned_folders() -> List[OwnedPath]: def get_owned_folders() -> List[OwnedPath]:

View file

@ -4,6 +4,8 @@ from abc import ABC, abstractmethod
import logging import logging
from typing import List, Optional from typing import List, Optional
from os.path import exists 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 import utils
from selfprivacy_api.services.config_item import ServiceConfigItem from selfprivacy_api.services.config_item import ServiceConfigItem
@ -110,6 +112,13 @@ class Service(ABC):
""" """
return cls.get_id() return cls.get_id()
@staticmethod
@abstractmethod
def get_units() -> List[str]:
"""
List of all units associated with this service.
"""
@classmethod @classmethod
def get_group(cls) -> Optional[str]: def get_group(cls) -> Optional[str]:
""" """
@ -228,23 +237,27 @@ class Service(ABC):
"""Disable the service. Usually this means disabling systemd unit.""" """Disable the service. Usually this means disabling systemd unit."""
cls._set_enable(False) cls._set_enable(False)
@staticmethod @classmethod
@abstractmethod def stop(cls):
def stop():
"""Stop the service. Usually this means stopping systemd unit.""" """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 @staticmethod
@abstractmethod def start(cls):
def start():
"""Start the service. Usually this means starting systemd unit.""" """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 @staticmethod
@abstractmethod def restart(cls):
def restart():
"""Restart the service. Usually this means restarting systemd unit.""" """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 @classmethod
def get_configuration(cls): def get_configuration(cls):

View file

@ -59,6 +59,13 @@ class DummyService(Service):
"""Return service description.""" """Return service description."""
return "A small service used for test purposes. Does nothing." 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 @staticmethod
def get_svg_icon() -> str: def get_svg_icon() -> str:
"""Read SVG icon from file and return it as base64 encoded string.""" """Read SVG icon from file and return it as base64 encoded string."""