feat: add storage migration dry run
这个提交存在于:
父节点
2666f8d4b6
当前提交
1198b9ae34
共有 6 个文件被更改,包括 104 次插入 和 1 次删除
|
|
@ -80,6 +80,7 @@ ROUTES: tuple[RouteSpec, ...] = (
|
||||||
RouteSpec("GET", "settings/snapshots/{snapshot_id}", "backend.routes.settings_routes.get_settings_snapshot", "config"),
|
RouteSpec("GET", "settings/snapshots/{snapshot_id}", "backend.routes.settings_routes.get_settings_snapshot", "config"),
|
||||||
RouteSpec("POST", "settings/rollback", "backend.routes.settings_routes.rollback_settings", "config"),
|
RouteSpec("POST", "settings/rollback", "backend.routes.settings_routes.rollback_settings", "config"),
|
||||||
RouteSpec("GET", "storage/status", "backend.routes.storage_routes.get_storage_status", "read"),
|
RouteSpec("GET", "storage/status", "backend.routes.storage_routes.get_storage_status", "read"),
|
||||||
|
RouteSpec("POST", "storage/migrate", "backend.routes.storage_routes.migrate_storage", "config"),
|
||||||
RouteSpec("GET", "media/sign", "backend.routes.media_routes.sign_media", "read"),
|
RouteSpec("GET", "media/sign", "backend.routes.media_routes.sign_media", "read"),
|
||||||
RouteSpec("GET", "media/fetch", "backend.routes.media_routes.fetch_media", "public_token"),
|
RouteSpec("GET", "media/fetch", "backend.routes.media_routes.fetch_media", "public_token"),
|
||||||
RouteSpec("GET", "material-categories", "backend.routes.material_routes.list_material_categories", "read"),
|
RouteSpec("GET", "material-categories", "backend.routes.material_routes.list_material_categories", "read"),
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,9 @@ from typing import Any
|
||||||
|
|
||||||
from backend.routes.common import handle_request
|
from backend.routes.common import handle_request
|
||||||
from backend.services.media_service import MediaService
|
from backend.services.media_service import MediaService
|
||||||
|
from backend.dto.common import ApiError, ErrorCode
|
||||||
from core.identity import RequestContext
|
from core.identity import RequestContext
|
||||||
from core.security import require_authenticated
|
from core.security import require_authenticated, require_config_manager
|
||||||
|
|
||||||
|
|
||||||
def get_storage_status(
|
def get_storage_status(
|
||||||
|
|
@ -21,3 +22,35 @@ def get_storage_status(
|
||||||
return media_service.storage_status()
|
return media_service.storage_status()
|
||||||
|
|
||||||
return handle_request(ctx, action)
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_storage(
|
||||||
|
ctx: RequestContext,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
service: MediaService | None = None,
|
||||||
|
) -> dict:
|
||||||
|
media_service = service or MediaService()
|
||||||
|
|
||||||
|
def action() -> dict[str, Any]:
|
||||||
|
require_config_manager(ctx)
|
||||||
|
source_uri = str(payload.get("source_uri") or "").strip()
|
||||||
|
target_uri = str(payload.get("target_uri") or "").strip()
|
||||||
|
if not source_uri:
|
||||||
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "source_uri is required", {"field": "source_uri"})
|
||||||
|
if not target_uri:
|
||||||
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "target_uri is required", {"field": "target_uri"})
|
||||||
|
return media_service.plan_storage_migration(
|
||||||
|
source_uri,
|
||||||
|
target_uri,
|
||||||
|
dry_run=_as_bool(payload.get("dry_run"), default=True),
|
||||||
|
)
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def _as_bool(value: Any, *, default: bool = False) -> bool:
|
||||||
|
if value is None:
|
||||||
|
return default
|
||||||
|
if isinstance(value, bool):
|
||||||
|
return value
|
||||||
|
return str(value).strip().lower() in {"1", "true", "yes", "on"}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,9 @@ class MediaService:
|
||||||
def storage_status(self) -> dict[str, Any]:
|
def storage_status(self) -> dict[str, Any]:
|
||||||
return self.storage.status()
|
return self.storage.status()
|
||||||
|
|
||||||
|
def plan_storage_migration(self, source_uri: str, target_uri: str, *, dry_run: bool = True) -> dict[str, Any]:
|
||||||
|
return self.storage.migration_plan(source_uri, target_uri, dry_run=dry_run)
|
||||||
|
|
||||||
def sign_media(self, uri: str) -> dict[str, Any]:
|
def sign_media(self, uri: str) -> dict[str, Any]:
|
||||||
return self.media_proxy.sign(uri)
|
return self.media_proxy.sign(uri)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,29 @@ class VfsStorage:
|
||||||
"total_bytes": sum(entry.bytes for entry in roots),
|
"total_bytes": sum(entry.bytes for entry in roots),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def migration_plan(self, source_uri: str, target_uri: str, *, dry_run: bool = True) -> dict[str, Any]:
|
||||||
|
if not dry_run:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"storage migration execution is not enabled; run with dry_run=true",
|
||||||
|
{"field": "dry_run"},
|
||||||
|
)
|
||||||
|
source_path = self.resolve_uri(source_uri)
|
||||||
|
self.resolve_uri(target_uri)
|
||||||
|
if not source_path.exists():
|
||||||
|
raise ApiError(ErrorCode.MEDIA_NOT_FOUND, "source media not found", {"uri": source_uri})
|
||||||
|
files, total_bytes = _path_file_stats(source_path)
|
||||||
|
return {
|
||||||
|
"dry_run": True,
|
||||||
|
"status": "planned",
|
||||||
|
"source_uri": source_uri,
|
||||||
|
"target_uri": target_uri,
|
||||||
|
"files": files,
|
||||||
|
"bytes": total_bytes,
|
||||||
|
"will_overwrite": False,
|
||||||
|
"execution_enabled": False,
|
||||||
|
}
|
||||||
|
|
||||||
def resolve_uri(self, uri: str) -> Path:
|
def resolve_uri(self, uri: str) -> Path:
|
||||||
root, relative = self.split_uri(uri)
|
root, relative = self.split_uri(uri)
|
||||||
root_path = self._root_paths().get(root)
|
root_path = self._root_paths().get(root)
|
||||||
|
|
@ -114,6 +137,20 @@ def _directory_size(path: Path) -> int:
|
||||||
return total
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
def _path_file_stats(path: Path) -> tuple[int, int]:
|
||||||
|
if path.is_file():
|
||||||
|
return 1, path.stat().st_size
|
||||||
|
if path.is_dir():
|
||||||
|
files = 0
|
||||||
|
total = 0
|
||||||
|
for child in path.rglob("*"):
|
||||||
|
if child.is_file():
|
||||||
|
files += 1
|
||||||
|
total += child.stat().st_size
|
||||||
|
return files, total
|
||||||
|
return 0, 0
|
||||||
|
|
||||||
|
|
||||||
def _is_writable(path: Path) -> bool:
|
def _is_writable(path: Path) -> bool:
|
||||||
target = path if path.exists() else path.parent
|
target = path if path.exists() else path.parent
|
||||||
return target.exists() and target.is_dir()
|
return target.exists() and target.is_dir()
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ class RouteRegistryTests(unittest.TestCase):
|
||||||
self.assertIn(("GET", "settings/snapshots"), keys)
|
self.assertIn(("GET", "settings/snapshots"), keys)
|
||||||
self.assertIn(("GET", "settings/snapshots/{snapshot_id}"), keys)
|
self.assertIn(("GET", "settings/snapshots/{snapshot_id}"), keys)
|
||||||
self.assertIn(("POST", "settings/rollback"), keys)
|
self.assertIn(("POST", "settings/rollback"), keys)
|
||||||
|
self.assertIn(("POST", "storage/migrate"), keys)
|
||||||
self.assertIn(("GET", "events/stream"), keys)
|
self.assertIn(("GET", "events/stream"), keys)
|
||||||
self.assertEqual(len(routes), len(ROUTES))
|
self.assertEqual(len(routes), len(ROUTES))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,7 @@ from backend.routes.settings_routes import (
|
||||||
validate_settings,
|
validate_settings,
|
||||||
)
|
)
|
||||||
from backend.routes.storage_routes import get_storage_status
|
from backend.routes.storage_routes import get_storage_status
|
||||||
|
from backend.routes.storage_routes import migrate_storage
|
||||||
from backend.routes.ui_routes import get_ui_actions, get_ui_bootstrap
|
from backend.routes.ui_routes import get_ui_actions, get_ui_bootstrap
|
||||||
from backend.routes.web_routes import get_web_manifest
|
from backend.routes.web_routes import get_web_manifest
|
||||||
from backend.routes.workflow_routes import (
|
from backend.routes.workflow_routes import (
|
||||||
|
|
@ -622,6 +623,33 @@ database:
|
||||||
self.assertEqual(fetched["data"]["size_bytes"], 5)
|
self.assertEqual(fetched["data"]["size_bytes"], 5)
|
||||||
self.assertNotIn(str(self.root), str(fetched["data"]))
|
self.assertNotIn(str(self.root), str(fetched["data"]))
|
||||||
|
|
||||||
|
planned = migrate_storage(
|
||||||
|
self.ctx,
|
||||||
|
{
|
||||||
|
"source_uri": "storage://images/sample.bin",
|
||||||
|
"target_uri": "storage://archives/sample.bin",
|
||||||
|
"dry_run": True,
|
||||||
|
},
|
||||||
|
self.media_service,
|
||||||
|
)
|
||||||
|
self.assertTrue(planned["ok"])
|
||||||
|
self.assertTrue(planned["data"]["dry_run"])
|
||||||
|
self.assertEqual(planned["data"]["files"], 1)
|
||||||
|
self.assertEqual(planned["data"]["bytes"], 5)
|
||||||
|
self.assertNotIn(str(self.root), str(planned["data"]))
|
||||||
|
|
||||||
|
execution = migrate_storage(
|
||||||
|
self.ctx,
|
||||||
|
{
|
||||||
|
"source_uri": "storage://images/sample.bin",
|
||||||
|
"target_uri": "storage://archives/sample.bin",
|
||||||
|
"dry_run": False,
|
||||||
|
},
|
||||||
|
self.media_service,
|
||||||
|
)
|
||||||
|
self.assertFalse(execution["ok"])
|
||||||
|
self.assertEqual(execution["error_code"], "VALIDATION_FAILED")
|
||||||
|
|
||||||
def test_media_signing_rejects_unsafe_or_missing_uri(self):
|
def test_media_signing_rejects_unsafe_or_missing_uri(self):
|
||||||
unsafe = sign_media(
|
unsafe = sign_media(
|
||||||
self.ctx,
|
self.ctx,
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用