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

added user disk quota limit

added quota warning in instance page
This commit is contained in:
Jan Krcmar 2016-03-31 13:12:52 +02:00
parent 6546fa2570
commit f8e681dbf4
7 changed files with 62 additions and 13 deletions

View file

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('accounts', '0005_userattributes_can_clone_instances'),
]
operations = [
migrations.AddField(
model_name='userattributes',
name='max_disk_size',
field=models.IntegerField(default=0),
),
]

View file

@ -27,6 +27,7 @@ class UserAttributes(models.Model):
max_instances = models.IntegerField(default=0) max_instances = models.IntegerField(default=0)
max_cpus = models.IntegerField(default=0) max_cpus = models.IntegerField(default=0)
max_memory = models.IntegerField(default=0) max_memory = models.IntegerField(default=0)
max_disk_size = models.IntegerField(default=0)
def __unicode__(self): def __unicode__(self):
return self.user.username return self.user.username

View file

@ -107,6 +107,12 @@
<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 }}">
</div> </div>
</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 }}">
</div>
</div>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="submit" class="pull-left btn btn-danger" name="delete"> <button type="submit" class="pull-left btn btn-danger" name="delete">

View file

@ -105,9 +105,11 @@ def accounts(request):
userattributes_max_instances = request.POST.get('userattributes_max_instances', 0) userattributes_max_instances = request.POST.get('userattributes_max_instances', 0)
userattributes_max_cpus = request.POST.get('userattributes_max_cpus', 0) userattributes_max_cpus = request.POST.get('userattributes_max_cpus', 0)
userattributes_max_memory = request.POST.get('userattributes_max_memory', 0) userattributes_max_memory = request.POST.get('userattributes_max_memory', 0)
userattributes_max_disk_size = request.POST.get('userattributes_max_disk_size', 0)
userattributes.max_instances = userattributes_max_instances if userattributes_max_instances else 0 userattributes.max_instances = userattributes_max_instances if userattributes_max_instances else 0
userattributes.max_cpus = userattributes_max_cpus if userattributes_max_cpus else 0 userattributes.max_cpus = userattributes_max_cpus if userattributes_max_cpus else 0
userattributes.max_memory = userattributes_max_memory if userattributes_max_memory else 0 userattributes.max_memory = userattributes_max_memory if userattributes_max_memory else 0
userattributes.max_disk_size = userattributes_max_disk_size if userattributes_max_disk_size else 0
userattributes.save() userattributes.save()
return HttpResponseRedirect(request.get_full_path()) return HttpResponseRedirect(request.get_full_path())
if 'block' in request.POST: if 'block' in request.POST:

View file

@ -39,6 +39,9 @@
{% endfor %} {% endfor %}
</tr> </tr>
</table> </table>
{% if user_quota_msg %}
<span class="label label-warning">{{ user_quota_msg|capfirst }} quota reached.</span>
{% endif %}
<hr> <hr>
</div> </div>

View file

