Ejemplo n.º 1
0
def add(request):
    form = InstanceTaskForm()

    if request.method == "POST":
        form = InstanceTaskForm(request.POST)
        if form.is_valid():
            name = InstanceTask.get_random_name()
            (instancetask, created) = InstanceTask.objects.get_or_create(
                name=name,
                user=form.cleaned_data["user"],
                creator=request.user,
                vcpu=form.cleaned_data["vcpu"],
                memory=form.cleaned_data["memory"],
                disk=form.cleaned_data["disk"],
                storagepool=form.cleaned_data["storagepool"],
            )
            if created:
                instancetask.save()
            task = create_instance.delay(instancetask.name)
            instancetask.task_id = task.id
            instancetask.save()
            messages.add_message(
                request, persistent_messages.INFO, "Attempting to create Instance: %s" % (instancetask)
            )
            return redirect("/instance/")

    return render_to_response("instance/add.html", {"form": form}, context_instance=RequestContext(request))
Ejemplo n.º 2
0
def add(request):
  form = InstanceTaskForm()

  if request.method == 'POST':
    form = InstanceTaskForm(request.POST)
    if form.is_valid():
      # first check if we are not overallocating
      hypervisor = form.cleaned_data['storagepool'].hypervisor
      instances = Instance.objects.filter(volume__storagepool__hypervisor=hypervisor)
      allocated_memory = 0
      allocated_vcpus = 0
      allocated_capacity = 0
      for i in StoragePool.objects.filter(hypervisor=hypervisor): 
        allocated_capacity += i.allocated
      for i in instances:
        allocated_memory += i.memory.size
        allocated_vcpus += i.vcpu
      if allocated_memory + form.cleaned_data['memory'].size > hypervisor.maximum_memory.size:
        messages.add_message(request, persistent_messages.ERROR,
          'Unable to create instance, <a href="/hypervisor/%d/">Hypervisor</a> has insufficient Memory available to allocate' % (hypervisor.id))
      elif allocated_vcpus + form.cleaned_data['vcpu'] > hypervisor.maximum_vcpus:
        messages.add_message(request, persistent_messages.ERROR,
          'Unable to create instance, <a href="/hypervisor/%d/">Hypervisor</a> has insufficient VCPUs available to allocate' % (hypervisor.id))
      elif allocated_capacity + form.cleaned_data['capacity'].size > hypervisor.maximum_hdd.size:
        messages.add_message(request, persistent_messages.ERROR,
          'Unable to create instance, <a href="/hypervisor/%d/">Hypervisor</a> has insufficient Disk space available to allocate' % (hypervisor.id))
      else:
        # end
        name = InstanceTask.get_random_name()
        (instancetask, created) = InstanceTask.objects.get_or_create(
          name=name,
          user=form.cleaned_data['user'],
          creator=request.user,
          vcpu=form.cleaned_data['vcpu'],
          memory=form.cleaned_data['memory'],
          capacity=form.cleaned_data['capacity'],
          storagepool=form.cleaned_data['storagepool'],
          network=form.cleaned_data['network'],
        )
        if created: instancetask.save()
        task = create_instance.delay(instancetask.name)
        instancetask.task_id = task.id
        instancetask.save()
        messages.add_message(request, persistent_messages.INFO,
          'Attempting to create Instance: %s' % (instancetask))
        return redirect('/instance/')

  return render_to_response('instance/add.html', {
      'form': form,
    },
    context_instance=RequestContext(request))