diff --git a/instances/models.py b/instances/models.py index ef95154..68d2ecc 100644 --- a/instances/models.py +++ b/instances/models.py @@ -204,10 +204,6 @@ class Instance(models.Model): def formats(self): return self.proxy.get_image_formats() - @cached_property - def interfaces(self): - return self.proxy.get_ifaces() - class PermissionSet(models.Model): """ diff --git a/instances/templates/add_instance_network_block.html b/instances/templates/add_instance_network_block.html index 6d82fd5..724ca80 100644 --- a/instances/templates/add_instance_network_block.html +++ b/instances/templates/add_instance_network_block.html @@ -27,7 +27,7 @@ {% for c_net in networks_host %} {% endfor %} - {% for c_iface in instance.interfaces %} + {% for c_iface in interfaces_host %} {% endfor %} diff --git a/instances/templates/instances/settings_tab.html b/instances/templates/instances/settings_tab.html index 2371258..de17658 100644 --- a/instances/templates/instances/settings_tab.html +++ b/instances/templates/instances/settings_tab.html @@ -401,7 +401,7 @@ {% for c_net in networks_host %} {% endfor %} - {% for c_iface in instance.interfaces %} + {% for c_iface in interfaces_host %} {% endfor %} diff --git a/instances/views.py b/instances/views.py index 6d4b33a..67130d5 100644 --- a/instances/views.py +++ b/instances/views.py @@ -25,6 +25,7 @@ from logs.views import addlogmsg from vrtManager import util from vrtManager.create import wvmCreate from vrtManager.instance import wvmInstances +from vrtManager.interface import wvmInterface from vrtManager.storage import wvmStorage from vrtManager.util import randomPasswd @@ -121,6 +122,7 @@ def instance(request, pk): memory_host = instance.proxy.get_max_memory() bus_host = instance.proxy.get_disk_bus_types(instance.arch, instance.machine) networks_host = sorted(instance.proxy.get_networks()) + interfaces_host = sorted(instance.proxy.get_ifaces()) nwfilters_host = instance.proxy.get_nwfilters() storages_host = sorted(instance.proxy.get_storages(True)) net_models_host = instance.proxy.get_network_models() @@ -904,6 +906,16 @@ def change_network(request, pk): (source, source_type) = utils.get_network_tuple(request.POST.get(post)) network_data[post] = source network_data[post + "-type"] = source_type + + if source_type == 'iface': + iface = wvmInterface( + instance.compute.hostname, + instance.compute.login, + instance.compute.password, + instance.compute.type, + source, + ) + network_data[post + "-type"] = iface.get_type() elif post.startswith("net-"): network_data[post] = request.POST.get(post, "") @@ -923,6 +935,16 @@ def add_network(request, pk): nwfilter = request.POST.get("add-net-nwfilter") (source, source_type) = utils.get_network_tuple(request.POST.get("add-net-network")) + if source_type == 'iface': + iface = wvmInterface( + instance.compute.hostname, + instance.compute.login, + instance.compute.password, + instance.compute.type, + source, + ) + source_type = iface.get_type() + instance.proxy.add_network(mac, source, source_type, nwfilter=nwfilter) msg = _("Add network: %(mac)s") % {"mac": mac} addlogmsg(request.user.username, instance.name, msg) diff --git a/interfaces/views.py b/interfaces/views.py index a6f8c0b..0ef2cb6 100644 --- a/interfaces/views.py +++ b/interfaces/views.py @@ -29,7 +29,8 @@ def interfaces(request, compute_id): netdevs = ["eth0", "eth1"] for iface in ifaces: - ifaces_all.append(conn.get_iface_info(iface)) + interf = wvmInterface(compute.hostname, compute.login, compute.password, compute.type, iface) + ifaces_all.append(interf.get_details()) if request.method == "POST": if "create" in request.POST: diff --git a/vrtManager/connection.py b/vrtManager/connection.py index 0272353..3f49667 100644 --- a/vrtManager/connection.py +++ b/vrtManager/connection.py @@ -482,7 +482,7 @@ class wvmConnect(object): def get_networks(self): """ - :return: list of networks + :return: list of host networks """ virtnet = [] for net in self.wvm.listNetworks(): @@ -493,7 +493,7 @@ class wvmConnect(object): def get_ifaces(self): """ - :return: list of network interfaces + :return: list of host interfaces """ interface = [] for inface in self.wvm.listInterfaces(): diff --git a/vrtManager/instance.py b/vrtManager/instance.py index 9784c1a..a59ec62 100644 --- a/vrtManager/instance.py +++ b/vrtManager/instance.py @@ -1366,19 +1366,15 @@ class wvmInstance(wvmConnect): return bridge_name def add_network(self, mac_address, source, source_type="net", model="virtio", nwfilter=None): - forward_mode = "" - if source_type != "iface": - forward_mode = self.get_network_forward(source) - - if forward_mode in ["nat", "isolated", "routed"]: + + if source_type == "net": interface_type = "network" - elif forward_mode == "": - interface_type = "direct" + elif source_type == "bridge": + interface_type = "bridge" else: - if self.get_bridge_name(source, source_type) is None: - interface_type = "network" - else: - interface_type = "bridge" + interface_type = "direct" + + # network modes not handled: default is bridge xml_iface = f""" @@ -1386,13 +1382,11 @@ class wvmInstance(wvmConnect): if interface_type == "network": xml_iface += f"""""" elif interface_type == "direct": - if source_type == "net": - xml_iface += f"""""" - else: - xml_iface += f"""""" + xml_iface += f"""""" + elif interface_type == "bridge": + xml_iface += f"""""" else: - bridge_name = self.get_bridge_name(source, source_type) - xml_iface += f"""""" + raise libvirtError(f"'{interface_type}' is an unexpected interface type.") xml_iface += f"""""" if nwfilter: xml_iface += f"""""" @@ -1416,8 +1410,35 @@ class wvmInstance(wvmConnect): self.instance.detachDeviceFlags(new_xml, VIR_DOMAIN_AFFECT_CONFIG) if self.get_status() == 5: self.instance.detachDeviceFlags(new_xml, VIR_DOMAIN_AFFECT_CONFIG) + return new_xml + return None def change_network(self, network_data): + net_mac = network_data.get("net-mac-0") + net_source = network_data.get("net-source-0") + net_source_type = network_data.get("net-source-0-type") + net_filter = network_data.get("net-nwfilter-0") + net_model = network_data.get("net-model-0") + + # Remove interface first, but keep network interface XML definition + # If there is an error happened while adding changed one, then add removed one to back. + status = self.delete_network(net_mac) + try: + self.add_network(net_mac, net_source, net_source_type, net_model, net_filter) + except libvirtError: + if status is not None: + if self.get_status() == 1: + self.instance.attachDeviceFlags(status, VIR_DOMAIN_AFFECT_LIVE) + self.instance.attachDeviceFlags(status, VIR_DOMAIN_AFFECT_CONFIG) + if self.get_status() == 5: + self.instance.attachDeviceFlags(status, VIR_DOMAIN_AFFECT_CONFIG) + + + def change_network_oldway(self, network_data): + ''' + change network firsh version... + will be removed if new one works as expected for all scenarios + ''' xml = self._XMLDesc(VIR_DOMAIN_XML_SECURE) tree = ElementTree.fromstring(xml) for num, interface in enumerate(tree.findall("devices/interface")): diff --git a/vrtManager/interface.py b/vrtManager/interface.py index b6e7c56..d422623 100644 --- a/vrtManager/interface.py +++ b/vrtManager/interface.py @@ -6,13 +6,6 @@ from vrtManager.connection import wvmConnect class wvmInterfaces(wvmConnect): - def get_iface_info(self, name): - iface = self.get_iface(name) - xml = iface.XMLDesc(0) - mac = iface.MACString() - itype = util.get_xml_path(xml, "/interface/@type") - state = iface.isActive() - return {"name": name, "type": itype, "state": state, "mac": mac} def define_iface(self, xml, flag=0): self.wvm.interfaceDefineXML(xml, flag) @@ -153,6 +146,12 @@ class wvmInterface(wvmConnect): else: return None + def get_details(self): + mac = self.get_mac() + itype = self.get_type() + state = self.is_active() + return {"name": self.iface, "type": itype, "state": state, "mac": mac} + def stop_iface(self): self.iface.destroy()