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 "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."""

View file

@ -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]:

View file

@ -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]:

View file

@ -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):

View file

@ -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."""