mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 09:26:03 +00:00
fix(computes): a libvirt host is one compute, and a compute's VMs stay on their host
One host could be added twice, under another name, as TCP and SSH, or by another address: its VMs were listed once per record, each with its own owners, and counted twice in the capacity totals. A compute with VMs could also be pointed at another host: the owners of a VM then reached the VM with the same UUID there, a restored or copied one. A compute now keeps its libvirt host UUID. Adding a compute or changing its connection, on the web or the API, connects and reads it: a host that is already a compute is refused, and a compute with VMs moves only to an address of the same host, which must answer. Computes added before are read when another one is checked.
This commit is contained in:
parent
2db25314ea
commit
29ec20682b
17 changed files with 567 additions and 259 deletions
|
|
@ -1,3 +1,5 @@
|
|||
from copy import copy
|
||||
|
||||
from computes.models import Compute
|
||||
from computes.validators import validate_gstfsd_key
|
||||
from rest_framework import serializers
|
||||
|
|
@ -35,6 +37,14 @@ class ComputeSerializer(serializers.ModelSerializer):
|
|||
def validate_gstfsd_key(self, value):
|
||||
return value or (self.instance.gstfsd_key if self.instance else value)
|
||||
|
||||
def validate(self, attrs):
|
||||
# as the web form: the connection is tried and its host checked (Compute.clean)
|
||||
compute = copy(self.instance) if self.instance else Compute()
|
||||
for field, value in attrs.items():
|
||||
setattr(compute, field, value)
|
||||
compute.clean()
|
||||
return {**attrs, "host_uuid": compute.host_uuid}
|
||||
|
||||
class Meta:
|
||||
model = Compute
|
||||
fields = ["id", "name", "hostname", "login", "password", "type", "details", "migration_address", "gstfsd_key"]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from admin.permissions import IsSuperUser
|
||||
from computes.models import Compute
|
||||
from computes.utils import libvirt_compute_lock
|
||||
from computes.utils import hosts_lock
|
||||
from django.shortcuts import get_object_or_404
|
||||
from drf_spectacular.types import OpenApiTypes
|
||||
from drf_spectacular.utils import extend_schema
|
||||
|
|
@ -22,20 +22,30 @@ class ComputeViewSet(viewsets.ModelViewSet):
|
|||
serializer_class = ComputeSerializer
|
||||
permission_classes = [IsSuperUser]
|
||||
|
||||
# checked and saved under hosts_lock, as on the web (Compute.clean)
|
||||
def create(self, request, *args, **kwargs):
|
||||
with hosts_lock():
|
||||
return super().create(request, *args, **kwargs)
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
# the row is written here, as on the web: a busy compute is refused with one too
|
||||
compute = self.get_object()
|
||||
with audit(request, compute, f"Compute {compute.name} ({compute.hostname})", "change") as self.entry, \
|
||||
hosts_lock(compute):
|
||||
self.entry.skip = True # until a change is saved
|
||||
return super().update(request, *args, **kwargs)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
data = serializer.validated_data
|
||||
with audit(self.request, data["name"], f"Compute {data['name']} ({data['hostname']})", "add"):
|
||||
serializer.save()
|
||||
|
||||
def perform_update(self, serializer):
|
||||
compute = serializer.instance
|
||||
subject = f"Compute {compute.name} ({compute.hostname})"
|
||||
before = snapshot(compute)
|
||||
with audit(self.request, compute, subject, "change") as entry, libvirt_compute_lock(compute):
|
||||
serializer.save()
|
||||
fields = changed_fields(before, snapshot(compute))
|
||||
entry.action = "change " + ", ".join(fields)
|
||||
entry.skip = not fields
|
||||
before = snapshot(serializer.instance)
|
||||
serializer.save()
|
||||
fields = changed_fields(before, snapshot(serializer.instance))
|
||||
self.entry.action = "change " + ", ".join(fields)
|
||||
self.entry.skip = not fields
|
||||
|
||||
def perform_destroy(self, instance):
|
||||
with audit(self.request, instance, f"Compute {instance.name} ({instance.hostname})", "delete"):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue