diff --git a/accounts/migrations/0006_userattributes_max_disk_size.py b/accounts/migrations/0006_userattributes_max_disk_size.py new file mode 100644 index 0000000..3d21f5f --- /dev/null +++ b/accounts/migrations/0006_userattributes_max_disk_size.py @@ -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), + ), + ] diff --git a/accounts/models.py b/accounts/models.py index 16f5f6e..5e829f0 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -27,6 +27,7 @@ class UserAttributes(models.Model): max_instances = models.IntegerField(default=0) max_cpus = models.IntegerField(default=0) max_memory = models.IntegerField(default=0) + max_disk_size = models.IntegerField(default=0) def __unicode__(self): return self.user.username diff --git a/accounts/templates/accounts.html b/accounts/templates/accounts.html index 6ad20b8..4fdfc8a 100644 --- a/accounts/templates/accounts.html +++ b/accounts/templates/accounts.html @@ -107,6 +107,12 @@ +
+ +
+ +
+
diff --git a/instances/views.py b/instances/views.py index 15c0689..62b390f 100644 --- a/instances/views.py +++ b/instances/views.py @@ -217,28 +217,41 @@ def instance(request, compute_id, vname): index += 1 return free_names - def check_user_quota(instance, cpu, memory): - user_instances = UserInstance.objects.filter(user_id=request.user.id) + def check_user_quota(instance, cpu, memory, disk_size): + user_instances = UserInstance.objects.filter(user_id=request.user.id, instance__is_template=False) instance += len(user_instances) for usr_inst in user_instances: if connection_manager.host_is_up(usr_inst.instance.compute.type, 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.password, - usr_inst.instance.compute.type) - wvm_instance = conn.get_user_instances(usr_inst.instance.name) - cpu += int(wvm_instance['vcpu']) - memory += int(wvm_instance['memory']) + usr_inst.instance.compute.type, + usr_inst.instance.name) + cpu += int(conn.get_vcpu()) + memory += int(conn.get_memory()) + for disk in conn.get_disk_device(): + disk_size += int(disk['size'])>>30 ua = request.user.userattributes + msg = "" 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: - 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: - return "memory" - return "" + msg = "memory" + 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: conn = wvmInstance(compute.hostname, @@ -278,6 +291,7 @@ def instance(request, compute_id, vname): clone_disks = show_clone_disk(disks, vname) console_passwd = conn.get_console_passwd() clone_free_names = get_clone_free_names() + user_quota_msg = check_user_quota(0, 0, 0, 0) try: 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): disk['size_new'] = input_disk_size 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: msg = _("User %s quota reached, cannot resize '%s'!" % (quota_msg, instance.name)) error_messages.append(msg) @@ -569,7 +585,8 @@ def instance(request, compute_id, vname): clone_data = {} 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']) if not request.user.is_superuser and quota_msg: diff --git a/webvirtcloud/settings.py b/webvirtcloud/settings.py index 052be0c..bcd3920 100644 --- a/webvirtcloud/settings.py +++ b/webvirtcloud/settings.py @@ -115,3 +115,4 @@ LIBVIRT_KEEPALIVE_COUNT = 5 ALLOW_INSTANCE_MULTIPLE_OWNER = True CLONE_INSTANCE_DEFAULT_PREFIX = 'ourea' LOGS_PER_PAGE = 100 +QUOTA_DEBUG = True