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

instances.views.check_user_quota correct cpu/memory sum

instance resize respects quota
This commit is contained in:
Jan Krcmar 2016-03-23 13:25:28 +01:00
parent 27030113fb
commit 8148620025

View file

@ -217,18 +217,26 @@ def instance(request, compute_id, vname):
index += 1 index += 1
return free_names return free_names
def check_user_quota(): def check_user_quota(instance, cpu, memory):
# TODO: count cpus, memory correctly user_instances = UserInstance.objects.filter(user_id=request.user.id)
userinstances = UserInstance.objects.filter(user__id=request.user.id) instance += len(user_instances)
instances_count = len(userinstances) for usr_inst in user_instances:
cpus_count = instances_count if connection_manager.host_is_up(usr_inst.instance.compute.type,
memory_count = instances_count * 2048 usr_inst.instance.compute.hostname):
conn = wvmHostDetails(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'])/1024
ua = request.user.userattributes ua = request.user.userattributes
if ua.max_instances > 0 and instances_count > ua.max_instances: if ua.max_instances > 0 and instance > ua.max_instances:
return "instance" return "instance"
if ua.max_cpus > 0 and cpus_count > ua.max_cpus: if ua.max_cpus > 0 and cpu > ua.max_cpus:
return "cpu" return "cpu"
if ua.max_memory > 0 and memory_count > ua.max_memory: if ua.max_memory > 0 and memory > ua.max_memory:
return "memory" return "memory"
return "" return ""
@ -393,10 +401,15 @@ 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)
conn.resize(cur_memory, memory, cur_vcpu, vcpu, disks_new) quota_msg = check_user_quota(0, vcpu, memory)
msg = _("Resize") if not request.user.is_superuser and quota_msg:
addlogmsg(request.user.username, instance.name, msg) msg = _("User %s quota reached, cannot resize '%s'!" % (quota_msg, instance.name))
return HttpResponseRedirect(request.get_full_path() + '#resize') error_messages.append(msg)
else:
conn.resize(cur_memory, memory, cur_vcpu, vcpu, disks_new)
msg = _("Resize")
addlogmsg(request.user.username, instance.name, msg)
return HttpResponseRedirect(request.get_full_path() + '#resize')
if 'umount_iso' in request.POST: if 'umount_iso' in request.POST:
image = request.POST.get('path', '') image = request.POST.get('path', '')
@ -556,7 +569,7 @@ 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() quota_msg = check_user_quota(1, conn.get_vcpu(), conn.get_memory())
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: