Exemplo n.º 1
0
def create_pool(pool, user):
    """Creates pool"""

    try:
        sp = ServerPool()
        sp.create_v3(pool, user)
    except exceptions.InvalidIdentifierAlreadyPoolException, e:
        raise ValidationAPIException(e.detail)
Exemplo n.º 2
0
def save_server_pool(user, id, identifier, default_port, hc, env, balancing, maxconn, id_pool_member, servicedownaction):
    # Save Server pool
    old_healthcheck = None

    if id:
        sp = ServerPool.objects.get(id=id)

        # store old healthcheck,lb method and service-down-action
        old_servicedownaction = sp.servicedownaction
        old_identifier = sp.identifier
        old_healthcheck = Healthcheck.objects.get(id=sp.healthcheck.id)
        old_lb_method = sp.lb_method
        old_maxconn = sp.default_limit

        # validate change of environment
        if sp.environment and sp.environment.id != env.id:
            validate_change_of_environment(id_pool_member, sp)

        # Pool already created, it is not possible to change Pool Identifier
        if(old_identifier != identifier and sp.pool_created):
            raise exceptions.CreatedPoolIdentifierException()

        update_pool_fields(default_port, env, identifier,
                           old_healthcheck, old_lb_method, old_maxconn, sp, user)

        update_pool_maxconn(maxconn, old_maxconn, sp, user)

        apply_health_check(hc, old_healthcheck, sp, user)

        update_load_balancing_method(balancing, old_lb_method, sp, user)

        apply_service_down_action(
            old_servicedownaction, servicedownaction, sp, user)
    else:
        sp = ServerPool(identifier=identifier, default_port=default_port, healthcheck=hc,
                        environment=env, pool_created=False, lb_method=balancing, default_limit=maxconn, servicedownaction=servicedownaction)
        sp.save()

    return sp, (old_healthcheck.id if old_healthcheck else None)
Exemplo n.º 3
0
def save_server_pool(user, id, identifier, default_port, hc, env, balancing, maxconn, id_pool_member, servicedownaction):
    # Save Server pool
    old_healthcheck = None

    if id:
        sp = ServerPool.objects.get(id=id)

        # storage old healthcheck , lb method and service-down-action
        old_servicedownaction = sp.servicedownaction
        old_identifier = sp.identifier
        old_healthcheck = Healthcheck.objects.get(id=sp.healthcheck.id)
        old_lb_method =  sp.lb_method
        old_maxconn = sp.default_limit

        #valid change environment
        if sp.environment and sp.environment.id != env.id:
            if sp.pool_created:
                raise exceptions.UpdateEnvironmentPoolCreatedException()
            del_smp = sp.serverpoolmember_set.exclude(id__in=id_pool_member)
            vip = sp.vipporttopool_set.count()
            if vip > 0:
                raise exceptions.UpdateEnvironmentVIPException()

            if len(del_smp) > 0:
                raise exceptions.UpdateEnvironmentServerPoolMemberException()

        #Pool already created, it is not possible to change Pool Identifier
        if(old_identifier != identifier and sp.pool_created):
            raise exceptions.CreatedPoolIdentifierException()

        sp.default_port = default_port
        sp.environment = env
        sp.default_limit = old_maxconn
        sp.healthcheck = old_healthcheck
        sp.lb_method = old_lb_method
        sp.identifier = identifier
        sp.save(user)

        sp.default_limit = maxconn
        sp.save(user)

        #If exists pool member, checks if all them have the same maxconn
        #before changing default maxconn of pool
        if(len(sp.serverpoolmember_set.all()) > 0):
            if(old_maxconn != sp.default_limit and sp.pool_created):

                for serverpoolmember in sp.serverpoolmember_set.all():
                    if serverpoolmember.limit != old_maxconn:
                        raise exceptions.ScriptAlterLimitPoolDiffMembersException()
                    else:
                        serverpoolmember.limit = maxconn
                        serverpoolmember.save(user)

                transaction.commit()
                command = settings.POOL_MANAGEMENT_LIMITS % (sp.id)
                code, _, _ = exec_script(command)
                if code != 0:
                    sp.default_limit = old_maxconn
                    for serverpoolmember in sp.serverpoolmember_set.all():
                        serverpoolmember.limit = old_maxconn
                        serverpoolmember.save(user)

                    sp.save(user)
                    transaction.commit()
                    raise exceptions.ScriptAlterLimitPoolException()

        #Applies new healthcheck in pool
        #Todo - new method
        sp.healthcheck = hc
        sp.save(user)
        if(old_healthcheck.id != hc.id and sp.pool_created):
            transaction.commit()
            command = settings.POOL_HEALTHCHECK % (sp.id)
            code, _, _ = exec_script(command)
            if code != 0:
                sp.healthcheck = old_healthcheck
                sp.save(user)
                transaction.commit()
                raise exceptions.ScriptCreatePoolException()
            
        #Applies new lb method in pool
        #Todo - new method
        sp.lb_method = balancing
        sp.save(user)
        if(old_lb_method != sp.lb_method and sp.pool_created):
            transaction.commit()
            command = settings.POOL_MANAGEMENT_LB_METHOD % (sp.id)
            code, _, _ = exec_script(command)
            if code != 0:
                sp.lb_method = old_lb_method
                sp.save(user)
                transaction.commit()
                raise exceptions.ScriptCreatePoolException()

        #Applies new service-down-action in pool
        #Todo - new method
        sp.servicedownaction = servicedownaction
        sp.save(user)
        if(old_servicedownaction != sp.servicedownaction and sp.pool_created):
            transaction.commit()
            command = settings.POOL_SERVICEDOWNACTION % (sp.id)
            code, _, _ = exec_script(command)
            if code != 0:
                sp.servicedownaction = old_servicedownaction
                sp.save(user)
                transaction.commit()
                raise exceptions.ScriptAlterServiceDownActionException()

    else:
        sp = ServerPool(identifier=identifier, default_port=default_port, healthcheck=hc,
                        environment=env, pool_created=False, lb_method=balancing, default_limit=maxconn, servicedownaction=servicedownaction)
        sp.save(user)

    return sp, (old_healthcheck.id if old_healthcheck else None)