123 lines
3.5 KiB
Python
123 lines
3.5 KiB
Python
import json
|
|
import os
|
|
from dataclasses import dataclass
|
|
from uuid import UUID
|
|
|
|
from .node_files import read_root_file, require_root_directory, write_new_file
|
|
|
|
OPERATION = "retire_resource"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class RetirementRequest:
|
|
resource: object
|
|
operation_id: str
|
|
operation = OPERATION
|
|
|
|
def __post_init__(self):
|
|
try:
|
|
if str(UUID(self.operation_id)) == self.operation_id:
|
|
return
|
|
except (AttributeError, TypeError, ValueError):
|
|
pass
|
|
raise ValueError("Expected a valid retirement operation ID.")
|
|
|
|
def identity(self):
|
|
return {
|
|
"operation": self.operation,
|
|
"operation_id": self.operation_id,
|
|
}
|
|
|
|
def manifest(self):
|
|
return {
|
|
"operation": self.operation,
|
|
"resource": self.resource.manifest(),
|
|
"operation_id": self.operation_id,
|
|
}
|
|
|
|
|
|
def parse_retirement(
|
|
data, parse_resource, fallback, request_type=RetirementRequest
|
|
):
|
|
if (
|
|
not isinstance(data, dict)
|
|
or data.get("operation") != request_type.operation
|
|
):
|
|
return fallback(data)
|
|
if data.keys() != {"operation", "resource", "operation_id"}:
|
|
raise ValueError("Expected a retirement request.")
|
|
return request_type(parse_resource(data["resource"]), data["operation_id"])
|
|
|
|
|
|
class RetirementRecord:
|
|
def __init__(self, base, request):
|
|
self.base = base / ".retired"
|
|
self.directory = self.base / request.operation_id
|
|
self.document = json.dumps(request.manifest(), sort_keys=True) + "\n"
|
|
self.started = self.directory / "request"
|
|
self.completed = self.directory / "complete"
|
|
|
|
def matches(self, marker):
|
|
if not os.path.lexists(marker):
|
|
return False
|
|
require_root_directory(self.directory)
|
|
if read_root_file(marker, mode=0o600) != self.document:
|
|
raise ValueError("The retirement record has a different identity.")
|
|
return True
|
|
|
|
@property
|
|
def is_complete(self):
|
|
return self.matches(self.completed)
|
|
|
|
def begin(self):
|
|
require_root_directory(self.base, create_mode=0o700)
|
|
require_root_directory(self.directory, create_mode=0o700)
|
|
if not self.matches(self.started):
|
|
self.write(self.started)
|
|
sync_directory(self.base)
|
|
sync_directory(self.base.parent)
|
|
|
|
def finish(self):
|
|
self.write(self.completed)
|
|
|
|
def write(self, path):
|
|
write_new_file(path, self.document, mode=0o600)
|
|
sync_directory(self.directory)
|
|
|
|
|
|
def sync_directory(path):
|
|
fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY)
|
|
try:
|
|
os.fsync(fd)
|
|
finally:
|
|
os.close(fd)
|
|
|
|
|
|
def archive_path(source, destination):
|
|
if not os.path.lexists(source):
|
|
return
|
|
if os.path.lexists(destination):
|
|
raise ValueError("The retirement destination already contains data.")
|
|
os.rename(source, destination)
|
|
sync_directory(source.parent)
|
|
sync_directory(destination.parent)
|
|
|
|
|
|
def archive_data_directory(source, root, operation_id):
|
|
if not os.path.lexists(source):
|
|
return
|
|
base = root / ".retired"
|
|
require_root_directory(base, create_mode=0o700)
|
|
info = source.lstat()
|
|
if (
|
|
not source.is_dir()
|
|
or source.is_symlink()
|
|
or info.st_dev != base.stat().st_dev
|
|
):
|
|
raise ValueError(
|
|
"Move the managed data to the retirement filesystem first."
|
|
)
|
|
# A rename avoids traversing user-controlled files and links.
|
|
archive_path(source, base / operation_id)
|
|
sync_directory(base.parent)
|