Ejemplo n.º 1
0
    def assign_instance(self, customer, product, plan):

        # check for first available instance
        instance = SaasInstance.objects.filter(product=product).filter(
            status=SaasInstance().AVAILABLE).first()
        if not instance:
            # send message to administrator
            self.notify_administrators(
                _("Missing free instance for %s") % (product.name, ),
                _("Assigning of %s instance for customer %d failed") %
                (product.name, customer.id))

            # TODO if no instance is available, then add the user to the waiting list

            return False
        else:
            # assign a free instance
            # there might be an unconfirmed contract already
            contract = LogicContracts().get_contract(customer, product)
            if not contract:
                contract = LogicContracts().get_new_contract(
                    customer, product, plan)
            contract.instance = instance
            contract.plan = plan
            contract.is_confirmed = True
            contract.save()
            instance.status = instance.ASSIGNED
            instance.save()

            # call activation url of hosted application
            [success, PasswordResetToken
             ] = LogicInstances().activate_instance(customer, product,
                                                    instance)

            if not success:
                # send message to administrator
                self.notify_administrators(
                    _("SaasAdmin Error during activation"),
                    _("Failed activation of %s instance %s for customer %d") %
                    (product.name, instance.identifier, customer.id))
                return False

            # send message to administrator
            instances_available = LogicInstances(
            ).get_number_of_available_instances(product)
            self.notify_administrators(
                _("Instance for %s assigned") % (product.name, ),
                _("Nice, an instance of %s was booked for customer %d") %
                (product.name, customer.id) + "\n" +
                _("Still %d instances are available for new customers.") %
                (instances_available, ))

            # TODO send invoice to customer, or send it later in batch processing?
            return True
Ejemplo n.º 2
0
    def assign_instance(self, customer, product, plan):

        # check for first available instance
        instance = SaasInstance.objects.filter(product=product).filter(status='free').first()
        if not instance:
            # send message to administrator
            self.notify_administrators(_("Missing free instance for %s") % (product.name,), _("Assigning of %s instance for customer %d failed") % (product.name, customer.id))

            # TODO if no instance is available, then add the user to the waiting list

            return False
        else:
            # assign a free instance
            contract = self.get_new_contract(customer, product, plan)
            contract.instance = instance
            contract.save()
            instance.status = 'assigned'
            instance.save()

            # call activation url of hosted application
            [success, PasswordResetToken] = LogicInstances().activate_instance(customer, product, instance)

            if not success:
                # send message to administrator
                self.notify_administrators(_("SaasAdmin Error during activation"), _("Failed activation of %s instance %s for customer %d") % (product.name, instance.identifier, customer.id))
                return False

            # TODO send notification email to customer, with password reset token
            # self.notify_customer(customer, _(""))

            # send message to administrator
            self.notify_administrators(_("Instance for %s assigned") % (product.name,), _("Nice, an instance of %s was booked for customer %d") % (product.name, customer.id))

            # TODO send invoice to customer, or send it later in batch processing?
            return True
Ejemplo n.º 3
0
def addinstances(request, product):
    product = SaasProduct.objects.filter(slug = product).first()

    if request.method == "POST":
        # request.POST is immutable, so make a copy
        values = request.POST.copy()
        form = AddInstancesForm(values)
        if form.is_valid():
            try:
                for x in range(0, int(form['count'].value())):
                    success, new_data = LogicInstances().create_new_instance(form['hostname'].value(), form['pacuser'].value(), product)
                    if not success:
                        raise Exception('there was an error creating a new instance')
                return redirect("/instances/%s/" % (product.slug,))
            except Exception as ex:
                print('Exception in addinstances: %s' % (ex,))
                raise
    else:
        # use the last used hostname for this product
        hostname = 'localhost'
        pacuser = '******'
        last_instance = SaasInstance.objects.filter(product=product).order_by('id')
        if last_instance.count() > 0:
            hostname = last_instance.last().hostname
            pacuser = last_instance.last().pacuser
        form = AddInstancesForm(initial={'product_id': product.id, 'count': 10, 'hostname': hostname, 'pacuser': pacuser})

    return render(request,'addinstances.html',{'form': form, 'product': product})
Ejemplo n.º 4
0
    def put(self, request, *args, **kwargs):
        hostname = self.getParam(request, 'hostname', '')
        product = LogicProducts().get_product(request, False)

        if hostname and product:
            success, new_data = LogicInstances().create_new_instance(
                hostname, product)
            if success:
                return Response(new_data, status=status.HTTP_201_CREATED)
        else:
            raise Exception('please specify hostname and product_name')

        raise Exception('could not create new instance')
Ejemplo n.º 5
0
 def handle(self, *args, **options):
     LogicContracts().update_dates_of_contracts()
     LogicInstances().deactivate_expired_instances()
     LogicInstances().mark_deactivated_instances_for_deletion()