mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-10 04:06:47 +00:00
fix(interfaces): interface changes roll back as one, a running one is not deleted, each interface is read once
Plan reviewed with Codex; tried on a udev host (AlmaLinux 10) and a netcf one (AlmaLinux 8). - Changes are one change of the host's configuration (libvirt's interface change transaction): a bridge that does not start used to be undefined, leaving its member without the addresses and start mode the bridge took from it; now the whole change rolls back, the member's own configuration too, and so does a commit libvirt refuses. A rollback that fails too is reported with the failure, saying the host's configuration may be changed. Deleting a bridge, which defines its members again on their own, is one change as well. - A running interface is not deleted: the page hid Delete, but a post deleted it. A bridge with addresses and several members is refused (which member takes the addresses is the user's choice); the confirmation says that its members are defined again. - A bridge takes only a device in no bridge, bond or VLAN as the host has them configured, at any depth (a bridge's bond's devices), not the loopback, a libvirt network's bridge or a VM's tap; an ethernet interface configures only such a device too. - Whether the host can change interfaces: unsupported or refused by its access rules is a no, bad XML a yes, anything else (the connection, the login) is raised instead of taken for a yes. A page whose host could not be read offers no actions. - Reads: the list reads each interface's XML once from one listing (listAllInterfaces: every state, not the active and the defined ones apart); a page reads the running and the saved definition once each (cached_property). netcf writes a link speed of -1 for a card that has none (a virtio one) and then refuses its own XML: the saved one is read instead, where virsh and the page failed. Name and MAC come from libvirt's object, without a call. Each family says how it is addressed (DHCP, autoconf, static or none) and every address, its prefix where it has one; no address is not "DHCP" any more. A bridge's members carry their link state and speed; an unknown speed (0, -1, 4294967295) is none. - The interface API lists from the same reader. get_details, get_bridge, get_bridge_slave_ifaces and the per-family getters go.
This commit is contained in:
parent
0ee03c0971
commit
680dd5dad4
11 changed files with 421 additions and 281 deletions
|
|
@ -13,70 +13,41 @@ from admin.decorators import superuser_only
|
|||
|
||||
@superuser_only
|
||||
def interfaces(request, compute_id):
|
||||
"""
|
||||
:param request:
|
||||
:param compute_id:
|
||||
:return:
|
||||
"""
|
||||
|
||||
ifaces_all = []
|
||||
"""The host's interfaces, and a new one where its libvirt can define them."""
|
||||
compute = get_object_or_404(Compute, pk=compute_id)
|
||||
|
||||
try:
|
||||
conn = wvmInterfaces(
|
||||
compute.hostname,
|
||||
compute.login,
|
||||
compute.password,
|
||||
compute.type
|
||||
)
|
||||
ifaces = conn.get_ifaces()
|
||||
conn = wvmInterfaces(compute.hostname, compute.login, compute.password, compute.type)
|
||||
ifaces_all = conn.get_ifaces_info()
|
||||
changeable = conn.can_change_interfaces()
|
||||
|
||||
# the host's devices are only the create form's choices
|
||||
netdevs, netdev_error = [], None
|
||||
netdevs, members, netdev_error = [], [], None
|
||||
if changeable:
|
||||
try:
|
||||
netdevs = conn.get_net_devices()
|
||||
except Exception as err:
|
||||
members = conn.get_member_candidates(netdevs)
|
||||
except Exception as err: # a host without the node device driver too
|
||||
netdev_error = err
|
||||
messages.error(request, _("The network devices of this host could not be read: %(error)s") % {"error": err})
|
||||
|
||||
for iface in ifaces:
|
||||
interf = wvmInterface(
|
||||
compute.hostname,
|
||||
compute.login,
|
||||
compute.password,
|
||||
compute.type,
|
||||
iface
|
||||
)
|
||||
ifaces_all.append(interf.get_details())
|
||||
|
||||
if request.method == "POST":
|
||||
# the page says why nothing is created on a host that cannot change
|
||||
# interfaces; without the host's devices nothing can be checked
|
||||
if "create" in request.POST and changeable and netdev_error is None:
|
||||
form = AddInterface(request.POST, netdevs=netdevs, defined=ifaces)
|
||||
if form.is_valid():
|
||||
data = form.cleaned_data
|
||||
with audit(request, compute, f"Interface {data['name']}", "add"):
|
||||
conn.create_iface(
|
||||
data["name"],
|
||||
data["itype"],
|
||||
data["start_mode"],
|
||||
data["netdev"],
|
||||
data["ipv4_type"],
|
||||
data["ipv4_addr"],
|
||||
data["ipv4_gw"],
|
||||
data["ipv6_type"],
|
||||
data["ipv6_addr"],
|
||||
data["ipv6_gw"],
|
||||
data["stp"],
|
||||
data["delay"],
|
||||
)
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
else:
|
||||
for msg_err in form.errors.values():
|
||||
messages.error(request, msg_err.as_text())
|
||||
# the page says why nothing is created on a host that cannot change
|
||||
# interfaces; without the host's devices nothing can be checked
|
||||
if request.method == "POST" and "create" in request.POST and changeable and netdev_error is None:
|
||||
form = AddInterface(request.POST, netdevs=netdevs, members=members,
|
||||
defined=[iface["name"] for iface in ifaces_all])
|
||||
if form.is_valid():
|
||||
data = form.cleaned_data
|
||||
with audit(request, compute, f"Interface {data['name']}", "add"):
|
||||
conn.create_iface(
|
||||
data["name"], data["itype"], data["start_mode"], data["netdev"],
|
||||
data["ipv4_type"], data["ipv4_addr"], data["ipv4_gw"],
|
||||
data["ipv6_type"], data["ipv6_addr"], data["ipv6_gw"],
|
||||
data["stp"], data["delay"],
|
||||
)
|
||||
return HttpResponseRedirect(reverse("interfaces", args=[compute_id]))
|
||||
for msg_err in form.errors.values():
|
||||
messages.error(request, msg_err.as_text())
|
||||
conn.close()
|
||||
except libvirtError as lib_err:
|
||||
messages.error(request, lib_err)
|
||||
|
|
@ -86,50 +57,36 @@ def interfaces(request, compute_id):
|
|||
|
||||
@superuser_only
|
||||
def interface(request, compute_id, iface):
|
||||
"""
|
||||
:param request:
|
||||
:param compute_id:
|
||||
:param iface:
|
||||
:return:
|
||||
"""
|
||||
|
||||
ifaces_all = []
|
||||
"""An interface of the host; started, stopped or deleted where the host's libvirt can."""
|
||||
compute = get_object_or_404(Compute, pk=compute_id)
|
||||
|
||||
try:
|
||||
conn = wvmInterface(
|
||||
compute.hostname,
|
||||
compute.login,
|
||||
compute.password,
|
||||
compute.type,
|
||||
iface
|
||||
)
|
||||
start_mode = conn.get_start_mode()
|
||||
state = conn.is_active()
|
||||
mac = conn.get_mac()
|
||||
itype = conn.get_type()
|
||||
ipv4 = conn.get_ipv4()
|
||||
ipv4_type = conn.get_ipv4_type()
|
||||
ipv6 = conn.get_ipv6()
|
||||
ipv6_type = conn.get_ipv6_type()
|
||||
bridge = conn.get_bridge()
|
||||
slave_ifaces = conn.get_bridge_slave_ifaces()
|
||||
conn = wvmInterface(compute.hostname, compute.login, compute.password, compute.type, iface)
|
||||
changeable = conn.can_change_interfaces()
|
||||
|
||||
# the page says why nothing is done on a host that cannot change interfaces
|
||||
if request.method == "POST" and changeable:
|
||||
if "stop" in request.POST:
|
||||
with audit(request, compute, f"Interface {iface}", "stop"):
|
||||
conn.stop_iface()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
if "start" in request.POST:
|
||||
with audit(request, compute, f"Interface {iface}", "start"):
|
||||
conn.start_iface()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
if "delete" in request.POST:
|
||||
with audit(request, compute, f"Interface {iface}", "delete"):
|
||||
conn.delete_iface()
|
||||
return HttpResponseRedirect(reverse("interfaces", args=[compute_id]))
|
||||
here = reverse("interface", args=[compute_id, iface])
|
||||
subject = f"Interface {iface}"
|
||||
try:
|
||||
if "stop" in request.POST:
|
||||
with audit(request, compute, subject, "stop"):
|
||||
conn.stop_iface()
|
||||
return HttpResponseRedirect(here)
|
||||
if "start" in request.POST:
|
||||
with audit(request, compute, subject, "start"):
|
||||
conn.start_iface()
|
||||
return HttpResponseRedirect(here)
|
||||
if "delete" in request.POST:
|
||||
with audit(request, compute, subject, "delete"):
|
||||
conn.delete_iface()
|
||||
return HttpResponseRedirect(reverse("interfaces", args=[compute_id]))
|
||||
except libvirtError as err: # OperationError too: an active one, a bridge whose addresses go nowhere
|
||||
messages.error(request, err)
|
||||
return HttpResponseRedirect(here)
|
||||
|
||||
info = conn.get_info()
|
||||
state = conn.is_active()
|
||||
conn.close()
|
||||
except libvirtError as lib_err:
|
||||
messages.error(request, lib_err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue