5349f21752
New FastAPI microservice (port 8020) providing unified blob storage via PUT/GET/DELETE/LIST HTTP API. Local filesystem backend is the default (zero extra deps). S3-compatible and WebDAV backends are built in. Backend is switchable at runtime via POST /migrate, which copies all objects to the new backend, verifies each one, atomically switches, then cleans up the old backend. WebDAV XML parsing uses defusedxml to prevent XXE attacks. Wired into docker-compose (storage_data volume) and registered in the backend service-health poller as 'storage-service'. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
import logging
|
|
|
|
from fastapi import APIRouter, BackgroundTasks, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
from app.services import migration
|
|
from app.services.backend_manager import build_backend, switch_backend
|
|
|
|
router = APIRouter()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class MigrateRequest(BaseModel):
|
|
driver: str
|
|
config: dict = {}
|
|
|
|
|
|
class BackendConfigRequest(BaseModel):
|
|
driver: str
|
|
config: dict = {}
|
|
|
|
|
|
@router.post("/migrate", status_code=202)
|
|
async def start_migration(body: MigrateRequest, background_tasks: BackgroundTasks):
|
|
"""
|
|
Validate the new backend, then start an async migration job that:
|
|
1. Copies all objects from the current backend to the new one
|
|
2. Verifies every object
|
|
3. Atomically switches the active backend
|
|
4. Deletes all objects from the old backend
|
|
|
|
Returns 409 if a migration is already in progress.
|
|
Returns 400 if the new backend config fails validation.
|
|
"""
|
|
if migration.is_in_progress():
|
|
raise HTTPException(status_code=409, detail="A migration is already in progress")
|
|
|
|
# Reset status and enter validating state before any async work
|
|
migration._status.state = "validating"
|
|
migration._status.total = 0
|
|
migration._status.done = 0
|
|
migration._status.failed = 0
|
|
migration._status.errors.clear()
|
|
|
|
try:
|
|
new_backend = build_backend(body.driver, body.config)
|
|
await new_backend.test_connection()
|
|
except Exception as exc:
|
|
migration._status.state = "idle"
|
|
raise HTTPException(status_code=400, detail=f"Backend validation failed: {exc}")
|
|
|
|
background_tasks.add_task(migration.run_migration, new_backend)
|
|
return {"status": "started", "driver": body.driver}
|
|
|
|
|
|
@router.get("/migrate/status")
|
|
async def migration_status():
|
|
"""Poll this to track migration progress."""
|
|
return migration.get_status()
|
|
|
|
|
|
@router.delete("/migrate", status_code=204)
|
|
async def cancel_migration():
|
|
"""
|
|
Request cancellation of a running migration.
|
|
The old backend remains active. Returns 409 if no migration is running.
|
|
"""
|
|
cancelled = await migration.cancel()
|
|
if not cancelled:
|
|
raise HTTPException(status_code=409, detail="No cancellable migration in progress")
|
|
|
|
|
|
@router.patch("/backend-config", status_code=204)
|
|
async def update_backend_config(body: BackendConfigRequest):
|
|
"""
|
|
Reconfigure the active backend without migrating data (e.g. update S3 credentials
|
|
for the same endpoint, or switch back to local after a failed migration).
|
|
|
|
Use POST /migrate when you need data to be moved to the new backend.
|
|
"""
|
|
if migration.is_in_progress():
|
|
raise HTTPException(status_code=409, detail="Cannot reconfigure while migration is in progress")
|
|
try:
|
|
new_backend = build_backend(body.driver, body.config)
|
|
await new_backend.test_connection()
|
|
except Exception as exc:
|
|
raise HTTPException(status_code=400, detail=f"Backend validation failed: {exc}")
|
|
switch_backend(new_backend)
|