1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-24 23:25:24 +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
return free_names
def check_user_quota():
# TODO: count cpus, memory correctly
userinstances = UserInstance.objects.filter(user__id=request.user.id)
instances_count = len(userinstances)
cpus_count = instances_count
memory_count = instances_count * 2048
def check_user_quota(instance, cpu, memory):
user_instances = UserInstance.objects.filter(user_id=request.user.id)
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,
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
if ua.max_instances > 0 and instances_count > ua.max_instances:
if ua.max_instances > 0 and instance > ua.max_instances:
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"
if ua.max_memory > 0 and memory_count > ua.max_memory:
if ua.max_memory > 0 and memory > ua.max_memory:
return "memory"
return ""
@ -393,6 +401,11 @@ 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, vcpu, memory)
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)
else:
conn.resize(cur_memory, memory, cur_vcpu, vcpu, disks_new)
msg = _("Resize")
addlogmsg(request.user.username, instance.name, msg)
@ -556,7 +569,7 @@ def instance(request, compute_id, vname):
clone_data = {}
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'])
if not request.user.is_superuser and quota_msg: