From 30da02e25e040eb20c8f3741170db96792a1145a Mon Sep 17 00:00:00 2001 From: catborise Date: Mon, 9 Nov 2026 21:24:38 +0300 Subject: [PATCH] fix(computes): a VM gone from its compute gives no copy its owners When a VM left its compute and its UUID was listed on another, its owners moved there: an out-of-band move. With the UUID on two other computes, the first record found got them, which may be an independent copy, giving its owners another VM and charging their quota for it. The owners now move only when one other compute lists the UUID; otherwise they are kept on the Removed VMs page. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- computes/tests/test_ownership.py | 15 +++++++++++++++ computes/utils.py | 11 ++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/computes/tests/test_ownership.py b/computes/tests/test_ownership.py index a074cdf3..b68912ce 100644 --- a/computes/tests/test_ownership.py +++ b/computes/tests/test_ownership.py @@ -98,6 +98,21 @@ class OwnershipTestCase(TestCase): self.assertEqual(self.ownership(self.host_a), (True, [(self.owner, True, False, True)])) self.assertEqual(self.ownership(self.host_b), (False, [])) + def test_a_move_to_one_of_two_copies_gives_the_owners_to_neither(self): + host_c = self.compute("host-c") + self.owned_vm(self.host_a) + self.on(self.host_a, [("vm", UUID)]) + for host in (self.host_b, host_c): + self.on(host, [("vm", UUID)]) + refresh_instance_database(host) + self.on(self.host_a, []) + refresh_instance_database(self.host_a) + + self.assertEqual(self.ownership(self.host_b), (False, [])) + self.assertEqual(self.ownership(host_c), (False, [])) + self.assertEqual(InstanceTombstone.objects.get().owners, + [{"user": self.owner.id, "is_change": True, "is_delete": False, "is_vnc": True}]) + @override_settings(INSTANCE_OWNERSHIP_RETENTION_DAYS=30) def test_ownership_is_kept_for_a_bounded_time(self): self.owned_vm(self.host_a) diff --git a/computes/utils.py b/computes/utils.py index d2d29a1f..1d21f651 100644 --- a/computes/utils.py +++ b/computes/utils.py @@ -85,11 +85,12 @@ def forget(inst): """Remove an instance whose VM its compute no longer lists, or whose compute is deleted. Its ownership goes to the same UUID on another compute (an out-of-band move) or is kept in a tombstone in case the VM - comes back.""" - elsewhere = Instance.objects.filter(uuid=inst.uuid).exclude(compute_id=inst.compute_id).first() - if elsewhere: - move_ownership(inst, elsewhere) - _log(inst, f"owners moved to {elsewhere.compute.name}") + comes back. Two other computes with the UUID: either may be a copy, so + the Removed VMs page shows the owners for an administrator to add.""" + elsewhere = list(Instance.objects.filter(uuid=inst.uuid).exclude(compute_id=inst.compute_id)[:2]) + if len(elsewhere) == 1: + move_ownership(inst, elsewhere[0]) + _log(inst, f"owners moved to {elsewhere[0].compute.name}") else: _bury(inst) inst.delete()