1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-25 23:55:24 +00:00
webvirtcloud/computes/views.py

279 lines
7 KiB
Python
Raw Normal View History

2015-03-19 13:37:16 +00:00
import json
2020-05-29 13:25:36 +00:00
2022-11-02 13:05:41 +00:00
from admin.decorators import superuser_only
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
2020-05-29 13:25:36 +00:00
from django.utils import timezone
2015-03-06 11:59:27 +00:00
from instances.models import Instance
2022-11-02 13:05:41 +00:00
from libvirt import libvirtError
from vrtManager.connection import (
CONN_SOCKET,
CONN_SSH,
CONN_TCP,
CONN_TLS,
connection_manager,
wvmConnect,
)
2015-02-27 08:53:51 +00:00
from vrtManager.hostdetails import wvmHostDetails
2022-11-02 13:05:41 +00:00
from computes.forms import (
SocketComputeForm,
SshComputeForm,
TcpComputeForm,
TlsComputeForm,
)
from computes.models import Compute
2020-07-13 09:33:09 +00:00
from . import utils
2015-02-27 08:53:51 +00:00
@superuser_only
2015-02-27 08:53:51 +00:00
def computes(request):
"""
:param request:
:return:
"""
computes = Compute.objects.filter().order_by("name")
return render(request, "computes/list.html", {"computes": computes})
2015-02-27 08:53:51 +00:00
2015-03-19 13:37:16 +00:00
@superuser_only
2015-03-04 13:43:29 +00:00
def overview(request, compute_id):
2015-04-02 12:39:40 +00:00
compute = get_object_or_404(Compute, pk=compute_id)
status = (
2022-11-02 05:54:35 +00:00
"true"
2022-11-02 13:05:41 +00:00
if connection_manager.host_is_up(compute.type, compute.hostname) is True
else "false"
)
2020-07-13 09:33:09 +00:00
conn = wvmHostDetails(
compute.hostname,
compute.login,
compute.password,
compute.type,
)
2022-11-02 05:54:35 +00:00
(
hostname,
host_arch,
host_memory,
logical_cpu,
model_cpu,
uri_conn,
) = conn.get_node_info()
2020-07-13 09:33:09 +00:00
hypervisor = conn.get_hypervisors_domain_types()
mem_usage = conn.get_memory_usage()
emulator = conn.get_emulator(host_arch)
version = conn.get_version()
lib_version = conn.get_lib_version()
conn.close()
2015-02-27 08:53:51 +00:00
return render(request, "overview.html", locals())
2015-03-19 13:37:16 +00:00
2020-07-13 09:33:09 +00:00
@superuser_only
def instances(request, compute_id):
compute = get_object_or_404(Compute, pk=compute_id)
utils.refresh_instance_database(compute)
2022-11-02 05:54:35 +00:00
instances = Instance.objects.filter(compute=compute).prefetch_related(
"userinstance_set"
)
2020-07-13 09:33:09 +00:00
2022-11-02 05:54:35 +00:00
return render(
2022-11-02 13:05:41 +00:00
request, "computes/instances.html", {"compute": compute, "instances": instances}
2022-11-02 05:54:35 +00:00
)
2020-07-13 09:33:09 +00:00
@superuser_only
def compute_create(request, FormClass):
form = FormClass(request.POST or None)
if form.is_valid():
form.save()
return redirect(reverse("computes"))
return render(request, "computes/form.html", {"form": form})
@superuser_only
def compute_update(request, compute_id):
compute = get_object_or_404(Compute, pk=compute_id)
if compute.type == 1:
FormClass = TcpComputeForm
elif compute.type == 2:
FormClass = SshComputeForm
elif compute.type == 3:
FormClass = TlsComputeForm
elif compute.type == 4:
FormClass = SocketComputeForm
form = FormClass(request.POST or None, instance=compute)
if form.is_valid():
form.save()
return redirect(reverse("computes"))
return render(request, "computes/form.html", {"form": form})
@superuser_only
def compute_delete(request, compute_id):
compute = get_object_or_404(Compute, pk=compute_id)
if request.method == "POST":
compute.delete()
return redirect("computes")
return render(
request,
"common/confirm_delete.html",
{"object": compute},
)
2015-03-19 13:37:16 +00:00
def compute_graph(request, compute_id):
"""
:param request:
2019-09-10 06:48:31 +00:00
:param compute_id:
2015-03-19 13:37:16 +00:00
:return:
"""
comp_mgr = ComputeManager(compute_id)
data = comp_mgr.compute_graph()
2015-03-19 13:37:16 +00:00
response = HttpResponse()
response["Content-Type"] = "text/javascript"
2015-03-19 13:37:16 +00:00
response.write(data)
2015-03-27 15:12:15 +00:00
return response
def get_compute_disk_buses(request, compute_id, arch, machine, disk):
2020-04-24 16:34:29 +00:00
"""
:param request:
:param compute_id:
:param arch:
:param machine:
:param disk:
:return:
"""
comp_mgr = ComputeManager(compute_id)
return HttpResponse(comp_mgr.get_disk_buses(arch, machine, disk))
def get_compute_machine_types(request, compute_id, arch):
2020-04-24 16:34:29 +00:00
"""
:param request:
:param compute_id:
:param arch:
:return:
"""
comp_mgr = ComputeManager(compute_id)
return HttpResponse(comp_mgr.get_machine_types(arch))
def get_compute_video_models(request, compute_id, arch, machine):
2020-04-24 16:34:29 +00:00
"""
:param request:
:param compute_id:
:param arch:
:param machine:
:return:
"""
comp_mgr = ComputeManager(compute_id)
return HttpResponse(comp_mgr.get_video_models(arch, machine))
def get_dom_capabilities(request, compute_id, arch, machine):
2020-04-24 16:34:29 +00:00
"""
:param request:
:param compute_id:
:param arch:
:param machine:
:return:
"""
comp_mgr = ComputeManager(compute_id)
return HttpResponse(comp_mgr.get_dom_capabilities(arch, machine))
class ComputeManager:
def __init__(self, compute_id):
self.compute = get_object_or_404(Compute, pk=compute_id)
self.conn = wvmConnect(
self.compute.hostname,
self.compute.login,
self.compute.password,
self.compute.type,
)
def get_video_models(self, arch, machine):
data = dict()
try:
data["videos"] = self.conn.get_video_models(arch, machine)
except libvirtError:
pass
return json.dumps(data)
def get_dom_capabilities(self, arch, machine):
data = dict()
try:
data["videos"] = self.conn.get_disk_device_types(arch, machine)
data["bus"] = self.conn.get_disk_device_types(arch, machine)
except libvirtError:
pass
return json.dumps(data)
def get_machine_types(self, arch):
data = dict()
try:
data["machines"] = self.conn.get_machine_types(arch)
except libvirtError:
pass
return json.dumps(data)
def get_disk_buses(self, arch, machine, disk):
data = dict()
try:
disk_device_types = self.conn.get_disk_device_types(arch, machine)
if disk in disk_device_types:
if disk == "disk":
data["bus"] = sorted(disk_device_types)
elif disk == "cdrom":
data["bus"] = ["ide", "sata", "scsi"]
elif disk == "floppy":
data["bus"] = ["fdc"]
elif disk == "lun":
data["bus"] = ["scsi"]
except libvirtError:
pass
return json.dumps(data)
def compute_graph(self):
try:
conn = wvmHostDetails(
self.compute.hostname,
self.compute.login,
self.compute.password,
self.compute.type,
)
current_time = timezone.now().strftime("%H:%M:%S")
cpu_usage = conn.get_cpu_usage()
mem_usage = conn.get_memory_usage()
conn.close()
except libvirtError:
cpu_usage = {"usage": 0}
mem_usage = {"usage": 0}
current_time = 0
return json.dumps(
{
"cpudata": cpu_usage["usage"],
"memdata": mem_usage,
"timeline": current_time,
}
)