1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00
webvirtcloud/vmbackups/serializers.py
catborise 823d34cc18 feat(vmbackups): restore a backup as a new VM
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.
2026-11-17 09:14:09 +03:00

43 lines
1.6 KiB
Python

from rest_framework import serializers
from computes.models import Compute
from instances.models import Instance
from .models import Backup, BackupFile, BackupTarget
class BackupFileSerializer(serializers.ModelSerializer):
class Meta:
model = BackupFile
fields = ["role", "dev", "volume", "format", "capacity", "size", "sha256"]
class BackupSerializer(serializers.ModelSerializer):
target = serializers.SlugRelatedField(slug_field="name", read_only=True)
files = BackupFileSerializer(many=True, read_only=True)
class Meta:
model = Backup
fields = ["uuid", "target", "instance", "vm_uuid", "vm_name", "compute_name", "created_at", "finished_at",
"state", "consistency", "size", "files", "operation"]
read_only_fields = fields
class CreateBackupSerializer(serializers.Serializer):
instance = serializers.PrimaryKeyRelatedField(queryset=Instance.objects.all())
target = serializers.PrimaryKeyRelatedField(queryset=BackupTarget.objects.all())
frozen = serializers.BooleanField(default=False)
submission_key = serializers.CharField(write_only=True)
class SubmissionSerializer(serializers.Serializer):
submission_key = serializers.CharField(write_only=True)
class RestoreBackupSerializer(serializers.Serializer):
compute = serializers.PrimaryKeyRelatedField(queryset=Compute.objects.all())
pool = serializers.CharField(default="default")
name = serializers.CharField(max_length=64)
connect = serializers.BooleanField(default=False)
start = serializers.BooleanField(default=False)
submission_key = serializers.CharField(write_only=True)