From 573a09fd4deec7a068eaf9b6590e06fc92f39d66 Mon Sep 17 00:00:00 2001 From: Retspen Date: Fri, 6 Mar 2015 13:59:27 +0200 Subject: [PATCH] Added full function with compute --- computes/forms.py | 164 ++++++++++++++++++++ computes/views.py | 69 ++++++++- templates/computes.html | 80 +++++----- templates/create_comp_block.html | 158 ++++++++++++++++++- templates/create_iface_block.html | 142 ++++++++++++++++- templates/create_instance.html | 1 - templates/create_net_block.html | 72 ++++++++- templates/create_secret_block.html | 59 ++++++- templates/create_stg_block.html | 237 ++++++++++++++++++++++++++++- templates/instances.html | 3 +- templates/interface.html | 67 ++++++++ templates/interfaces.html | 27 ++++ templates/network.html | 2 +- templates/networks.html | 15 ++ templates/secrets.html | 30 ++-- 15 files changed, 1052 insertions(+), 74 deletions(-) create mode 100644 computes/forms.py create mode 100644 templates/interface.html diff --git a/computes/forms.py b/computes/forms.py new file mode 100644 index 0000000..a626106 --- /dev/null +++ b/computes/forms.py @@ -0,0 +1,164 @@ +import re +from django import forms +from django.utils.translation import ugettext_lazy as _ +from computes.models import Compute + + +class ComputeAddTcpForm(forms.Form): + name = forms.CharField(error_messages={'required': _('No hostname has been entered')}, + max_length=20) + hostname = forms.CharField(error_messages={'required': _('No IP / Domain name has been entered')}, + max_length=100) + login = forms.CharField(error_messages={'required': _('No login has been entered')}, + max_length=100) + password = forms.CharField(error_messages={'required': _('No password has been entered')}, + max_length=100) + + def clean_name(self): + name = self.cleaned_data['name'] + have_symbol = re.match('[^a-zA-Z0-9._-]+', name) + if have_symbol: + raise forms.ValidationError(_('The host name must not contain any special characters')) + elif len(name) > 20: + raise forms.ValidationError(_('The host name must not exceed 20 characters')) + try: + Compute.objects.get(name=name) + except Compute.DoesNotExist: + return name + raise forms.ValidationError(_('This host is already connected')) + + def clean_hostname(self): + hostname = self.cleaned_data['hostname'] + have_symbol = re.match('[^a-z0-9.-]+', hostname) + wrong_ip = re.match('^0.|^255.', hostname) + if have_symbol: + raise forms.ValidationError(_('Hostname must contain only numbers, or the domain name separated by "."')) + elif wrong_ip: + raise forms.ValidationError(_('Wrong IP address')) + try: + Compute.objects.get(hostname=hostname) + except Compute.DoesNotExist: + return hostname + raise forms.ValidationError(_('This host is already connected')) + + +class ComputeAddSshForm(forms.Form): + name = forms.CharField(error_messages={'required': _('No hostname has been entered')}, + max_length=20) + hostname = forms.CharField(error_messages={'required': _('No IP / Domain name has been entered')}, + max_length=100) + login = forms.CharField(error_messages={'required': _('No login has been entered')}, + max_length=20) + + def clean_name(self): + name = self.cleaned_data['name'] + have_symbol = re.match('[^a-zA-Z0-9._-]+', name) + if have_symbol: + raise forms.ValidationError(_('The name of the host must not contain any special characters')) + elif len(name) > 20: + raise forms.ValidationError(_('The name of the host must not exceed 20 characters')) + try: + Compute.objects.get(name=name) + except Compute.DoesNotExist: + return name + raise forms.ValidationError(_('This host is already connected')) + + def clean_hostname(self): + hostname = self.cleaned_data['hostname'] + have_symbol = re.match('[^a-zA-Z0-9._-]+', hostname) + wrong_ip = re.match('^0.|^255.', hostname) + if have_symbol: + raise forms.ValidationError(_('Hostname must contain only numbers, or the domain name separated by "."')) + elif wrong_ip: + raise forms.ValidationError(_('Wrong IP address')) + try: + Compute.objects.get(hostname=hostname) + except Compute.DoesNotExist: + return hostname + raise forms.ValidationError(_('This host is already connected')) + + +class ComputeAddTlsForm(forms.Form): + name = forms.CharField(error_messages={'required': _('No hostname has been entered')}, + max_length=20) + hostname = forms.CharField(error_messages={'required': _('No IP / Domain name has been entered')}, + max_length=100) + login = forms.CharField(error_messages={'required': _('No login has been entered')}, + max_length=100) + password = forms.CharField(error_messages={'required': _('No password has been entered')}, + max_length=100) + + def clean_name(self): + name = self.cleaned_data['name'] + have_symbol = re.match('[^a-zA-Z0-9._-]+', name) + if have_symbol: + raise forms.ValidationError(_('The host name must not contain any special characters')) + elif len(name) > 20: + raise forms.ValidationError(_('The host name must not exceed 20 characters')) + try: + Compute.objects.get(name=name) + except Compute.DoesNotExist: + return name + raise forms.ValidationError(_('This host is already connected')) + + def clean_hostname(self): + hostname = self.cleaned_data['hostname'] + have_symbol = re.match('[^a-z0-9.-]+', hostname) + wrong_ip = re.match('^0.|^255.', hostname) + if have_symbol: + raise forms.ValidationError(_('Hostname must contain only numbers, or the domain name separated by "."')) + elif wrong_ip: + raise forms.ValidationError(_('Wrong IP address')) + try: + Compute.objects.get(hostname=hostname) + except Compute.DoesNotExist: + return hostname + raise forms.ValidationError(_('This host is already connected')) + + +class ComputeEditHostForm(forms.Form): + host_id = forms.CharField() + name = forms.CharField(error_messages={'required': _('No hostname has been entered')}, + max_length=20) + hostname = forms.CharField(error_messages={'required': _('No IP / Domain name has been entered')}, + max_length=100) + login = forms.CharField(error_messages={'required': _('No login has been entered')}, + max_length=100) + password = forms.CharField(max_length=100) + + def clean_name(self): + name = self.cleaned_data['name'] + have_symbol = re.match('[^a-zA-Z0-9._-]+', name) + if have_symbol: + raise forms.ValidationError(_('The name of the host must not contain any special characters')) + elif len(name) > 20: + raise forms.ValidationError(_('The name of the host must not exceed 20 characters')) + return name + + def clean_hostname(self): + hostname = self.cleaned_data['hostname'] + have_symbol = re.match('[^a-zA-Z0-9._-]+', hostname) + wrong_ip = re.match('^0.|^255.', hostname) + if have_symbol: + raise forms.ValidationError(_('Hostname must contain only numbers, or the domain name separated by "."')) + elif wrong_ip: + raise forms.ValidationError(_('Wrong IP address')) + return hostname + + +class ComputeAddSocketForm(forms.Form): + name = forms.CharField(error_messages={'required': _('No hostname has been entered')}, + max_length=20) + + def clean_name(self): + name = self.cleaned_data['name'] + have_symbol = re.match('[^a-zA-Z0-9._-]+', name) + if have_symbol: + raise forms.ValidationError(_('The host name must not contain any special characters')) + elif len(name) > 20: + raise forms.ValidationError(_('The host name must not exceed 20 characters')) + try: + Compute.objects.get(name=name) + except Compute.DoesNotExist: + return name + raise forms.ValidationError(_('This host is already connected')) diff --git a/computes/views.py b/computes/views.py index be8558e..699909a 100644 --- a/computes/views.py +++ b/computes/views.py @@ -2,8 +2,10 @@ from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse from django.shortcuts import render from computes.models import Compute +from instances.models import Instance +from computes.forms import ComputeAddTcpForm, ComputeAddSshForm, ComputeEditHostForm, ComputeAddTlsForm, ComputeAddSocketForm from vrtManager.hostdetails import wvmHostDetails -from vrtManager.connection import connection_manager +from vrtManager.connection import CONN_SSH, CONN_TCP, CONN_TLS, CONN_SOCKET, connection_manager from libvirt import libvirtError @@ -38,6 +40,71 @@ def computes(request): computes = Compute.objects.filter() computes_info = get_hosts_status(computes) + if request.method == 'POST': + if 'host_del' in request.POST: + compute_id = request.POST.get('host_id', '') + 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() + 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'], + password=data['password']) + new_tcp_host.save() + return HttpResponseRedirect(request.get_full_path()) + 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, + login=data['login']) + new_ssh_host.save() + return HttpResponseRedirect(request.get_full_path()) + 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'], + password=data['password']) + new_tls_host.save() + return HttpResponseRedirect(request.get_full_path()) + 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'], + hostname='localhost', + type=CONN_SOCKET, + login='', + password='') + new_socket_host.save() + 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'] + compute_edit.save() + return HttpResponseRedirect(request.get_full_path()) + return render(request, 'computes.html', locals()) def overview(request, compute_id): diff --git a/templates/computes.html b/templates/computes.html index d97bb9c..399afd5 100644 --- a/templates/computes.html +++ b/templates/computes.html @@ -63,34 +63,25 @@
- +
-
- +
-
- +
-
- +
@@ -111,32 +102,23 @@ @@ -158,37 +140,51 @@ {% csrf_token %}
-
- - + +
-
- +
-
- +
-
- + +
+
+ + + {% endifequal %} + {% ifequal compute.type 4 %} + @@ -219,7 +215,7 @@ {% trans "Warning:" %} {% trans "Hypervisor doesn't have any Computes" %} - + {% endif %} {% endblock %} \ No newline at end of file diff --git a/templates/create_comp_block.html b/templates/create_comp_block.html index e81a484..57e327a 100644 --- a/templates/create_comp_block.html +++ b/templates/create_comp_block.html @@ -1,5 +1,159 @@ +{% load i18n %} {% if request.user.is_superuser %} - + + + + {% endif %} \ No newline at end of file diff --git a/templates/create_iface_block.html b/templates/create_iface_block.html index e81a484..2700481 100644 --- a/templates/create_iface_block.html +++ b/templates/create_iface_block.html @@ -1,5 +1,143 @@ +{% load i18n %} {% if request.user.is_superuser %} - + + + + {% endif %} \ No newline at end of file diff --git a/templates/create_instance.html b/templates/create_instance.html index 53f24f1..f4b91db 100644 --- a/templates/create_instance.html +++ b/templates/create_instance.html @@ -13,7 +13,6 @@ {% include 'errors_block.html' %} - diff --git a/templates/create_net_block.html b/templates/create_net_block.html index e81a484..0ab42b4 100644 --- a/templates/create_net_block.html +++ b/templates/create_net_block.html @@ -1,5 +1,73 @@ +{% load i18n %} {% if request.user.is_superuser %} - + + + + {% endif %} \ No newline at end of file diff --git a/templates/create_secret_block.html b/templates/create_secret_block.html index e81a484..9a71469 100644 --- a/templates/create_secret_block.html +++ b/templates/create_secret_block.html @@ -1,5 +1,60 @@ +{% load i18n %} {% if request.user.is_superuser %} - + + + + {% endif %} \ No newline at end of file diff --git a/templates/create_stg_block.html b/templates/create_stg_block.html index e81a484..24dd2fd 100644 --- a/templates/create_stg_block.html +++ b/templates/create_stg_block.html @@ -1,5 +1,238 @@ +{% load i18n %} {% if request.user.is_superuser %} - + + + + {% endif %} \ No newline at end of file diff --git a/templates/instances.html b/templates/instances.html index 7a49d33..f50147a 100644 --- a/templates/instances.html +++ b/templates/instances.html @@ -90,8 +90,7 @@ title="{% trans "Destroy" %}" onclick="return confirm('Are you sure?')"> - + {% endifequal %} diff --git a/templates/interface.html b/templates/interface.html new file mode 100644 index 0000000..c55750d --- /dev/null +++ b/templates/interface.html @@ -0,0 +1,67 @@ +{% extends "base.html" %} +{% load i18n %} +{% block title %}{% trans "Interface" %} - {{ iface }}{% endblock %} +{% block content %} + +
+
+

