1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-23 22:55:23 +00:00

Libxml2 converted to lxml, django framework upgrade to 1.11.x and some bug fixes

This commit is contained in:
catborise 2018-06-27 10:20:51 +03:00
parent beea57189c
commit ace30ca952
16 changed files with 265 additions and 106 deletions

1
.gitignore vendored
View file

@ -9,3 +9,4 @@ console/cert.pem*
tags
dhcpd.*
webvirtcloud/settings.py
*migrations/*

View file

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-06-25 12:36
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0009_auto_20171026_0805'),
]
operations = [
migrations.AlterField(
model_name='userattributes',
name='max_cpus',
field=models.IntegerField(default=1, help_text=b'-1 for unlimited. Any integer value'),
),
migrations.AlterField(
model_name='userattributes',
name='max_disk_size',
field=models.IntegerField(default=20, help_text=b'-1 for unlimited. Any integer value'),
),
migrations.AlterField(
model_name='userattributes',
name='max_instances',
field=models.IntegerField(default=1, help_text=b'-1 for unlimited. Any integer value'),
),
migrations.AlterField(
model_name='userattributes',
name='max_memory',
field=models.IntegerField(default=2048, help_text=b'-1 for unlimited. Any integer value'),
),
]

View file

@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-06-25 13:13
from __future__ import unicode_literals
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0010_auto_20180625_1236'),
]
operations = [
migrations.AlterField(
model_name='userattributes',
name='max_cpus',
field=models.IntegerField(default=1, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.MinValueValidator(-1)]),
),
migrations.AlterField(
model_name='userattributes',
name='max_disk_size',
field=models.IntegerField(default=20, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.MinValueValidator(-1)]),
),
migrations.AlterField(
model_name='userattributes',
name='max_instances',
field=models.IntegerField(default=1, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.MinValueValidator(-1)]),
),
migrations.AlterField(
model_name='userattributes',
name='max_memory',
field=models.IntegerField(default=2048, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.MinValueValidator(-1)]),
),
]

View file

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-06-25 13:31
from __future__ import unicode_literals
import django.core.validators
from django.db import migrations, models
import re
class Migration(migrations.Migration):
dependencies = [
('accounts', '0011_auto_20180625_1313'),
]
operations = [
migrations.AlterField(
model_name='userattributes',
name='max_cpus',
field=models.IntegerField(default=1, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.RegexValidator(re.compile('^-?\\d+\\Z'), code='invalid', message='Enter a valid integer.')]),
),
migrations.AlterField(
model_name='userattributes',
name='max_disk_size',
field=models.IntegerField(default=20, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.RegexValidator(re.compile('^-?\\d+\\Z'), code='invalid', message='Enter a valid integer.')]),
),
migrations.AlterField(
model_name='userattributes',
name='max_instances',
field=models.IntegerField(default=1, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.RegexValidator(re.compile('^-?\\d+\\Z'), code='invalid', message='Enter a valid integer.')]),
),
migrations.AlterField(
model_name='userattributes',
name='max_memory',
field=models.IntegerField(default=2048, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.RegexValidator(re.compile('^-?\\d+\\Z'), code='invalid', message='Enter a valid integer.')]),
),
]

View file

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-06-25 13:58
from __future__ import unicode_literals
import django.core.validators
from django.db import migrations, models
import re
class Migration(migrations.Migration):
dependencies = [
('accounts', '0012_auto_20180625_1331'),
]
operations = [
migrations.AlterField(
model_name='userattributes',
name='max_cpus',
field=models.IntegerField(default=1, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.RegexValidator(re.compile('^-?\\d+\\Z'), code='invalid', message='Enter a valid integer.'), django.core.validators.MinValueValidator(-1)]),
),
migrations.AlterField(
model_name='userattributes',
name='max_disk_size',
field=models.IntegerField(default=20, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.RegexValidator(re.compile('^-?\\d+\\Z'), code='invalid', message='Enter a valid integer.'), django.core.validators.MinValueValidator(-1)]),
),
migrations.AlterField(
model_name='userattributes',
name='max_instances',
field=models.IntegerField(default=1, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.RegexValidator(re.compile('^-?\\d+\\Z'), code='invalid', message='Enter a valid integer.'), django.core.validators.MinValueValidator(-1)]),
),
migrations.AlterField(
model_name='userattributes',
name='max_memory',
field=models.IntegerField(default=2048, help_text=b'-1 for unlimited. Any integer value', validators=[django.core.validators.RegexValidator(re.compile('^-?\\d+\\Z'), code='invalid', message='Enter a valid integer.'), django.core.validators.MinValueValidator(-1)]),
),
]

View file

@ -2,6 +2,7 @@ from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from instances.models import Instance
from django.core.validators import integer_validator, MinValueValidator
class UserInstance(models.Model):
@ -26,10 +27,10 @@ class UserSSHKey(models.Model):
class UserAttributes(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
can_clone_instances = models.BooleanField(default=True)
max_instances = models.IntegerField(default=1)
max_cpus = models.IntegerField(default=1)
max_memory = models.IntegerField(default=2048)
max_disk_size = models.IntegerField(default=20)
max_instances = models.IntegerField(default=1, help_text="-1 for unlimited. Any integer value", validators=[integer_validator,MinValueValidator(-1),])
max_cpus = models.IntegerField(default=1, help_text="-1 for unlimited. Any integer value", validators=[integer_validator,MinValueValidator(-1)])
max_memory = models.IntegerField(default=2048, help_text="-1 for unlimited. Any integer value", validators=[integer_validator,MinValueValidator(-1)])
max_disk_size = models.IntegerField(default=20, help_text="-1 for unlimited. Any integer value", validators=[integer_validator,MinValueValidator(-1)])
@staticmethod
def create_missing_userattributes(user):

View file

@ -92,25 +92,25 @@
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Max instances" %}</label>
<div class="col-sm-6">
<input type="text" name="userattributes_max_instances" class="form-control" value="{{ user.userattributes.max_instances }}">
<input type="text" name="userattributes_max_instances" class="form-control" value="{{ user.userattributes.max_instances}}" required="True" >
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Max cpus" %}</label>
<div class="col-sm-6">
<input type="text" name="userattributes_max_cpus" class="form-control" value="{{ user.userattributes.max_cpus }}">
<input type="text" name="userattributes_max_cpus" class="form-control" value="{{ user.userattributes.max_cpus }}" required="True">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Max memory (MB)" %}</label>
<div class="col-sm-6">
<input type="text" name="userattributes_max_memory" class="form-control" value="{{ user.userattributes.max_memory }}">
<input type="text" name="userattributes_max_memory" class="form-control" value="{{ user.userattributes.max_memory}}" required="True">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Max disk size (GB)" %}</label>
<div class="col-sm-6">
<input type="text" name="userattributes_max_disk_size" class="form-control" value="{{ user.userattributes.max_disk_size }}">
<input type="text" name="userattributes_max_disk_size" class="form-control" value="{{ user.userattributes.max_disk_size }}" required="True">
</div>
</div>
</div>

View file

@ -1,12 +1,11 @@
from django.conf.urls import url
from django.contrib import auth
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
url(r'^login/$', 'django.contrib.auth.views.login',
{'template_name': 'login.html'}, name='login'),
url(r'^logout/$', 'django.contrib.auth.views.logout',
{'template_name': 'logout.html'}, name='logout'),
url(r'^profile/$', views.profile, name='profile'),
url(r'^$', views.accounts, name='accounts'),
url(r'^login/$', auth_views.LoginView.as_view(template_name='login.html'), name='login'),
url(r'^logout/$', auth_views.LogoutView.as_view(template_name='logout.html'), name='logout'),
url(r'^profile/$', views.profile, name='profile'), url(r'^$', views.accounts, name='accounts'),
url(r'^profile/(?P<user_id>[0-9]+)/$', views.account, name='account'),
]

View file

@ -8,6 +8,8 @@ from accounts.models import *
from instances.models import Instance
from accounts.forms import UserAddForm
from django.conf import settings
from django.core.validators import ValidationError
@login_required
@ -92,22 +94,31 @@ def accounts(request):
UserAttributes.configure_user(new_user)
return HttpResponseRedirect(request.get_full_path())
if 'edit' in request.POST:
CHECKBOX_MAPPING = {'on': True, 'off': False, }
user_id = request.POST.get('user_id', '')
user_pass = request.POST.get('user_pass', '')
user_edit = User.objects.get(id=user_id)
user_edit.set_password(user_pass)
user_edit.is_staff = request.POST.get('user_is_staff', False)
user_edit.is_superuser = request.POST.get('user_is_superuser', False)
if user_pass != '': user_edit.set_password(user_pass)
user_edit.is_staff = CHECKBOX_MAPPING.get(request.POST.get('user_is_staff', 'off'))
user_edit.is_superuser = CHECKBOX_MAPPING.get(request.POST.get('user_is_superuser', 'off'))
user_edit.save()
userattributes = user_edit.userattributes
userattributes.can_clone_instances = request.POST.get('userattributes_can_clone_instances', False)
userattributes.max_instances = request.POST.get('userattributes_max_instances', 0)
userattributes.max_cpus = request.POST.get('userattributes_max_cpus', 0)
userattributes.max_memory = request.POST.get('userattributes_max_memory', 0)
userattributes.max_disk_size = request.POST.get('userattributes_max_disk_size', 0)
userattributes.save()
return HttpResponseRedirect(request.get_full_path())
UserAttributes.create_missing_userattributes(user_edit)
user_edit.userattributes.can_clone_instances = CHECKBOX_MAPPING.get(request.POST.get('userattributes_can_clone_instances', 'off'))
user_edit.userattributes.max_instances = request.POST.get('userattributes_max_instances', 0)
user_edit.userattributes.max_cpus = request.POST.get('userattributes_max_cpus', 0)
user_edit.userattributes.max_memory = request.POST.get('userattributes_max_memory', 0)
user_edit.userattributes.max_disk_size = request.POST.get('userattributes_max_disk_size', 0)
try:
user_edit.userattributes.clean_fields()
except ValidationError as exc:
error_messages.append(exc)
else:
user_edit.userattributes.save()
return HttpResponseRedirect(request.get_full_path())
if 'block' in request.POST:
user_id = request.POST.get('user_id', '')
user_block = User.objects.get(id=user_id)

View file

@ -123,7 +123,7 @@ def computes(request):
compute_edit.hostname = data['hostname']
compute_edit.login = data['login']
compute_edit.password = data['password']
compute.edit_details = data['details']
#compute_edit.details = data['details']
compute_edit.save()
return HttpResponseRedirect(request.get_full_path())
else:

View file

@ -444,8 +444,8 @@ class wvmConnect(object):
def get_net_device(self):
netdevice = []
def get_info(ctx):
dev_type = util.get_xpath(ctx, '/device/capability/@type')
interface = util.get_xpath(ctx, '/device/capability/interface')
dev_type = util.get_xpath('/device/capability/@type')
interface = util.get_xpath('/device/capability/interface')
return (dev_type, interface)
for dev in self.wvm.listAllDevices(0):
xml = dev.XMLDesc(0)
@ -456,17 +456,17 @@ class wvmConnect(object):
def get_host_instances(self):
vname = {}
def get_info(ctx):
mem = util.get_xpath(ctx, "/domain/currentMemory")
def get_info(doc):
mem = util.get_xpath(doc, "/domain/currentMemory")
mem = int(mem) / 1024
cur_vcpu = util.get_xpath(ctx, "/domain/vcpu/@current")
cur_vcpu = util.get_xpath(doc, "/domain/vcpu/@current")
if cur_vcpu:
vcpu = cur_vcpu
else:
vcpu = util.get_xpath(ctx, "/domain/vcpu")
title = util.get_xpath(ctx, "/domain/title")
vcpu = util.get_xpath(doc, "/domain/vcpu")
title = util.get_xpath(doc, "/domain/title")
title = title if title else ''
description = util.get_xpath(ctx, "/domain/description")
description = util.get_xpath(doc, "/domain/description")
description = description if description else ''
return (mem, vcpu, title, description)
for name in self.get_instances():

View file

@ -3,11 +3,11 @@ from vrtManager.connection import wvmConnect
from vrtManager.util import get_xml_path
def cpu_version(ctx):
for info in ctx.xpathEval('/sysinfo/processor/entry'):
elem = info.xpathEval('@name')[0].content
def cpu_version(doc):
for info in doc.xpath('/sysinfo/processor/entry'):
elem = info.xpath('@name')[0]
if elem == 'version':
return info.content
return info
return 'Unknown'

View file

@ -203,10 +203,10 @@ class wvmInstance(wvmConnect):
def get_net_device(self):
def get_mac_ipaddr(net, mac_host):
def fixed(ctx):
for net in ctx.xpathEval('/network/ip/dhcp/host'):
mac = net.xpathEval('@mac')[0].content
host = net.xpathEval('@ip')[0].content
def fixed(doc):
for net in doc.xpath('/network/ip/dhcp/host'):
mac = net.xpath('@mac')[0]
host = net.xpath('@ip')[0]
if mac == mac_host:
return host
return None
@ -215,9 +215,9 @@ class wvmInstance(wvmConnect):
def networks(ctx):
result = []
for net in ctx.xpathEval('/domain/devices/interface'):
mac_host = net.xpathEval('mac/@address')[0].content
nic_host = net.xpathEval('source/@network|source/@bridge|source/@dev|target/@dev')[0].content
for net in ctx.xpath('/domain/devices/interface'):
mac_host = net.xpath('mac/@address')[0]
nic_host = net.xpath('source/@network|source/@bridge|source/@dev|target/@dev')[0]
try:
net = self.get_network(nic_host)
ip = get_mac_ipaddr(net, mac_host)
@ -229,7 +229,7 @@ class wvmInstance(wvmConnect):
return util.get_xml_path(self._XMLDesc(0), func=networks)
def get_disk_device(self):
def disks(ctx):
def disks(doc):
result = []
dev = None
volume = None
@ -237,13 +237,14 @@ class wvmInstance(wvmConnect):
src_fl = None
disk_format = None
disk_size = None
for disk in ctx.xpathEval('/domain/devices/disk'):
device = disk.xpathEval('@device')[0].content
for disk in doc.xpath('/domain/devices/disk'):
device = disk.xpath('@device')[0]
if device == 'disk':
try:
dev = disk.xpathEval('target/@dev')[0].content
src_fl = disk.xpathEval('source/@file|source/@dev|source/@name|source/@volume')[0].content
disk_format = disk.xpathEval('driver/@type')[0].content
dev = disk.xpath('target/@dev')[0]
src_fl = disk.xpath('source/@file|source/@dev|source/@name|source/@volume')[0]
disk_format = disk.xpath('driver/@type')[0]
try:
vol = self.get_volume_by_path(src_fl)
volume = vol.name()
@ -263,19 +264,19 @@ class wvmInstance(wvmConnect):
return util.get_xml_path(self._XMLDesc(0), func=disks)
def get_media_device(self):
def disks(ctx):
def disks(doc):
result = []
dev = None
volume = None
storage = None
src_fl = None
for media in ctx.xpathEval('/domain/devices/disk'):
device = media.xpathEval('@device')[0].content
for media in doc.xpath('/domain/devices/disk'):
device = media.xpath('@device')[0]
if device == 'cdrom':
try:
dev = media.xpathEval('target/@dev')[0].content
dev = media.xpath('target/@dev')
try:
src_fl = media.xpathEval('source/@file')[0].content
src_fl = media.xpath('source/@file')
vol = self.get_volume_by_path(src_fl)
volume = vol.name()
stg = vol.storagePoolLookupByVolume()
@ -487,8 +488,7 @@ class wvmInstance(wvmConnect):
return socket
def get_console_type(self):
console_type = util.get_xml_path(self._XMLDesc(0),
"/domain/devices/graphics/@type")
console_type = util.get_xml_path(self._XMLDesc(0),"/domain/devices/graphics/@type")
return console_type
def set_console_type(self, console_type):

View file

@ -166,11 +166,11 @@ class wvmNetwork(wvmConnect):
return bool(util.get_xml_path(xml, "/network/ip/dhcp/bootp/@file"))
def get_mac_ipaddr(self):
def network(ctx):
def network(doc):
result = []
for net in ctx.xpathEval('/network/ip/dhcp/host'):
host = net.xpathEval('@ip')[0].content
mac = net.xpathEval('@mac')[0].content
for net in doc.xpath('/network/ip/dhcp/host'):
host = net.xpath('@ip')[0].text
mac = net.xpathEval('@mac')[0].text
result.append({'host': host, 'mac': mac})
return result

View file

@ -1,5 +1,5 @@
import random
import libxml2
import lxml.etree as etree
import libvirt
import string
@ -86,41 +86,43 @@ def get_xml_path(xml, path=None, func=None):
of a passed function (receives xpathContext as its only arg)
"""
doc = None
ctx = None
#ctx = None
result = None
try:
doc = libxml2.parseDoc(xml)
ctx = doc.xpathNewContext()
#try:
doc = etree.fromstring(xml)
#ctx = doc.xpathNewContext()
if path:
result = get_xpath(doc, path)
if path:
result = get_xpath(ctx, path)
elif func:
result = func(doc)
elif func:
result = func(ctx)
else:
raise ValueError("'path' or 'func' is required.")
finally:
if doc:
doc.freeDoc()
if ctx:
ctx.xpathFreeContext()
else:
raise ValueError("'path' or 'func' is required.")
#finally:
#if doc:
# doc.freeDoc()
#if ctx:
# ctx.xpathFreeContext()
return result
def get_xpath(ctx, path):
def get_xpath(doc, path):
result = None
ret = ctx.xpathEval(path)
ret = doc.xpath(path)
if ret is not None:
if type(ret) == list:
if len(ret) >= 1:
result = ret[0].content
if hasattr(ret[0],'text'):
result = ret[0].text
else:
result = ret[0]
else:
result = ret
return result
return result
def pretty_mem(val):
val = int(val)

View file

@ -1,9 +1,17 @@
from django.conf.urls import patterns, include, url
from django.conf.urls import include, url
from instances.views import instances, instance, index
from storages.views import storages, storage
from networks.views import networks, network
from secrets.views import secrets
from create.views import create_instance
from interfaces.views import interfaces, interface
from console.views import console
# from django.contrib import admin
urlpatterns = patterns('',
url(r'^$', 'instances.views.index', name='index'),
url(r'^instances/$', 'instances.views.instances', name='instances'),
urlpatterns = [
url(r'^$', index, name='index'),
url(r'^instances/$', instances, name='instances'),
url(r'^instance/', include('instances.urls')),
url(r'^accounts/', include('accounts.urls')),
@ -11,23 +19,15 @@ urlpatterns = patterns('',
url(r'^logs/', include('logs.urls')),
url(r'^datasource/', include('datasource.urls')),
url(r'^compute/(?P<compute_id>[0-9]+)/storages/$',
'storages.views.storages', name='storages'),
url(r'^compute/(?P<compute_id>[0-9]+)/storage/(?P<pool>[\w\-\.\/]+)/$',
'storages.views.storage', name='storage'),
url(r'^compute/(?P<compute_id>[0-9]+)/networks/$',
'networks.views.networks', name='networks'),
url(r'^compute/(?P<compute_id>[0-9]+)/network/(?P<pool>[\w\-\.]+)/$',
'networks.views.network', name='network'),
url(r'^compute/(?P<compute_id>[0-9]+)/interfaces/$',
'interfaces.views.interfaces', name='interfaces'),
url(r'^compute/(?P<compute_id>[0-9]+)/interface/(?P<iface>[\w\-\.\:]+)/$',
'interfaces.views.interface', name='interface'),
url(r'^compute/(?P<compute_id>[0-9]+)/secrets/$',
'secrets.views.secrets', name='secrets'),
url(r'^compute/(?P<compute_id>[0-9]+)/create/$',
'create.views.create_instance', name='create_instance'),
url(r'^compute/(?P<compute_id>[0-9]+)/storages/$', storages, name='storages'),
url(r'^compute/(?P<compute_id>[0-9]+)/storage/(?P<pool>[\w\-\.\/]+)/$', storage, name='storage'),
url(r'^compute/(?P<compute_id>[0-9]+)/networks/$', networks, name='networks'),
url(r'^compute/(?P<compute_id>[0-9]+)/network/(?P<pool>[\w\-\.]+)/$', network, name='network'),
url(r'^compute/(?P<compute_id>[0-9]+)/interfaces/$', interfaces, name='interfaces'),
url(r'^compute/(?P<compute_id>[0-9]+)/interface/(?P<iface>[\w\-\.\:]+)/$', interface, name='interface'),
url(r'^compute/(?P<compute_id>[0-9]+)/secrets/$', secrets, name='secrets'),
url(r'^compute/(?P<compute_id>[0-9]+)/create/$', create_instance, name='create_instance'),
url(r'^console/$', 'console.views.console', name='console'),
url(r'^console/$', console, name='console'),
# (r'^admin/', include(admin.site.urls)),
)
]