2015-03-19 13:37:16 +00:00
|
|
|
import json
|
2020-05-29 13:25:36 +00:00
|
|
|
|
|
|
|
from django.contrib import messages
|
2015-03-19 13:37:16 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2020-05-29 13:25:36 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, render, reverse
|
2020-01-24 07:09:46 +00:00
|
|
|
from django.urls import reverse
|
2020-05-29 13:25:36 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from libvirt import libvirtError
|
|
|
|
|
|
|
|
from accounts.models import UserInstance
|
|
|
|
from admin.decorators import superuser_only
|
|
|
|
from computes.forms import (ComputeEditHostForm, SocketComputeForm, SshComputeForm, TcpComputeForm, TlsComputeForm)
|
2015-02-27 08:53:51 +00:00
|
|
|
from computes.models import Compute
|
2015-03-06 11:59:27 +00:00
|
|
|
from instances.models import Instance
|
2020-05-29 13:25:36 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
@superuser_only
|
2015-02-27 08:53:51 +00:00
|
|
|
def computes(request):
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
def get_hosts_status(computes):
|
|
|
|
"""
|
|
|
|
Function return all hosts all vds on host
|
|
|
|
"""
|
|
|
|
compute_data = []
|
|
|
|
for compute in computes:
|
2020-05-27 12:24:06 +00:00
|
|
|
compute_data.append({
|
|
|
|
'id': compute.id,
|
|
|
|
'name': compute.name,
|
|
|
|
'hostname': compute.hostname,
|
|
|
|
'status': connection_manager.host_is_up(compute.type, compute.hostname),
|
|
|
|
'type': compute.type,
|
|
|
|
'login': compute.login,
|
|
|
|
'password': compute.password,
|
|
|
|
'details': compute.details
|
|
|
|
})
|
2015-02-27 08:53:51 +00:00
|
|
|
return compute_data
|
|
|
|
|
2015-03-10 14:24:10 +00:00
|
|
|
error_messages = []
|
2015-11-13 09:13:36 +00:00
|
|
|
computes = Compute.objects.filter().order_by('name')
|
2015-02-27 08:53:51 +00:00
|
|
|
computes_info = get_hosts_status(computes)
|
2020-05-27 12:24:06 +00:00
|
|
|
|
2015-03-06 11:59:27 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
if 'host_del' in request.POST:
|
|
|
|
compute_id = request.POST.get('host_id', '')
|
|
|
|
try:
|
2015-04-21 11:46:58 +00:00
|
|
|
del_user_inst_on_host = UserInstance.objects.filter(instance__compute_id=compute_id)
|
|
|
|
del_user_inst_on_host.delete()
|
2015-03-06 11:59:27 +00:00
|
|
|
finally:
|
2015-03-10 14:24:10 +00:00
|
|
|
try:
|
|
|
|
del_inst_on_host = Instance.objects.filter(compute_id=compute_id)
|
|
|
|
del_inst_on_host.delete()
|
|
|
|
finally:
|
|
|
|
del_host = Compute.objects.get(id=compute_id)
|
|
|
|
del_host.delete()
|
2015-03-06 11:59:27 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
if 'host_edit' in request.POST:
|
|
|
|
form = ComputeEditHostForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
data = form.cleaned_data
|
|
|
|
compute_edit = Compute.objects.get(id=data['host_id'])
|
|
|
|
compute_edit.name = data['name']
|
|
|
|
compute_edit.hostname = data['hostname']
|
|
|
|
compute_edit.login = data['login']
|
|
|
|
compute_edit.password = data['password']
|
2018-07-31 07:15:59 +00:00
|
|
|
compute_edit.details = data['details']
|
2015-03-06 11:59:27 +00:00
|
|
|
compute_edit.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-03-10 14:24:10 +00:00
|
|
|
else:
|
|
|
|
for msg_err in form.errors.values():
|
|
|
|
error_messages.append(msg_err.as_text())
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'computes.html', locals())
|
2015-02-27 08:53:51 +00:00
|
|
|
|
2015-03-19 13:37:16 +00:00
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
@superuser_only
|
2015-03-04 13:43:29 +00:00
|
|
|
def overview(request, compute_id):
|
2015-02-27 08:53:51 +00:00
|
|
|
"""
|
|
|
|
:param request:
|
2019-09-10 06:48:31 +00:00
|
|
|
:param compute_id:
|
2015-02-27 08:53:51 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2015-03-03 09:34:09 +00:00
|
|
|
error_messages = []
|
2015-04-02 12:39:40 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
try:
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmHostDetails(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
)
|
2015-02-27 08:53:51 +00:00
|
|
|
hostname, host_arch, host_memory, logical_cpu, model_cpu, uri_conn = conn.get_node_info()
|
2019-12-13 13:47:51 +00:00
|
|
|
hypervisor = conn.get_hypervisors_domain_types()
|
2015-02-27 08:53:51 +00:00
|
|
|
mem_usage = conn.get_memory_usage()
|
2018-10-02 12:15:41 +00:00
|
|
|
emulator = conn.get_emulator(host_arch)
|
2018-12-05 08:08:21 +00:00
|
|
|
version = conn.get_version()
|
|
|
|
lib_version = conn.get_lib_version()
|
2015-02-27 08:53:51 +00:00
|
|
|
conn.close()
|
2015-03-03 09:34:09 +00:00
|
|
|
except libvirtError as lib_err:
|
|
|
|
error_messages.append(lib_err)
|
2015-02-27 08:53:51 +00:00
|
|
|
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'overview.html', locals())
|
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:
|
|
|
|
"""
|
2015-04-02 12:39:40 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2015-03-19 13:37:16 +00:00
|
|
|
try:
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmHostDetails(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
)
|
2019-05-21 06:10:32 +00:00
|
|
|
current_time = timezone.now().strftime("%H:%M:%S")
|
2015-03-19 13:37:16 +00:00
|
|
|
cpu_usage = conn.get_cpu_usage()
|
|
|
|
mem_usage = conn.get_memory_usage()
|
|
|
|
conn.close()
|
|
|
|
except libvirtError:
|
2019-05-21 06:10:32 +00:00
|
|
|
cpu_usage = {'usage': 0}
|
|
|
|
mem_usage = {'usage': 0}
|
2015-03-19 13:37:16 +00:00
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
data = json.dumps({
|
|
|
|
'cpudata': cpu_usage['usage'],
|
|
|
|
'memdata': mem_usage,
|
|
|
|
'timeline': current_time,
|
|
|
|
})
|
2015-03-19 13:37:16 +00:00
|
|
|
response = HttpResponse()
|
|
|
|
response['Content-Type'] = "text/javascript"
|
|
|
|
response.write(data)
|
2015-03-27 15:12:15 +00:00
|
|
|
return response
|
2018-11-23 12:18:32 +00:00
|
|
|
|
|
|
|
|
2019-12-13 13:47:51 +00:00
|
|
|
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:
|
|
|
|
"""
|
2019-12-13 13:47:51 +00:00
|
|
|
data = dict()
|
2018-11-23 12:18:32 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
|
|
|
try:
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmConnect(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
)
|
2018-11-23 12:18:32 +00:00
|
|
|
|
2019-12-13 13:47:51 +00:00
|
|
|
disk_device_types = conn.get_disk_device_types(arch, machine)
|
2018-11-23 12:18:32 +00:00
|
|
|
|
|
|
|
if disk in disk_device_types:
|
|
|
|
if disk == 'disk':
|
|
|
|
data['bus'] = sorted(disk_device_types)
|
|
|
|
elif disk == 'cdrom':
|
2019-09-10 06:48:31 +00:00
|
|
|
data['bus'] = ['ide', 'sata', 'scsi']
|
2018-11-23 12:18:32 +00:00
|
|
|
elif disk == 'floppy':
|
2019-09-10 06:48:31 +00:00
|
|
|
data['bus'] = ['fdc']
|
2018-11-23 12:18:32 +00:00
|
|
|
elif disk == 'lun':
|
2019-09-10 06:48:31 +00:00
|
|
|
data['bus'] = ['scsi']
|
2018-11-23 12:18:32 +00:00
|
|
|
except libvirtError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return HttpResponse(json.dumps(data))
|
|
|
|
|
2019-12-13 13:47:51 +00:00
|
|
|
|
|
|
|
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:
|
|
|
|
"""
|
2019-12-13 13:47:51 +00:00
|
|
|
data = dict()
|
|
|
|
try:
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmConnect(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
)
|
2019-12-13 13:47:51 +00:00
|
|
|
data['machines'] = conn.get_machine_types(arch)
|
|
|
|
except libvirtError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return HttpResponse(json.dumps(data))
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
"""
|
2019-12-13 13:47:51 +00:00
|
|
|
data = dict()
|
|
|
|
try:
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmConnect(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
)
|
2019-12-13 13:47:51 +00:00
|
|
|
data['videos'] = conn.get_video_models(arch, machine)
|
|
|
|
except libvirtError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return HttpResponse(json.dumps(data))
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
"""
|
2019-12-13 13:47:51 +00:00
|
|
|
data = dict()
|
|
|
|
try:
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmConnect(
|
|
|
|
compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
)
|
2019-12-13 13:47:51 +00:00
|
|
|
data['videos'] = conn.get_disk_device_types(arch, machine)
|
|
|
|
data['bus'] = conn.get_disk_device_types(arch, machine)
|
|
|
|
except libvirtError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return HttpResponse(json.dumps(data))
|
2020-05-29 13:25:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def add_host(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})
|