feat: v1.1.0 — backup module, IPAM node binding, HA, task queue, theme CSS

- Backup module: full CRUD for storages, backup files, scheduled jobs (APScheduler)
- Proxmox client: storage/backup/restore/HA methods
- IPAM: proxmox_node field, server+node binding for networks and allocations
- Task queue service: improvements and new task types
- Monitoring worker: extended metrics and alerts
- HA support: add/remove VMs from Proxmox HA groups
- tasks.html: new task tracking page
- proxmox_vms.html: major UI update (bulk ops, HA, snapshots)
- backups.html: full backup management UI (3 tabs)
- base.html: sidebar backup link, websocket manager
- theme.css: full CSS variables theme, 395 lines added
- migrations: migration #15 (BackupJob), #16 (proxmox_node)
- RBAC: backup resource permissions
- websocket_manager.py: new module for WS connections
This commit is contained in:
markmorado
2026-03-08 18:29:29 +01:00
parent 2c8becc6d4
commit b3db346cfb
21 changed files with 3676 additions and 93 deletions

View File

@@ -11,7 +11,7 @@ import httpx
import websockets
from ...db import get_db
from ...models import ProxmoxServer, VMInstance, User, IPAMAllocation, IPAMNetwork, VMSnapshotArchive
from ...models import ProxmoxServer, VMInstance, User, IPAMAllocation, IPAMNetwork, VMSnapshotArchive, ProxmoxTask
from ...schemas import ProxmoxServerCreate, ProxmoxServerUpdate, ProxmoxServerResponse
from ...proxmox_client import ProxmoxClient, get_proxmox_resources
from ...auth import get_current_user, PermissionChecker, require_permission, check_permission
@@ -28,6 +28,60 @@ from ._helpers import (check_vm_access, require_vm_access, _get_proxmox_client,
router = APIRouter()
# ==================== ProxmoxTaskService ====================
class ProxmoxTaskService:
"""Service for managing Proxmox UPID tasks stored in DB."""
@staticmethod
def register(
db: Session,
upid: str,
server_id: int,
user_id: int,
action: str,
node: str = None,
vmid: int = None,
vm_type: str = None,
description: str = None,
) -> ProxmoxTask:
task = ProxmoxTask(
upid=upid,
server_id=server_id,
user_id=user_id,
node=node,
vmid=vmid,
vm_type=vm_type,
action=action,
description=description,
status="running",
)
db.add(task)
db.commit()
db.refresh(task)
logger.info(f"[PROXMOX TASK] Registered UPID task #{task.id}: {action} on {node}/{vmid}")
return task
@staticmethod
def get_user_tasks(db: Session, user_id: int, limit: int = 50) -> list:
return (
db.query(ProxmoxTask)
.filter(ProxmoxTask.user_id == user_id)
.order_by(ProxmoxTask.created_at.desc())
.limit(limit)
.all()
)
@staticmethod
def get_active(db: Session, user_id: int) -> list:
return (
db.query(ProxmoxTask)
.filter(ProxmoxTask.user_id == user_id, ProxmoxTask.status == "running")
.order_by(ProxmoxTask.created_at.desc())
.all()
)
@router.get("/api/{server_id}/task/{upid}/status")
def get_task_status(
server_id: int,
@@ -86,33 +140,30 @@ def get_task_log(
raise HTTPException(status_code=500, detail=str(e))
# ─── Bulk (TaskQueue) endpoints ─────────────────────────────────────────────
@router.get("/api/tasks")
def get_user_tasks(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
limit: int = Query(20, ge=1, le=100)
):
"""Get recent tasks for current user"""
"""Get recent bulk-operation tasks for current user"""
tasks = TaskQueueService.get_user_tasks(db, current_user.id, limit)
return {
"tasks": [task.to_dict() for task in tasks]
}
return {"tasks": [task.to_dict() for task in tasks]}
@router.get("/api/tasks/active")
def get_active_tasks(
def get_active_bulk_tasks(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Get all active (pending/running) tasks for current user"""
"""Get all active (pending/running) bulk tasks for current user"""
tasks = db.query(TaskQueue).filter(
TaskQueue.user_id == current_user.id,
TaskQueue.status.in_(['pending', 'running'])
).order_by(TaskQueue.created_at.desc()).all()
return {
"tasks": [task.to_dict() for task in tasks]
}
return {"tasks": [task.to_dict() for task in tasks]}
@router.get("/api/tasks/{task_id}")
@@ -121,7 +172,7 @@ def get_task_by_id(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Get status of a specific task"""
"""Get status of a specific bulk task"""
task = db.query(TaskQueue).filter(
TaskQueue.id == task_id,
TaskQueue.user_id == current_user.id
@@ -139,10 +190,92 @@ def cancel_task(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Cancel a pending task"""
"""Cancel a pending bulk task"""
success = TaskQueueService.cancel_task(db, task_id, current_user.id)
if not success:
raise HTTPException(status_code=400, detail="Task cannot be cancelled (not found, not pending, or not yours)")
return {"success": True, "message": "Task cancelled"}
# ─── All tasks (bulk + proxmox UPID) combined ──────────────────────────────
@router.get("/api/all-tasks")
def get_all_tasks(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
limit: int = Query(30, ge=1, le=200),
status_filter: str = Query(None, alias="status"),
):
"""
Return both TaskQueue (bulk ops) and ProxmoxTask (UPID) records for the
current user, merged and sorted by created_at descending.
"""
bulk_q = db.query(TaskQueue).filter(TaskQueue.user_id == current_user.id)
proxmox_q = db.query(ProxmoxTask).filter(ProxmoxTask.user_id == current_user.id)
if status_filter:
# Normalise statuses: proxmox uses running/completed/failed; bulk uses the same + pending/cancelled
bulk_q = bulk_q.filter(TaskQueue.status == status_filter)
proxmox_q = proxmox_q.filter(ProxmoxTask.status == status_filter)
bulk_tasks = bulk_q.order_by(TaskQueue.created_at.desc()).limit(limit).all()
proxmox_tasks = proxmox_q.order_by(ProxmoxTask.created_at.desc()).limit(limit).all()
all_tasks = sorted(
[t.to_dict() for t in bulk_tasks] + [t.to_dict() for t in proxmox_tasks],
key=lambda t: t.get("created_at") or "",
reverse=True,
)[:limit]
return {"tasks": all_tasks}
@router.get("/api/all-tasks/active-count")
def get_active_count(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
"""Return count of currently active tasks (pending/running) for navbar badge."""
bulk_count = db.query(func.count(TaskQueue.id)).filter(
TaskQueue.user_id == current_user.id,
TaskQueue.status.in_(["pending", "running"]),
).scalar() or 0
proxmox_count = db.query(func.count(ProxmoxTask.id)).filter(
ProxmoxTask.user_id == current_user.id,
ProxmoxTask.status == "running",
).scalar() or 0
return {"count": bulk_count + proxmox_count}
# ─── Proxmox UPID task log from DB ──────────────────────────────────────────
@router.get("/api/proxmox-tasks")
def list_proxmox_tasks(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
limit: int = Query(20, ge=1, le=100),
):
"""List ProxmoxTask records for current user."""
tasks = ProxmoxTaskService.get_user_tasks(db, current_user.id, limit)
return {"tasks": [t.to_dict() for t in tasks]}
@router.get("/api/proxmox-tasks/{task_id}/log")
def get_proxmox_task_log_db(
task_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
"""Get the cached log text for a ProxmoxTask stored in DB."""
task = db.query(ProxmoxTask).filter(
ProxmoxTask.id == task_id,
ProxmoxTask.user_id == current_user.id,
).first()
if not task:
raise HTTPException(status_code=404, detail="Task not found")
return {"log_text": task.log_text or "", "status": task.status, "exit_status": task.exit_status}