Beispiel #1
0
def getDB(name):
    if name=='mongo':
        conn=MongoReplicaSetClient('dbserver-1:27017,dbserver-2:27017,dbserver-3:27017', replicaSet='ats_rs')
        conn.read_preference = ReadPreference.SECONDARY_PREFERRED
        return conn.brauth
    elif name=='redis':
        return redis.Redis(connection_pool = redis.ConnectionPool(host='192.168.7.210', port=6379, db=6))
    else:
        return None  
Beispiel #2
0
def getDB(name):
    if name == 'mongo':
        conn = MongoReplicaSetClient(
            'dbserver-1:27017,dbserver-2:27017,dbserver-3:27017',
            replicaSet='ats_rs')
        conn.read_preference = ReadPreference.SECONDARY_PREFERRED
        return conn.brauth
    elif name == 'redis':
        return redis.Redis(connection_pool=redis.ConnectionPool(
            host='192.168.7.210', port=6379, db=6))
    else:
        return None
Beispiel #3
0
def init_mongodb_client(conf_item):
    section = conf_item
    servers = section.get('servers')

    def build_node_desc(node_conf):
        return '%s:%s' % (node_conf['host'], node_conf['port'])

    if section.get('replica', False):
        from pymongo import MongoReplicaSetClient
        from pymongo import ReadPreference

        client = MongoReplicaSetClient(','.join(map(build_node_desc, servers)), replicaSet=section.get('replName'))
        pref = section.get('readPref', 'PRIMARY')
        client.read_preference = getattr(ReadPreference, pref)
    else:
        from pymongo import MongoClient

        s = servers[0]
        client = MongoClient(s['host'], s['port'])

    setattr(client, 'auth_list', set([]))

    return client
Beispiel #4
0
def init_mongodb_client(conf_item):
    section = conf_item
    servers = section.get('servers')

    def build_node_desc(node_conf):
        return '%s:%s' % (node_conf['host'], node_conf['port'])

    if section.get('replica', False):
        from pymongo import MongoReplicaSetClient
        from pymongo import ReadPreference

        client = MongoReplicaSetClient(','.join(map(build_node_desc, servers)),
                                       replicaSet=section.get('replName'))
        pref = section.get('readPref', 'PRIMARY')
        client.read_preference = getattr(ReadPreference, pref)
    else:
        from pymongo import MongoClient

        s = servers[0]
        client = MongoClient(s['host'], s['port'])

    setattr(client, 'auth_list', set([]))

    return client
Beispiel #5
0
 def getMongoDB(self):
     conn=MongoReplicaSetClient(Config['ReplicaSetServer'], replicaSet=Config['ReplicaSetName'])
     conn.read_preference = ReadPreference.SECONDARY_PREFERRED
     return conn.bugreporter
Beispiel #6
0
    def __new__(mcs, name, bases, attrs):
        new_class = super(ModelBase,
                          mcs).__new__(mcs, name, bases, attrs)
        parents = [b for b in bases if isinstance(b, ModelBase)]
        if not parents:
            # If this isn't a subclass of Model, don't do anything special.
            return new_class

        # Processing Model metadata.
        try:
            meta = getattr(new_class, 'Meta')
        except AttributeError:
            meta = None
        else:
            # Won't need the original metadata container anymore.
            delattr(new_class, 'Meta')

        options = _Options(meta)
        options.collection = options.collection or to_underscore(name)

        if options.interface:
            new_class._meta = None
            new_class.database = None
            new_class.collection = DummyCollection
            return new_class

        if not (options.host and options.port and options.database):
            raise Exception(
                'Model %r improperly configured: %s %s %s' % (
                    name, options.host, options.port, options.database))

        # Checking connection / client pool for an existing connection / client.
        pool_key = options.host,options.port
        if options.replica_set_name:
            logging.debug("Using replica_set_name=%s as database pool key." % options.replica_set_name)
            pool_key = options.replica_set_name

        if pool_key in mcs._connections:
            client = mcs._connections[pool_key]
            logging.debug("Got database client from pool for pool_key=%s" % (pool_key,))
        else:
            logging.debug("Creating new database client for pool_key=%s" % (pool_key,))
            if options.replica_set_name:
                logging.debug("Setting up a replica set client...")
                client = MongoReplicaSetClient(options.replica_set_uri, replicaSet=options.replica_set_name)
                client.read_preference = ReadPreference.SECONDARY_PREFERRED
            else:
                logging.debug("Setting up a normal client...")
                client = MongoClient(options.host, options.port)

            mcs._connections[pool_key] = client

        new_class._meta = options
        new_class.connection = client
        new_class.database = client[options.database]
        if options.username and options.password:
            new_class.database.authenticate(options.username, options.password)
        new_class.collection = options.collection_class(
            new_class.database, options.collection, document_class=new_class)

        if options.auto_index:
            new_class.auto_index()   # Generating required indices.

        return new_class
Beispiel #7
0
 def getMongoDB(self):
     conn = MongoReplicaSetClient(Config['ReplicaSetServer'],
                                  replicaSet=Config['ReplicaSetName'])
     conn.read_preference = ReadPreference.SECONDARY_PREFERRED
     return conn.bugreporter