diff --git a/computes/models.py b/computes/models.py index 57fdff14..645d1c9c 100644 --- a/computes/models.py +++ b/computes/models.py @@ -168,6 +168,9 @@ class Compute(Model): before = Compute.objects.filter(pk=self.pk).values(*ENDPOINT, "host_uuid").first() if self.pk else None if before and all(before[field] == getattr(self, field) for field in ENDPOINT): return + # connected anew: a connection this process still has proves no credentials (another + # process may have seen them change) + self.retire_connection() try: uuid = wvmHostDetails(self.hostname, self.login, self.password, self.type).host_uuid() except libvirtError as error: @@ -190,6 +193,16 @@ class Compute(Model): "add it as a new host")) self.host_uuid = uuid + def retire_connection(self): + """Its connection as saved is not used again (connection_manager).""" + connection_manager.retire_connection(self.hostname, self.login, self.password, self.type) + + def save(self, *args, **kwargs): + before = Compute.objects.filter(pk=self.pk).first() if self.pk else None + super().save(*args, **kwargs) + if before and any(getattr(before, field) != getattr(self, field) for field in ENDPOINT): + before.retire_connection() + def delete(self, *args, **kwargs): """Its VMs keep running on the host: their owners wait in tombstones, or follow a VM already listed on another compute. The compute is held @@ -203,4 +216,6 @@ class Compute(Model): Compute.objects.select_for_update().filter(pk=self.pk).first() for inst in self.instance_set.select_for_update(): forget(inst) - return super().delete(*args, **kwargs) + deleted = super().delete(*args, **kwargs) + self.retire_connection() + return deleted diff --git a/computes/tests/test_changed_connection.py b/computes/tests/test_changed_connection.py new file mode 100644 index 00000000..a461a16f --- /dev/null +++ b/computes/tests/test_changed_connection.py @@ -0,0 +1,42 @@ +"""A compute whose connection changes or that is deleted: its old connection +is not used again, and a VM listing of the old host is not saved.""" + +from unittest.mock import MagicMock, patch + +from django.test import TestCase +from instances.models import Instance + +from computes.models import Compute +from computes.tests.fakes import Domain +from computes.utils import refresh_instance_database + + +class ChangedConnectionTests(TestCase): + def setUp(self): + self.compute = Compute.objects.create(name="kvm1", hostname="192.0.2.1", login="wvc", password="old", type=1) + + @patch("computes.models.connection_manager") + def test_the_old_connection_is_retired_on_a_change_and_a_delete(self, manager): + self.compute.details = "rack 3" + self.compute.save() + manager.retire_connection.assert_not_called() # the same connection + self.compute.password = "new" + self.compute.save() + manager.retire_connection.assert_called_once_with("192.0.2.1", "wvc", "old", 1) + self.compute.delete() + manager.retire_connection.assert_called_with("192.0.2.1", "wvc", "new", 1) + + @patch("computes.models.wvmHostDetails") + @patch("computes.models.connection_manager") + def test_a_connection_checked_is_opened_anew(self, manager, host): + manager.retire_connection.side_effect = lambda *args: host.assert_not_called() + Compute(name="kvm2", hostname="192.0.2.2", login="wvc", password="pw", type=1).clean() + manager.retire_connection.assert_called_once_with("192.0.2.2", "wvc", "pw", 1) + host.assert_called_once() + + def test_a_listing_read_before_the_compute_changed_is_not_saved(self): + proxy = MagicMock(**{"wvm.listAllDomains.return_value": [Domain("web-1", "11111111-1111-1111-1111-111111111111")]}) + self.compute.__dict__.update(proxy=proxy, status=True) + Compute.objects.filter(pk=self.compute.pk).update(hostname="192.0.2.2") # an edit meanwhile + refresh_instance_database(self.compute) + self.assertFalse(Instance.objects.exists()) diff --git a/computes/utils.py b/computes/utils.py index 5839db26..92d872ec 100644 --- a/computes/utils.py +++ b/computes/utils.py @@ -7,7 +7,7 @@ from django.conf import settings from django.contrib.auth import get_user_model from django.db import connection, transaction from django.utils import timezone -from computes.models import Compute +from computes.models import ENDPOINT, Compute from instances.models import Instance, InstanceTombstone from logs.audit import write from operations import claims @@ -193,6 +193,10 @@ def refresh_instance_database(compute): # undoing a newer one. with claims.hold({claims.compute_key(compute_pk): Claim.SHARED, claims.reconcile_key(compute_pk): Claim.EXCLUSIVE}): + # a compute whose connection changed since it was read: the listing would be the old host's + if Compute.objects.filter(pk=compute_pk).values_list(*ENDPOINT).first() != \ + tuple(getattr(compute, field, None) for field in ENDPOINT): + return host_domains = _host_domains(compute) if host_domains is None: return diff --git a/vrtManager/connection.py b/vrtManager/connection.py index bec1478f..0e7f23ea 100644 --- a/vrtManager/connection.py +++ b/vrtManager/connection.py @@ -432,6 +432,13 @@ class wvmConnectionManager(object): return connection return None + def retire_connection(self, host, login, passwd, conn): + """The connection with these credentials, of a compute changed or + deleted, is not used again: asked again, it connects anew.""" + connection = self._search_connection(str(host), str(login), str(passwd) if passwd is not None else None, conn) + if connection is not None: + connection.retire() + def get_connection(self, host, login, passwd, conn): """ returns a connection object (as returned by the libvirt.open* methods) for the given host and credentials diff --git a/vrtManager/tests/test_connection.py b/vrtManager/tests/test_connection.py index 18401bf2..50c48c45 100644 --- a/vrtManager/tests/test_connection.py +++ b/vrtManager/tests/test_connection.py @@ -177,6 +177,17 @@ class BoundedOpenTestCase(unittest.TestCase): self.assertIsNone(entry.connection) self.assertEqual(entry.last_error, {"text": "Connection closed (keepalive timeout)", "values": {}}) + def test_a_retired_connection_of_a_changed_compute_is_opened_anew(self): + self.release.set() + old, new = alive_connection(), alive_connection() + with self.open_auth(old): + self.get() + self.manager.retire_connection("h", "u", "p", CONN_TCP) + self.manager.retire_connection("other", "u", "p", CONN_TCP) # none: nothing to do + with self.open_auth(new): + self.assertIs(self.get()[0], new) + self.assertEqual(self.calls, 2) + def test_an_rpc_error_opens_new_connections(self): self.release.set() old, new = alive_connection(), alive_connection()