2022-11-02 13:05:41 +00:00
|
|
|
import contextlib
|
2020-10-15 14:18:45 +00:00
|
|
|
from django.contrib import messages
|
2015-04-02 12:39:40 +00:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
2015-02-27 09:28:22 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2020-01-24 07:09:46 +00:00
|
|
|
from django.urls import reverse
|
2020-06-04 12:40:57 +00:00
|
|
|
from libvirt import libvirtError
|
2015-02-27 09:28:22 +00:00
|
|
|
from computes.models import Compute
|
|
|
|
from interfaces.forms import AddInterface
|
|
|
|
from vrtManager.interface import wvmInterface, wvmInterfaces
|
2020-05-27 12:24:06 +00:00
|
|
|
from admin.decorators import superuser_only
|
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 interfaces(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:
|
|
|
|
"""
|
|
|
|
|
|
|
|
ifaces_all = []
|
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:
|
2022-11-02 05:54:35 +00:00
|
|
|
conn = wvmInterfaces(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type
|
|
|
|
)
|
2015-02-27 09:28:22 +00:00
|
|
|
ifaces = conn.get_ifaces()
|
2022-11-02 13:05:41 +00:00
|
|
|
|
|
|
|
netdevs = ["eth0", "eth1"]
|
|
|
|
with contextlib.suppress(Exception):
|
2019-12-23 12:48:11 +00:00
|
|
|
netdevs = conn.get_net_devices()
|
2015-02-27 09:28:22 +00:00
|
|
|
|
|
|
|
for iface in ifaces:
|
2022-11-02 05:54:35 +00:00
|
|
|
interf = wvmInterface(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
iface
|
|
|
|
)
|
2021-02-12 13:05:16 +00:00
|
|
|
ifaces_all.append(interf.get_details())
|
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 = AddInterface(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
data = form.cleaned_data
|
2020-11-05 09:34:31 +00:00
|
|
|
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"],
|
|
|
|
)
|
2015-02-27 09:28:22 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
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, "interfaces.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 interface(request, compute_id, iface):
|
|
|
|
"""
|
|
|
|
:param request:
|
2019-10-30 08:05:50 +00:00
|
|
|
:param compute_id:
|
2019-09-10 06:48:31 +00:00
|
|
|
:param iface:
|
2015-02-27 09:28:22 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
ifaces_all = []
|
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:
|
2022-11-02 05:54:35 +00:00
|
|
|
conn = wvmInterface(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
iface
|
|
|
|
)
|
2015-02-27 09:28:22 +00:00
|
|
|
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()
|
2019-11-04 09:03:13 +00:00
|
|
|
slave_ifaces = conn.get_bridge_slave_ifaces()
|
2015-02-27 09:28:22 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
if "stop" in request.POST:
|
2015-02-27 09:28:22 +00:00
|
|
|
conn.stop_iface()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2020-11-05 09:34:31 +00:00
|
|
|
if "start" in request.POST:
|
2015-02-27 09:28:22 +00:00
|
|
|
conn.start_iface()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2020-11-05 09:34:31 +00:00
|
|
|
if "delete" in request.POST:
|
2015-02-27 09:28:22 +00:00
|
|
|
conn.delete_iface()
|
2020-11-05 09:34:31 +00:00
|
|
|
return HttpResponseRedirect(reverse("interfaces", args=[compute_id]))
|
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, "interface.html", locals())
|