1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-09 03:36:48 +00:00

feat(interfaces): the interface pages on Bootstrap's cards, tables and dialogs

As the storage and network pages; plan reviewed with Codex.

- The list is grouped by type (bridges, bonds, VLANs, ethernet; a type
  the host has none of is left out), three cards a row, with a search
  of their names, MACs and networks (table_filter.js, which now takes
  any element with data-search as a row); a card per interface: its link
  as the kernel reports it (up green, down grey, another state or
  unknown told as such, no red card for a link that is merely down),
  MAC, MTU, speed when known, the members it reports and the libvirt
  networks that use it, linked.
- An interface's page: an Interface card (type, libvirt's own state
  where it manages interfaces, link, start mode, MAC, MTU, speed, STP,
  networks using it); on a host whose libvirt changes interfaces, a
  card per family with how it is addressed and its addresses; the
  members of a bridge, bond or VLAN with their link and speed, or that
  none is reported; for a bridge, the running VMs' cards on it,
  directly or through a network using it, with their tap and MAC,
  linked (wvmInterface.get_vms: one domain listing). Not the bridge's
  delay: netcf tells it in seconds, the udev backend in hundredths.
  Start or Stop in its heading, Delete in its menu, disabled while it
  runs, on hosts that can change interfaces only.
- A host whose libvirt only reads interfaces says so once, with that
  it does not tell their addresses, instead of "None" on every page.
- The New interface dialog is one form, as the New pool and New
  network ones (choice_dialog.js, radio_card.html): Bridge or
  Ethernet, a bridge's member from the devices it can take, an
  ethernet interface's device from the ones the host does not define
  yet, a static address's fields with its choice; a refused interface,
  or one libvirt refuses, opens it again with what was entered; a made
  one opens its page.
- networks_by_device maps the libvirt networks to their bridges or
  interfaces on the page's own connection.
This commit is contained in:
catborise 2026-10-28 22:17:19 +03:00
parent 680dd5dad4
commit 050d071ff9
13 changed files with 532 additions and 308 deletions

View file

@ -3,6 +3,8 @@ from functools import cached_property
from xml.etree import ElementTree
from libvirt import (
VIR_CONNECT_LIST_DOMAINS_ACTIVE,
VIR_ERR_NO_DOMAIN,
VIR_ERR_NO_INTERFACE,
VIR_ERR_XML_ERROR,
VIR_INTERFACE_XML_INACTIVE,
@ -64,8 +66,8 @@ def attribute(element, path, name):
def interface_info(iface, tree):
"""What an interface (its libvirt object, which holds its name and MAC)
and its XML tell: type, MAC, MTU, link state and speed, and the members
of a bridge or bond, or a VLAN's device, as reported."""
and its XML tell: type, MAC, MTU, link state and speed, the members of a
bridge or bond, or a VLAN's device, as reported, and a bridge's STP."""
state, speed = link_of(tree)
members = []
for member in tree.findall("bridge/interface") + tree.findall("bond/interface") + tree.findall("vlan/interface"):
@ -73,7 +75,24 @@ def interface_info(iface, tree):
members.append({"name": member.get("name"), "type": member.get("type"), "mac": attribute(member, "mac", "address"),
"link": member_state, "speed": member_speed})
return {"name": iface.name(), "type": tree.get("type"), "mac": iface.MACString() or None,
"mtu": attribute(tree, "mtu", "size"), "link": state, "speed": speed, "members": members}
"mtu": attribute(tree, "mtu", "size"), "link": state, "speed": speed, "members": members,
"stp": attribute(tree, "bridge", "stp")}
def networks_by_device(conn):
"""The host's libvirt networks by the devices they use: their bridge, or
every interface or physical function they forward to. One listing, one
XML read per network."""
by_device = {}
for net in conn.listAllNetworks(0):
tree = ElementTree.fromstring(net.XMLDesc(0))
devices = {attribute(tree, "bridge", "name")}
for forward in tree.findall("forward/interface") + tree.findall("forward/pf"):
devices.add(forward.get("dev"))
devices.discard(None)
for device in devices:
by_device.setdefault(device, []).append(net.name())
return by_device
class wvmInterfaces(wvmConnect):
@ -214,6 +233,25 @@ class wvmInterface(wvmConnect):
info[family] = {"method": method, "addresses": addresses}
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."""
name = self.iface.name()
cards = []
for dom in self.wvm.listAllDomains(VIR_CONNECT_LIST_DOMAINS_ACTIVE):
try:
tree = ElementTree.fromstring(dom.XMLDesc(0))
except libvirtError as err: # a VM stopped or undefined meanwhile is not on it
if err.get_error_code() != VIR_ERR_NO_DOMAIN:
raise
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):
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):
self.iface.destroy()