{% trans "Interface:" %} {{ iface }}

+ +
+
+ + + {% include 'errors_block.html' %} + +
+
+

{% trans "Interface:" %}

+

{% trans "IPv4:" %} ({% ifequal ipv4 None %}None{% else %}{{ ipv4_type }}{% endifequal %})

+

{% trans "IPv6:" %} ({% ifequal ipv6 None %}None{% else %}{{ ipv6_type }}{% endifequal %})

+

{% trans "MAC Adress:" %}

+

{% trans "Interface Type:" %}

+ {% ifequal itype 'bridge' %} +

{% trans "Bridge device" %}

+ {% endifequal %} +

{% trans "Boot Mode:" %}

+

{% trans "State:" %}

+
+
+

{{ iface }}

+

{{ ipv4 }}

+

{{ ipv6 }}

+

{{ mac }}

+

{{ itype }}

+ {% ifequal itype 'bridge' %} +

{{ bridge }}

+ {% endifequal %} +

{{ start_mode }}

+

+

{% csrf_token %} + {% ifequal state 0 %} + + + {% else %} + + {% endifequal %} +
+

+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/interfaces.html b/templates/interfaces.html index 4f27504..c8b0546 100644 --- a/templates/interfaces.html +++ b/templates/interfaces.html @@ -63,4 +63,31 @@ {% endfor %} {% endif %} +{% endblock %} +{% block script %} + {% endblock %} \ No newline at end of file diff --git a/templates/network.html b/templates/network.html index 33799ec..16e84bc 100644 --- a/templates/network.html +++ b/templates/network.html @@ -1,6 +1,6 @@ {% extends "base.html" %} {% load i18n %} -{% block title %}{% trans "Networks" %} - {{ compute.name }}{% endblock %} +{% block title %}{% trans "Network" %} - {{ pool }}{% endblock %} {% block content %}
diff --git a/templates/networks.html b/templates/networks.html index 553e00d..3deab5b 100644 --- a/templates/networks.html +++ b/templates/networks.html @@ -62,4 +62,19 @@ {% endfor %} {% endif %}
+{% endblock %} +{% block script %} + {% endblock %} \ No newline at end of file diff --git a/templates/secrets.html b/templates/secrets.html index c171981..cb6a2b0 100644 --- a/templates/secrets.html +++ b/templates/secrets.html @@ -6,7 +6,7 @@
{% include 'create_secret_block.html' %} -

{{ compute.name }}

+

{% trans "Secrets" %}

{% else %} - +
+
+
@@ -66,43 +68,35 @@
{% trans "UUID" %}
{% csrf_token %} - + -
+
+ + {% endif %} {% endblock %} \ No newline at end of file