Beispiel #1
0
    def getNodes(self):
        try:
            db = self.cnx['local']
            node = db.system.replset.find_one()
            if node is None:
                raise DBError("No node found!")
            return node

        except Exception as exc:
            raise DBError(str(exc))
Beispiel #2
0
 def checkDataIntegrity(self, collection):
     '''
         You can use the validate command on to check if the contents of a collection are valid.
     '''
     if collection.find(DOT) == -1:
         raise DBError("Database or collections not specified. Use checkDataIntegrity(<db.colection>) ")
     res = collection.split(DOT)
     try:
         database   = res[0]
         collection = res[1]
         db = self.cnx[database]
         return db.command({DbAdmin.CMD_VALIDATE:collection})
     except Exception, exc:
         raise DBError(str(exc))
Beispiel #3
0
 def compactCollection(self, collection):
     '''
         The compact command compacts and defragments a collection. Indexes are rebuilt at the same time.
         It is conceptually similar to repairDatabase, but works on a single collection rather than an entire database.
         
         More details at http://www.mongodb.org/display/DOCS/Compact+Command
     '''
     if collection.find(DOT) == -1:
         raise DBError("Database or collections not specified. Use checkDataIntegrity(<db.colection>) ")
     res = collection.split(DOT)
     try:
         database   = res[0]
         collection = res[1]
         db = self.cnx[database]
         return db.command({DbAdmin.CMD_COMPACT_COLLECTION:collection})
     except Exception,exc:
         raise DBError(str(exc))
Beispiel #4
0
 def _get_shards(self, db):
     shards = db.shards.find()
     if shards.count() == 0:
         raise DBError("No shards defined!")
     srv_shards = []
     for shard in shards:
         srv_shards.append({'_id' : shard['_id'], 'host' : shard['host']})
     return srv_shards
Beispiel #5
0
 def repairDB(self, database):
     if not isinstance(database, basestring):
         raise TypeError("Database must be an instance of (Database, str, unicode)")
     
     try:
         db = self.cnx[database]
         return db.command({DbAdmin.CMD_REPAIR_DATABASE:1})
     except Exception, exc:
         raise DBError(str(exc))
Beispiel #6
0
 def getIndexes(self, database):
     '''
          View database indexes
          return JSON object:
              [{
                  'ns': 'test.hat', 'name': '_id_', 'key': {u'_id': 1}, 'v': 1
               },{
                  'ns': 'test.hata','name': '_id_', 'key': {u'_id': 1}, 'v': 1
               },{
                  'ns': 'test.hat','name': 'myindex', 'background': False, 'dropDups': False, 'key': {'aaaa': 1}, 'v': 1, 'unique': True
               }]
     '''
     try:
         db = self.cnx[database]
         index_list = db.system.indexes.find()
         if index_list.count() == 0:
             raise DBError("No index defined!")
         return list(index_list)
     except Exception, exc:
         raise DBError(str(exc))
Beispiel #7
0
 def addIndex(self, collection, index_name, index_key, unique=False, drop_duplicates=False, create_in_background=False, index_version=1):
     '''
         Example:
             mongo.addIndex('test.hat','_idx_hat',{'name':1},unique=True,drop_duplicates=True,create_in_backgroud=True,index_version=2)
     
     
         unique - MongoDB indexes may optionally impose a unique key constraint, 
                  which guarantees that no documents are inserted whose values for the indexed keys match
                  those of an existing document.
         drop_duplicates - A unique index cannot be created on a key that has pre-existing duplicate values.
                  If you would like to create the index anyway, keeping the first document the database indexes and 
                  deleting all subsequent documents that have duplicate values, add the dropDups option.
         create_in_backgroud - v1.4+ has a background index build option.This option has significant limitations in a replicated cluster
         index_version - index version = 1 in v2.0
                  
         More details at: http://www.mongodb.org/display/DOCS/Indexes
         
     '''
     if collection.find(DOT) == -1:
         raise DBError("Database or collections not specified. Use addIndex(<db.colection>,...) ")
     if not isinstance(index_key, dict):
         raise DBError("Index key not instance of dict!")
     res = collection.split(DOT)
     try:
         database = res[0]
         col_name = res[1]
         db = self.cnx[database]
         col_indexes = db.system.indexes
         idx_obj = {
                    'name':index_name,
                    'ns': collection,
                    'key': index_key,
                    'background': create_in_background,
                    'dropDups': drop_duplicates,
                    'unique':unique,
                    'v': index_version
                    }
         col_indexes.insert(idx_obj,safe=True)
         return list(self.getIndexes(database))
     except Exception as exc:
         raise DBError(str(exc))
Beispiel #8
0
 def dropIndex(self, collection, index_name):
     '''
         CMD_DELETE_INDEX
     '''
     if collection.find(DOT) == -1:
         raise DBError("Database or collections not specified. Use addIndex(<db.colection>,...) ")
     res = collection.split(DOT)
     try:
         database = res[0]
         col = res[1]
         db = self.cnx[database]
         
         cmd = SON(
              [
                 (DbAdmin.CMD_DROP_INDEX,col),
                 ('index', index_name)
               ]
               )     
         db.command(cmd)       
         return list(self.getIndexes(database))
     except Exception as exc:
         raise DBError(str(exc))
Beispiel #9
0
    def showMasterStatus(self):
        ''' 
            Show replication MASTER status
            db.printReplicationInfo() -> inspects contents of local.oplog.$main on master and reports status

        '''
        try:
            db  = self.cnx['local']
            status = {}
            status['master'] = list(db.oplog['$main'].find())
            return status
        except Exception, exc:
            raise DBError(str(exc))
Beispiel #10
0
 def routerStatus(self):
     '''
         Show router status
     '''        
     try:
         db  = self.cnx['config']
         status = {}
         status['version'] = self._router_ver(db)
         status['locks'] = self._router_locks(db)
         status['pings'] = self._router_lockpings(db)
         status['routers'] = self._routers(db)
         return status
     except Exception, exc:
         raise DBError(str(exc))
Beispiel #11
0
    def showSlaveStatus(self):
        ''' 
            Show replication SLAVE status
            db.printSlaveReplicationInfo() -> inspects contents of local.sources on the slave and reports status

        '''
        try:
            db  = self.cnx['local']
            status = {}
            status['whoiam'] = list(db.me.find()) 
            status['slave']  = list(db.sources.find())
            return status
        except Exception,exc:
            raise DBError(str(exc))
Beispiel #12
0
 def shardStatus(self):
     '''
         Print sharding status - runt this throught mongos
     '''
     try:
         db  = self.cnx['config']
         status = {}
         status['version'] = self._router_ver(db)
         # extract from "shards"
         status['shards'] = self._get_shards(db)
         # extract from "databases"
         status['databases'] = self._db_list(db)            
         return status
     except Exception, exc:
         raise DBError(str(exc))
Beispiel #13
0
    def addNode(self, server_node, is_arbiter=False, is_hidden=False, priority=1, votes=1):
        '''
            arbiterOnly = false : If true, this member will participate in vote but receive no data.
            votes  = 1: Number of votes this member has in an election. Generally you should not change this
        '''
        nodes = self.getNodes()
        if nodes.has_key('version'):
            # incrase version by 1
            nodes['version'] += 1

        if nodes.has_key('members'):
            total_members = len(nodes['members'])
            node = {'host': server_node, '_id': total_members, 'arbiterOnly': is_arbiter, 'hidden': is_hidden, 'priority': priority, 'votes': votes }
            nodes['members'].append(node)
        try:
            cmd = {DbAdmin.CMD_RECONFIG_REPLICASET : nodes}
            return self._executeCommand(cmd)
        except Exception, exc:
            raise DBError(str(exc))
Beispiel #14
0
 def isMaster(self):
     '''
              { 
                 'me': '172.16.101.229:27017',
                 'ismaster': True,
                 'ok': 1.0, 
                 'setName': 'csign', 
                 'primary': '172.16.101.229:27017', 
                 'hosts': [
                           '172.16.101.229:27017'
                          ], 
                  'maxBsonObjectSize': 16777216, 
                  'passives': ['172.16.101.229:27018'], 
                  'secondary': False
              }            
     '''
     try:
         return self._executeCommand(DbAdmin.CMD_REPLICA_SET_IS_MASTER)
     except Exception, exc:
         raise DBError(str(exc))
Beispiel #15
0
 def viewCurrentOperation(self):
     try:
         db = self.cnx.db['$cmd.sys.inprog']
         return db.find_one()
     except Exception,exc:
         raise DBError(str(exc))
Beispiel #16
0
 def _executeCommand(self, command):
     try:
         return self.admin.command(command)
     except Exception as exc:
         raise DBError(str(exc))
Beispiel #17
0
 def addUser(self, username, password, database='admin'):
     try:
         db = self.cnx[database]
         db.add_user(username, password)
     except Exception as exc:
         raise DBError(str(exc))
Beispiel #18
0
 def _router_ver(self, db):
     version = db.version.find_one()
     if version is None:
         raise DBError("Not a shard database!")
     return version
Beispiel #19
0
 def delUser(self, username, database='admin'):
     try:
         db = self.cnx[database]
         db.remove_user(username)
     except Exception as exc:
         raise DBError(str(exc))
Beispiel #20
0
 def setChunkSize(self, port):
     try:
         config = Connection('localhost', int(port)).config
         config.settings.save({'_id' : 'chunksize', 'value' : self.CHUNK_SIZE}, safe=True)
     except Exception as exc:
         raise DBError(str(exc))