mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 08:56:07 +00:00
A ready backup has a Restore button (Backups page and the VM's tab) and POST /api/v1/backups/<uuid>/restore/. The form takes the new VM's name (default <vm>-restored), a compute that has the target's pool, the pool for its disks, Connect the network (off by default: the restored guest keeps the VM's addresses and host name) and Start after restore. The job page follows the restore. The backup.restore job holds what a new VM's job holds, the backup (shared) and the target. Before anything is made it checks again: the host is not in maintenance; the name is free in the panel and on the host (a VM made with virsh too); the backup's files are in the target, the disks at their size and the small files with their SHA-256; the host offers the definition's emulator, machine type and firmware, its networks, bridges and network filters; the destination pool has room and none of the copies' names. The new VM's UUID, MACs, volume names and NVRAM path are recorded first. Then the disks are copied from the target, the NVRAM file uploaded, maintenance checked once more, and the VM defined last and given to the superuser who restored it. The definition is the kept one rewritten as a clone's (rewrite_for_new_vm, taken out of _clone_definition for both): new name, UUID and SMBIOS UUID, MACs and filter parameters, a VNC password, no cloud-init seed of the VM's; with a new generation ID, each disk's source the restored file without the old chain, the NVRAM path, and links down unless connected. Recovery records a VM defined by the recorded UUID; otherwise it removes the copies and the NVRAM file by their recorded names.
56 lines
2.8 KiB
Python
56 lines
2.8 KiB
Python
from django.urls import reverse
|
|
from drf_spectacular.utils import extend_schema
|
|
from rest_framework import mixins, status, viewsets
|
|
from rest_framework.decorators import action
|
|
from rest_framework.response import Response
|
|
|
|
from admin.permissions import IsSuperUser
|
|
from operations import jobs
|
|
from operations.api.serializers import OperationSerializer
|
|
from webvirtcloud.serializers import ErrorSerializer
|
|
|
|
from .models import Backup
|
|
from .serializers import BackupSerializer, CreateBackupSerializer, RestoreBackupSerializer, SubmissionSerializer
|
|
|
|
|
|
def _accepted(request, op):
|
|
return Response(OperationSerializer(op, context={"request": request}).data, status=status.HTTP_202_ACCEPTED,
|
|
headers={"Location": reverse("operation-detail", args=[op.pk])})
|
|
|
|
|
|
class BackupViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
|
|
"""VM backups: list, read, back a VM up and delete a backup (jobs,
|
|
answered 202)."""
|
|
|
|
permission_classes = [IsSuperUser]
|
|
serializer_class = BackupSerializer
|
|
queryset = Backup.objects.select_related("target").prefetch_related("files")
|
|
lookup_field = "uuid"
|
|
|
|
@extend_schema(request=CreateBackupSerializer, responses={202: OperationSerializer, 400: dict, 409: ErrorSerializer})
|
|
def create(self, request):
|
|
serializer = CreateBackupSerializer(data=request.data)
|
|
if not serializer.is_valid():
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
data = serializer.validated_data
|
|
raw = {"instance": data["instance"].pk, "target": data["target"].pk, "frozen": data["frozen"]}
|
|
return _accepted(request, jobs.submit("backup.create", request.user, raw, data["submission_key"]))
|
|
|
|
@extend_schema(request=SubmissionSerializer, responses={202: OperationSerializer, 409: ErrorSerializer})
|
|
def destroy(self, request, uuid=None):
|
|
backup = self.get_object()
|
|
serializer = SubmissionSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
key = serializer.validated_data["submission_key"]
|
|
return _accepted(request, jobs.submit("backup.delete", request.user, {"backup": str(backup.uuid)}, key))
|
|
|
|
@extend_schema(request=RestoreBackupSerializer, responses={202: OperationSerializer, 400: dict, 409: ErrorSerializer})
|
|
@action(detail=True, methods=["post"])
|
|
def restore(self, request, uuid=None):
|
|
backup = self.get_object()
|
|
serializer = RestoreBackupSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
data = dict(serializer.validated_data)
|
|
key = data.pop("submission_key")
|
|
raw = {**data, "backup": str(backup.uuid), "compute": data["compute"].pk}
|
|
return _accepted(request, jobs.submit("backup.restore", request.user, raw, key))
|