@ -217,28 +217,41 @@ def instance(request, compute_id, vname):
index += 1 index += 1
return free_names return free_names
def check_user_quota(instance, cpu, memory): def check_user_quota(instance, cpu, memory, disk_size):
user_instances = UserInstance.objects.filter(user_id=request.user.id) user_instances = UserInstance.objects.filter(user_id=request.user.id, instance__is_template=False)
instance += len(user_instances) instance += len(user_instances)
for usr_inst in user_instances: for usr_inst in user_instances:
if connection_manager.host_is_up(usr_inst.instance.compute.type, if connection_manager.host_is_up(usr_inst.instance.compute.type,
usr_inst.instance.compute.hostname): usr_inst.instance.compute.hostname):
conn = wvmHostDetails(usr_inst.instance.compute, conn = wvmInstance(usr_inst.instance.compute,
usr_inst.instance.compute.login, usr_inst.instance.compute.login,
usr_inst.instance.compute.password, usr_inst.instance.compute.password,
usr_inst.instance.compute.type) usr_inst.instance.compute.type,
wvm_instance = conn.get_user_instances(usr_inst.instance.name) usr_inst.instance.name)
cpu += int(wvm_instance['vcpu']) cpu += int(conn.get_vcpu())
memory += int(wvm_instance['memory']) memory += int(conn.get_memory())
for disk in conn.get_disk_device():
disk_size += int(disk['size'])>>30
ua = request.user.userattributes ua = request.user.userattributes
msg = ""
if ua.max_instances > 0 and instance > ua.max_instances: if ua.max_instances > 0 and instance > ua.max_instances:
return "instance" msg = "instance"
if settings.QUOTA_DEBUG:
msg += " (%s > %s)" % (instance, ua.max_instances)
if ua.max_cpus > 0 and cpu > ua.max_cpus: if ua.max_cpus > 0 and cpu > ua.max_cpus:
return "cpu" msg = "cpu"
if settings.QUOTA_DEBUG:
msg += " (%s > %s)" % (cpu, ua.max_cpus)
if ua.max_memory > 0 and memory > ua.max_memory: if ua.max_memory > 0 and memory > ua.max_memory:
return "memory" msg = "memory"
return "" 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
try: try:
conn = wvmInstance(compute.hostname, conn = wvmInstance(compute.hostname,
@ -278,6 +291,7 @@ def instance(request, compute_id, vname):
clone_disks = show_clone_disk(disks, vname) clone_disks = show_clone_disk(disks, vname)
console_passwd = conn.get_console_passwd() console_passwd = conn.get_console_passwd()
clone_free_names = get_clone_free_names() clone_free_names = get_clone_free_names()
user_quota_msg = check_user_quota(0, 0, 0, 0)
try: try:
instance = Instance.objects.get(compute_id=compute_id, name=vname) instance = Instance.objects.get(compute_id=compute_id, name=vname)
@ -397,7 +411,9 @@ def instance(request, compute_id, vname):
if input_disk_size > disk['size']+(64<<20): if input_disk_size > disk['size']+(64<<20):
disk['size_new'] = input_disk_size disk['size_new'] = input_disk_size
disks_new.append(disk) disks_new.append(disk)
quota_msg = check_user_quota(0, int(new_vcpu)-vcpu, int(new_memory)-memory) 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)
if not request.user.is_superuser and quota_msg: if not request.user.is_superuser and quota_msg:
msg = _("User %s quota reached, cannot resize '%s'!" % (quota_msg, instance.name)) msg = _("User %s quota reached, cannot resize '%s'!" % (quota_msg, instance.name))
error_messages.append(msg) error_messages.append(msg)
@ -569,7 +585,8 @@ def instance(request, compute_id, vname):
clone_data = {} clone_data = {}
clone_data['name'] = request.POST.get('name', '') clone_data['name'] = request.POST.get('name', '')
quota_msg = check_user_quota(1, conn.get_vcpu(), conn.get_memory()) disk_sum = sum([disk['size']>>30 for disk in disks])
quota_msg = check_user_quota(1, vcpu, memory, disk_sum)
check_instance = Instance.objects.filter(name=clone_data['name']) check_instance = Instance.objects.filter(name=clone_data['name'])
if not request.user.is_superuser and quota_msg: if not request.user.is_superuser and quota_msg:

View file

@ -115,3 +115,4 @@ LIBVIRT_KEEPALIVE_COUNT = 5
ALLOW_INSTANCE_MULTIPLE_OWNER = True ALLOW_INSTANCE_MULTIPLE_OWNER = True
CLONE_INSTANCE_DEFAULT_PREFIX = 'ourea' CLONE_INSTANCE_DEFAULT_PREFIX = 'ourea'
LOGS_PER_PAGE = 100 LOGS_PER_PAGE = 100
QUOTA_DEBUG = True