2022-07-25 14:08:31 +00:00
|
|
|
"""Abstract class for a service running on a server"""
|
|
|
|
from abc import ABC, abstractmethod
|
2024-07-02 19:35:28 +00:00
|
|
|
from typing import Any, List, Optional
|
2022-07-25 14:08:31 +00:00
|
|
|
|
2024-03-08 09:04:05 +00:00
|
|
|
from selfprivacy_api import utils
|
|
|
|
from selfprivacy_api.utils import ReadUserData, WriteUserData
|
|
|
|
from selfprivacy_api.utils.waitloop import wait_until_true
|
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
|
|
|
|
2024-03-08 09:04:05 +00:00
|
|
|
from selfprivacy_api.jobs import Job, Jobs, JobStatus, report_progress
|
2024-03-08 10:25:04 +00:00
|
|
|
from selfprivacy_api.jobs.upgrade_system import rebuild_system
|
2024-03-08 09:04:05 +00:00
|
|
|
|
|
|
|
from selfprivacy_api.models.services import ServiceStatus, ServiceDnsRecord
|
2023-04-17 14:02:07 +00:00
|
|
|
from selfprivacy_api.services.generic_size_counter import get_storage_usage
|
2024-02-26 15:01:07 +00:00
|
|
|
from selfprivacy_api.services.owned_path import OwnedPath, Bind
|
2024-02-18 23:58:00 +00:00
|
|
|
from selfprivacy_api.services.moving import (
|
2024-02-26 15:01:07 +00:00
|
|
|
check_binds,
|
2024-02-18 23:58:00 +00:00
|
|
|
check_volume,
|
|
|
|
unbind_folders,
|
|
|
|
bind_folders,
|
|
|
|
ensure_folder_ownership,
|
|
|
|
MoveError,
|
2024-02-26 15:01:07 +00:00
|
|
|
move_data_to_volume,
|
2024-02-18 23:58:00 +00:00
|
|
|
)
|
2024-02-12 14:41:15 +00:00
|
|
|
|
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 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
|
|
|
|
|
2024-06-26 12:00:51 +00:00
|
|
|
@classmethod
|
2022-08-25 17:03:56 +00:00
|
|
|
@abstractmethod
|
2024-06-26 12:00:51 +00:00
|
|
|
def get_url(cls) -> 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
|
|
|
|
|
2024-06-26 12:00:51 +00:00
|
|
|
@classmethod
|
2024-03-01 00:21:31 +00:00
|
|
|
@abstractmethod
|
2024-06-26 12:00:51 +00:00
|
|
|
def get_subdomain(cls) -> Optional[str]:
|
2024-03-01 00:21:31 +00:00
|
|
|
"""
|
|
|
|
The assigned primary subdomain for this service.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2023-04-17 15:00:36 +00:00
|
|
|
@classmethod
|
2024-03-01 00:21:31 +00:00
|
|
|
def get_user(cls) -> 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
|
2024-03-01 00:21:31 +00:00
|
|
|
def get_group(cls) -> 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:
|
2024-01-03 19:19:29 +00:00
|
|
|
return user_data.get("modules", {}).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
|
|
|
@classmethod
|
2024-01-03 19:19:29 +00:00
|
|
|
def _set_enable(cls, enable: bool):
|
2023-10-20 16:05:12 +00:00
|
|
|
name = cls.get_id()
|
|
|
|
with WriteUserData() as user_data:
|
2024-01-03 19:19:29 +00:00
|
|
|
if "modules" not in user_data:
|
|
|
|
user_data["modules"] = {}
|
|
|
|
if name not in user_data["modules"]:
|
|
|
|
user_data["modules"][name] = {}
|
|
|
|
user_data["modules"][name]["enable"] = enable
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def enable(cls):
|
|
|
|
"""Enable the service. Usually this means enabling systemd unit."""
|
|
|
|
cls._set_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."""
|
2024-01-03 19:19:29 +00:00
|
|
|
cls._set_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
|
|
|
|
|
2024-07-02 19:35:28 +00:00
|
|
|
@classmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2024-07-02 19:35:28 +00:00
|
|
|
def get_configuration(cls) -> dict:
|
2022-07-25 14:08:31 +00:00
|
|
|
pass
|
|
|
|
|
2024-07-02 19:35:28 +00:00
|
|
|
@classmethod
|
2022-07-25 14:08:31 +00:00
|
|
|
@abstractmethod
|
2024-07-02 19:35:28 +00:00
|
|
|
def set_configuration(cls, 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
|
|
|
|
2024-03-01 00:21:31 +00:00
|
|
|
@classmethod
|
|
|
|
def get_dns_records(cls, ip4: str, ip6: Optional[str]) -> List[ServiceDnsRecord]:
|
|
|
|
subdomain = cls.get_subdomain()
|
|
|
|
display_name = cls.get_display_name()
|
|
|
|
if subdomain is None:
|
|
|
|
return []
|
|
|
|
dns_records = [
|
|
|
|
ServiceDnsRecord(
|
|
|
|
type="A",
|
|
|
|
name=subdomain,
|
|
|
|
content=ip4,
|
|
|
|
ttl=3600,
|
|
|
|
display_name=display_name,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
if ip6 is not None:
|
|
|
|
dns_records.append(
|
|
|
|
ServiceDnsRecord(
|
|
|
|
type="AAAA",
|
|
|
|
name=subdomain,
|
|
|
|
content=ip6,
|
|
|
|
ttl=3600,
|
|
|
|
display_name=f"{display_name} (IPv6)",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return dns_records
|
2022-07-25 14:08:31 +00:00
|
|
|
|
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):
|
2024-01-09 18:58:09 +00:00
|
|
|
return (
|
|
|
|
userdata.get("modules", {})
|
|
|
|
.get(cls.get_id(), {})
|
|
|
|
.get(
|
|
|
|
"location",
|
|
|
|
root_device,
|
|
|
|
)
|
2023-08-02 05:51:42 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
return root_device
|
2022-07-30 14:48:33 +00:00
|
|
|
|
2023-04-17 15:37:11 +00:00
|
|
|
@classmethod
|
2024-03-01 00:21:31 +00:00
|
|
|
def get_folders(cls) -> 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
|
2024-03-01 00:21:31 +00:00
|
|
|
def get_owned_folders(cls) -> 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]
|
|
|
|
|
2024-02-12 14:41:15 +00:00
|
|
|
# TODO: with better json utils, it can be one line, and not a separate function
|
|
|
|
@classmethod
|
|
|
|
def set_location(cls, volume: BlockDevice):
|
|
|
|
"""
|
2024-02-18 23:58:00 +00:00
|
|
|
Only changes userdata
|
2024-02-12 14:41:15 +00:00
|
|
|
"""
|
|
|
|
|
2024-02-19 00:10:13 +00:00
|
|
|
service_id = cls.get_id()
|
2024-02-12 14:41:15 +00:00
|
|
|
with WriteUserData() as user_data:
|
|
|
|
if "modules" not in user_data:
|
|
|
|
user_data["modules"] = {}
|
|
|
|
if service_id not in user_data["modules"]:
|
|
|
|
user_data["modules"][service_id] = {}
|
|
|
|
user_data["modules"][service_id]["location"] = volume.name
|
|
|
|
|
2024-03-04 14:26:26 +00:00
|
|
|
def binds(self) -> List[Bind]:
|
2024-02-26 15:01:07 +00:00
|
|
|
owned_folders = self.get_owned_folders()
|
|
|
|
|
|
|
|
return [
|
|
|
|
Bind.from_owned_path(folder, self.get_drive()) for folder in owned_folders
|
|
|
|
]
|
|
|
|
|
2024-02-12 14:41:15 +00:00
|
|
|
def assert_can_move(self, new_volume):
|
|
|
|
"""
|
2024-02-18 23:58:00 +00:00
|
|
|
Checks if the service can be moved to new volume
|
|
|
|
Raises errors if it cannot
|
2024-02-12 14:41:15 +00:00
|
|
|
"""
|
2024-02-18 23:58:00 +00:00
|
|
|
service_name = self.get_display_name()
|
|
|
|
if not self.is_movable():
|
|
|
|
raise MoveError(f"{service_name} is not movable")
|
|
|
|
|
2024-02-12 14:41:15 +00:00
|
|
|
with ReadUserData() as user_data:
|
|
|
|
if not user_data.get("useBinds", False):
|
|
|
|
raise MoveError("Server is not using binds.")
|
|
|
|
|
|
|
|
current_volume_name = self.get_drive()
|
|
|
|
if current_volume_name == new_volume.name:
|
|
|
|
raise MoveError(f"{service_name} is already on volume {new_volume}")
|
|
|
|
|
|
|
|
check_volume(new_volume, space_needed=self.get_storage_usage())
|
|
|
|
|
2024-02-26 15:01:07 +00:00
|
|
|
binds = self.binds()
|
|
|
|
if binds == []:
|
2024-02-12 14:41:15 +00:00
|
|
|
raise MoveError("nothing to move")
|
2024-02-26 15:01:07 +00:00
|
|
|
check_binds(current_volume_name, binds)
|
2024-02-12 14:41:15 +00:00
|
|
|
|
|
|
|
def do_move_to_volume(
|
|
|
|
self,
|
|
|
|
new_volume: BlockDevice,
|
|
|
|
job: Job,
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Move a service to another volume.
|
2024-02-26 15:01:07 +00:00
|
|
|
Note: It may be much simpler to write it per bind, but a bit less safe?
|
2024-02-12 14:41:15 +00:00
|
|
|
"""
|
|
|
|
service_name = self.get_display_name()
|
2024-02-26 15:01:07 +00:00
|
|
|
binds = self.binds()
|
2024-02-12 14:41:15 +00:00
|
|
|
|
2024-02-18 23:58:00 +00:00
|
|
|
report_progress(10, job, "Unmounting folders from old volume...")
|
2024-02-26 15:01:07 +00:00
|
|
|
unbind_folders(binds)
|
2024-02-18 23:58:00 +00:00
|
|
|
|
|
|
|
report_progress(20, job, "Moving data to new volume...")
|
2024-02-26 15:01:07 +00:00
|
|
|
binds = move_data_to_volume(binds, new_volume, job)
|
2024-02-12 14:41:15 +00:00
|
|
|
|
2024-02-18 23:58:00 +00:00
|
|
|
report_progress(70, job, f"Making sure {service_name} owns its files...")
|
2024-02-12 14:41:15 +00:00
|
|
|
try:
|
2024-02-26 15:01:07 +00:00
|
|
|
ensure_folder_ownership(binds)
|
2024-02-18 23:58:00 +00:00
|
|
|
except Exception as error:
|
|
|
|
# We have logged it via print and we additionally log it here in the error field
|
|
|
|
# We are continuing anyway but Job has no warning field
|
2024-02-12 14:41:15 +00:00
|
|
|
Jobs.update(
|
2024-02-18 23:58:00 +00:00
|
|
|
job,
|
|
|
|
JobStatus.RUNNING,
|
|
|
|
error=f"Service {service_name} will not be able to write files: "
|
|
|
|
+ str(error),
|
2024-02-12 14:41:15 +00:00
|
|
|
)
|
|
|
|
|
2024-02-18 23:58:00 +00:00
|
|
|
report_progress(90, job, f"Mounting {service_name} data...")
|
2024-02-26 15:01:07 +00:00
|
|
|
bind_folders(binds)
|
2024-02-12 14:41:15 +00:00
|
|
|
|
2024-02-18 23:58:00 +00:00
|
|
|
report_progress(95, job, f"Finishing moving {service_name}...")
|
|
|
|
self.set_location(new_volume)
|
|
|
|
|
|
|
|
def move_to_volume(self, volume: BlockDevice, job: Job) -> Job:
|
|
|
|
service_name = self.get_display_name()
|
|
|
|
|
|
|
|
report_progress(0, job, "Performing pre-move checks...")
|
|
|
|
self.assert_can_move(volume)
|
|
|
|
|
|
|
|
report_progress(5, job, f"Stopping {service_name}...")
|
|
|
|
assert self is not None
|
|
|
|
with StoppedService(self):
|
2024-02-19 18:37:00 +00:00
|
|
|
report_progress(9, job, "Stopped service, starting the move...")
|
2024-02-18 23:58:00 +00:00
|
|
|
self.do_move_to_volume(volume, job)
|
2024-03-13 12:48:11 +00:00
|
|
|
|
2024-03-08 10:25:04 +00:00
|
|
|
report_progress(98, job, "Move complete, rebuilding...")
|
|
|
|
rebuild_system(job, upgrade=False)
|
2024-03-13 12:48:11 +00:00
|
|
|
|
2024-03-08 09:28:58 +00:00
|
|
|
Jobs.update(
|
|
|
|
job=job,
|
|
|
|
status=JobStatus.FINISHED,
|
|
|
|
result=f"{service_name} moved successfully.",
|
|
|
|
status_text=f"Starting {service_name}...",
|
|
|
|
progress=100,
|
|
|
|
)
|
2024-02-18 23:58:00 +00:00
|
|
|
|
|
|
|
return job
|
2023-02-08 15:27:49 +00:00
|
|
|
|
2023-04-17 15:37:11 +00:00
|
|
|
@classmethod
|
|
|
|
def owned_path(cls, path: str):
|
2024-03-04 17:16:08 +00:00
|
|
|
"""Default folder ownership"""
|
|
|
|
service_name = cls.get_display_name()
|
|
|
|
|
|
|
|
try:
|
|
|
|
owner = cls.get_user()
|
|
|
|
if owner is None:
|
|
|
|
# TODO: assume root?
|
|
|
|
# (if we do not want to do assumptions, maybe not declare user optional?)
|
|
|
|
raise LookupError(f"no user for service: {service_name}")
|
|
|
|
group = cls.get_group()
|
|
|
|
if group is None:
|
|
|
|
raise LookupError(f"no group for service: {service_name}")
|
|
|
|
except Exception as error:
|
|
|
|
raise LookupError(
|
|
|
|
f"when deciding a bind for folder {path} of service {service_name}, error: {str(error)}"
|
|
|
|
)
|
|
|
|
|
2023-04-17 15:37:11 +00:00
|
|
|
return OwnedPath(
|
|
|
|
path=path,
|
2024-03-04 17:16:08 +00:00
|
|
|
owner=owner,
|
|
|
|
group=group,
|
2023-04-17 15:37:11 +00:00
|
|
|
)
|
|
|
|
|
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
|