1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 19:26:48 +00:00
webvirtcloud/instances/api/serializers.py
catborise 5061cc7f2c fix(instances): a migration runs in the mode the VM's state allows, and is logged as it ran
moveto read the VM state for each flag and dropped options that did
not fit it, so a request could run as something else than asked; the
log then named the mode from the form ("live" or "offline"), not the
one used. The API did not log at all and answered "instance migrate is
started" after the migration had finished.

- moveto reads the state once. A running VM migrates live, or without
  live paused for the transfer (non-live); a paused VM stays paused; a
  shut-off VM moves offline. Live for a shut-off VM, offline for a
  running or paused one, and any other state are refused before
  anything changes. unsafe, auto converge (live only) and compressed
  apply to a running VM.
- migrate_instance logs the outcome for the VM page and the API: the
  mode and options used, or the error. The API answers
  "instance is migrated (<mode>)" and accepts requests that leave out
  an optional option.
- Post copy is gone from the form, the view and the API: only the
  flag was set, and without migrateStartPostCopy, which the app never
  called, the migration never switched to post copy.
2026-10-06 15:26:49 +03:00

105 lines
2.7 KiB
Python

from instances.models import CreateInstance, Flavor, Instance, MigrateInstance
from instances.utils import nic_macs
from rest_framework import serializers
class InstanceSerializer(serializers.ModelSerializer):
class Meta:
model = Instance
fields = ["id", "compute", "name", "uuid", "is_template", "created", "drbd"]
class InstanceDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = Instance
fields = [
"id",
"compute",
"status",
"uuid",
"name",
"title",
"description",
"is_template",
"created",
"drbd",
"arch",
"machine",
"vcpu",
"memory",
"firmware",
"nvram",
"bootmenu",
"boot_order",
"disks",
"media",
"media_iso",
"snapshots",
"networks",
"console_type",
"console_port",
"console_keymap",
"console_listener_address",
"video_model",
"guest_agent_ready",
"autostart",
]
class FlavorSerializer(serializers.ModelSerializer):
class Meta:
model = Flavor
fields = ["label", "memory", "vcpu", "disk"]
class CreateInstanceSerializer(serializers.ModelSerializer):
video = serializers.CharField(initial="vga")
storage = serializers.CharField(initial="default")
cache_mode = serializers.CharField(initial="none")
virtio = serializers.BooleanField(initial=True)
qemu_ga = serializers.BooleanField(initial=True)
def validate(self, attrs):
try:
nic_macs(attrs.get("mac"), attrs.get("networks", ""))
except ValueError as err:
raise serializers.ValidationError({"mac": str(err)})
return attrs
class Meta:
model = CreateInstance
fields = [
"name",
"vcpu",
"vcpu_mode",
"memory",
"networks",
"mac",
"nwfilter",
"storage",
"hdd_size",
"cache_mode",
"meta_prealloc",
"virtio",
"qemu_ga",
"console_pass",
"video",
"listener_addr",
]
class MigrateSerializer(serializers.ModelSerializer):
instance = Instance.objects.all().prefetch_related("userinstance_set")
live = serializers.BooleanField(initial=True)
class Meta:
model = MigrateInstance
fields = [
"instance",
"target_compute",
"live",
"offline",
"autoconverge",
"compress",
"unsafe",
]