1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

fix(computes): a compute is not edited while a job on it is admitted

A job resolves its compute's connection when it runs. Changing the
compute's hostname meanwhile sent the job to another host, where a VM
with the same UUID (a copy) could be deleted or changed instead. An edit
now holds the compute as a delete does, and is refused while a job or VM
operation on it is admitted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-09 21:23:16 +03:00
parent a2f4d3fe06
commit 12e33f7964
3 changed files with 15 additions and 2 deletions

View file

@ -1,5 +1,6 @@
from admin.permissions import IsSuperUser
from computes.models import Compute
from computes.utils import libvirt_compute_lock
from django.shortcuts import get_object_or_404
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import extend_schema
@ -30,7 +31,7 @@ class ComputeViewSet(viewsets.ModelViewSet):
compute = serializer.instance
subject = f"Compute {compute.name} ({compute.hostname})"
before = snapshot(compute)
with audit(self.request, compute, subject, "change") as entry:
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)

View file

@ -54,6 +54,17 @@ class ComputeAuditRowTests(TestCase):
self.assertFalse(row.success)
self.assertTrue(Compute.objects.exists())
def test_a_compute_with_a_job_is_not_changed(self):
# the job would run on whatever host the compute names when it starts
compute = Compute.objects.create(name="c1", hostname="192.0.2.5", login="admin", password="secret", type=1)
claims.admit({claims.compute_key(compute.id): Claim.SHARED}, holder="192.0.2.30:1:1")
self.client.post(reverse("compute_update", args=[compute.id]), self.form_data("192.0.2.6"))
res = self.client.patch(f"/api/v1/computes/{compute.id}/", {"hostname": "192.0.2.6"},
content_type="application/json")
self.assertEqual(res.status_code, 409)
self.assertEqual(Compute.objects.get().hostname, "192.0.2.5")
self.assertEqual([r.success for r in Logs.objects.all()], [False, False])
def test_the_api_writes_the_same_rows(self):
data = {"name": "c1", "hostname": "192.0.2.5", "login": "admin", "password": "secret", "type": 1}
res = self.client.post("/api/v1/computes/", data, content_type="application/json")

View file

@ -179,7 +179,8 @@ def compute_update(request, compute_id):
before = snapshot(compute)
form = FormClass(request.POST or None, instance=compute)
if form.is_valid():
with audit(request, compute, subject, "change") as entry:
# held as a delete holds it: a job on the compute keeps the host it was admitted for
with audit(request, compute, subject, "change") as entry, utils.libvirt_compute_lock(compute):
form.save()
fields = changed_fields(before, snapshot(compute))
entry.action = "change " + ", ".join(fields)