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

228 lines
8.8 KiB
Python
Raw Normal View History

2015-03-19 13:37:16 +00:00
import time
import json
from django.http import HttpResponse, HttpResponseRedirect
2015-02-27 08:53:51 +00:00
from django.core.urlresolvers import reverse
2015-04-02 12:39:40 +00:00
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
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
2015-03-18 15:13:44 +00:00
from accounts.models import UserInstance
2015-03-06 11:59:27 +00:00
from computes.forms import ComputeAddTcpForm, ComputeAddSshForm, ComputeEditHostForm, ComputeAddTlsForm, ComputeAddSocketForm
2015-02-27 08:53:51 +00:00
from vrtManager.hostdetails import wvmHostDetails
2015-03-06 11:59:27 +00:00
from vrtManager.connection import CONN_SSH, CONN_TCP, CONN_TLS, CONN_SOCKET, connection_manager
2015-02-27 08:53:51 +00:00
from libvirt import libvirtError
@login_required
2015-02-27 08:53:51 +00:00
def computes(request):
"""
:param request:
:return:
"""
2015-03-03 09:34:09 +00:00
if not request.user.is_superuser:
return HttpResponseRedirect(reverse('index'))
2015-02-27 08:53:51 +00:00
def get_hosts_status(computes):
"""
Function return all hosts all vds on host
"""
compute_data = []
for compute in computes:
compute_data.append({'id': compute.id,
2015-02-27 09:28:22 +00:00
'name': compute.name,
'hostname': compute.hostname,
'status': connection_manager.host_is_up(compute.type, compute.hostname),
'type': compute.type,
'login': compute.login,
2016-03-18 18:21:44 +00:00
'password': compute.password,
'details': compute.details
2015-03-27 15:12:15 +00:00
})
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)
2016-03-18 18:21:44 +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_tcp_add' in request.POST:
form = ComputeAddTcpForm(request.POST)
if form.is_valid():
data = form.cleaned_data
new_tcp_host = Compute(name=data['name'],
hostname=data['hostname'],
type=CONN_TCP,
login=data['login'],
2018-07-31 07:15:59 +00:00
password=data['password'],
details=data['details'])
2015-03-06 11:59:27 +00:00
new_tcp_host.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-03-06 11:59:27 +00:00
if 'host_ssh_add' in request.POST:
form = ComputeAddSshForm(request.POST)
if form.is_valid():
data = form.cleaned_data
new_ssh_host = Compute(name=data['name'],
hostname=data['hostname'],
type=CONN_SSH,
2018-07-31 07:15:59 +00:00
login=data['login'],
details=data['details'])
2015-03-06 11:59:27 +00:00
new_ssh_host.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-03-06 11:59:27 +00:00
if 'host_tls_add' in request.POST:
form = ComputeAddTlsForm(request.POST)
if form.is_valid():
data = form.cleaned_data
new_tls_host = Compute(name=data['name'],
hostname=data['hostname'],
type=CONN_TLS,
login=data['login'],
2018-07-31 07:15:59 +00:00
password=data['password'],
details=data['details'])
2015-03-06 11:59:27 +00:00
new_tls_host.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-03-06 11:59:27 +00:00
if 'host_socket_add' in request.POST:
form = ComputeAddSocketForm(request.POST)
if form.is_valid():
data = form.cleaned_data
new_socket_host = Compute(name=data['name'],
2016-03-18 18:21:44 +00:00
details=data['details'],
2015-03-06 11:59:27 +00:00
hostname='localhost',
type=CONN_SOCKET,
login='',
password='')
new_socket_host.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-03-06 11:59:27 +00:00
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
@login_required
2015-03-04 13:43:29 +00:00
def overview(request, compute_id):
2015-02-27 08:53:51 +00:00
"""
:param request:
:return:
"""
2015-03-03 09:34:09 +00:00
if not request.user.is_superuser:
return HttpResponseRedirect(reverse('index'))
2015-02-27 08:53:51 +00:00
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:
conn = wvmHostDetails(compute.hostname,
compute.login,
compute.password,
compute.type)
hostname, host_arch, host_memory, logical_cpu, model_cpu, uri_conn = conn.get_node_info()
hypervisor = conn.hypervisor_type()
mem_usage = conn.get_memory_usage()
emulator = conn.emulator()
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
@login_required
2015-03-19 13:37:16 +00:00
def compute_graph(request, compute_id):
"""
:param request:
:return:
"""
points = 5
datasets = {}
cookies = {}
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
curent_time = time.strftime("%H:%M:%S")
try:
conn = wvmHostDetails(compute.hostname,
compute.login,
compute.password,
compute.type)
cpu_usage = conn.get_cpu_usage()
mem_usage = conn.get_memory_usage()
conn.close()
except libvirtError:
cpu_usage = 0
mem_usage = 0
try:
cookies['cpu'] = request.COOKIES['cpu']
cookies['mem'] = request.COOKIES['mem']
cookies['timer'] = request.COOKIES['timer']
except KeyError:
cookies['cpu'] = None
cookies['mem'] = None
2015-03-25 09:59:22 +00:00
if not cookies['cpu'] or not cookies['mem']:
datasets['cpu'] = [0] * points
datasets['mem'] = [0] * points
datasets['timer'] = [0] * points
2015-03-19 13:37:16 +00:00
else:
datasets['cpu'] = eval(cookies['cpu'])
datasets['mem'] = eval(cookies['mem'])
datasets['timer'] = eval(cookies['timer'])
datasets['timer'].append(curent_time)
datasets['cpu'].append(int(cpu_usage['usage']))
datasets['mem'].append(int(mem_usage['usage']) / 1048576)
if len(datasets['timer']) > points:
datasets['timer'].pop(0)
if len(datasets['cpu']) > points:
datasets['cpu'].pop(0)
if len(datasets['mem']) > points:
datasets['mem'].pop(0)
data = json.dumps({'cpudata': datasets['cpu'], 'memdata': datasets['mem'], 'timeline': datasets['timer']})
response = HttpResponse()
response['Content-Type'] = "text/javascript"
response.cookies['cpu'] = datasets['cpu']
response.cookies['timer'] = datasets['timer']
response.cookies['mem'] = datasets['mem']
response.write(data)
2015-03-27 15:12:15 +00:00
return response