mirror of
https://github.com/retspen/webvirtcloud
synced 2025-07-31 12:41:08 +00:00
build new tree
This commit is contained in:
parent
4d48e79341
commit
dd5f98cbe8
22 changed files with 745 additions and 60 deletions
73
interfaces/forms.py
Normal file
73
interfaces/forms.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import re
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
||||
class AddInterface(forms.Form):
|
||||
name = forms.CharField(max_length=10, required=True)
|
||||
itype = forms.ChoiceField(required=True, choices=(('bridge', 'bridge'), ('ethernet', 'ethernet')))
|
||||
start_mode = forms.ChoiceField(required=True,
|
||||
choices=(('none', 'none'), ('onboot', 'onboot'), ('hotplug', 'hotplug')))
|
||||
netdev = forms.CharField(max_length=15, required=True)
|
||||
ipv4_type = forms.ChoiceField(required=True, choices=(('dhcp', 'dhcp'), ('static', 'static'), ('none', 'none')))
|
||||
ipv4_addr = forms.CharField(max_length=18, required=False)
|
||||
ipv4_gw = forms.CharField(max_length=15, required=False)
|
||||
ipv6_type = forms.ChoiceField(required=True, choices=(('dhcp', 'dhcp'), ('static', 'static'), ('none', 'none')))
|
||||
ipv6_addr = forms.CharField(max_length=100, required=False)
|
||||
ipv6_gw = forms.CharField(max_length=100, required=False)
|
||||
stp = forms.ChoiceField(required=False, choices=(('on', 'on'), ('off', 'off')))
|
||||
delay = forms.IntegerField(required=False)
|
||||
|
||||
def clean_ipv4_addr(self):
|
||||
ipv4_addr = self.cleaned_data['ipv4_addr']
|
||||
have_symbol = re.match('^[0-9./]+$', ipv4_addr)
|
||||
if not have_symbol:
|
||||
raise forms.ValidationError(_('The ipv4 must not contain any special characters'))
|
||||
elif len(ipv4_addr) > 20:
|
||||
raise forms.ValidationError(_('The ipv4 must not exceed 20 characters'))
|
||||
return ipv4_addr
|
||||
|
||||
def clean_ipv4_gw(self):
|
||||
ipv4_gw = self.cleaned_data['ipv4_gw']
|
||||
have_symbol = re.match('^[0-9.]+$', ipv4_gw)
|
||||
if not have_symbol:
|
||||
raise forms.ValidationError(_('The ipv4 gateway must not contain any special characters'))
|
||||
elif len(ipv4_gw) > 20:
|
||||
raise forms.ValidationError(_('The ipv4 gateway must not exceed 20 characters'))
|
||||
return ipv4_gw
|
||||
|
||||
def clean_ipv6_addr(self):
|
||||
ipv6_addr = self.cleaned_data['ipv6_addr']
|
||||
have_symbol = re.match('^[0-9a-f./:]+$', ipv6_addr)
|
||||
if not have_symbol:
|
||||
raise forms.ValidationError(_('The ipv6 must not contain any special characters'))
|
||||
elif len(ipv6_addr) > 100:
|
||||
raise forms.ValidationError(_('The ipv6 must not exceed 100 characters'))
|
||||
return ipv6_addr
|
||||
|
||||
def clean_ipv6_gw(self):
|
||||
ipv6_gw = self.cleaned_data['ipv6_gw']
|
||||
have_symbol = re.match('^[0-9.]+$', ipv6_gw)
|
||||
if not have_symbol:
|
||||
raise forms.ValidationError(_('The ipv6 gateway must not contain any special characters'))
|
||||
elif len(ipv6_gw) > 100:
|
||||
raise forms.ValidationError(_('The ipv6 gateway must not exceed 100 characters'))
|
||||
return ipv6_gw
|
||||
|
||||
def clean_name(self):
|
||||
name = self.cleaned_data['name']
|
||||
have_symbol = re.match('^[a-z0-9.]+$', name)
|
||||
if not have_symbol:
|
||||
raise forms.ValidationError(_('The interface must not contain any special characters'))
|
||||
elif len(name) > 10:
|
||||
raise forms.ValidationError(_('The interface must not exceed 10 characters'))
|
||||
return name
|
||||
|
||||
def clean_netdev(self):
|
||||
netdev = self.cleaned_data['netdev']
|
||||
have_symbol = re.match('^[a-z0-9.]+$', netdev)
|
||||
if not have_symbol:
|
||||
raise forms.ValidationError(_('The interface must not contain any special characters'))
|
||||
elif len(netdev) > 10:
|
||||
raise forms.ValidationError(_('The interface must not exceed 10 characters'))
|
||||
return netdev
|
||||
|
|
@ -1,3 +1,97 @@
|
|||
from django.shortcuts import render
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.core.urlresolvers import reverse
|
||||
from computes.models import Compute
|
||||
from interfaces.forms import AddInterface
|
||||
from vrtManager.interface import wvmInterface, wvmInterfaces
|
||||
from libvirt import libvirtError
|
||||
|
||||
# Create your views here.
|
||||
|
||||
def interfaces(request, compute_id):
|
||||
"""
|
||||
:param request:
|
||||
:return:
|
||||
"""
|
||||
|
||||
if not request.user.is_authenticated():
|
||||
return HttpResponseRedirect(reverse('index'))
|
||||
|
||||
errors = []
|
||||
ifaces_all = []
|
||||
compute = Compute.objects.get(id=compute_id)
|
||||
|
||||
try:
|
||||
conn = wvmInterfaces(compute.hostname,
|
||||
compute.login,
|
||||
compute.password,
|
||||
compute.type)
|
||||
ifaces = conn.get_ifaces()
|
||||
try:
|
||||
netdevs = conn.get_net_device()
|
||||
except:
|
||||
netdevs = ['eth0', 'eth1']
|
||||
|
||||
for iface in ifaces:
|
||||
ifaces_all.append(conn.get_iface_info(iface))
|
||||
|
||||
if request.method == 'POST':
|
||||
if 'create' in request.POST:
|
||||
form = AddInterface(request.POST)
|
||||
if form.is_valid():
|
||||
data = form.cleaned_data
|
||||
conn.create_iface(data['name'], data['itype'], data['start_mode'], data['netdev'],
|
||||
data['ipv4_type'], data['ipv4_addr'], data['ipv4_gw'],
|
||||
data['ipv6_type'], data['ipv6_addr'], data['ipv6_gw'],
|
||||
data['stp'], data['delay'])
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
conn.close()
|
||||
except libvirtError as err:
|
||||
errors.append(err)
|
||||
|
||||
return render(request, 'interfaces.html', locals())
|
||||
|
||||
|
||||
def interface(request, compute_id, iface):
|
||||
"""
|
||||
:param request:
|
||||
:return:
|
||||
"""
|
||||
|
||||
if not request.user.is_authenticated():
|
||||
return HttpResponseRedirect(reverse('index'))
|
||||
|
||||
errors = []
|
||||
ifaces_all = []
|
||||
compute = Compute.objects.get(id=compute_id)
|
||||
|
||||
try:
|
||||
conn = wvmInterface(compute.hostname,
|
||||
compute.login,
|
||||
compute.password,
|
||||
compute.type,
|
||||
iface)
|
||||
start_mode = conn.get_start_mode()
|
||||
state = conn.is_active()
|
||||
mac = conn.get_mac()
|
||||
itype = conn.get_type()
|
||||
ipv4 = conn.get_ipv4()
|
||||
ipv4_type = conn.get_ipv4_type()
|
||||
ipv6 = conn.get_ipv6()
|
||||
ipv6_type = conn.get_ipv6_type()
|
||||
bridge = conn.get_bridge()
|
||||
|
||||
if request.method == 'POST':
|
||||
if 'stop' in request.POST:
|
||||
conn.stop_iface()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
if 'start' in request.POST:
|
||||
conn.start_iface()
|
||||
return HttpResponseRedirect(request.get_full_path())
|
||||
if 'delete' in request.POST:
|
||||
conn.delete_iface()
|
||||
return HttpResponseRedirect(reverse('interfaces', args=[host_id]))
|
||||
conn.close()
|
||||
except libvirtError as err:
|
||||
errors.append(err)
|
||||
|
||||
return render(request, 'interface.html', locals())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue