Ejemplo n.º 1
0
class MongoDBConnection:

    _instance = None

    def __init__(self):
        self.connection = Connection(DBNAME, tz_aware=True)
        self.admin_db = Database(self.connection, "admin")
        self.admin_db.authenticate(USERNAME, PASSWD)

    @classmethod
    def instance(cls):
        if cls._instance == None:
            MongoDBConnection._instance = MongoDBConnection()
        return MongoDBConnection._instance

    def reauthenticate(self):
        self.admin_db = Database(self.connection, "admin")
        self.admin_db.authenticate(USERNAME, PASSWD)

    def reconnect(self):
        self.connection = Connection(DBNAME)
        self.reauthenticate()

    def get_connection(self):
        return self.connection
Ejemplo n.º 2
0
class MongoDB(object):
    __instance = None
    __db_connection = None

    # def __new__(cls, *args, **kwargs):
    #     if MongoDB.__instance is None:
    #         MongoDB.__instance = object.__new__(cls, *args, **kwargs)
    #     return MongoDB.__instance

    def __init__(self, **kwargs):
        if 'host' not in kwargs or not kwargs['host']:
            self.host = MONGODB_HOST
        else:
            self.host = kwargs['host']

        if 'port' not in kwargs or not kwargs['port']:
            self.port = MONGODB_PORT
        else:
            self.port = kwargs['port']

        if 'db' not in kwargs or not kwargs['db']:
            self.db = MONGODB_DBNAME
        else:
            self.db = kwargs['db']

        # if 'rs' not in kwargs or not kwargs['rs']:
        #     self.rs = settings.MONGODB_RS
        # else:
        #     self.rs = kwargs['rs']
        #
        if 'user' not in kwargs or not kwargs['user']:
            self.user = MONGODB_USER
        else:
            self.user = kwargs['user']
        #
        if 'pwd' not in kwargs or not kwargs['pwd']:
            self.pwd = MONGODB_PWD
        else:
            self.pwd = kwargs['pwd']

    def get_database(self):
        if self.__db_connection is None:
            from pymongo.read_preferences import ReadPreference
            # self.__db_connection = Database(Connection(self.host, self.port), self.db)
            self.__db_connection = Database(
                Connection(self.host, self.port), self.db
            )  #replicaSet='', read_preference=ReadPreference.SECONDARY_PREFERRED), self.db)
            self.__db_connection.authenticate(self.user, self.pwd)

        return self.__db_connection
Ejemplo n.º 3
0
def get_db(is_local_deployed=False):
    if is_local_deployed:
        #for local launch
        connection = Connection()
        db = connection.wiki
        return db
    else:
        # for dotcloud launch
        mongodb_info = json.load(file("/home/dotcloud/environment.json"))
        print mongodb_info
        connection = Connection(mongodb_info["DOTCLOUD_DATA_MONGODB_HOST"], int(mongodb_info["DOTCLOUD_DATA_MONGODB_PORT"]))
        database = Database(connection, "wiki")
        database.authenticate("xinz", "hellopy")
        db = connection.wiki
        return db
Ejemplo n.º 4
0
def connect(conf, prod):
    log.info('setting up mongo connection')

    url = conf.get('mongo', 'url')
    port = conf.getint('mongo', 'port')
    db = conf.get('mongo', 'db')

    conn = Connection(url, port)
    db = Database(conn, db)

    if (prod):
        log.info('authenticating mongo connection')
        username = conf.get('mongo', 'username')
        pwd = conf.get('mongo', 'password')

        db.authenticate(username, pwd)

    return db
Ejemplo n.º 5
0
    def init_connection(self):
        if hasattr(self, 'app'):
            config = self.app.config
        else:
            config = self._default_config

        connection = Connection(
            host=config.get('MONGODB_HOST'),
            port=config.get('MONGODB_PORT'),
            slave_okay=config.get('MONGODB_SLAVE_OKAY')
        )

        database = Database(connection, config.get('MONGODB_DATABASE'))

        if config.get('MONGODB_USERNAME') is not None:
            database.authenticate(
                config.get('MONGODB_USERNAME'),
                config.get('MONGODB_PASSWORD')
            )

        return connection, database
Ejemplo n.º 6
0
# -*- encoding: utf-8 -*-
'''
Created on 2014-11-7

@author: [email protected]
'''

import pymongo
from pymongo.database import Database
from pymongo.mongo_client import MongoClient
from pymongo.collection import Collection

from gridfs import GridFS

if __name__ == '__main__':
    con = pymongo.Connection('172.16.10.170', 27017)
#     con = MongoClient(host='172.16.10.170', port=27017, max_pool_size=200)
            
    db = Database(con, 'blog')
    db.authenticate('admin', 'admin')
    coll = Collection(db, 'foo')
    coll.insert({'a':1, 'b':2, 'c':3})
#     coll.update({'a':1}, {'$set':{'b':4}}, multi=True)
    print [x for x in coll.find()]
#     MongoClient()
#     print user.find({'id':1}).count()
    pass
Ejemplo n.º 7
0
    username = sys.argv[1]
    password = sys.argv[2]
    url = sys.argv[3]
    file_list = sys.argv[4:]

    print username
    print password
    print len(file_list)

    if (len(url) == 0):
        connection = Connection()  # Connect to localhost
    else:
        connection = Connection(url)  # Connect to remote db

    db = Database(connection, 'zip')  # Get zip database
    db.authenticate(username, password)  # Authenticate

    for csv_file in file_list:  # Add all the files
        add(csv_file)

########NEW FILE########
__FILENAME__ = remove
import sys
import os
import csv

from pymongo import Connection
from pymongo.database import Database

appendix_file = '../appendix/country.txt'
header_file = '../appendix/headers.txt'
Ejemplo n.º 8
0
                    record[ headers[ii] ] = unicode( row[ii], 'utf-8' )
            erase(record)

    pass

def erase( record ):
    db['global'].remove(record)

if __name__ == "__main__" :
    
    if ( len(sys.argv) != 4 ):
        print "Usage: %s <username> <password> <csv-files>" % sys.argv[0]

    username = sys.argv[1]
    password = sys.argv[2]
    csv_file = sys.argv[3]
    
    print username
    print password
    print csv_file

    connection = Connection()                   # Replace with mongo-url
    db = Database(connection,'zip')              # Get zip database
    db.authenticate(username,password)           # Authenticate
    
    remove( csv_file )

    


Ejemplo n.º 9
0
class Mongoop(object):

    def __init__(self, mongodb_host, mongodb_port, mongodb_credentials=None,
                 mongodb_options=None, frequency=0, op_triggers=None,
                 balancer_triggers=None, threshold_timeout=None, query=None):
        try:
            # mongodb
            self._mongodb_host = mongodb_host
            self._mongodb_port = mongodb_port
            self._mongodb_credentials = mongodb_credentials or {}
            self._mongodb_options = mongodb_options or {}

            # mongoop triggers
            self._frequency = frequency or 30
            self.op_triggers = op_triggers or {}
            self.balancer_triggers = balancer_triggers or {}
            self._threshold_timeout = threshold_timeout or 60
            self._query = query or {}

            # NOTE: retrieve the minimum threshold.
            if self.op_triggers:
                self._threshold_timeout = min([v['threshold'] for v in self.op_triggers.values() if 'threshold' in v])
            self._base_op_query = {
                'secs_running': {'$gte': self._threshold_timeout},
                'op': {'$ne': 'none'}
            }
            self._base_op_query.update(self._query)

            self.conn = MongoClient(
                host=self._mongodb_host,
                port=self._mongodb_port,
                read_preference=ReadPreference.PRIMARY,
                **self._mongodb_options
            )
            self.db = Database(self.conn, 'admin')
            if self._mongodb_credentials:
                # NOTE: avoid a breaking chance since the version 0.5
                username = self._mongodb_credentials.get('name') or self._mongodb_credentials.get('username')
                self.db.authenticate(username, self._mongodb_credentials['password'])

            # NOTE: add the callable for each trigger
            self.cycle_op_triggers = []
            self.cycle_balancer_triggers = []

            for t_name, t_values in self.op_triggers.items():
                _callable = self._get_trigger_callable(t_name, t_values)
                if _callable:
                    self.cycle_op_triggers.append(_callable)

            for t_name, t_values in self.balancer_triggers.items():
                _callable = self._get_trigger_callable(t_name, t_values, category='balancer')
                if _callable:
                    self.cycle_balancer_triggers.append(_callable)

        except TypeError as e:
            logging.error('unable to authenticate to admin database :: {}'.format(e))
            exit(1)
        except OperationFailure as e:
            logging.error('authentication failure :: {}'.format(e))
        except ConnectionFailure as e:
            logging.error('unable to connect to database :: {}'.format(e))
        else:
            logging.info('start mongoop :: {}'.format(self))

    def __str__(self):
        return u'{} :: frequency={} :: slow_query={} :: op_triggers={} :: balancer_triggers={}'.format(
            self.conn, self._frequency, self._base_op_query, len(self.cycle_op_triggers),
            len(self.cycle_balancer_triggers))

    def __call__(self):
        """ Main function.
        """
        while True:
            start = time()
            self.call_op_triggers()
            self.call_balancer_triggers()
            exec_time = time() - start
            if exec_time < self._frequency:
                sleep(self._frequency - exec_time)

    def call_op_triggers(self):
        """ Main function to run the op triggers.
        """
        operations = self._current_op()
        for trigger in self.cycle_op_triggers:
            trigger.run(operations=operations)

    def call_balancer_triggers(self):
        """ Main function to run the balancer triggers.
        """
        if not self.balancer_triggers:
            return True

        balancer_state = self._get_balancer_state()
        for trigger in self.cycle_balancer_triggers:
            trigger.run(balancer_state=balancer_state)

    def _get_trigger_callable(self, trigger_name, trigger_params, category='op'):
        """ Retrieve the corresponding trigger by name and add into the triggers list.

        Args:
        """
        try:
            trigger_module = import_module('mongoop.triggers.{}'.format(trigger_params['type']))
            trigger_class = getattr(trigger_module, 'MongoopTrigger')
            trigger = trigger_class(name=trigger_name, params=trigger_params,
                mongoop=self, category=category)
        except Exception as e:
            logging.error('unable to retrieve the trigger callable :: {}'.format(e))
        else:
            return trigger

    def _current_op(self):
        """ Get informations on operations currently running.
        """
        try:
            op_inprog = {}
            coll = self.db.get_collection("$cmd.sys.inprog")
            result = coll.find_one(self._base_op_query)
            op_inprog = result.get('inprog', {})
        except Exception as e:
            logging.error('unable to retrieve op :: {}'.format(e))
        else:
            if op_inprog:
                logging.info('found {} slow op'.format(len(op_inprog)))
            logging.debug('found {} slow op'.format(len(op_inprog)))
        finally:
            return op_inprog

    def _get_balancer_state(self):
        """ Return the balancer state.

            Returns:
                bool: True it's running, False otherwhise.
        """
        try:
            if self.conn.config.settings.find_one({'_id': 'balancer', 'stopped': True}):
                logging.info('balancer state :: stopped')
                return False
            logging.info('balancer state :: started')
            return True
        except Exception as e:
            logging.error('unable to get the balancer state :: {}'.format(e))
Ejemplo n.º 10
0
from pymongo.mongo_client import MongoClient
from pymongo.collection import Collection, ObjectId
from pymongo.database import Database

try:
    tasks_collection_name = 'tasks'
    defect_collection_name = 'defect'
    hsegap_collection_name = 'HSE_gaps_pbe_structure'
    db_info = {
        "aliases": {},
        "database": "results_GY",
        "host": "marilyn.pcpm.ucl.ac.be",
        "port": 27017,
        "admin_user": "******",
        "admin_password": "******",
        "readonly_user": "******",
        "readonly_password": "******"
    }

    clt = MongoClient(host=db_info['host'], port=db_info['port'])
    db = Database(clt, db_info['database'])
    db.authenticate(db_info['admin_user'], db_info['admin_password'])
except:
    print 'Can not connect database'
Ejemplo n.º 11
0
Created by AFD on 2010-10-14.
Copyright (c) 2010 A. Frederick Dudley. All rights reserved.
"""

from stores.mongo_store import *
from stores.store import *
from pymongo.connection import Connection
from pymongo.database import Database

from binary_tactics.player  import Player
from binary_tactics.helpers import *
from binary_tactics.weapons import *
from binary_tactics.units   import *
from binary_tactics.stone   import *

connection = Connection(host='bt.hipeland.org')
db = Database(connection, 'binary_tactics')
db.authenticate('rix', 'fhpxguvf'.decode('rot13'))

exists = {'$exists': True}
#squads = db.binary_tactics.find({'squad': exists})
#grids  = db.binary_tactics.find({'grid': exists})

#squad   = [convert_dict(get_dict(db.binary_tactics.find({'squad.value': 1004})[0], db.binary_tactics, db))]
squad   = [rand_squad()]
units   = [n for n in squad[0]]
weapons = [n.weapon for n in squad[0]]
stones  = [rand_comp() for n in xrange(6)]
w = Player('The World', squad, stones, units, weapons, [])

Ejemplo n.º 12
0
class DatabaseConnection(object):
    """Establishes a connection to the mongodb."""
    def __init__(self, database=None, host='localhost', port=27017):
        """
        Can be initialized without parameters.
        :param database: Str name of database.
        :param host: Host.
        :param port: Port.
        """

        self.timeout = 1
        self.host = str(host)
        if not isinstance(port, int):
            raise ValueError(
                'Entered port is "{}"! Must be integer type'.format(port))
        self.port = port
        self.client = MongoClient(host=self.host,
                                  port=self.port,
                                  serverSelectionTimeoutMS=self.timeout)
        try:
            if database is not None:
                if database in self.client.database_names():
                    self.database = Database(self.client, database)
                    if self.database.command("serverStatus")['ok'] == 1.0:
                        print('connecting to: mongodb://{}:{}'.format(
                            self.host, self.port))
                        print('database - "{}"'.format(self.database.name))
                    else:
                        pass

                else:
                    raise ValueError
            else:
                self.database = database

        except ServerSelectionTimeoutError as err:
            sys.exit('Error! {}'.format(err))
        except ValueError:
            print('"{}" is not in database names! Please check the name of '
                  'database!'.format(database))

    def use_db(self, database=None):
        """
        Switches between databases.
        :param database: Str name of database.
        """
        try:
            if database in self.client.database_names():
                self.database = Database(self.client, database)
                print('switched to db {}'.format(self.database.name))
            else:
                raise ValueError
        except ServerSelectionTimeoutError as err:
            sys.exit('Error! {}'.format(err))
        except ValueError:
            print('"{}" is not in database names! Please check the name of '
                  'database!'.format(database))

    def authenticate(self, user=None, password=None):
        """
        Authenticates the user in the connected database.
        :param user: The name of the user.
        :param password: The password.
        """
        if user is not None and password is not None:
            try:
                if user in [
                        user['user'] for user in (
                            self.database.command('usersInfo'))['users']
                ]:
                    try:
                        if self.database.authenticate(user, password):
                            self.auth = self.database.authenticate(
                                user, password)
                            print('1')
                        else:
                            raise OperationFailure
                    except OperationFailure:
                        print('Error: Authentication failed.')
                        print('0')
                else:
                    print('"{}" user is not in "{}" database users!'.format(
                        user, self.database.name))
            except AttributeError:
                print('At first you must connect to the database with use '
                      'method!')
        else:
            print('Please check entered user and password!')
Ejemplo n.º 13
0
class Mongoop(object):
    def __init__(self,
                 mongodb_host,
                 mongodb_port,
                 mongodb_credentials=None,
                 mongodb_options=None,
                 frequency=0,
                 op_triggers=None,
                 balancer_triggers=None,
                 threshold_timeout=None,
                 query=None):
        try:
            # mongodb
            self._mongodb_host = mongodb_host
            self._mongodb_port = mongodb_port
            self._mongodb_credentials = mongodb_credentials or {}
            self._mongodb_options = mongodb_options or {}

            # mongoop triggers
            self._frequency = frequency or 30
            self.op_triggers = op_triggers or {}
            self.balancer_triggers = balancer_triggers or {}
            self._threshold_timeout = threshold_timeout or 60
            self._query = query or {}

            # NOTE: retrieve the minimum threshold.
            if self.op_triggers:
                self._threshold_timeout = min([
                    v['threshold'] for v in self.op_triggers.values()
                    if 'threshold' in v
                ])
            self._base_op_query = {
                'secs_running': {
                    '$gte': self._threshold_timeout
                },
                'op': {
                    '$ne': 'none'
                }
            }
            self._base_op_query.update(self._query)

            self.conn = MongoClient(host=self._mongodb_host,
                                    port=self._mongodb_port,
                                    read_preference=ReadPreference.PRIMARY,
                                    **self._mongodb_options)
            self.db = Database(self.conn, 'admin')
            if self._mongodb_credentials:
                # NOTE: avoid a breaking chance since the version 0.5
                username = self._mongodb_credentials.get(
                    'name') or self._mongodb_credentials.get('username')
                self.db.authenticate(username,
                                     self._mongodb_credentials['password'])

            # NOTE: add the callable for each trigger
            self.cycle_op_triggers = []
            self.cycle_balancer_triggers = []

            for t_name, t_values in self.op_triggers.items():
                _callable = self._get_trigger_callable(t_name, t_values)
                if _callable:
                    self.cycle_op_triggers.append(_callable)

            for t_name, t_values in self.balancer_triggers.items():
                _callable = self._get_trigger_callable(t_name,
                                                       t_values,
                                                       category='balancer')
                if _callable:
                    self.cycle_balancer_triggers.append(_callable)

        except TypeError as e:
            logging.error(
                'unable to authenticate to admin database :: {}'.format(e))
            exit(1)
        except OperationFailure as e:
            logging.error('authentication failure :: {}'.format(e))
        except ConnectionFailure as e:
            logging.error('unable to connect to database :: {}'.format(e))
        else:
            logging.info('start mongoop :: {}'.format(self))

    def __str__(self):
        return u'{} :: frequency={} :: slow_query={} :: op_triggers={} :: balancer_triggers={}'.format(
            self.conn, self._frequency, self._base_op_query,
            len(self.cycle_op_triggers), len(self.cycle_balancer_triggers))

    def __call__(self):
        """ Main function.
        """
        while True:
            start = time()
            self.call_op_triggers()
            self.call_balancer_triggers()
            exec_time = time() - start
            if exec_time < self._frequency:
                sleep(self._frequency - exec_time)

    def call_op_triggers(self):
        """ Main function to run the op triggers.
        """
        operations = self._current_op()
        for trigger in self.cycle_op_triggers:
            trigger.run(operations=operations)

    def call_balancer_triggers(self):
        """ Main function to run the balancer triggers.
        """
        if not self.balancer_triggers:
            return True

        balancer_state = self._get_balancer_state()
        for trigger in self.cycle_balancer_triggers:
            trigger.run(balancer_state=balancer_state)

    def _get_trigger_callable(self,
                              trigger_name,
                              trigger_params,
                              category='op'):
        """ Retrieve the corresponding trigger by name and add into the triggers list.

        Args:
        """
        try:
            trigger_module = import_module('mongoop.triggers.{}'.format(
                trigger_params['type']))
            trigger_class = getattr(trigger_module, 'MongoopTrigger')
            trigger = trigger_class(name=trigger_name,
                                    params=trigger_params,
                                    mongoop=self,
                                    category=category)
        except Exception as e:
            logging.error(
                'unable to retrieve the trigger callable :: {}'.format(e))
        else:
            return trigger

    def _current_op(self):
        """ Get informations on operations currently running.
        """
        try:
            op_inprog = {}
            coll = self.db.get_collection("$cmd.sys.inprog")
            result = coll.find_one(self._base_op_query)
            op_inprog = result.get('inprog', {})
        except Exception as e:
            logging.error('unable to retrieve op :: {}'.format(e))
        else:
            if op_inprog:
                logging.info('found {} slow op'.format(len(op_inprog)))
            logging.debug('found {} slow op'.format(len(op_inprog)))
        finally:
            return op_inprog

    def _get_balancer_state(self):
        """ Return the balancer state.

            Returns:
                bool: True it's running, False otherwhise.
        """
        try:
            if self.conn.config.settings.find_one({
                    '_id': 'balancer',
                    'stopped': True
            }):
                logging.info('balancer state :: stopped')
                return False
            logging.info('balancer state :: started')
            return True
        except Exception as e:
            logging.error('unable to get the balancer state :: {}'.format(e))
Ejemplo n.º 14
0
Created by AFD on 2010-10-14.
Copyright (c) 2010 A. Frederick Dudley. All rights reserved.
"""

from stores.mongo_store import *
from stores.store import *
from pymongo.connection import Connection
from pymongo.database import Database

from binary_tactics.player import Player
from binary_tactics.helpers import *
from binary_tactics.weapons import *
from binary_tactics.units import *
from binary_tactics.stone import *

connection = Connection(host='bt.hipeland.org')
db = Database(connection, 'binary_tactics')
db.authenticate('rix', 'fhpxguvf'.decode('rot13'))

exists = {'$exists': True}
#squads = db.binary_tactics.find({'squad': exists})
#grids  = db.binary_tactics.find({'grid': exists})

#squad   = [convert_dict(get_dict(db.binary_tactics.find({'squad.value': 1004})[0], db.binary_tactics, db))]
squad = [rand_squad()]
units = [n for n in squad[0]]
weapons = [n.weapon for n in squad[0]]
stones = [rand_comp() for n in xrange(6)]
w = Player('The World', squad, stones, units, weapons, [])