diff --git a/console/__init__.py b/console/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/console/admin.py b/console/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/console/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/console/migrations/__init__.py b/console/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/console/models.py b/console/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/console/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/console/tests.py b/console/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/console/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/console/views.py b/console/views.py new file mode 100644 index 0000000..a123064 --- /dev/null +++ b/console/views.py @@ -0,0 +1,56 @@ +import re +from django.shortcuts import render +from django.http import HttpResponseRedirect +from django.core.urlresolvers import reverse +from instances.models import Instance +from vrtManager.instance import wvmInstance +from webvirtcloud.settings import WS_PORT +from webvirtcloud.settings import WS_PUBLIC_HOST +from libvirt import libvirtError + + +def console(request): + """ + :param request: + :return: + """ + + if not request.user.is_authenticated(): + return HttpResponseRedirect(reverse('login')) + + if request.method == 'GET': + token = request.GET.get('token', '') + + try: + temptoken = token.split('-', 1) + host = int(temptoken[0]) + uuid = temptoken[1] + instance = Instance.objects.get(compute_id=host, uuid=uuid) + conn = wvmInstance(instance.compute.hostname, + instance.compute.login, + instance.compute.password, + instance.compute.type, + instance.name) + console_type = conn.get_console_type() + console_websocket_port = conn.get_console_websocket_port() + console_passwd = conn.get_console_passwd() + except libvirtError as lib_err: + console_type = None + console_websocket_port = None + console_passwd = None + + ws_port = console_websocket_port if console_websocket_port else WS_PORT + ws_host = WS_PUBLIC_HOST if WS_PUBLIC_HOST else request.get_host() + + if ':' in ws_host: + ws_host = re.sub(':[0-9]+', '', ws_host) + + if console_type == 'vnc': + response = render(request, 'console-vnc.html', locals()) + elif console_type == 'spice': + response = render(request, 'console-spice.html', locals()) + else: + response = "Console type %s no support" % console_type + + response.set_cookie('token', token) + return response diff --git a/definst/__init__.py b/definst/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/definst/admin.py b/definst/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/definst/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/definst/forms.py b/definst/forms.py new file mode 100644 index 0000000..ec866f0 --- /dev/null +++ b/definst/forms.py @@ -0,0 +1,55 @@ +import re +from django import forms +from django.utils.translation import ugettext_lazy as _ +from definst.models import Flavor + + +class FlavorAddForm(forms.Form): + label = forms.CharField(label="Name", + error_messages={'required': _('No flavor name has been entered')}, + max_length=20) + vcpu = forms.IntegerField(label="VCPU", + error_messages={'required': _('No VCPU has been entered')}, ) + disk = forms.IntegerField(label="HDD", + error_messages={'required': _('No HDD image has been entered')}, ) + memory = forms.IntegerField(label="RAM", + error_messages={'required': _('No RAM size has been entered')}, ) + + def clean_name(self): + label = self.cleaned_data['label'] + have_symbol = re.match('^[a-zA-Z0-9._-]+$', label) + if not have_symbol: + raise forms.ValidationError(_('The flavor name must not contain any special characters')) + elif len(label) > 20: + raise forms.ValidationError(_('The flavor name must not exceed 20 characters')) + try: + Flavor.objects.get(label=label) + except Flavor.DoesNotExist: + return label + raise forms.ValidationError(_('Flavor name is already use')) + + +class NewVMForm(forms.Form): + name = forms.CharField(error_messages={'required': _('No Virtual Machine name has been entered')}, + max_length=20) + vcpu = forms.IntegerField(error_messages={'required': _('No VCPU has been entered')}) + host_model = forms.BooleanField(required=False) + disk = forms.IntegerField(required=False) + memory = forms.IntegerField(error_messages={'required': _('No RAM size has been entered')}) + networks = forms.CharField(error_messages={'required': _('No Network pool has been choice')}) + storage = forms.CharField(max_length=20, required=False) + template = forms.CharField(required=False) + images = forms.CharField(required=False) + hdd_size = forms.IntegerField(required=False) + meta_prealloc = forms.BooleanField(required=False) + virtio = forms.BooleanField(required=False) + mac = forms.CharField(required=False) + + def clean_name(self): + name = self.cleaned_data['name'] + have_symbol = re.match('^[a-zA-Z0-9._-]+$', name) + if not have_symbol: + raise forms.ValidationError(_('The name of the virtual machine must not contain any special characters')) + elif len(name) > 20: + raise forms.ValidationError(_('The name of the virtual machine must not exceed 20 characters')) + return name diff --git a/definst/migrations/__init__.py b/definst/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/definst/models.py b/definst/models.py new file mode 100644 index 0000000..0572fdd --- /dev/null +++ b/definst/models.py @@ -0,0 +1,11 @@ +from django.db import models + + +class Flavor(models.Model): + label = models.CharField(max_length=12) + memory = models.IntegerField() + vcpu = models.IntegerField() + disk = models.IntegerField() + + def __unicode__(self): + return self.name diff --git a/definst/tests.py b/definst/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/definst/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/definst/views.py b/definst/views.py new file mode 100644 index 0000000..9f17680 --- /dev/null +++ b/definst/views.py @@ -0,0 +1,136 @@ +from django.shortcuts import render +from django.http import HttpResponseRedirect +from django.utils.translation import ugettext_lazy as _ +from django.core.urlresolvers import reverse +from computes.models import Compute +from definst.models import Flavor +from definst.forms import FlavorAddForm, NewVMForm +from instances.models import Instance +from vrtManager.create import wvmCreate +from vrtManager import util +from libvirt import libvirtError + + +def create(request, host_id): + """ + :param request: + :return: + """ + + if not request.user.is_authenticated(): + return HttpResponseRedirect(reverse('index')) + + conn = None + error_messages = [] + storages = [] + networks = [] + meta_prealloc = False + compute = Compute.objects.get(id=host_id) + flavors = Flavor.objects.filter().order_by('id') + + try: + conn = wvmCreate(compute.hostname, + compute.login, + compute.password, + compute.type) + + storages = sorted(conn.get_storages()) + networks = sorted(conn.get_networks()) + instances = conn.get_instances() + get_images = sorted(conn.get_storages_images()) + mac_auto = util.randomMAC() + except libvirtError as lib_err: + error_messages.append(lib_err) + + if conn: + if not storages: + msg = _("You haven't defined have any storage pools") + error_messages.append(msg) + if not networks: + msg = _("You haven't defined have any network pools") + error_messages.append(msg) + + if request.method == 'POST': + if 'create_flavor' in request.POST: + form = FlavorAddForm(request.POST) + if form.is_valid(): + data = form.cleaned_data + create_flavor = Flavor(label=data['label'], + vcpu=data['vcpu'], + memory=data['memory'], + disk=data['disk']) + create_flavor.save() + return HttpResponseRedirect(request.get_full_path()) + if 'delete_flavor' in request.POST: + flavor_id = request.POST.get('flavor', '') + delete_flavor = Flavor.objects.get(id=flavor_id) + delete_flavor.delete() + return HttpResponseRedirect(request.get_full_path()) + if 'create_xml' in request.POST: + xml = request.POST.get('from_xml', '') + try: + name = util.get_xml_path(xml, '/domain/name') + except util.libxml2.parserError: + name = None + if name in instances: + error_msg = _("A virtual machine with this name already exists") + error_messages.append(error_msg) + else: + try: + conn._defineXML(xml) + return HttpResponseRedirect(reverse('instance', args=[host_id, name])) + except libvirtError as lib_err: + error_messages.append(lib_err.message) + if 'create' in request.POST: + volumes = {} + form = NewVMForm(request.POST) + if form.is_valid(): + data = form.cleaned_data + if data['meta_prealloc']: + meta_prealloc = True + if instances: + if data['name'] in instances: + msg = _("A virtual machine with this name already exists") + error_messages.append(msg) + if not error_messages: + if data['hdd_size']: + if not data['mac']: + error_msg = _("No Virtual Machine MAC has been entered") + error_messages.append(error_msg) + else: + try: + path = conn.create_volume(data['storage'], data['name'], data['hdd_size'], + metadata=meta_prealloc) + volumes[path] = conn.get_volume_type(path) + except libvirtError as lib_err: + error_messages.append(lib_err.message) + elif data['template']: + templ_path = conn.get_volume_path(data['template']) + clone_path = conn.clone_from_template(data['name'], templ_path, metadata=meta_prealloc) + volumes[clone_path] = conn.get_volume_type(clone_path) + else: + if not data['images']: + error_msg = _("First you need to create or select an image") + error_messages.append(error_msg) + else: + for vol in data['images'].split(','): + try: + path = conn.get_volume_path(vol) + volumes[path] = conn.get_volume_type(path) + except libvirtError as lib_err: + error_messages.append(lib_err.message) + if not error_messages: + uuid = util.randomUUID() + try: + conn.create_instance(data['name'], data['memory'], data['vcpu'], data['host_model'], + uuid, volumes, data['networks'], data['virtio'], data['mac']) + create_instance = Instance(compute_id=host_id, name=data['name'], uuid=uuid) + create_instance.save() + return HttpResponseRedirect(reverse('instance', args=[host_id, data['name']])) + except libvirtError as lib_err: + if data['hdd_size']: + conn.delete_volume(volumes.keys()[0]) + error_messages.append(lib_err) + conn.close() + + return render('definst.html', locals()) diff --git a/templates/404.html b/templates/404.html new file mode 100644 index 0000000..9c5c079 --- /dev/null +++ b/templates/404.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} +{% load i18n %} +{% block title %}{% trans "404" %}{% endblock %} +{% block content %} +
+
+

