2022-08-25 17:03:56 +00:00
|
|
|
"""Generic handler for moving services"""
|
|
|
|
|
2023-04-17 16:01:51 +00:00
|
|
|
from __future__ import annotations
|
2022-08-25 17:03:56 +00:00
|
|
|
import subprocess
|
|
|
|
import pathlib
|
|
|
|
import shutil
|
2024-01-29 16:51:35 +00:00
|
|
|
from typing import List
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from selfprivacy_api.jobs import Job, JobStatus, Jobs
|
|
|
|
from selfprivacy_api.utils.huey import huey
|
|
|
|
from selfprivacy_api.utils.block_devices import BlockDevice
|
|
|
|
from selfprivacy_api.utils import ReadUserData, WriteUserData
|
2024-01-29 16:51:35 +00:00
|
|
|
from selfprivacy_api.services.service import Service
|
2023-04-17 16:01:51 +00:00
|
|
|
from selfprivacy_api.services.owned_path import OwnedPath
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2024-01-29 16:51:35 +00:00
|
|
|
from selfprivacy_api.services.service import StoppedService
|
|
|
|
|
|
|
|
|
|
|
|
class MoveError(Exception):
|
|
|
|
"""Move failed"""
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
class FolderMoveNames(BaseModel):
|
|
|
|
name: str
|
|
|
|
bind_location: str
|
|
|
|
owner: str
|
|
|
|
group: str
|
|
|
|
|
2023-04-17 16:01:51 +00:00
|
|
|
@staticmethod
|
|
|
|
def from_owned_path(path: OwnedPath) -> FolderMoveNames:
|
|
|
|
return FolderMoveNames(
|
2023-04-19 12:12:46 +00:00
|
|
|
name=FolderMoveNames.get_foldername(path.path),
|
2023-04-17 16:01:51 +00:00
|
|
|
bind_location=path.path,
|
|
|
|
owner=path.owner,
|
|
|
|
group=path.group,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_foldername(path: str) -> str:
|
|
|
|
return path.split("/")[-1]
|
|
|
|
|
2023-04-19 12:43:47 +00:00
|
|
|
@staticmethod
|
2023-07-14 10:58:31 +00:00
|
|
|
def default_foldermoves(service: Service) -> list[FolderMoveNames]:
|
|
|
|
return [
|
|
|
|
FolderMoveNames.from_owned_path(folder)
|
|
|
|
for folder in service.get_owned_folders()
|
|
|
|
]
|
2023-04-19 12:43:47 +00:00
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
@huey.task()
|
|
|
|
def move_service(
|
|
|
|
service: Service,
|
2024-01-29 16:51:35 +00:00
|
|
|
new_volume: BlockDevice,
|
2022-08-25 17:03:56 +00:00
|
|
|
job: Job,
|
2024-01-29 16:51:35 +00:00
|
|
|
folder_names: List[FolderMoveNames],
|
|
|
|
userdata_location: str = None, # deprecated, not used
|
2022-08-25 17:03:56 +00:00
|
|
|
):
|
2024-01-29 16:51:35 +00:00
|
|
|
"""
|
|
|
|
Move a service to another volume.
|
|
|
|
Is not allowed to raise errors because it is a task.
|
|
|
|
"""
|
2022-08-25 17:03:56 +00:00
|
|
|
service_name = service.get_display_name()
|
2024-01-29 16:51:35 +00:00
|
|
|
old_volume = service.get_drive()
|
|
|
|
report_progress(0, job, "Performing pre-move checks...")
|
|
|
|
|
|
|
|
try:
|
|
|
|
with ReadUserData() as user_data:
|
|
|
|
if not user_data.get("useBinds", False):
|
|
|
|
raise MoveError("Server is not using binds.")
|
|
|
|
|
|
|
|
check_volume(new_volume, service)
|
|
|
|
check_folders(old_volume, folder_names)
|
|
|
|
|
|
|
|
report_progress(5, job, f"Stopping {service_name}...")
|
|
|
|
|
|
|
|
with StoppedService(service):
|
|
|
|
report_progress(10, job, "Unmounting folders from old volume...")
|
|
|
|
unmount_old_volume(folder_names)
|
|
|
|
|
|
|
|
report_progress(20, job, "Moving data to new volume...")
|
|
|
|
move_folders_to_volume(folder_names, old_volume, new_volume, job)
|
|
|
|
|
|
|
|
report_progress(70, job, f"Making sure {service_name} owns its files...")
|
|
|
|
chown_folders(folder_names, new_volume, job, service)
|
|
|
|
|
|
|
|
report_progress(90, job, f"Mounting {service_name} data...")
|
|
|
|
mount_folders(folder_names, new_volume)
|
|
|
|
|
|
|
|
report_progress(95, job, f"Finishing moving {service_name}...")
|
|
|
|
update_volume_in_userdata(service, new_volume)
|
|
|
|
|
2022-10-27 14:01:11 +00:00
|
|
|
Jobs.update(
|
2022-08-25 17:03:56 +00:00
|
|
|
job=job,
|
2024-01-29 16:51:35 +00:00
|
|
|
status=JobStatus.FINISHED,
|
|
|
|
result=f"{service_name} moved successfully.",
|
|
|
|
status_text=f"Starting {service_name}...",
|
|
|
|
progress=100,
|
2022-08-25 17:03:56 +00:00
|
|
|
)
|
2024-01-29 16:51:35 +00:00
|
|
|
except Exception as e:
|
2022-10-27 14:01:11 +00:00
|
|
|
Jobs.update(
|
2022-08-25 17:03:56 +00:00
|
|
|
job=job,
|
|
|
|
status=JobStatus.ERROR,
|
2024-01-29 16:51:35 +00:00
|
|
|
error=type(e).__name__ + " " + str(e),
|
2022-08-25 17:03:56 +00:00
|
|
|
)
|
2024-01-29 16:51:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_volume(new_volume: BlockDevice, service: Service) -> bool:
|
|
|
|
service_name = service.get_display_name()
|
|
|
|
old_volume_name: str = service.get_drive()
|
|
|
|
|
|
|
|
# Check if we are on the same volume
|
|
|
|
if old_volume_name == new_volume.name:
|
|
|
|
raise MoveError(f"{service_name} is already on volume {new_volume}")
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
# Check if there is enough space on the new volume
|
2024-01-29 16:51:35 +00:00
|
|
|
if int(new_volume.fsavail) < service.get_storage_usage():
|
|
|
|
raise MoveError("Not enough space on the new volume.")
|
|
|
|
|
2022-08-25 17:03:56 +00:00
|
|
|
# Make sure the volume is mounted
|
2024-01-29 16:51:35 +00:00
|
|
|
if (
|
|
|
|
not new_volume.is_root()
|
|
|
|
and f"/volumes/{new_volume.name}" not in new_volume.mountpoints
|
|
|
|
):
|
|
|
|
raise MoveError("Volume is not mounted.")
|
|
|
|
|
|
|
|
|
|
|
|
def check_folders(old_volume: BlockDevice, folder_names: List[FolderMoveNames]) -> None:
|
2022-08-25 17:03:56 +00:00
|
|
|
# Make sure current actual directory exists and if its user and group are correct
|
|
|
|
for folder in folder_names:
|
2024-01-29 16:51:35 +00:00
|
|
|
path = pathlib.Path(f"/volumes/{old_volume}/{folder.name}")
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2024-01-29 16:51:35 +00:00
|
|
|
if not path.exists():
|
|
|
|
raise MoveError(f"{path} is not found.")
|
|
|
|
if not path.is_dir():
|
|
|
|
raise MoveError(f"{path} is not a directory.")
|
|
|
|
if path.owner() != folder.owner:
|
|
|
|
raise MoveError(f"{path} owner is not {folder.owner}.")
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2024-01-29 16:51:35 +00:00
|
|
|
|
|
|
|
def unmount_old_volume(folder_names: List[FolderMoveNames]) -> None:
|
2022-08-25 17:03:56 +00:00
|
|
|
for folder in folder_names:
|
|
|
|
try:
|
|
|
|
subprocess.run(
|
|
|
|
["umount", folder.bind_location],
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
except subprocess.CalledProcessError:
|
2024-01-29 16:51:35 +00:00
|
|
|
raise MoveError("Unable to unmount old volume.")
|
|
|
|
|
|
|
|
|
|
|
|
def move_folders_to_volume(
|
|
|
|
folder_names: List[FolderMoveNames],
|
|
|
|
old_volume: BlockDevice,
|
|
|
|
new_volume: BlockDevice,
|
|
|
|
job: Job,
|
|
|
|
) -> None:
|
2022-08-25 17:03:56 +00:00
|
|
|
# Move data to new volume and set correct permissions
|
2024-01-29 16:51:35 +00:00
|
|
|
current_progress = job.progress
|
2022-08-25 17:03:56 +00:00
|
|
|
folder_percentage = 50 // len(folder_names)
|
|
|
|
for folder in folder_names:
|
|
|
|
shutil.move(
|
|
|
|
f"/volumes/{old_volume}/{folder.name}",
|
2024-01-29 16:51:35 +00:00
|
|
|
f"/volumes/{new_volume.name}/{folder.name}",
|
2022-08-25 17:03:56 +00:00
|
|
|
)
|
2024-01-29 16:51:35 +00:00
|
|
|
progress = current_progress + folder_percentage
|
|
|
|
report_progress(progress, job, "Moving data to new volume...")
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2024-01-29 16:51:35 +00:00
|
|
|
|
|
|
|
def chown_folders(
|
|
|
|
folder_names: List[FolderMoveNames], volume: BlockDevice, job: Job, service: Service
|
|
|
|
) -> None:
|
|
|
|
service_name = service.get_display_name()
|
2022-08-25 17:03:56 +00:00
|
|
|
for folder in folder_names:
|
|
|
|
try:
|
|
|
|
subprocess.run(
|
|
|
|
[
|
|
|
|
"chown",
|
|
|
|
"-R",
|
2022-09-19 00:04:57 +00:00
|
|
|
f"{folder.owner}:{folder.group}",
|
2022-08-25 17:03:56 +00:00
|
|
|
f"/volumes/{volume.name}/{folder.name}",
|
|
|
|
],
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
except subprocess.CalledProcessError as error:
|
|
|
|
print(error.output)
|
2022-10-27 14:01:11 +00:00
|
|
|
Jobs.update(
|
2022-08-25 17:03:56 +00:00
|
|
|
job=job,
|
|
|
|
status=JobStatus.RUNNING,
|
|
|
|
error=f"Unable to set ownership of new volume. {service_name} may not be able to access its files. Continuing anyway.",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-29 16:51:35 +00:00
|
|
|
def mount_folders(folder_names: List[FolderMoveNames], volume: BlockDevice) -> None:
|
2022-08-25 17:03:56 +00:00
|
|
|
for folder in folder_names:
|
|
|
|
try:
|
|
|
|
subprocess.run(
|
|
|
|
[
|
|
|
|
"mount",
|
|
|
|
"--bind",
|
|
|
|
f"/volumes/{volume.name}/{folder.name}",
|
|
|
|
folder.bind_location,
|
|
|
|
],
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
except subprocess.CalledProcessError as error:
|
|
|
|
print(error.output)
|
2024-01-29 16:51:35 +00:00
|
|
|
raise MoveError(f"Unable to mount new volume:{error.output}")
|
2022-08-25 17:03:56 +00:00
|
|
|
|
2024-01-29 16:51:35 +00:00
|
|
|
|
|
|
|
def update_volume_in_userdata(service: Service, volume: BlockDevice):
|
2022-08-25 17:03:56 +00:00
|
|
|
with WriteUserData() as user_data:
|
2024-01-29 16:51:35 +00:00
|
|
|
service_id = service.get_id()
|
2024-01-09 18:58:09 +00:00
|
|
|
if "modules" not in user_data:
|
|
|
|
user_data["modules"] = {}
|
2024-01-29 16:51:35 +00:00
|
|
|
if service_id not in user_data["modules"]:
|
|
|
|
user_data["modules"][service_id] = {}
|
|
|
|
user_data["modules"][service_id]["location"] = volume.name
|
|
|
|
|
|
|
|
|
|
|
|
def report_progress(progress: int, job: Job, status_text: str) -> None:
|
2022-10-27 14:01:11 +00:00
|
|
|
Jobs.update(
|
2022-08-25 17:03:56 +00:00
|
|
|
job=job,
|
2024-01-29 16:51:35 +00:00
|
|
|
status=JobStatus.RUNNING,
|
|
|
|
status_text=status_text,
|
|
|
|
progress=progress,
|
2022-08-25 17:03:56 +00:00
|
|
|
)
|