2016-02-11 13:48:41 +00:00
|
|
|
import os
|
2015-03-20 10:06:32 +00:00
|
|
|
import time
|
|
|
|
import json
|
2015-05-20 13:44:30 +00:00
|
|
|
import socket
|
|
|
|
import crypt
|
2016-09-06 11:01:45 +00:00
|
|
|
import re
|
2015-03-12 14:15:36 +00:00
|
|
|
from string import letters, digits
|
|
|
|
from random import choice
|
|
|
|
from bisect import insort
|
2015-03-20 10:06:32 +00:00
|
|
|
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
|
2015-03-12 14:15:36 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2015-12-22 14:09:02 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2015-02-27 08:53:51 +00:00
|
|
|
from computes.models import Compute
|
2015-03-02 15:31:25 +00:00
|
|
|
from instances.models import Instance
|
2015-05-27 13:23:49 +00:00
|
|
|
from accounts.models import UserInstance, UserSSHKey
|
2015-02-27 08:53:51 +00:00
|
|
|
from vrtManager.hostdetails import wvmHostDetails
|
2015-03-11 12:54:34 +00:00
|
|
|
from vrtManager.instance import wvmInstance, wvmInstances
|
2015-02-27 08:53:51 +00:00
|
|
|
from vrtManager.connection import connection_manager
|
2016-05-08 10:24:43 +00:00
|
|
|
from vrtManager.util import randomPasswd
|
2015-03-12 14:15:36 +00:00
|
|
|
from libvirt import libvirtError, VIR_DOMAIN_XML_SECURE
|
|
|
|
from webvirtcloud.settings import QEMU_KEYMAPS, QEMU_CONSOLE_TYPES
|
2015-03-18 13:48:29 +00:00
|
|
|
from logs.views import addlogmsg
|
2016-03-23 08:00:42 +00:00
|
|
|
from django.conf import settings
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
|
2015-12-22 14:09:02 +00:00
|
|
|
@login_required
|
2015-02-27 08:53:51 +00:00
|
|
|
def index(request):
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2015-12-22 14:09:02 +00:00
|
|
|
return HttpResponseRedirect(reverse('instances'))
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
|
2015-12-22 14:09:02 +00:00
|
|
|
@login_required
|
2015-02-27 08:53:51 +00:00
|
|
|
def instances(request):
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2015-02-27 09:51:33 +00:00
|
|
|
error_messages = []
|
2015-02-27 08:53:51 +00:00
|
|
|
all_host_vms = {}
|
2015-03-02 15:31:25 +00:00
|
|
|
all_user_vms = {}
|
|
|
|
computes = Compute.objects.all()
|
2015-02-27 08:53:51 +00:00
|
|
|
|
2016-05-02 10:23:18 +00:00
|
|
|
def get_userinstances_info(instance):
|
|
|
|
info = {}
|
|
|
|
uis = UserInstance.objects.filter(instance=instance)
|
|
|
|
info['count'] = len(uis)
|
|
|
|
if len(uis) > 0:
|
|
|
|
info['first_user'] = uis[0]
|
|
|
|
else:
|
|
|
|
info['first_user'] = None
|
|
|
|
return info
|
|
|
|
|
2017-04-20 12:46:42 +00:00
|
|
|
def refresh_instance_database(comp, vm, info):
|
|
|
|
instances_count = Instance.objects.filter(name=vm).count()
|
|
|
|
if instances_count > 1:
|
|
|
|
for i in Instance.objects.filter(name=vm):
|
|
|
|
user_instances_count = UserInstance.objects.filter(instance=i).count()
|
|
|
|
if user_instances_count == 0:
|
|
|
|
addlogmsg(request.user.username, i.name, _("Deleting due to multiple records."))
|
|
|
|
i.delete()
|
|
|
|
|
|
|
|
try:
|
|
|
|
check_uuid = Instance.objects.get(compute_id=comp.id, name=vm)
|
|
|
|
if check_uuid.uuid != info['uuid']:
|
|
|
|
check_uuid.save()
|
|
|
|
all_host_vms[comp.id, comp.name][vm]['is_template'] = check_uuid.is_template
|
|
|
|
all_host_vms[comp.id, comp.name][vm]['userinstances'] = get_userinstances_info(check_uuid)
|
|
|
|
except Instance.DoesNotExist:
|
|
|
|
check_uuid = Instance(compute_id=comp.id, name=vm, uuid=info['uuid'])
|
|
|
|
check_uuid.save()
|
|
|
|
|
2015-03-02 15:31:25 +00:00
|
|
|
if not request.user.is_superuser:
|
2015-05-21 14:48:04 +00:00
|
|
|
user_instances = UserInstance.objects.filter(user_id=request.user.id)
|
2015-03-02 15:31:25 +00:00
|
|
|
for usr_inst in user_instances:
|
|
|
|
if connection_manager.host_is_up(usr_inst.instance.compute.type,
|
|
|
|
usr_inst.instance.compute.hostname):
|
|
|
|
conn = wvmHostDetails(usr_inst.instance.compute,
|
|
|
|
usr_inst.instance.compute.login,
|
|
|
|
usr_inst.instance.compute.password,
|
|
|
|
usr_inst.instance.compute.type)
|
2015-03-11 09:39:44 +00:00
|
|
|
all_user_vms[usr_inst] = conn.get_user_instances(usr_inst.instance.name)
|
2015-03-11 12:54:34 +00:00
|
|
|
all_user_vms[usr_inst].update({'compute_id': usr_inst.instance.compute.id})
|
2015-03-02 15:31:25 +00:00
|
|
|
else:
|
2015-03-11 12:54:34 +00:00
|
|
|
for comp in computes:
|
|
|
|
if connection_manager.host_is_up(comp.type, comp.hostname):
|
2015-03-02 15:31:25 +00:00
|
|
|
try:
|
2015-03-11 12:54:34 +00:00
|
|
|
conn = wvmHostDetails(comp, comp.login, comp.password, comp.type)
|
2015-03-25 20:30:57 +00:00
|
|
|
if conn.get_host_instances():
|
|
|
|
all_host_vms[comp.id, comp.name] = conn.get_host_instances()
|
|
|
|
for vm, info in conn.get_host_instances().items():
|
2017-04-20 12:46:42 +00:00
|
|
|
refresh_instance_database(comp, vm, info)
|
|
|
|
|
2015-03-02 15:31:25 +00:00
|
|
|
conn.close()
|
|
|
|
except libvirtError as lib_err:
|
|
|
|
error_messages.append(lib_err)
|
2015-02-27 08:53:51 +00:00
|
|
|
|
2015-03-11 12:54:34 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
name = request.POST.get('name', '')
|
|
|
|
compute_id = request.POST.get('compute_id', '')
|
2015-03-18 13:48:29 +00:00
|
|
|
instance = Instance.objects.get(compute_id=compute_id, name=name)
|
2015-03-11 12:54:34 +00:00
|
|
|
try:
|
2015-03-18 13:48:29 +00:00
|
|
|
conn = wvmInstances(instance.compute.hostname,
|
|
|
|
instance.compute.login,
|
|
|
|
instance.compute.password,
|
|
|
|
instance.compute.type)
|
|
|
|
if 'poweron' in request.POST:
|
|
|
|
msg = _("Power On")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-11 12:54:34 +00:00
|
|
|
conn.start(name)
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-03-27 09:53:13 +00:00
|
|
|
|
|
|
|
if 'poweroff' in request.POST:
|
|
|
|
msg = _("Power Off")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-27 09:53:13 +00:00
|
|
|
conn.shutdown(name)
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
|
2015-03-18 13:48:29 +00:00
|
|
|
if 'powercycle' in request.POST:
|
|
|
|
msg = _("Power Cycle")
|
2015-03-11 12:54:34 +00:00
|
|
|
conn.force_shutdown(name)
|
2015-03-27 09:34:43 +00:00
|
|
|
conn.start(name)
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-11 12:54:34 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2016-04-16 13:06:39 +00:00
|
|
|
|
2015-09-08 10:47:03 +00:00
|
|
|
if 'getvvfile' in request.POST:
|
|
|
|
msg = _("Send console.vv file")
|
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
|
|
|
response = HttpResponse(content='', content_type='application/x-virt-viewer', status=200, reason=None, charset='utf-8')
|
|
|
|
response.writelines('[virt-viewer]\n')
|
|
|
|
response.writelines('type=' + conn.graphics_type(name) + '\n')
|
|
|
|
response.writelines('host=' + conn.graphics_listen(name) + '\n')
|
|
|
|
response.writelines('port=' + conn.graphics_port(name) + '\n')
|
|
|
|
response.writelines('title=' + conn.domain_name(name) + '\n')
|
|
|
|
response.writelines('password=' + conn.graphics_passwd(name) + '\n')
|
|
|
|
response.writelines('enable-usbredir=1\n')
|
|
|
|
response.writelines('disable-effects=all\n')
|
|
|
|
response.writelines('secure-attention=ctrl+alt+ins\n')
|
|
|
|
response.writelines('release-cursor=ctrl+alt\n')
|
|
|
|
response.writelines('fullscreen=1\n')
|
|
|
|
response.writelines('delete-this-file=1\n')
|
|
|
|
response['Content-Disposition'] = 'attachment; filename="console.vv"'
|
|
|
|
return response
|
2015-03-27 09:53:13 +00:00
|
|
|
|
2015-03-18 13:48:29 +00:00
|
|
|
if request.user.is_superuser:
|
2015-03-27 09:53:13 +00:00
|
|
|
|
2015-03-18 13:48:29 +00:00
|
|
|
if 'suspend' in request.POST:
|
|
|
|
msg = _("Suspend")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-18 13:48:29 +00:00
|
|
|
conn.suspend(name)
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-03-27 09:53:13 +00:00
|
|
|
|
2015-03-18 13:48:29 +00:00
|
|
|
if 'resume' in request.POST:
|
|
|
|
msg = _("Resume")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-18 13:48:29 +00:00
|
|
|
conn.resume(name)
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-03-27 09:53:13 +00:00
|
|
|
|
2015-03-11 12:54:34 +00:00
|
|
|
except libvirtError as lib_err:
|
|
|
|
error_messages.append(lib_err)
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, lib_err.message)
|
2015-03-11 12:54:34 +00:00
|
|
|
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'instances.html', locals())
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
|
2015-12-22 14:09:02 +00:00
|
|
|
@login_required
|
2015-03-12 14:15:36 +00:00
|
|
|
def instance(request, compute_id, vname):
|
2015-02-27 08:53:51 +00:00
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2015-03-16 09:57:55 +00:00
|
|
|
error_messages = []
|
|
|
|
messages = []
|
2015-04-02 12:39:40 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2015-03-16 09:57:55 +00:00
|
|
|
computes = Compute.objects.all()
|
|
|
|
computes_count = len(computes)
|
2015-05-27 13:23:49 +00:00
|
|
|
publickeys = UserSSHKey.objects.filter(user_id=request.user.id)
|
2015-03-16 09:57:55 +00:00
|
|
|
keymaps = QEMU_KEYMAPS
|
|
|
|
console_types = QEMU_CONSOLE_TYPES
|
|
|
|
try:
|
|
|
|
userinstace = UserInstance.objects.get(instance__compute_id=compute_id,
|
|
|
|
instance__name=vname,
|
|
|
|
user__id=request.user.id)
|
|
|
|
except UserInstance.DoesNotExist:
|
|
|
|
userinstace = None
|
|
|
|
|
|
|
|
if not request.user.is_superuser:
|
|
|
|
if not userinstace:
|
|
|
|
return HttpResponseRedirect(reverse('index'))
|
|
|
|
|
2016-02-08 09:13:19 +00:00
|
|
|
def show_clone_disk(disks, vname=''):
|
2015-03-12 14:15:36 +00:00
|
|
|
clone_disk = []
|
|
|
|
for disk in disks:
|
|
|
|
if disk['image'] is None:
|
|
|
|
continue
|
2016-02-08 09:13:19 +00:00
|
|
|
if disk['image'].count("-") and disk['image'].rsplit("-", 1)[0] == vname:
|
|
|
|
name, suffix = disk['image'].rsplit("-", 1)
|
|
|
|
image = name + "-clone" + "-" + suffix
|
|
|
|
elif disk['image'].count(".") and len(disk['image'].rsplit(".", 1)[1]) <= 7:
|
2015-03-12 14:15:36 +00:00
|
|
|
name, suffix = disk['image'].rsplit(".", 1)
|
|
|
|
image = name + "-clone" + "." + suffix
|
|
|
|
else:
|
|
|
|
image = disk['image'] + "-clone"
|
|
|
|
clone_disk.append(
|
2015-04-02 12:39:40 +00:00
|
|
|
{'dev': disk['dev'], 'storage': disk['storage'],
|
|
|
|
'image': image, 'format': disk['format']})
|
2015-03-12 14:15:36 +00:00
|
|
|
return clone_disk
|
2015-11-24 08:53:13 +00:00
|
|
|
|
|
|
|
def filesizefstr(size_str):
|
|
|
|
if size_str == '':
|
|
|
|
return 0
|
|
|
|
size_str = size_str.encode('ascii', 'ignore').upper().translate(None, " B")
|
|
|
|
if 'K' == size_str[-1]:
|
|
|
|
return long(float(size_str[:-1]))<<10
|
|
|
|
elif 'M' == size_str[-1]:
|
|
|
|
return long(float(size_str[:-1]))<<20
|
|
|
|
elif 'G' == size_str[-1]:
|
|
|
|
return long(float(size_str[:-1]))<<30
|
|
|
|
elif 'T' == size_str[-1]:
|
|
|
|
return long(float(size_str[:-1]))<<40
|
|
|
|
elif 'P' == size_str[-1]:
|
|
|
|
return long(float(size_str[:-1]))<<50
|
|
|
|
else:
|
|
|
|
return long(float(size_str))
|
2015-03-12 14:15:36 +00:00
|
|
|
|
2016-03-23 08:00:42 +00:00
|
|
|
def get_clone_free_names(size=10):
|
|
|
|
prefix = settings.CLONE_INSTANCE_DEFAULT_PREFIX
|
|
|
|
free_names = []
|
|
|
|
existing_names = [i.name for i in Instance.objects.filter(name__startswith=prefix)]
|
|
|
|
index = 1
|
|
|
|
while len(free_names) < size:
|
|
|
|
new_name = prefix + str(index)
|
|
|
|
if new_name not in existing_names:
|
|
|
|
free_names.append(new_name)
|
|
|
|
index += 1
|
|
|
|
return free_names
|
|
|
|
|
2016-03-31 11:12:52 +00:00
|
|
|
def check_user_quota(instance, cpu, memory, disk_size):
|
|
|
|
user_instances = UserInstance.objects.filter(user_id=request.user.id, instance__is_template=False)
|
2016-03-23 12:25:28 +00:00
|
|
|
instance += len(user_instances)
|
|
|
|
for usr_inst in user_instances:
|
|
|
|
if connection_manager.host_is_up(usr_inst.instance.compute.type,
|
|
|
|
usr_inst.instance.compute.hostname):
|
2016-03-31 11:12:52 +00:00
|
|
|
conn = wvmInstance(usr_inst.instance.compute,
|
2016-03-23 12:25:28 +00:00
|
|
|
usr_inst.instance.compute.login,
|
|
|
|
usr_inst.instance.compute.password,
|
2016-03-31 11:12:52 +00:00
|
|
|
usr_inst.instance.compute.type,
|
|
|
|
usr_inst.instance.name)
|
|
|
|
cpu += int(conn.get_vcpu())
|
|
|
|
memory += int(conn.get_memory())
|
|
|
|
for disk in conn.get_disk_device():
|
|
|
|
disk_size += int(disk['size'])>>30
|
2016-03-23 12:25:28 +00:00
|
|
|
|
2016-03-23 11:04:15 +00:00
|
|
|
ua = request.user.userattributes
|
2016-03-31 11:12:52 +00:00
|
|
|
msg = ""
|
2016-03-23 12:25:28 +00:00
|
|
|
if ua.max_instances > 0 and instance > ua.max_instances:
|
2016-03-31 11:12:52 +00:00
|
|
|
msg = "instance"
|
|
|
|
if settings.QUOTA_DEBUG:
|
|
|
|
msg += " (%s > %s)" % (instance, ua.max_instances)
|
2016-03-23 12:25:28 +00:00
|
|
|
if ua.max_cpus > 0 and cpu > ua.max_cpus:
|
2016-03-31 11:12:52 +00:00
|
|
|
msg = "cpu"
|
|
|
|
if settings.QUOTA_DEBUG:
|
|
|
|
msg += " (%s > %s)" % (cpu, ua.max_cpus)
|
2016-03-23 12:25:28 +00:00
|
|
|
if ua.max_memory > 0 and memory > ua.max_memory:
|
2016-03-31 11:12:52 +00:00
|
|
|
msg = "memory"
|
|
|
|
if settings.QUOTA_DEBUG:
|
|
|
|
msg += " (%s > %s)" % (memory, ua.max_memory)
|
|
|
|
if ua.max_disk_size > 0 and disk_size > ua.max_disk_size:
|
|
|
|
msg = "disk"
|
|
|
|
if settings.QUOTA_DEBUG:
|
|
|
|
msg += " (%s > %s)" % (disk_size, ua.max_disk_size)
|
|
|
|
return msg
|
2016-03-23 08:00:42 +00:00
|
|
|
|
2015-03-12 14:15:36 +00:00
|
|
|
try:
|
|
|
|
conn = wvmInstance(compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
vname)
|
|
|
|
|
|
|
|
status = conn.get_status()
|
|
|
|
autostart = conn.get_autostart()
|
|
|
|
vcpu = conn.get_vcpu()
|
|
|
|
cur_vcpu = conn.get_cur_vcpu()
|
|
|
|
uuid = conn.get_uuid()
|
|
|
|
memory = conn.get_memory()
|
|
|
|
cur_memory = conn.get_cur_memory()
|
2016-02-23 13:43:32 +00:00
|
|
|
title = conn.get_title()
|
2015-03-12 14:15:36 +00:00
|
|
|
description = conn.get_description()
|
|
|
|
disks = conn.get_disk_device()
|
|
|
|
media = conn.get_media_device()
|
|
|
|
networks = conn.get_net_device()
|
|
|
|
media_iso = sorted(conn.get_iso_media())
|
|
|
|
vcpu_range = conn.get_max_cpus()
|
|
|
|
memory_range = [256, 512, 768, 1024, 2048, 4096, 6144, 8192, 16384]
|
|
|
|
if memory not in memory_range:
|
|
|
|
insort(memory_range, memory)
|
|
|
|
if cur_memory not in memory_range:
|
|
|
|
insort(memory_range, cur_memory)
|
|
|
|
memory_host = conn.get_max_memory()
|
|
|
|
vcpu_host = len(vcpu_range)
|
|
|
|
telnet_port = conn.get_telnet_port()
|
|
|
|
console_type = conn.get_console_type()
|
|
|
|
console_port = conn.get_console_port()
|
|
|
|
console_keymap = conn.get_console_keymap()
|
|
|
|
snapshots = sorted(conn.get_snapshot(), reverse=True)
|
|
|
|
inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE)
|
|
|
|
has_managed_save_image = conn.get_managed_save_image()
|
2016-02-08 09:13:19 +00:00
|
|
|
clone_disks = show_clone_disk(disks, vname)
|
2015-03-12 14:15:36 +00:00
|
|
|
console_passwd = conn.get_console_passwd()
|
2016-03-23 08:00:42 +00:00
|
|
|
clone_free_names = get_clone_free_names()
|
2016-03-31 11:12:52 +00:00
|
|
|
user_quota_msg = check_user_quota(0, 0, 0, 0)
|
2015-03-12 14:15:36 +00:00
|
|
|
|
2015-03-16 09:57:55 +00:00
|
|
|
try:
|
|
|
|
instance = Instance.objects.get(compute_id=compute_id, name=vname)
|
|
|
|
if instance.uuid != uuid:
|
|
|
|
instance.uuid = uuid
|
|
|
|
instance.save()
|
|
|
|
except Instance.DoesNotExist:
|
|
|
|
instance = Instance(compute_id=compute_id, name=vname, uuid=uuid)
|
2015-03-12 14:15:36 +00:00
|
|
|
instance.save()
|
|
|
|
|
2016-04-28 10:50:11 +00:00
|
|
|
userinstances = UserInstance.objects.filter(instance=instance).order_by('user__username')
|
|
|
|
|
2015-03-12 14:15:36 +00:00
|
|
|
if request.method == 'POST':
|
2015-03-13 09:06:34 +00:00
|
|
|
if 'poweron' in request.POST:
|
2015-03-27 09:53:13 +00:00
|
|
|
conn.start()
|
2015-03-18 13:48:29 +00:00
|
|
|
msg = _("Power On")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-13 09:06:34 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#poweron')
|
2015-03-13 12:06:51 +00:00
|
|
|
|
2015-03-13 09:06:34 +00:00
|
|
|
if 'powercycle' in request.POST:
|
|
|
|
conn.force_shutdown()
|
|
|
|
conn.start()
|
2015-03-27 09:53:13 +00:00
|
|
|
msg = _("Power Cycle")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-13 09:06:34 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#powercycle')
|
2015-03-13 12:06:51 +00:00
|
|
|
|
2015-07-08 05:03:36 +00:00
|
|
|
if 'poweroff' in request.POST:
|
2015-03-27 09:53:13 +00:00
|
|
|
conn.shutdown()
|
2015-03-18 13:48:29 +00:00
|
|
|
msg = _("Power Off")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-13 09:06:34 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#poweroff')
|
2015-03-13 12:06:51 +00:00
|
|
|
|
2015-03-27 09:53:13 +00:00
|
|
|
if 'powerforce' in request.POST:
|
|
|
|
conn.force_shutdown()
|
|
|
|
msg = _("Force Off")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-27 15:12:15 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#powerforce')
|
2015-03-27 09:53:13 +00:00
|
|
|
|
2016-05-09 10:07:30 +00:00
|
|
|
if 'delete' in request.POST and (request.user.is_superuser or userinstace.is_delete):
|
2015-03-12 14:15:36 +00:00
|
|
|
if conn.get_status() == 1:
|
|
|
|
conn.force_shutdown()
|
2015-07-09 07:41:56 +00:00
|
|
|
if request.POST.get('delete_disk', ''):
|
2016-11-04 08:33:49 +00:00
|
|
|
for snap in snapshots:
|
|
|
|
conn.snapshot_delete(snap['name'])
|
2015-07-09 07:41:56 +00:00
|
|
|
conn.delete_disk()
|
|
|
|
conn.delete()
|
|
|
|
|
|
|
|
instance = Instance.objects.get(compute_id=compute_id, name=vname)
|
|
|
|
instance_name = instance.name
|
|
|
|
instance.delete()
|
|
|
|
|
2016-03-23 12:47:04 +00:00
|
|
|
try:
|
|
|
|
del_userinstance = UserInstance.objects.filter(instance__compute_id=compute_id, instance__name=vname)
|
2015-07-09 07:41:56 +00:00
|
|
|
del_userinstance.delete()
|
2016-03-23 12:47:04 +00:00
|
|
|
except UserInstance.DoesNotExist:
|
|
|
|
pass
|
2015-07-09 07:41:56 +00:00
|
|
|
|
|
|
|
msg = _("Destroy")
|
|
|
|
addlogmsg(request.user.username, instance_name, msg)
|
|
|
|
|
2015-03-16 09:57:55 +00:00
|
|
|
return HttpResponseRedirect(reverse('instances'))
|
2015-03-13 12:06:51 +00:00
|
|
|
|
2015-05-20 13:44:30 +00:00
|
|
|
if 'rootpasswd' in request.POST:
|
|
|
|
passwd = request.POST.get('passwd', '')
|
|
|
|
passwd_hash = crypt.crypt(passwd, '$6$kgPoiREy')
|
2015-05-27 13:23:49 +00:00
|
|
|
data = {'action': 'password', 'passwd': passwd_hash, 'vname': vname}
|
2015-05-20 13:44:30 +00:00
|
|
|
|
2015-05-21 08:52:10 +00:00
|
|
|
if conn.get_status() == 5:
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.connect((compute.hostname, 16510))
|
|
|
|
s.send(json.dumps(data))
|
|
|
|
result = json.loads(s.recv(1024))
|
|
|
|
s.close()
|
|
|
|
msg = _("Reset root password")
|
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-05-20 13:44:30 +00:00
|
|
|
|
2015-05-21 08:52:10 +00:00
|
|
|
if result['return'] == 'success':
|
|
|
|
messages.append(msg)
|
|
|
|
else:
|
|
|
|
error_messages.append(msg)
|
2015-05-20 13:44:30 +00:00
|
|
|
else:
|
2015-05-21 08:52:10 +00:00
|
|
|
msg = _("Please shutdow down your instance and then try again")
|
|
|
|
error_messages.append(msg)
|
2015-05-20 13:44:30 +00:00
|
|
|
|
2015-05-27 13:23:49 +00:00
|
|
|
if 'addpublickey' in request.POST:
|
|
|
|
sshkeyid = request.POST.get('sshkeyid', '')
|
|
|
|
publickey = UserSSHKey.objects.get(id=sshkeyid)
|
|
|
|
data = {'action': 'publickey', 'key': publickey.keypublic, 'vname': vname}
|
|
|
|
|
|
|
|
if conn.get_status() == 5:
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.connect((compute.hostname, 16510))
|
|
|
|
s.send(json.dumps(data))
|
|
|
|
result = json.loads(s.recv(1024))
|
|
|
|
s.close()
|
|
|
|
msg = _("Installed new ssh public key %s" % publickey.keyname)
|
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
|
|
|
|
|
|
|
if result['return'] == 'success':
|
|
|
|
messages.append(msg)
|
|
|
|
else:
|
|
|
|
error_messages.append(msg)
|
|
|
|
else:
|
|
|
|
msg = _("Please shutdow down your instance and then try again")
|
|
|
|
error_messages.append(msg)
|
|
|
|
|
2016-05-09 10:07:30 +00:00
|
|
|
if 'resize' in request.POST and (request.user.is_superuser or userinstace.is_change):
|
2016-03-23 13:29:40 +00:00
|
|
|
new_vcpu = request.POST.get('vcpu', '')
|
|
|
|
new_cur_vcpu = request.POST.get('cur_vcpu', '')
|
|
|
|
new_memory = request.POST.get('memory', '')
|
|
|
|
new_memory_custom = request.POST.get('memory_custom', '')
|
|
|
|
if new_memory_custom:
|
|
|
|
new_memory = new_memory_custom
|
|
|
|
new_cur_memory = request.POST.get('cur_memory', '')
|
|
|
|
new_cur_memory_custom = request.POST.get('cur_memory_custom', '')
|
|
|
|
if new_cur_memory_custom:
|
|
|
|
new_cur_memory = new_cur_memory_custom
|
2015-11-24 08:53:13 +00:00
|
|
|
disks_new = []
|
|
|
|
for disk in disks:
|
|
|
|
input_disk_size = filesizefstr(request.POST.get('disk_size_' + disk['dev'], ''))
|
|
|
|
if input_disk_size > disk['size']+(64<<20):
|
|
|
|
disk['size_new'] = input_disk_size
|
|
|
|
disks_new.append(disk)
|
2016-03-31 11:12:52 +00:00
|
|
|
disk_sum = sum([disk['size']>>30 for disk in disks_new])
|
|
|
|
disk_new_sum = sum([disk['size_new']>>30 for disk in disks_new])
|
|
|
|
quota_msg = check_user_quota(0, int(new_vcpu)-vcpu, int(new_memory)-memory, disk_new_sum-disk_sum)
|
2016-03-23 12:25:28 +00:00
|
|
|
if not request.user.is_superuser and quota_msg:
|
|
|
|
msg = _("User %s quota reached, cannot resize '%s'!" % (quota_msg, instance.name))
|
|
|
|
error_messages.append(msg)
|
|
|
|
else:
|
2016-03-23 13:29:40 +00:00
|
|
|
cur_memory = new_cur_memory
|
|
|
|
memory = new_memory
|
|
|
|
cur_vcpu = new_cur_vcpu
|
|
|
|
vcpu = new_vcpu
|
2016-03-23 12:25:28 +00:00
|
|
|
conn.resize(cur_memory, memory, cur_vcpu, vcpu, disks_new)
|
|
|
|
msg = _("Resize")
|
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#resize')
|
2015-03-13 12:06:51 +00:00
|
|
|
|
2015-03-12 14:15:36 +00:00
|
|
|
if 'umount_iso' in request.POST:
|
|
|
|
image = request.POST.get('path', '')
|
|
|
|
dev = request.POST.get('umount_iso', '')
|
|
|
|
conn.umount_iso(dev, image)
|
2015-03-27 09:53:13 +00:00
|
|
|
msg = _("Mount media")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-24 07:22:30 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#media')
|
2015-03-13 12:06:51 +00:00
|
|
|
|
2015-03-12 14:15:36 +00:00
|
|
|
if 'mount_iso' in request.POST:
|
|
|
|
image = request.POST.get('media', '')
|
|
|
|
dev = request.POST.get('mount_iso', '')
|
|
|
|
conn.mount_iso(dev, image)
|
2015-03-27 09:53:13 +00:00
|
|
|
msg = _("Umount media")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-24 07:22:30 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#media')
|
|
|
|
|
|
|
|
if 'snapshot' in request.POST:
|
|
|
|
name = request.POST.get('name', '')
|
|
|
|
conn.create_snapshot(name)
|
2015-03-27 09:53:13 +00:00
|
|
|
msg = _("New snapshot")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-05-20 13:44:30 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#restoresnapshot')
|
2015-03-13 12:06:51 +00:00
|
|
|
|
2015-03-12 14:15:36 +00:00
|
|
|
if 'delete_snapshot' in request.POST:
|
|
|
|
snap_name = request.POST.get('name', '')
|
|
|
|
conn.snapshot_delete(snap_name)
|
2015-03-27 09:53:13 +00:00
|
|
|
msg = _("Delete snapshot")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-04-21 12:40:18 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#restoresnapshot')
|
2015-03-13 12:06:51 +00:00
|
|
|
|
2015-03-12 14:15:36 +00:00
|
|
|
if 'revert_snapshot' in request.POST:
|
|
|
|
snap_name = request.POST.get('name', '')
|
|
|
|
conn.snapshot_revert(snap_name)
|
|
|
|
msg = _("Successful revert snapshot: ")
|
|
|
|
msg += snap_name
|
|
|
|
messages.append(msg)
|
2015-03-27 09:53:13 +00:00
|
|
|
msg = _("Revert snapshot")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-13 12:06:51 +00:00
|
|
|
|
2015-03-18 13:48:29 +00:00
|
|
|
if request.user.is_superuser:
|
|
|
|
if 'suspend' in request.POST:
|
2015-03-27 09:53:13 +00:00
|
|
|
conn.suspend()
|
2015-03-18 13:48:29 +00:00
|
|
|
msg = _("Suspend")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-18 13:48:29 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#resume')
|
|
|
|
|
|
|
|
if 'resume' in request.POST:
|
2015-03-27 09:53:13 +00:00
|
|
|
conn.resume()
|
2015-03-18 13:48:29 +00:00
|
|
|
msg = _("Resume")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-18 13:48:29 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#suspend')
|
|
|
|
|
|
|
|
if 'set_autostart' in request.POST:
|
2015-03-27 09:53:13 +00:00
|
|
|
conn.set_autostart(1)
|
2015-03-18 13:48:29 +00:00
|
|
|
msg = _("Set autostart")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-24 07:22:30 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#autostart')
|
2015-03-18 13:48:29 +00:00
|
|
|
|
|
|
|
if 'unset_autostart' in request.POST:
|
2015-03-27 09:53:13 +00:00
|
|
|
conn.set_autostart(0)
|
2015-03-18 13:48:29 +00:00
|
|
|
msg = _("Unset autostart")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-24 07:22:30 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#autostart')
|
2015-03-18 13:48:29 +00:00
|
|
|
|
|
|
|
if 'change_xml' in request.POST:
|
|
|
|
exit_xml = request.POST.get('inst_xml', '')
|
|
|
|
if exit_xml:
|
|
|
|
conn._defineXML(exit_xml)
|
2015-03-27 09:53:13 +00:00
|
|
|
msg = _("Edit XML")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-24 07:22:30 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#xmledit')
|
2015-03-18 13:48:29 +00:00
|
|
|
|
2016-05-09 10:08:31 +00:00
|
|
|
if request.user.is_superuser or userinstace.is_vnc:
|
2015-03-18 13:48:29 +00:00
|
|
|
if 'set_console_passwd' in request.POST:
|
|
|
|
if request.POST.get('auto_pass', ''):
|
2016-05-08 10:24:43 +00:00
|
|
|
passwd = randomPasswd()
|
2015-03-18 13:48:29 +00:00
|
|
|
else:
|
|
|
|
passwd = request.POST.get('console_passwd', '')
|
|
|
|
clear = request.POST.get('clear_pass', False)
|
|
|
|
if clear:
|
|
|
|
passwd = ''
|
|
|
|
if not passwd and not clear:
|
|
|
|
msg = _("Enter the console password or select Generate")
|
|
|
|
error_messages.append(msg)
|
|
|
|
if not error_messages:
|
|
|
|
if not conn.set_console_passwd(passwd):
|
|
|
|
msg = _("Error setting console password. You should check that your instance have an graphic device.")
|
|
|
|
error_messages.append(msg)
|
|
|
|
else:
|
2015-03-27 09:53:13 +00:00
|
|
|
msg = _("Set VNC password")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-04-21 12:40:18 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#vncsettings')
|
2015-03-18 13:48:29 +00:00
|
|
|
|
|
|
|
if 'set_console_keymap' in request.POST:
|
|
|
|
keymap = request.POST.get('console_keymap', '')
|
|
|
|
clear = request.POST.get('clear_keymap', False)
|
|
|
|
if clear:
|
|
|
|
conn.set_console_keymap('')
|
|
|
|
else:
|
|
|
|
conn.set_console_keymap(keymap)
|
2015-03-27 09:53:13 +00:00
|
|
|
msg = _("Set VNC keymap")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-04-21 12:40:18 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#vncsettings')
|
2015-03-18 13:48:29 +00:00
|
|
|
|
|
|
|
if 'set_console_type' in request.POST:
|
|
|
|
console_type = request.POST.get('console_type', '')
|
|
|
|
conn.set_console_type(console_type)
|
2015-03-27 09:53:13 +00:00
|
|
|
msg = _("Set VNC type")
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-04-21 12:40:18 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#vncsettings')
|
2015-03-18 13:48:29 +00:00
|
|
|
|
2016-05-09 10:08:31 +00:00
|
|
|
if request.user.is_superuser:
|
2015-03-18 13:48:29 +00:00
|
|
|
if 'migrate' in request.POST:
|
|
|
|
compute_id = request.POST.get('compute_id', '')
|
|
|
|
live = request.POST.get('live_migrate', False)
|
|
|
|
unsafe = request.POST.get('unsafe_migrate', False)
|
|
|
|
xml_del = request.POST.get('xml_delete', False)
|
2017-01-04 12:14:30 +00:00
|
|
|
offline = request.POST.get('offline_migrate', False)
|
2015-03-18 13:48:29 +00:00
|
|
|
new_compute = Compute.objects.get(id=compute_id)
|
|
|
|
conn_migrate = wvmInstances(new_compute.hostname,
|
|
|
|
new_compute.login,
|
|
|
|
new_compute.password,
|
|
|
|
new_compute.type)
|
2017-01-04 12:14:30 +00:00
|
|
|
conn_migrate.moveto(conn, vname, live, unsafe, xml_del, offline)
|
2016-04-21 11:08:20 +00:00
|
|
|
instance.compute = new_compute
|
|
|
|
instance.save()
|
2015-03-18 13:48:29 +00:00
|
|
|
conn_migrate.close()
|
2017-03-09 13:26:55 +00:00
|
|
|
if autostart:
|
|
|
|
conn_new = wvmInstance(new_compute.hostname,
|
|
|
|
new_compute.login,
|
|
|
|
new_compute.password,
|
|
|
|
new_compute.type,
|
|
|
|
vname)
|
|
|
|
conn_new.set_autostart(1)
|
|
|
|
conn_new.close()
|
|
|
|
msg = _("Migrate to %s" % new_compute.hostname)
|
2015-05-18 19:00:30 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2015-03-18 13:48:29 +00:00
|
|
|
return HttpResponseRedirect(reverse('instance', args=[compute_id, vname]))
|
|
|
|
|
2016-01-14 15:59:50 +00:00
|
|
|
if 'change_network' in request.POST:
|
|
|
|
network_data = {}
|
|
|
|
|
|
|
|
for post in request.POST:
|
2016-02-10 16:52:50 +00:00
|
|
|
if post.startswith('net-'):
|
2016-01-14 15:59:50 +00:00
|
|
|
network_data[post] = request.POST.get(post, '')
|
|
|
|
|
|
|
|
conn.change_network(network_data)
|
|
|
|
msg = _("Edit network")
|
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#network')
|
|
|
|
|
2016-02-23 13:43:32 +00:00
|
|
|
if 'change_options' in request.POST:
|
2016-01-20 14:40:09 +00:00
|
|
|
instance.is_template = request.POST.get('is_template', False)
|
|
|
|
instance.save()
|
2016-02-23 13:43:32 +00:00
|
|
|
|
|
|
|
options = {}
|
|
|
|
for post in request.POST:
|
|
|
|
if post in ['title', 'description']:
|
|
|
|
options[post] = request.POST.get(post, '')
|
|
|
|
conn.set_options(options)
|
|
|
|
|
|
|
|
msg = _("Edit options")
|
2016-01-20 14:40:09 +00:00
|
|
|
addlogmsg(request.user.username, instance.name, msg)
|
2016-02-23 13:43:32 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path() + '#options')
|
2016-01-20 14:40:09 +00:00
|
|
|
|
2016-03-23 08:00:42 +00:00
|
|
|
if request.user.is_superuser or request.user.userattributes.can_clone_instances:
|
|
|
|
if 'clone' in request.POST:
|
|
|
|
clone_data = {}
|
|
|
|
clone_data['name'] = request.POST.get('name', '')
|
|
|
|
|
2016-03-31 11:12:52 +00:00
|
|
|
disk_sum = sum([disk['size']>>30 for disk in disks])
|
|
|
|
quota_msg = check_user_quota(1, vcpu, memory, disk_sum)
|
2016-03-23 11:04:15 +00:00
|
|
|
check_instance = Instance.objects.filter(name=clone_data['name'])
|
|
|
|
|
|
|
|
if not request.user.is_superuser and quota_msg:
|
2016-03-23 08:00:42 +00:00
|
|
|
msg = _("User %s quota reached, cannot create '%s'!" % (quota_msg, clone_data['name']))
|
|
|
|
error_messages.append(msg)
|
2016-03-23 11:04:15 +00:00
|
|
|
elif check_instance:
|
2016-03-23 08:00:42 +00:00
|
|
|
msg = _("Instance '%s' already exists!" % clone_data['name'])
|
|
|
|
error_messages.append(msg)
|
2016-09-06 11:01:45 +00:00
|
|
|
elif not re.match(r'^[a-zA-Z0-9-]+$', clone_data['name']):
|
|
|
|
msg = _("Instance name '%s' contains invalid characters!" % clone_data['name'])
|
|
|
|
error_messages.append(msg)
|
2016-03-23 08:00:42 +00:00
|
|
|
else:
|
|
|
|
for post in request.POST:
|
|
|
|
clone_data[post] = request.POST.get(post, '')
|
|
|
|
|
|
|
|
new_uuid = conn.clone_instance(clone_data)
|
|
|
|
new_instance = Instance(compute_id=compute_id, name=clone_data['name'], uuid=new_uuid)
|
|
|
|
new_instance.save()
|
2016-03-23 12:47:04 +00:00
|
|
|
userinstance = UserInstance(instance_id=new_instance.id, user_id=request.user.id, is_delete=True)
|
2016-03-23 08:00:42 +00:00
|
|
|
userinstance.save()
|
|
|
|
|
|
|
|
msg = _("Clone of '%s'" % instance.name)
|
|
|
|
addlogmsg(request.user.username, new_instance.name, msg)
|
|
|
|
return HttpResponseRedirect(reverse('instance', args=[compute_id, clone_data['name']]))
|
|
|
|
|
2015-03-12 14:15:36 +00:00
|
|
|
conn.close()
|
|
|
|
|
2015-03-16 09:57:55 +00:00
|
|
|
except libvirtError as lib_err:
|
2015-03-18 13:48:29 +00:00
|
|
|
error_messages.append(lib_err.message)
|
2015-07-02 05:11:05 +00:00
|
|
|
addlogmsg(request.user.username, vname, lib_err.message)
|
2015-03-12 14:15:36 +00:00
|
|
|
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'instance.html', locals())
|
2015-03-20 10:06:32 +00:00
|
|
|
|
|
|
|
|
2015-12-22 14:09:02 +00:00
|
|
|
@login_required
|
2015-04-02 12:39:40 +00:00
|
|
|
def inst_status(request, compute_id, vname):
|
2015-03-23 12:28:24 +00:00
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2015-04-02 12:39:40 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2015-03-24 12:45:38 +00:00
|
|
|
response = HttpResponse()
|
|
|
|
response['Content-Type'] = "text/javascript"
|
2015-03-23 12:28:24 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
conn = wvmInstance(compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
vname)
|
2015-03-24 12:45:38 +00:00
|
|
|
data = json.dumps({'status': conn.get_status()})
|
2015-03-23 12:28:24 +00:00
|
|
|
conn.close()
|
|
|
|
except libvirtError:
|
2015-03-24 12:45:38 +00:00
|
|
|
data = json.dumps({'error': 'Error 500'})
|
2015-03-23 12:28:24 +00:00
|
|
|
response.write(data)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2015-12-22 14:09:02 +00:00
|
|
|
@login_required
|
2015-03-20 10:06:32 +00:00
|
|
|
def inst_graph(request, compute_id, vname):
|
|
|
|
"""
|
2015-03-23 12:28:24 +00:00
|
|
|
:param request:
|
|
|
|
:return:
|
2015-03-20 10:06:32 +00:00
|
|
|
"""
|
2015-03-23 12:28:24 +00:00
|
|
|
|
2015-03-20 10:06:32 +00:00
|
|
|
datasets = {}
|
|
|
|
json_blk = []
|
2015-03-25 09:59:22 +00:00
|
|
|
datasets_blk = {}
|
2015-03-20 10:06:32 +00:00
|
|
|
json_net = []
|
2015-03-25 09:59:22 +00:00
|
|
|
datasets_net = {}
|
|
|
|
cookies = {}
|
2015-03-20 10:06:32 +00:00
|
|
|
points = 5
|
|
|
|
curent_time = time.strftime("%H:%M:%S")
|
2015-04-02 12:39:40 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2015-03-23 12:28:24 +00:00
|
|
|
response = HttpResponse()
|
|
|
|
response['Content-Type'] = "text/javascript"
|
|
|
|
|
|
|
|
def check_points(dataset):
|
|
|
|
if len(dataset) > points:
|
|
|
|
dataset.pop(0)
|
|
|
|
return dataset
|
2015-03-20 10:06:32 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
conn = wvmInstance(compute.hostname,
|
|
|
|
compute.login,
|
|
|
|
compute.password,
|
|
|
|
compute.type,
|
|
|
|
vname)
|
|
|
|
cpu_usage = conn.cpu_usage()
|
|
|
|
blk_usage = conn.disk_usage()
|
|
|
|
net_usage = conn.net_usage()
|
|
|
|
conn.close()
|
|
|
|
|
2015-03-25 09:59:22 +00:00
|
|
|
try:
|
|
|
|
cookies['cpu'] = request.COOKIES['cpu']
|
|
|
|
cookies['blk'] = request.COOKIES['blk']
|
|
|
|
cookies['net'] = request.COOKIES['net']
|
|
|
|
cookies['timer'] = request.COOKIES['timer']
|
|
|
|
except KeyError:
|
|
|
|
cookies['cpu'] = None
|
|
|
|
cookies['blk'] = None
|
|
|
|
cookies['net'] = None
|
|
|
|
|
|
|
|
if not cookies['cpu']:
|
|
|
|
datasets['cpu'] = [0] * points
|
|
|
|
datasets['timer'] = [0] * points
|
2015-03-20 10:06:32 +00:00
|
|
|
else:
|
2015-03-25 09:59:22 +00:00
|
|
|
datasets['cpu'] = eval(cookies['cpu'])
|
|
|
|
datasets['timer'] = eval(cookies['timer'])
|
2015-03-20 10:06:32 +00:00
|
|
|
|
|
|
|
datasets['timer'].append(curent_time)
|
|
|
|
datasets['cpu'].append(int(cpu_usage['cpu']))
|
|
|
|
|
2015-03-23 12:28:24 +00:00
|
|
|
datasets['timer'] = check_points(datasets['timer'])
|
|
|
|
datasets['cpu'] = check_points(datasets['cpu'])
|
2015-03-20 10:06:32 +00:00
|
|
|
|
|
|
|
for blk in blk_usage:
|
2015-03-25 09:59:22 +00:00
|
|
|
if not cookies['blk']:
|
|
|
|
datasets_wr = [0] * points
|
|
|
|
datasets_rd = [0] * points
|
2015-03-20 10:06:32 +00:00
|
|
|
else:
|
2015-03-25 09:59:22 +00:00
|
|
|
datasets['blk'] = eval(cookies['blk'])
|
2015-03-23 12:28:24 +00:00
|
|
|
datasets_rd = datasets['blk'][blk['dev']][0]
|
|
|
|
datasets_wr = datasets['blk'][blk['dev']][1]
|
2015-03-20 10:06:32 +00:00
|
|
|
|
|
|
|
datasets_rd.append(int(blk['rd']) / 1048576)
|
|
|
|
datasets_wr.append(int(blk['wr']) / 1048576)
|
|
|
|
|
2015-03-23 12:28:24 +00:00
|
|
|
datasets_rd = check_points(datasets_rd)
|
|
|
|
datasets_wr = check_points(datasets_wr)
|
2015-03-20 10:06:32 +00:00
|
|
|
|
|
|
|
json_blk.append({'dev': blk['dev'], 'data': [datasets_rd, datasets_wr]})
|
2015-03-25 09:59:22 +00:00
|
|
|
datasets_blk[blk['dev']] = [datasets_rd, datasets_wr]
|
2015-03-20 10:06:32 +00:00
|
|
|
|
|
|
|
for net in net_usage:
|
2015-03-25 09:59:22 +00:00
|
|
|
if not cookies['net']:
|
|
|
|
datasets_rx = [0] * points
|
|
|
|
datasets_tx = [0] * points
|
2015-03-20 10:06:32 +00:00
|
|
|
else:
|
2015-03-25 09:59:22 +00:00
|
|
|
datasets['net'] = eval(cookies['net'])
|
2015-03-23 12:28:24 +00:00
|
|
|
datasets_rx = datasets['net'][net['dev']][0]
|
|
|
|
datasets_tx = datasets['net'][net['dev']][1]
|
2015-03-20 10:06:32 +00:00
|
|
|
|
|
|
|
datasets_rx.append(int(net['rx']) / 1048576)
|
|
|
|
datasets_tx.append(int(net['tx']) / 1048576)
|
|
|
|
|
2015-03-23 12:28:24 +00:00
|
|
|
datasets_rx = check_points(datasets_rx)
|
|
|
|
datasets_tx = check_points(datasets_tx)
|
2015-03-20 10:06:32 +00:00
|
|
|
|
|
|
|
json_net.append({'dev': net['dev'], 'data': [datasets_rx, datasets_tx]})
|
2015-03-25 09:59:22 +00:00
|
|
|
datasets_net[net['dev']] = [datasets_rx, datasets_tx]
|
2015-03-20 10:06:32 +00:00
|
|
|
|
2015-04-02 12:39:40 +00:00
|
|
|
data = json.dumps({'cpudata': datasets['cpu'], 'blkdata': json_blk,
|
|
|
|
'netdata': json_net, 'timeline': datasets['timer']})
|
2015-03-20 10:06:32 +00:00
|
|
|
|
|
|
|
response.cookies['cpu'] = datasets['cpu']
|
|
|
|
response.cookies['timer'] = datasets['timer']
|
2015-03-25 09:59:22 +00:00
|
|
|
response.cookies['blk'] = datasets_blk
|
|
|
|
response.cookies['net'] = datasets_net
|
2015-03-23 12:28:24 +00:00
|
|
|
except libvirtError:
|
|
|
|
data = json.dumps({'error': 'Error 500'})
|
|
|
|
|
2015-03-20 10:06:32 +00:00
|
|
|
response.write(data)
|
2015-03-24 07:22:30 +00:00
|
|
|
return response
|
2016-02-08 11:28:52 +00:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def guess_mac_address(request, vname):
|
2016-02-11 11:56:36 +00:00
|
|
|
dhcp_file = '/srv/webvirtcloud/dhcpd.conf'
|
2016-02-08 11:28:52 +00:00
|
|
|
data = { 'vname': vname, 'mac': '52:54:00:' }
|
2016-02-11 13:48:41 +00:00
|
|
|
if os.path.isfile(dhcp_file):
|
|
|
|
with open(dhcp_file, 'r') as f:
|
|
|
|
name_found = False
|
|
|
|
for line in f:
|
|
|
|
if "host %s." % vname in line:
|
|
|
|
name_found = True
|
|
|
|
if name_found and "hardware ethernet" in line:
|
|
|
|
data['mac'] = line.split(' ')[-1].strip().strip(';')
|
|
|
|
break
|
2017-05-11 08:46:39 +00:00
|
|
|
return HttpResponse(json.dumps(data))
|
2016-02-11 13:37:26 +00:00
|
|
|
|
2016-05-27 12:13:24 +00:00
|
|
|
@login_required
|
|
|
|
def guess_clone_name(request):
|
|
|
|
dhcp_file = '/srv/webvirtcloud/dhcpd.conf'
|
|
|
|
prefix = settings.CLONE_INSTANCE_DEFAULT_PREFIX
|
|
|
|
if os.path.isfile(dhcp_file):
|
|
|
|
instance_names = [i.name for i in Instance.objects.filter(name__startswith=prefix)]
|
|
|
|
with open(dhcp_file, 'r') as f:
|
|
|
|
for line in f:
|
|
|
|
line = line.strip()
|
|
|
|
if "host %s" % prefix in line:
|
2016-06-08 11:37:26 +00:00
|
|
|
fqdn = line.split(' ')[1]
|
|
|
|
hostname = fqdn.split('.')[0]
|
2016-05-27 12:13:24 +00:00
|
|
|
if hostname.startswith(prefix) and hostname not in instance_names:
|
|
|
|
return HttpResponse(json.dumps({'name': hostname}))
|
2017-05-11 08:46:39 +00:00
|
|
|
return HttpResponse(json.dumps({}))
|
2016-05-27 12:13:24 +00:00
|
|
|
|
2016-02-11 13:37:26 +00:00
|
|
|
@login_required
|
|
|
|
def check_instance(request, vname):
|
|
|
|
check_instance = Instance.objects.filter(name=vname)
|
|
|
|
data = { 'vname': vname, 'exists': False }
|
|
|
|
if check_instance:
|
|
|
|
data['exists'] = True
|
2017-05-11 08:46:39 +00:00
|
|
|
return HttpResponse(json.dumps(data))
|
|
|
|
|
|
|
|
def sshkeys(request, vname):
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:param vm:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
instance_keys = []
|
|
|
|
userinstances = UserInstance.objects.filter(instance__name=vname)
|
|
|
|
|
|
|
|
for ui in userinstances:
|
|
|
|
keys = UserSSHKey.objects.filter(user=ui.user)
|
|
|
|
for k in keys:
|
|
|
|
instance_keys.append(k.keypublic)
|
|
|
|
return HttpResponse(json.dumps(instance_keys))
|