2022-07-25 14:08:31 +00:00
|
|
|
"""Abstract class for a service running on a server"""
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from enum import Enum
|
2022-07-30 14:48:33 +00:00
|
|
|
import typing
|
2022-07-25 14:08:31 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
from pydantic import BaseModel
|
|
|
|
from selfprivacy_api.jobs import Job
|
|
|
|
|
2023-08-02 05:51:42 +00:00
|
|
|
from selfprivacy_api.utils.block_devices import BlockDevice, BlockDevices
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2023-04-17 14:02:07 +00:00
|
|
|
from selfprivacy_api.services.generic_size_counter import get_storage_usage
|
2023-04-17 15:37:11 +00:00
|
|
|
from selfprivacy_api.services.owned_path import OwnedPath
|
2023-08-02 05:51:42 +00:00
|
|
|
from selfprivacy_api import utils
|
2023-07-12 15:02:45 +00:00
|
|
|
from selfprivacy_api.utils.waitloop import wait_until_true
|
2023-10-20 16:05:12 +00:00
|
|
|
from selfprivacy_api.utils import ReadUserData, WriteUserData, get_domain
|
2023-04-17 14:02:07 +00:00
|
|
|
|
2023-08-23 13:40:04 +00:00
|
|
|
DEFAULT_START_STOP_TIMEOUT = 5 * 60
|
2023-07-12 17:02:54 +00:00
|
|
|
|
2022-07-25 14:08:31 +00:00
|
|
|
|
|
|
|
class ServiceStatus(Enum):
|
|
|
|
"""Enum for service status"""
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
ACTIVE = "ACTIVE"
|
|
|
|
RELOADING = "RELOADING"
|
|
|
|
INACTIVE = "INACTIVE"
|
|
|
|
FAILED = "FAILED"
|
|
|
|
ACTIVATING = "ACTIVATING"
|
|
|
|
DEACTIVATING = "DEACTIVATING"
|
2022-07-25 14:08:31 +00:00
|
|
|
OFF = "OFF"
|
|
|
|
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
class ServiceDnsRecord(BaseModel):
|
2022-07-30 14:48:33 +00:00
|
|
|
type: str
|
|
|
|
name: str
|
|
|
|
content: str
|
|
|
|
ttl: int
|
2023-11-24 10:57:52 +00:00
|
|
|
display_name: str
|
2022-08-25 17:03:56 +00:00
|
|
|
priority: typing.Optional[int] = None
|
2022-07-30 14:48:33 +00:00
|
|
|
|
|
|
|
|
2022-07-25 14:08:31 +00:00
|
|
|
class Service(ABC):
|
|
|
|
"""
|
|
|
|
Service here is some software that is hosted on the server and
|
|
|
|
can be installed, configured and used by a user.
|
|
|
|
"""
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def get_id() -> str:
|
2023-06-29 11:27:08 +00:00
|
|
|
"""
|
|
|
|
The unique id of the service.
|
|
|
|
"""
|
2022-08-25 17:03:56 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def get_display_name() -> str:
|
2023-06-29 11:27:08 +00:00
|
|
|
"""
|
|
|
|
The name of the service that is shown to the user.
|
|
|
|
"""
|
2022-08-25 17:03:56 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def get_description() -> str:
|
2023-06-29 11:27:08 +00:00
|
|
|
"""
|
|
|
|
The description of the service that is shown to the user.
|
|
|
|
"""
|
2022-08-25 17:03:56 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def get_svg_icon() -> str:
|
2023-06-29 11:27:08 +00:00
|
|
|
"""
|
|
|
|
The monochrome svg icon of the service.
|
|
|
|
"""
|
2022-08-25 17:03:56 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def get_url() -> typing.Optional[str]:
|
2023-06-29 11:27:08 +00:00
|
|
|
"""
|
|
|
|
The url of the service if it is accessible from the internet browser.
|
|
|
|
"""
|
2022-08-25 17:03:56 +00:00
|
|
|
pass
|
|
|
|
|
2023-04-17 15:00:36 +00:00
|
|
|
@classmethod
|
|
|
|
def get_user(cls) -> typing.Optional[str]:
|
2023-06-29 11:27:08 +00:00
|
|
|
"""
|
|
|
|
The user that owns the service's files.
|
|
|
|
Defaults to the service's id.
|
|
|
|
"""
|
2023-04-17 15:00:36 +00:00
|
|
|
return cls.get_id()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_group(cls) -> typing.Optional[str]:
|
2023-06-29 11:27:08 +00:00
|
|
|
"""
|
|
|
|
The group that owns the service's files.
|
|
|
|
Defaults to the service's user.
|
|
|
|
"""
|
2023-04-17 15:00:36 +00:00
|
|
|
return cls.get_user()
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
def is_movable() -> bool:
|
2023-06-29 11:27:08 +00:00
|
|
|
"""`True` if the service can be moved to the non-system volume."""
|
2022-07-25 14:08:31 +00:00
|
|
|
pass
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
def is_required() -> bool:
|
2023-06-29 11:27:08 +00:00
|
|
|
"""`True` if the service is required for the server to function."""
|
2022-07-25 14:08:31 +00:00
|
|
|
pass
|
|
|
|
|
2023-06-29 10:45:00 +00:00
|
|
|
@staticmethod
|
|
|
|
def can_be_backed_up() -> bool:
|
2023-06-29 11:27:08 +00:00
|
|
|
"""`True` if the service can be backed up."""
|
2023-06-29 10:45:00 +00:00
|
|
|
return True
|
|
|
|
|
2023-06-29 11:27:08 +00:00
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def get_backup_description() -> str:
|
|
|
|
"""
|
|
|
|
The text shown to the user that exlplains what data will be
|
|
|
|
backed up.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2023-10-20 16:14:43 +00:00
|
|
|
@classmethod
|
|
|
|
def is_enabled(cls) -> bool:
|
2023-11-22 16:51:07 +00:00
|
|
|
"""
|
|
|
|
`True` if the service is enabled.
|
|
|
|
`False` if it is not enabled or not defined in file
|
|
|
|
If there is nothing in the file, this is equivalent to False
|
|
|
|
because NixOS won't enable it then.
|
|
|
|
"""
|
2023-10-20 16:14:43 +00:00
|
|
|
name = cls.get_id()
|
|
|
|
with ReadUserData() as user_data:
|
|
|
|
return user_data.get(name, {}).get("enable", False)
|
2022-07-25 14:08:31 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
def get_status() -> ServiceStatus:
|
2023-06-29 11:27:08 +00:00
|
|
|
"""The status of the service, reported by systemd."""
|
2022-07-25 14:08:31 +00:00
|
|
|
pass
|
|
|
|
|
2023-10-20 16:05:12 +00:00
|
|
|
# But they do not really enable?
|
|
|
|
@classmethod
|
|
|
|
def enable(cls):
|
2023-06-29 11:27:08 +00:00
|
|
|
"""Enable the service. Usually this means enabling systemd unit."""
|
2023-10-20 16:05:12 +00:00
|
|
|
name = cls.get_id()
|
|
|
|
with WriteUserData() as user_data:
|
2023-11-22 14:50:53 +00:00
|
|
|
if name not in user_data:
|
2023-10-20 16:05:12 +00:00
|
|
|
user_data[name] = {}
|
|
|
|
user_data[name]["enable"] = True
|
2022-07-25 14:08:31 +00:00
|
|
|
|
2023-10-20 16:05:12 +00:00
|
|
|
@classmethod
|
|
|
|
def disable(cls):
|
2023-06-29 11:27:08 +00:00
|
|
|
"""Disable the service. Usually this means disabling systemd unit."""
|
2023-10-20 16:05:12 +00:00
|
|
|
name = cls.get_id()
|
|
|
|
with WriteUserData() as user_data:
|
2023-11-22 14:50:53 +00:00
|
|
|
if name not in user_data:
|
2023-10-20 16:05:12 +00:00
|
|
|
user_data[name] = {}
|
|
|
|
user_data[name]["enable"] = False
|
2022-07-25 14:08:31 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
def stop():
|
2023-06-29 11:27:08 +00:00
|
|
|
"""Stop the service. Usually this means stopping systemd unit."""
|
2022-07-25 14:08:31 +00:00
|
|
|
pass
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
def start():
|
2023-06-29 11:27:08 +00:00
|
|
|
"""Start the service. Usually this means starting systemd unit."""
|
2022-07-25 14:08:31 +00:00
|
|
|
pass
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
def restart():
|
2023-06-29 11:27:08 +00:00
|
|
|
"""Restart the service. Usually this means restarting systemd unit."""
|
2022-07-25 14:08:31 +00:00
|
|
|
pass
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
def get_configuration():
|
2022-07-25 14:08:31 +00:00
|
|
|
pass
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
def set_configuration(config_items):
|
2022-07-25 14:08:31 +00:00
|
|
|
pass
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
def get_logs():
|
2022-07-25 14:08:31 +00:00
|
|
|
pass
|
|
|
|
|
2023-04-17 14:02:07 +00:00
|
|
|
@classmethod
|
|
|
|
def get_storage_usage(cls) -> int:
|
2023-04-17 14:18:44 +00:00
|
|
|
"""
|
|
|
|
Calculate the real storage usage of folders occupied by service
|
|
|
|
Calculate using pathlib.
|
|
|
|
Do not follow symlinks.
|
|
|
|
"""
|
2023-04-17 14:02:07 +00:00
|
|
|
storage_used = 0
|
|
|
|
for folder in cls.get_folders():
|
|
|
|
storage_used += get_storage_usage(folder)
|
|
|
|
return storage_used
|
2022-07-25 14:08:31 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
@staticmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
def get_dns_records() -> typing.List[ServiceDnsRecord]:
|
2022-07-25 14:08:31 +00:00
|
|
|
pass
|
|
|
|
|
2023-08-02 05:51:42 +00:00
|
|
|
@classmethod
|
|
|
|
def get_drive(cls) -> str:
|
|
|
|
"""
|
|
|
|
Get the name of the drive/volume where the service is located.
|
|
|
|
Example values are `sda1`, `vda`, `sdb`.
|
|
|
|
"""
|
|
|
|
root_device: str = BlockDevices().get_root_block_device().name
|
|
|
|
if not cls.is_movable():
|
|
|
|
return root_device
|
|
|
|
with utils.ReadUserData() as userdata:
|
|
|
|
if userdata.get("useBinds", False):
|
|
|
|
return userdata.get(cls.get_id(), {}).get(
|
|
|
|
"location",
|
|
|
|
root_device,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return root_device
|
2022-07-30 14:48:33 +00:00
|
|
|
|
2023-04-17 15:37:11 +00:00
|
|
|
@classmethod
|
2023-04-17 16:04:21 +00:00
|
|
|
def get_folders(cls) -> typing.List[str]:
|
2023-04-17 15:37:11 +00:00
|
|
|
"""
|
|
|
|
get a plain list of occupied directories
|
|
|
|
Default extracts info from overriden get_owned_folders()
|
|
|
|
"""
|
|
|
|
if cls.get_owned_folders == Service.get_owned_folders:
|
|
|
|
raise NotImplementedError(
|
|
|
|
"you need to implement at least one of get_folders() or get_owned_folders()"
|
|
|
|
)
|
|
|
|
return [owned_folder.path for owned_folder in cls.get_owned_folders()]
|
|
|
|
|
|
|
|
@classmethod
|
2023-04-17 16:04:21 +00:00
|
|
|
def get_owned_folders(cls) -> typing.List[OwnedPath]:
|
2023-04-17 15:37:11 +00:00
|
|
|
"""
|
|
|
|
Get a list of occupied directories with ownership info
|
|
|
|
Default extracts info from overriden get_folders()
|
|
|
|
"""
|
|
|
|
if cls.get_folders == Service.get_folders:
|
|
|
|
raise NotImplementedError(
|
|
|
|
"you need to implement at least one of get_folders() or get_owned_folders()"
|
|
|
|
)
|
|
|
|
return [cls.owned_path(path) for path in cls.get_folders()]
|
2023-04-14 10:32:14 +00:00
|
|
|
|
2023-04-17 12:54:21 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_foldername(path: str) -> str:
|
|
|
|
return path.split("/")[-1]
|
|
|
|
|
2022-07-30 14:48:33 +00:00
|
|
|
@abstractmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
def move_to_volume(self, volume: BlockDevice) -> Job:
|
2023-10-06 13:17:48 +00:00
|
|
|
"""Cannot raise errors.
|
|
|
|
Returns errors as an errored out Job instead."""
|
2022-07-30 14:48:33 +00:00
|
|
|
pass
|
2023-02-08 15:27:49 +00:00
|
|
|
|
2023-04-17 15:37:11 +00:00
|
|
|
@classmethod
|
|
|
|
def owned_path(cls, path: str):
|
|
|
|
"""A default guess on folder ownership"""
|
|
|
|
return OwnedPath(
|
|
|
|
path=path,
|
|
|
|
owner=cls.get_user(),
|
|
|
|
group=cls.get_group(),
|
|
|
|
)
|
|
|
|
|
2023-02-08 15:27:49 +00:00
|
|
|
def pre_backup(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def post_restore(self):
|
|
|
|
pass
|
2023-07-12 15:02:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StoppedService:
|
|
|
|
"""
|
2023-07-12 17:02:54 +00:00
|
|
|
A context manager that stops the service if needed and reactivates it
|
|
|
|
after you are done if it was active
|
|
|
|
|
|
|
|
Example:
|
|
|
|
```
|
|
|
|
assert service.get_status() == ServiceStatus.ACTIVE
|
|
|
|
with StoppedService(service) [as stopped_service]:
|
|
|
|
assert service.get_status() == ServiceStatus.INACTIVE
|
|
|
|
```
|
2023-07-12 15:02:45 +00:00
|
|
|
"""
|
2023-07-12 17:02:54 +00:00
|
|
|
|
2023-07-12 15:02:45 +00:00
|
|
|
def __init__(self, service: Service):
|
|
|
|
self.service = service
|
|
|
|
self.original_status = service.get_status()
|
|
|
|
|
|
|
|
def __enter__(self) -> Service:
|
|
|
|
self.original_status = self.service.get_status()
|
2023-08-23 13:39:12 +00:00
|
|
|
if self.original_status not in [ServiceStatus.INACTIVE, ServiceStatus.FAILED]:
|
2023-08-23 13:55:23 +00:00
|
|
|
try:
|
|
|
|
self.service.stop()
|
|
|
|
wait_until_true(
|
|
|
|
lambda: self.service.get_status() == ServiceStatus.INACTIVE,
|
|
|
|
timeout_sec=DEFAULT_START_STOP_TIMEOUT,
|
|
|
|
)
|
2023-08-25 16:28:37 +00:00
|
|
|
except TimeoutError as error:
|
|
|
|
raise TimeoutError(
|
|
|
|
f"timed out waiting for {self.service.get_display_name()} to stop"
|
|
|
|
) from error
|
2023-07-12 15:02:45 +00:00
|
|
|
return self.service
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
if self.original_status in [ServiceStatus.ACTIVATING, ServiceStatus.ACTIVE]:
|
2023-08-23 13:55:23 +00:00
|
|
|
try:
|
|
|
|
self.service.start()
|
|
|
|
wait_until_true(
|
|
|
|
lambda: self.service.get_status() == ServiceStatus.ACTIVE,
|
|
|
|
timeout_sec=DEFAULT_START_STOP_TIMEOUT,
|
|
|
|
)
|
2023-08-25 16:28:37 +00:00
|
|
|
except TimeoutError as error:
|
|
|
|
raise TimeoutError(
|
|
|
|
f"timed out waiting for {self.service.get_display_name()} to start"
|
|
|
|
) from error
|