Commit d9e3534b by Őry Máté

one: implement quota on vm_add

parent 658889da
...@@ -179,10 +179,19 @@ class Share(models.Model): ...@@ -179,10 +179,19 @@ class Share(models.Model):
owner = models.ForeignKey(User, null=True, blank=True) owner = models.ForeignKey(User, null=True, blank=True)
def get_running_or_stopped(self): def get_running_or_stopped(self, user=None):
return Instance.objects.all().exclude(state='DONE').filter(share=self).count() running = Instance.objects.all().exclude(state='DONE').filter(share=self)
def get_running(self): if user:
return Instance.objects.all().exclude(state='DONE').exclude(state='STOPPED').filter(share=self).count() return running.filter(owner=user).count()
else:
return running.count()
def get_running(self, user=None):
running = Instance.objects.all().exclude(state='DONE').exclude(state='STOPPED').filter(share=self)
if user:
return running.filter(owner=user).count()
else:
return running.count()
def get_instance_pc(self): def get_instance_pc(self):
return float(self.get_running()) / self.instance_limit * 100 return float(self.get_running()) / self.instance_limit * 100
""" """
......
...@@ -199,9 +199,48 @@ def vm_saveas(request, vmid): ...@@ -199,9 +199,48 @@ def vm_saveas(request, vmid):
messages.success(request, _("Template is being saved...")) messages.success(request, _("Template is being saved..."))
return redirect(inst) return redirect(inst)
def vm_new_ajax(request, template ): def vm_new_ajax(request, template):
return vm_new(request, template, redir=False) return vm_new(request, template, redir=False)
def _redirect_or_201(path, redir):
if redir:
return redirect(path)
else:
response = HttpResponse("Created", status=201)
response['Location'] = path
return response
def _template_for_save(base, request):
if base.owner != request.user and not base.public and not request.user.is_superuser:
raise PermissionDenied()
name = request.POST['name']
t = Template.objects.create(name=name, disk=base.disk, instance_type_id=request.POST['size'], network=base.network, owner=request.user)
t.access_type = base.access_type
t.description = request.POST['description']
t.system = base.system
t.save()
return t
def _check_quota(request, template, share):
"""
Returns if the given request is permitted to run the new vm.
"""
det = UserCloudDetails.objects.get(user=request.user)
if det.get_weighted_instance_count() + template.instance_type.credit >= det.instance_quota:
messages.error(request, _('You do not have any free quota. You can not launch this until you stop an other instance.'))
return False
if share:
if share.get_running() + 1 > share.instance_limit:
messages.error(request, _('The share does not have any free quota. You can not launch this until someone stops an instance.'))
return False
elif share.get_running_or_stopped(request.user) + 1 > share.per_user_limit:
messages.error(request, _('You do not have any free quota for this share. You can not launch this until you stop an other instance.'))
return False
if not share.group.members.filter(user=request.user) and not share.group.owners.filter(user=request.user):
messages.error(request, _('You are not a member of the share group.'))
return False
return True
@require_POST @require_POST
@login_required @login_required
def vm_new(request, template=None, share=None, redir=True): def vm_new(request, template=None, share=None, redir=True):
...@@ -212,43 +251,33 @@ def vm_new(request, template=None, share=None, redir=True): ...@@ -212,43 +251,33 @@ def vm_new(request, template=None, share=None, redir=True):
else: else:
share = get_object_or_404(Share, pk=share) share = get_object_or_404(Share, pk=share)
base = share.template base = share.template
if "name" in request.POST: if "name" in request.POST:
if base.owner != request.user and not base.public and not request.user.is_superuser: base = _template_for_save(base, request)
raise PermissionDenied()
name = request.POST['name']
t = Template.objects.create(name=name, disk=base.disk, instance_type_id=request.POST['size'], network=base.network, owner=request.user)
t.access_type = base.access_type
t.description = request.POST['description']
t.system = base.system
t.save()
base = t
extra = "<RECONTEXT>YES</RECONTEXT>" extra = "<RECONTEXT>YES</RECONTEXT>"
try: go = _check_quota(request, base, share)
#Gány quota
if (share == None or (share != None and share.get_running() < share.instance_limit)) or extra: if not share and not base.public and base.owner != request.user:
time_of_suspend = None messages.error(request, _('You have no permission to try this instance without a share. Launch a new instance through a share.'))
time_of_delete = None go = False
try: type = share.type if share else 'LAB'
TYPES[share.type]['suspend'] TYPES[type]['suspend']
time_of_suspend = TYPES[share.type]['suspend']+datetime.now() time_of_suspend = TYPES[type]['suspend']+datetime.now()
TYPES[share.type]['delete'] TYPES[type]['delete']
time_of_delete = TYPES[share.type]['delete']+datetime.now() time_of_delete = TYPES[type]['delete']+datetime.now()
except: inst = None
pass if go:
i = Instance.submit(base, request.user, extra=extra, share=share) try:
i.time_of_suspend = time_of_suspend inst = Instance.submit(base, request.user, extra=extra, share=share)
i.time_of_delete = time_of_delete except Exception as e:
i.save() logger.error('Failed to create virtual machine.' + unicode(e))
if redir: messages.error(request, _('Failed to create virtual machine.'))
return redirect(i) inst = None
else: if inst:
response = HttpResponse("Created", status=201) inst.time_of_suspend = time_of_suspend
response['Location'] = i.get_absolute_url() inst.time_of_delete = time_of_delete
return response inst.save()
except Exception as e: return _redirect_or_201(inst.get_absolute_url() if inst else '/', redir)
logger.error('Failed to create virtual machine.' + unicode(e))
messages.error(request, _('Failed to create virtual machine.')+unicode(e))
return redirect('/')
class VmListView(ListView): class VmListView(ListView):
context_object_name = 'instances' context_object_name = 'instances'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment