2022-11-02 13:05:41 +00:00
|
|
|
from admin.decorators import superuser_only
|
|
|
|
from computes.models import Compute
|
2020-05-27 12:24:06 +00:00
|
|
|
from django.contrib import messages
|
2015-02-27 09:28:22 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2020-05-27 12:24:06 +00:00
|
|
|
from django.shortcuts import get_object_or_404, render
|
2020-01-24 07:09:46 +00:00
|
|
|
from django.urls import reverse
|
2020-10-14 12:27:57 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-05-27 12:24:06 +00:00
|
|
|
from libvirt import libvirtError
|
2022-11-02 13:05:41 +00:00
|
|
|
from vrtManager.network import network_size, wvmNetwork, wvmNetworks
|
2020-05-27 12:24:06 +00:00
|
|
|
|
2015-02-27 09:28:22 +00:00
|
|
|
from networks.forms import AddNetPool
|
2015-02-27 08:53:51 +00:00
|
|
|
|
2015-02-27 09:28:22 +00:00
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
@superuser_only
|
2015-02-27 09:28:22 +00:00
|
|
|
def networks(request, compute_id):
|
|
|
|
"""
|
|
|
|
:param request:
|
2019-09-10 06:48:31 +00:00
|
|
|
:param compute_id:
|
2015-02-27 09:28:22 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2015-04-02 12:39:40 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2020-10-15 14:18:45 +00:00
|
|
|
errors = False
|
2015-02-27 09:28:22 +00:00
|
|
|
|
|
|
|
try:
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmNetworks(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
)
|
2015-02-27 09:28:22 +00:00
|
|
|
networks = conn.get_networks_info()
|
2020-11-05 09:34:31 +00:00
|
|
|
dhcp4 = netmask4 = gateway4 = ""
|
|
|
|
dhcp6 = prefix6 = gateway6 = ""
|
2019-11-08 15:26:47 +00:00
|
|
|
ipv4 = ipv6 = False
|
2015-02-27 09:28:22 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
if "create" in request.POST:
|
2015-02-27 09:28:22 +00:00
|
|
|
form = AddNetPool(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
data = form.cleaned_data
|
2020-11-05 09:34:31 +00:00
|
|
|
if data["name"] in networks:
|
2019-11-08 15:26:47 +00:00
|
|
|
msg = _("Network pool name already in use")
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, msg)
|
|
|
|
errors = True
|
2022-11-02 05:54:35 +00:00
|
|
|
if (
|
|
|
|
data["forward"] in ["bridge", "macvtap"]
|
|
|
|
and data["bridge_name"] == ""
|
|
|
|
):
|
2020-11-05 09:34:31 +00:00
|
|
|
messages.error(request, _("Please enter bridge/dev name"))
|
2020-10-15 14:18:45 +00:00
|
|
|
errors = True
|
2020-11-05 09:34:31 +00:00
|
|
|
if data["subnet"]:
|
2019-11-08 15:26:47 +00:00
|
|
|
ipv4 = True
|
2022-11-02 05:54:35 +00:00
|
|
|
gateway4, netmask4, dhcp4 = network_size(
|
|
|
|
data["subnet"], data["dhcp4"]
|
|
|
|
)
|
2020-11-05 09:34:31 +00:00
|
|
|
if data["subnet6"]:
|
2019-11-08 15:26:47 +00:00
|
|
|
ipv6 = True
|
2022-11-02 05:54:35 +00:00
|
|
|
gateway6, prefix6, dhcp6 = network_size(
|
|
|
|
data["subnet6"], data["dhcp6"]
|
|
|
|
)
|
2020-11-05 09:34:31 +00:00
|
|
|
if prefix6 != "64":
|
|
|
|
messages.error(
|
2022-11-02 05:54:35 +00:00
|
|
|
request,
|
|
|
|
_("For libvirt, the IPv6 network prefix must be /64"),
|
2020-11-05 09:34:31 +00:00
|
|
|
)
|
2020-10-15 14:18:45 +00:00
|
|
|
errors = True
|
|
|
|
if not errors:
|
2020-05-27 12:24:06 +00:00
|
|
|
conn.create_network(
|
2020-11-05 09:34:31 +00:00
|
|
|
data["name"],
|
|
|
|
data["forward"],
|
2020-05-27 12:24:06 +00:00
|
|
|
ipv4,
|
|
|
|
gateway4,
|
|
|
|
netmask4,
|
|
|
|
dhcp4,
|
|
|
|
ipv6,
|
|
|
|
gateway6,
|
|
|
|
prefix6,
|
|
|
|
dhcp6,
|
2020-11-05 09:34:31 +00:00
|
|
|
data["bridge_name"],
|
|
|
|
data["openvswitch"],
|
|
|
|
data["fixed"],
|
|
|
|
)
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse("network", args=[compute_id, data["name"]])
|
2020-05-27 12:24:06 +00:00
|
|
|
)
|
2015-03-10 14:24:10 +00:00
|
|
|
else:
|
|
|
|
for msg_err in form.errors.values():
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, msg_err.as_text())
|
2015-02-27 09:28:22 +00:00
|
|
|
conn.close()
|
2015-02-27 09:51:33 +00:00
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2015-02-27 09:28:22 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
return render(request, "networks.html", locals())
|
2015-02-27 09:28:22 +00:00
|
|
|
|
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
@superuser_only
|
2015-02-27 09:28:22 +00:00
|
|
|
def network(request, compute_id, pool):
|
|
|
|
"""
|
|
|
|
:param request:
|
2019-09-10 06:48:31 +00:00
|
|
|
:param compute_id:
|
|
|
|
:param pool:
|
2015-02-27 09:28:22 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2015-04-02 12:39:40 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2015-02-27 09:28:22 +00:00
|
|
|
|
|
|
|
try:
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmNetwork(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
pool,
|
|
|
|
)
|
2015-02-27 09:28:22 +00:00
|
|
|
networks = conn.get_networks()
|
|
|
|
state = conn.is_active()
|
|
|
|
device = conn.get_bridge_device()
|
|
|
|
autostart = conn.get_autostart()
|
2019-11-07 11:07:57 +00:00
|
|
|
net_mac = conn.get_network_mac()
|
2019-11-07 07:33:36 +00:00
|
|
|
net_forward = conn.get_network_forward()
|
2019-11-15 08:35:22 +00:00
|
|
|
qos = conn.get_qos()
|
2019-11-07 07:33:36 +00:00
|
|
|
dhcp_range_start = ipv4_dhcp_range_end = dict()
|
|
|
|
|
|
|
|
ip_networks = conn.get_ip_networks()
|
|
|
|
for family, ip_network in ip_networks.items():
|
|
|
|
if family == "ipv4":
|
|
|
|
ipv4_dhcp_range_start = conn.get_dhcp_range_start(family)
|
|
|
|
ipv4_dhcp_range_end = conn.get_dhcp_range_end(family)
|
|
|
|
ipv4_network = ip_network
|
|
|
|
ipv4_fixed_address = conn.get_dhcp_host_addr(family)
|
|
|
|
elif family == "ipv6":
|
|
|
|
ipv6_dhcp_range_start = conn.get_dhcp_range_start(family)
|
|
|
|
ipv6_dhcp_range_end = conn.get_dhcp_range_end(family)
|
|
|
|
ipv6_network = ip_network
|
|
|
|
ipv6_fixed_address = conn.get_dhcp_host_addr(family)
|
|
|
|
else:
|
2020-01-24 07:09:46 +00:00
|
|
|
raise Exception(_("Unknown Network Family"))
|
2019-11-07 07:33:36 +00:00
|
|
|
|
2019-01-24 12:02:19 +00:00
|
|
|
xml = conn._XMLDesc(0)
|
2015-02-27 09:51:33 +00:00
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2020-11-05 09:34:31 +00:00
|
|
|
return HttpResponseRedirect(reverse("networks", args=compute_id))
|
2015-02-27 09:28:22 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
if "start" in request.POST:
|
2015-02-27 09:28:22 +00:00
|
|
|
try:
|
|
|
|
conn.start()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-02-27 09:51:33 +00:00
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2020-11-05 09:34:31 +00:00
|
|
|
if "stop" in request.POST:
|
2015-02-27 09:28:22 +00:00
|
|
|
try:
|
|
|
|
conn.stop()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-02-27 09:51:33 +00:00
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2020-11-05 09:34:31 +00:00
|
|
|
if "delete" in request.POST:
|
2015-02-27 09:28:22 +00:00
|
|
|
try:
|
|
|
|
conn.delete()
|
2020-11-05 09:34:31 +00:00
|
|
|
return HttpResponseRedirect(reverse("networks", args=[compute_id]))
|
2015-02-27 09:51:33 +00:00
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2020-11-05 09:34:31 +00:00
|
|
|
if "set_autostart" in request.POST:
|
2015-02-27 09:28:22 +00:00
|
|
|
try:
|
|
|
|
conn.set_autostart(1)
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-02-27 09:51:33 +00:00
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2020-11-05 09:34:31 +00:00
|
|
|
if "unset_autostart" in request.POST:
|
2015-02-27 09:28:22 +00:00
|
|
|
try:
|
|
|
|
conn.set_autostart(0)
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-02-27 09:51:33 +00:00
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2020-11-05 09:34:31 +00:00
|
|
|
if "modify_fixed_address" in request.POST:
|
|
|
|
name = request.POST.get("name", "")
|
|
|
|
address = request.POST.get("address", "")
|
|
|
|
family = request.POST.get("family", "ipv4")
|
2019-11-07 07:33:36 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
if family == "ipv4":
|
|
|
|
mac_duid = request.POST.get("mac", "")
|
|
|
|
if family == "ipv6":
|
|
|
|
mac_duid = request.POST.get("id", "")
|
2019-11-07 07:33:36 +00:00
|
|
|
|
2019-01-22 13:53:10 +00:00
|
|
|
try:
|
2019-11-07 07:33:36 +00:00
|
|
|
ret_val = conn.modify_fixed_address(name, address, mac_duid, family)
|
2022-11-02 05:54:35 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("Fixed address operation completed for %(family)s")
|
|
|
|
% {"family": family.upper()},
|
|
|
|
)
|
2019-01-22 13:53:10 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2019-01-22 13:53:10 +00:00
|
|
|
except ValueError as val_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, val_err)
|
2020-11-05 09:34:31 +00:00
|
|
|
if "delete_fixed_address" in request.POST:
|
|
|
|
ip = request.POST.get("address", "")
|
|
|
|
family = request.POST.get("family", "ipv4")
|
2019-11-07 07:33:36 +00:00
|
|
|
conn.delete_fixed_address(ip, family)
|
2022-11-02 05:54:35 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("%(family)s Fixed Address is Deleted.") % {"family": family.upper()},
|
|
|
|
)
|
2019-01-22 13:53:10 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2020-11-05 09:34:31 +00:00
|
|
|
if "modify_dhcp_range" in request.POST:
|
|
|
|
range_start = request.POST.get("range_start", "")
|
|
|
|
range_end = request.POST.get("range_end", "")
|
|
|
|
family = request.POST.get("family", "ipv4")
|
2019-01-22 13:53:10 +00:00
|
|
|
try:
|
2019-11-07 07:33:36 +00:00
|
|
|
conn.modify_dhcp_range(range_start, range_end, family)
|
2022-11-02 05:54:35 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("%(family)s DHCP Range is Changed.") % {"family": family.upper()},
|
|
|
|
)
|
2019-01-22 13:53:10 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2020-11-05 09:34:31 +00:00
|
|
|
if "edit_network" in request.POST:
|
|
|
|
edit_xml = request.POST.get("edit_xml", "")
|
2019-01-24 12:02:19 +00:00
|
|
|
if edit_xml:
|
2019-11-15 08:35:22 +00:00
|
|
|
conn.edit_network(edit_xml)
|
|
|
|
if conn.is_active():
|
2020-11-05 09:34:31 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_(
|
|
|
|
"Network XML is changed. \\"
|
|
|
|
"Stop and start network to activate new config."
|
|
|
|
),
|
|
|
|
)
|
2019-11-15 08:35:22 +00:00
|
|
|
else:
|
|
|
|
messages.success(request, _("Network XML is changed."))
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2020-11-05 09:34:31 +00:00
|
|
|
if "set_qos" in request.POST:
|
|
|
|
qos_dir = request.POST.get("qos_direction", "")
|
|
|
|
average = request.POST.get("qos_average") or 0
|
|
|
|
peak = request.POST.get("qos_peak") or 0
|
|
|
|
burst = request.POST.get("qos_burst") or 0
|
2019-11-15 08:35:22 +00:00
|
|
|
|
2019-11-20 05:42:09 +00:00
|
|
|
try:
|
|
|
|
conn.set_qos(qos_dir, average, peak, burst)
|
|
|
|
if conn.is_active():
|
2020-05-27 12:24:06 +00:00
|
|
|
messages.success(
|
2020-10-14 12:27:57 +00:00
|
|
|
request,
|
2022-11-02 05:54:35 +00:00
|
|
|
_(
|
|
|
|
"%(qos_dir)s QoS is updated. Network XML is changed. Stop and start network to activate new config"
|
|
|
|
)
|
|
|
|
% {"qos_dir": qos_dir.capitalize()},
|
2020-11-05 09:34:31 +00:00
|
|
|
)
|
2019-11-20 05:42:09 +00:00
|
|
|
else:
|
2022-11-02 05:54:35 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("%(qos_dir)s QoS is set") % {"qos_dir": qos_dir.capitalize()},
|
|
|
|
)
|
2020-03-16 13:59:45 +00:00
|
|
|
except libvirtError as lib_err:
|
|
|
|
messages.error(request, lib_err)
|
2019-11-15 08:35:22 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2020-11-05 09:34:31 +00:00
|
|
|
if "unset_qos" in request.POST:
|
|
|
|
qos_dir = request.POST.get("qos_direction", "")
|
2019-11-15 08:35:22 +00:00
|
|
|
conn.unset_qos(qos_dir)
|
2015-02-27 09:28:22 +00:00
|
|
|
|
2019-11-15 08:35:22 +00:00
|
|
|
if conn.is_active():
|
2020-05-27 12:24:06 +00:00
|
|
|
messages.success(
|
2020-10-14 12:27:57 +00:00
|
|
|
request,
|
2022-11-02 05:54:35 +00:00
|
|
|
_(
|
|
|
|
"%(qos_dir)s QoS is deleted. Network XML is changed. \
|
|
|
|
Stop and start network to activate new config"
|
|
|
|
)
|
|
|
|
% {"qos_dir": qos_dir.capitalize()},
|
2020-11-05 09:34:31 +00:00
|
|
|
)
|
2019-11-15 08:35:22 +00:00
|
|
|
else:
|
2022-11-02 05:54:35 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
_("%(qos_dir)s QoS is deleted") % {"qos_dir": qos_dir.capitalize()},
|
|
|
|
)
|
2019-11-15 08:35:22 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-02-27 09:28:22 +00:00
|
|
|
conn.close()
|
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
return render(request, "network.html", locals())
|