Oops!

+ +

{% trans "404 Not Found" %}

+ +

{% trans "The requested page was not found on this server." %}

+ ← Back +
+
+{% endblock %} diff --git a/templates/500.html b/templates/500.html new file mode 100644 index 0000000..b2a0b34 --- /dev/null +++ b/templates/500.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} +{% load i18n %} +{% block title %}{% trans "500" %}{% endblock %} +{% block content %} +
+
+

Oops!

+ +

{% trans "500 Internal Server Error" %}

+ +

{% trans "The server encountered an internal error or misconfiguration and was unable to complete you request." %}

+ ← Back +
+
+{% endblock %} diff --git a/templates/header.html b/templates/base.html similarity index 62% rename from templates/header.html rename to templates/base.html index 6433298..62f6d84 100644 --- a/templates/header.html +++ b/templates/base.html @@ -5,13 +5,16 @@ + + {% block title %}{% endblock %} + - - Dashboard Template for Bootstrap - - - - - \ No newline at end of file + + + {% block content %}{% endblock %} + + + + \ No newline at end of file diff --git a/templates/compute.html b/templates/compute.html index 068a0f9..4476309 100644 --- a/templates/compute.html +++ b/templates/compute.html @@ -1,7 +1,7 @@ -{% load static %} +{% extends "base.html" %} {% load i18n %} -{% include 'header.html' %} - +{% block title %}{% trans "Compute" %} - {{ compute.name }}{% endblock %} +{% block content %}
@@ -53,5 +53,4 @@
- -{% include 'footer.html' %} +{% endblock %} \ No newline at end of file diff --git a/templates/computes.html b/templates/computes.html index 03eb4cf..b3be28c 100644 --- a/templates/computes.html +++ b/templates/computes.html @@ -1,7 +1,7 @@ -{% load static %} +{% extends "base.html" %} {% load i18n %} -{% include 'header.html' %} - +{% block title %}{% trans "Computes" %}{% endblock %} +{% block content %}
@@ -227,5 +227,4 @@
- -{% include 'footer.html' %} +{% endblock %} \ No newline at end of file diff --git a/templates/console-spice.html b/templates/console-spice.html new file mode 100644 index 0000000..a8c016f --- /dev/null +++ b/templates/console-spice.html @@ -0,0 +1,208 @@ +{% load i18n %} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + + + diff --git a/templates/console-vnc.html b/templates/console-vnc.html new file mode 100644 index 0000000..fe5d219 --- /dev/null +++ b/templates/console-vnc.html @@ -0,0 +1,186 @@ +{% load i18n %} + + + + + + + + +
+
+ + + + + +
+
{% trans "Loading..." %}
+
+
+ + + + + + + +
+
+
+ + {% trans "Canvas not supported." %} + +
+ + + diff --git a/templates/footer.html b/templates/footer.html deleted file mode 100644 index 486610b..0000000 --- a/templates/footer.html +++ /dev/null @@ -1,3 +0,0 @@ -{% load static %} - - \ No newline at end of file diff --git a/templates/instances.html b/templates/instances.html index cb005a4..4a5e304 100644 --- a/templates/instances.html +++ b/templates/instances.html @@ -1,7 +1,7 @@ -{% load static %} +{% extends "base.html" %} {% load i18n %} -{% include 'header.html' %} - +{% block title %}{% trans "Instances" %}{% endblock %} +{% block content %}
@@ -100,8 +100,8 @@
+
- -{% include 'footer.html' %} +{% endblock %} diff --git a/webvirtcloud/settings.py b/webvirtcloud/settings.py index 385573e..a4b5365 100644 --- a/webvirtcloud/settings.py +++ b/webvirtcloud/settings.py @@ -1,11 +1,6 @@ """ Django settings for webvirtcloud project. -For more information on this file, see -https://docs.djangoproject.com/en/1.7/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/1.7/ref/settings/ """ import os @@ -70,4 +65,10 @@ STATICFILES_DIRS = ( TEMPLATE_DIRS = ( os.path.join(BASE_DIR, 'templates'), -) \ No newline at end of file +) + +# WebVirtCloud settings +WS_PORT = 6080 +WS_HOST = '0.0.0.0' +WS_PUBLIC_HOST = None +WS_CERT = None \ No newline at end of file