mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 09:26:03 +00:00
fix(networks): a network or interface in use is not stopped, and a new one is whole
An interface was stopped from the Interfaces page while running VMs had cards on it, directly or through a network using it, and they were cut off; a network in the same case was already refused. Stopping it is now refused the same way, naming the VMs. A new network whose autostart could not be set stayed defined and running while the page said it failed; it is now stopped and undefined like one that does not start, and a rollback that fails is said with the first error. A network named "." or ".." is refused: its page address was another page. A VM create checks when it is asked and again when its job starts that the host has the networks and the filter it names, and that no card is left without a network: one deleted meanwhile, or an empty name in the list, left a VM that does not start once its disks were made.
This commit is contained in:
parent
3308a75557
commit
534782fdda
14 changed files with 314 additions and 183 deletions
|
|
@ -234,9 +234,10 @@ class wvmInterface(wvmConnect):
|
|||
return info
|
||||
|
||||
def get_vms(self, networks=()):
|
||||
"""The running VMs' network cards on this bridge, directly or through
|
||||
the libvirt networks of networks (which use it): VM, tap device and
|
||||
the VM's MAC. One listing, one XML read per running VM."""
|
||||
"""The running VMs' network cards on this interface, directly (a
|
||||
bridge's, or a macvtap card's) or through the libvirt networks of
|
||||
networks (which use it): VM, tap device and the VM's MAC. One
|
||||
listing, one XML read per running VM."""
|
||||
name = self.iface.name()
|
||||
cards = []
|
||||
for dom in self.wvm.listAllDomains(VIR_CONNECT_LIST_DOMAINS_ACTIVE):
|
||||
|
|
@ -248,11 +249,16 @@ class wvmInterface(wvmConnect):
|
|||
continue
|
||||
for card in tree.findall("devices/interface"):
|
||||
source = card.find("source")
|
||||
if source is not None and (source.get("bridge") == name or source.get("network") in networks):
|
||||
if source is not None and (name in (source.get("bridge"), source.get("dev")) or source.get("network") in networks):
|
||||
cards.append({"vm": dom.name(), "tap": attribute(card, "target", "dev"), "mac": attribute(card, "mac", "address")})
|
||||
return sorted(cards, key=lambda card: (card["vm"], card["tap"] or ""))
|
||||
|
||||
def stop_iface(self):
|
||||
"""Refused while a running VM has a card on it (get_vms): that VM would be cut off."""
|
||||
name = self.iface.name()
|
||||
if cards := self.get_vms(networks_by_device(self.wvm).get(name, [])):
|
||||
raise util.OperationError(gettext_noop("Interface %(iface)s is in use by the running VMs %(vms)s; shut them down or move their network cards first"),
|
||||
iface=name, vms=", ".join(sorted({card["vm"] for card in cards})))
|
||||
self.iface.destroy()
|
||||
|
||||
def start_iface(self):
|
||||
|
|
|
|||
|
|
@ -184,10 +184,16 @@ class wvmNetworks(wvmConnect):
|
|||
net = self.wvm.networkDefineXML(xml)
|
||||
try:
|
||||
net.create()
|
||||
except libvirtError: # e.g. the subnet is in use: the name stays free for another try
|
||||
net.undefine()
|
||||
net.setAutostart(1)
|
||||
except libvirtError as err: # e.g. the subnet is in use: the name stays free for another try
|
||||
try:
|
||||
if net.isActive():
|
||||
net.destroy()
|
||||
net.undefine()
|
||||
except libvirtError as rollback_err:
|
||||
raise util.OperationError(gettext_noop("%(err)s; rolling the change back failed too, the host's configuration may be changed: %(rollback_err)s"),
|
||||
err=err, rollback_err=rollback_err) from err
|
||||
raise
|
||||
net.setAutostart(1)
|
||||
|
||||
|
||||
class wvmNetwork(wvmConnect):
|
||||
|
|
|
|||
|
|
@ -117,3 +117,22 @@ class BridgeUseTests(SimpleTestCase):
|
|||
{"vm": "db", "tap": "vnet7", "mac": "52:54:00:00:00:02"},
|
||||
{"vm": "web", "tap": "vnet3", "mac": "52:54:00:00:00:01"},
|
||||
])
|
||||
|
||||
def test_an_interface_a_running_vm_uses_is_not_stopped(self):
|
||||
# a macvtap card on it, or a card on a network that uses it: stopping it would cut the VM off
|
||||
from vrtManager.util import OperationError
|
||||
|
||||
for cards, nets in (("<interface type='direct'><source dev='eno2' mode='bridge'/></interface>", []),
|
||||
("<interface type='network'><source network='direct'/></interface>",
|
||||
[MagicMock(**{"name.return_value": "direct", "XMLDesc.return_value":
|
||||
"<network><forward mode='bridge'><interface dev='eno2'/></forward></network>"})])):
|
||||
dom = MagicMock(**{"name.return_value": "web", "XMLDesc.return_value": f"<domain><devices>{cards}</devices></domain>"})
|
||||
conn = wvmInterface.__new__(wvmInterface)
|
||||
conn.iface = MagicMock(**{"name.return_value": "eno2"})
|
||||
conn.wvm = MagicMock(**{"listAllDomains.return_value": [dom], "listAllNetworks.return_value": nets})
|
||||
with self.subTest(cards), self.assertRaisesRegex(OperationError, "in use by the running VMs web"):
|
||||
conn.stop_iface()
|
||||
conn.iface.destroy.assert_not_called()
|
||||
conn.wvm.listAllDomains.return_value = []
|
||||
conn.stop_iface()
|
||||
conn.iface.destroy.assert_called_once()
|
||||
|
|
|
|||
|
|
@ -192,6 +192,22 @@ class NetworkChangeTests(SimpleTestCase):
|
|||
"n1", "nat", True, gateway, mask, dhcp, False, None, None, None, "", False)
|
||||
self.assertNotIn("n1", self.conn.listDefinedNetworks() + self.conn.listNetworks())
|
||||
|
||||
def test_a_rollback_that_fails_is_said_with_the_first_error(self):
|
||||
gateway, mask, dhcp = network_size("192.0.2.0/24", True)
|
||||
with patch.object(libvirt.virNetwork, "setAutostart", side_effect=libvirt.libvirtError("cannot create symlink")), \
|
||||
patch.object(libvirt.virNetwork, "destroy", side_effect=libvirt.libvirtError("device busy")):
|
||||
with self.assertRaisesRegex(OperationError, "cannot create symlink; rolling the change back failed too.*device busy"):
|
||||
wvmNetworks("localhost", "", "", 4).create_network(
|
||||
"n1", "nat", True, gateway, mask, dhcp, False, None, None, None, "", False)
|
||||
|
||||
def test_a_network_whose_autostart_fails_is_not_left_running(self):
|
||||
gateway, mask, dhcp = network_size("192.0.2.0/24", True)
|
||||
with patch.object(libvirt.virNetwork, "setAutostart", side_effect=libvirt.libvirtError("cannot create symlink")):
|
||||
with self.assertRaises(libvirt.libvirtError):
|
||||
wvmNetworks("localhost", "", "", 4).create_network(
|
||||
"n1", "nat", True, gateway, mask, dhcp, False, None, None, None, "", False)
|
||||
self.assertNotIn("n1", self.conn.listDefinedNetworks() + self.conn.listNetworks())
|
||||
|
||||
|
||||
class LeasesTests(SimpleTestCase):
|
||||
def network(self, leases, active=True):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue