Пример #1
0
 def add_private_net(self, tenant_id=None, auth_token=None, user_name=None):
     from trove.patch.vip.models import DBPrivateNet
     if tenant_id is None or auth_token is None or user_name is None:
         raise Exception("tenant_id: %s, auth_token: %s, user_name: %s" % (tenant_id, auth_token, user_name))
     self.db_api.configure_db(CONF)
     try:
         DBPrivateNet.create(tenant_id=str(tenant_id).strip(), auth_token=str(auth_token).strip(),
                             user_name=str(user_name).strip())
     except Exception as ex:
         print "add_private_net failed, tenant_id: %s, auth_token: %s, user_name: %s, ex: %s" \
               % (tenant_id, auth_token, user_name, ex)
     else:
         print "add_private_net finished, tenant_id: %s, auth_token: %s, user_name: %s" \
               % (tenant_id, auth_token, user_name)
Пример #2
0
def lb_client(context):
    #auth_token = "e99e2cea639848b69845022d6237d164:b659f63886794b0abf5a2654c1b50de0"
    #user = "******"
    #tenant = "b659f63886794b0abf5a2654c1b50de0"
    #client = Client(username=context.user, token=context.auth_token,
    #               tenant_id=context.tenant, auth_url=PROXY_AUTH_URL)
    # add conf by trove.conf trove-taskmanager.conf
    auth_token = CONF.lb_auth_token
    user = CONF.lb_user
    tenant = CONF.lb_tenant

    reuqest_tenant_id = context.tenant
    LOG.debug("default auth_token: %s, user: %s, tenant: %s, reuqest_tenant_id: %s ", auth_token, user, tenant,
              reuqest_tenant_id)
    private_net = None
    try:
        if reuqest_tenant_id is not None:
            private_net = DBPrivateNet.find_by(context, tenant_id=reuqest_tenant_id, deleted=False)
        else:
            LOG.error("reuqest_tenant_id is None, %s " % reuqest_tenant_id)
    except Exception as ex:
        LOG.debug("private_net not found! reuqest_tenant_id:%s, ex:%s" % (reuqest_tenant_id, ex))

    if private_net:
        tenant = private_net.tenant_id
        auth_token = private_net.auth_token
        user = private_net.user_name
        LOG.debug("use private_net, auth_token: %s, user: %s, tenant: %s ", auth_token, user,
                  tenant)

    client = Client(username=user, token=auth_token,
                    tenant_id=tenant, auth_url=PROXY_AUTH_URL, timeout=10)
    client.httpclient.endpoint_url = CONF.lb_endpoint_url
    return client
Пример #3
0
 def del_private_net(self, tenant_id=None):
     from trove.patch.vip.models import DBPrivateNet
     if tenant_id is None:
         raise Exception("tenant_id: %s" % tenant_id)
     self.db_api.configure_db(CONF)
     try:
         private_net = DBPrivateNet.find_by(tenant_id=str(tenant_id).strip(), deleted=False)
         private_net.delete()
     except Exception as ex:
         print "del_private_net failed, tenant_id: %s, ex: %s" % (tenant_id, ex)
     else:
         print "del_private_net finished, tenant_id: %s" % tenant_id
Пример #4
0
    def allocate(cls, context, instance_id,rip,vip_address= None,
                 rport=3306,vport=3306,weight=1,lb_method="ROUND_ROBIN",connection_limit=1000,deleted=False, attached_virtual_instid = None):
        if not rip:
            raise GetRipFailed(**locals())

        vip = None
        vip_id = None
        mid = None
        pool_id = None
        if not vip_address:
            try:
                rds_tenant_id = CONF.lb_tenant
                reuqest_tenant_id = context.tenant
                LOG.debug("default reuqest_tenant_id: %s ", reuqest_tenant_id)
                private_net = None
                try:
                    if reuqest_tenant_id is not None:
                        private_net = DBPrivateNet.find_by(context, tenant_id=reuqest_tenant_id, deleted=False)
                    else:
                        LOG.error("reuqest_tenant_id is None, %s " % reuqest_tenant_id)
                except Exception as ex:
                    LOG.debug("private_net not found! reuqest_tenant_id:%s, ex:%s" % (reuqest_tenant_id, ex))

                if private_net:
                    rds_tenant_id = private_net.tenant_id
                    LOG.debug("use private_net, rds_tenant_id: %s ", rds_tenant_id)
                networks = list_networks(context)
                LOG.debug("list_networks: %s" % networks)
                network_list = filter(
                    lambda x: x is not None and x['tenant_id'] == rds_tenant_id \
                              and x['shared'] is False and x['type'] == 'private', networks['networks'])
                LOG.debug("filtered networks by tenant_id, rds_tenant_id: %s, network_list: %s"
                          % (rds_tenant_id, network_list))
                use_network_type = False
                if network_list and len(network_list) > 0:
                    subnets = list_subnets(context)
                    LOG.debug("list_subnets: %s" % subnets)
                    network_ids = map(lambda x: x['id'], network_list)
                    LOG.debug("network_ids: %s" % network_ids)
                    subnets_list = filter(lambda x: x['network_id'] in network_ids, subnets['subnets'])
                    LOG.debug("filtered subnets by network_ids, subnets_list: %s, network_ids: %s"
                              % (subnets_list, network_ids))
                    if subnets_list and len(subnets_list) > 0:
                        for subnet in subnets_list:
                            try:
                                subnet_id = subnet["id"]
                                LOG.debug("create_pool, subnet:%s, subnet_id:%s" % (subnet, subnet_id))
                                pool_id = create_pool(context, subnet_id=subnet_id)
                                if pool_id:
                                    LOG.debug("create_pool finished, subnet:%s, pool_id:%s" % (subnet, pool_id))
                                    break
                            except Exception as ex:
                                LOG.error("create_pool failed! subnet:%s, ex:%s" % (subnet, ex.message))
                                continue
                    else:
                        use_network_type = True
                else:
                    use_network_type = True
                if use_network_type:  # to compitable the region did not subed a single network to rds
                    network_type = CONF.lb_subnet_name
                    LOG.warn("filtered networks by tenant_id failed, use network_type: %s,"
                             " rds_tenant_id: %s, network_list: %s" % (network_type, rds_tenant_id, network_list))
                    pool_id = create_pool(context, network_type=network_type)
                if not pool_id:
                    msg = "create_pool failed! network_list:%s, subnets_list:%s" % (network_list, subnets_list)
                    LOG.error(msg)
                    raise Exception(msg)

                LOG.debug("pool_id: %s" % pool_id)
                vip,lb_id = create_vip(context, pool_id, vport, connection_limit)
                mid = create_member(context,rip, lb_id, instance_id,rport, weight)
                vip_id = models.DBVips.create(vip=vip,vport=vport,lb_id =lb_id).id
            except Exception as e:
                raise Exception(stackinfo())
        else:
            try:
                vip_info = models.DBVips.find_by(context,vip=vip_address,deleted=deleted)
                vip_id =vip_info.id
                lb_id = vip_info.lb_id

                mid = create_member(context,rip, lb_id, instance_id, rport, weight)
                vip  = vip_address
            except Exception as e:
                raise Exception(stackinfo())

        if attached_virtual_instid != None:
            try:

                inst_vip_dbinfo = DBInstanceVip.find_by(context, instance_id=instance_id, 
                                                        virtual_instance_id = attached_virtual_instid, deleted = 0)
                inst_vip_dbinfo.rip = rip
                inst_vip_dbinfo.rport = rport
                inst_vip_dbinfo.vip_id = vip_id
                inst_vip_dbinfo.mid = mid
                inst_vip_dbinfo.save()

            except Exception, e:
                models.DBInstanceVip.create(instance_id=instance_id,rip=rip,rport=rport,
                                            vip_id=vip_id,mid = mid, virtual_instance_id = attached_virtual_instid)