Beispiel #1
0
def connect(db=None,
            async_framework='asyncio',
            alias=DEFAULT_CONNECTION_NAME,
            **kwargs):
    """Connect to the database specified by the 'db' argument.

    Connection settings may be provided here as well if the database is not
    running on the default port on localhost. If authentication is needed,
    provide username and password arguments as well.

    Multiple databases are supported by using aliases.  Provide a separate
    `alias` to connect to a different instance of :program:`mongod`.

    Parameters are the same as for :func:`mongoengine.connection.connect`
    plus one:

    :param async_framework: Which asynchronous framework should be used.
      It can be `tornado` or `asyncio`. Defaults to `asyncio`.

    """

    clients = CLIENTS[async_framework]
    with MonkeyPatcher() as patcher:
        patcher.patch_db_clients(*clients)
        patcher.patch_sync_connections()
        ret = me_connect(db=db, alias=alias, **kwargs)

    # here we register a connection that will use the original pymongo
    # client and if used will block the process
    sync_alias = utils.get_sync_alias(alias)
    register_connection(sync_alias, db, **kwargs)

    return ret
Beispiel #2
0
    def ready(self):
        if not hasattr(settings, 'MONGODB_DATABASES'):
            raise ImproperlyConfigured(
                "Missing `MONGODB_DATABASES` in settings.py")

        for alias, conn_settings in settings.MONGODB_DATABASES.items():
            connection.register_connection(alias, **conn_settings)
Beispiel #3
0
    def setup(self):
        if self.alias in _connection_settings:
            return

        mongo_conf_object = self.container.config.get('MONGO') or defaultdict(
            dict)
        replica_set = os.environ.get(
            REPLICA_SET,
            mongo_conf_object.get(REPLICA_SET.lower(), self.replica_set))
        host = os.environ.get(MONGO_HOST,
                              mongo_conf_object.get('host', self.host))
        port = os.environ.get(MONGO_PORT,
                              mongo_conf_object.get('port', self.port))
        db = os.environ.get(MONGO_DB,
                            mongo_conf_object.get('db', self.db_name))

        # @TODO login/pass auth with MongoDB

        is_replica_set = replica_set is None
        register_connection(
            alias=self.alias,
            name=db,
            host=host,
            port=port,
            read_preference=ReadPreference.SECONDARY_PREFERRED
            if not is_replica_set else None,
            tz_aware=True,
            replicaSet=replica_set if not is_replica_set else None)
Beispiel #4
0
    def ready(self):
        if not hasattr(settings, 'MONGODB_DATABASES'):
            raise ImproperlyConfigured(
                "Missing `MONGODB_DATABASES` in settings.py")

        for alias, conn_settings in settings.MONGODB_DATABASES.items():
            logger.info("Registering connection '%s' with args: %s", alias,
                        conn_settings)
            connection.register_connection(alias, **conn_settings)
Beispiel #5
0
    def _pre_setup(self):
        """ (MongoTestMixin) -> (NoneType)
        create a new mongo connection.
        """
        super(MongoTestCase, self)._pre_setup()

        for alias, conn_settings in settings.MONGODB_DATABASES.items():
            connection.register_connection(alias, **conn_settings)

        self.db = connect(get_db())
Beispiel #6
0
 def drop_database_and_reconnect(self, reconnect=True):
     disconnect()
     disconnect('rhic_serve')
     self.db.drop_database(self.db_name)
     # Mongoengine sometimes doesn't recreate unique indexes
     # in between test runs, adding the below 'reset' to fix this
     # https://github.com/hmarr/mongoengine/issues/422
     QuerySet._reset_already_indexed()
     if reconnect:
         self.db = connect(self.db_name, tz_aware=True)
         register_connection('rhic_serve', self.db_name)
Beispiel #7
0
def prepare_mongodb_settings():
    global _settings_prepared
    if _settings_prepared:
        return
    if not hasattr(settings, 'MONGODB_DATABASES'):
        raise ImproperlyConfigured("Missing `MONGODB_DATABASES` "
                                   "in settings.py")

    for alias, conn_settings in settings.MONGODB_DATABASES.items():
        connection.register_connection(alias, **conn_settings)
    _settings_prepared = True
Beispiel #8
0
    def setup(self):
        config = {}
        if isinstance(self.config, six.text_type):
            config = dict(host=self.config)
        elif isinstance(self.config, dict):
            config = self.register_dictionary_config()
        elif isinstance(self.config, (list, tuple)):
            config = self.register_list_config()

        if config:
            if self.tz_aware:
                config['tz_aware'] = self.tz_aware
            register_connection(alias=self.alias,
                                document_class=self.document_class,
                                **config)
Beispiel #9
0
    def verify_connection_setting(self, alias, conn_properties):
        if alias in _connection_settings:
            return True

        register_connection(
            alias,
            db=conn_properties.get('db'),
            name=conn_properties.get('name'),
            host=conn_properties.get('host'),
            port=conn_properties.get('port'),
            read_preference=ReadPreference.PRIMARY,
            username=conn_properties.get('username'),
            password=conn_properties.get('password'),
            authentication_source=conn_properties.get('authentication_source'),
            authentication_mechanism=conn_properties.get(
                'authentication_mechanism'),
            **conn_properties.get('kwargs', {}))
    def setup_databases(self, **kwargs):
        from mongoengine.connection import connect, disconnect, register_connection

        # main mongo database
        # disconnect()
        connect(self.mongodb_name, **self._get_params())
        print('Creating mongo test database %s for alias \'default\'' %
              self.mongodb_name)

        # additional databases
        addition_aliases = self.get_addition_mongodb_aliases()
        for alias in addition_aliases:
            print('Creating mongo test database %s for alias \'%s\'' %
                  (self.mongodb_name, alias))
            disconnect(alias=alias)
            register_connection(alias, self.mongodb_name, **self._get_params())

        return super(TestRunnerWithMongo, self).setup_databases(**kwargs)
Beispiel #11
0
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

# Django settings for rhic_serve project.

from mongoengine import connect
from mongoengine.connection import register_connection

from splice.common import config
from splice.common.settings import *

MONGO_DATABASE_NAME = config.CONFIG.get('rhic_serve', 'db_name')
MONGO_DATABASE_HOST = config.CONFIG.get('rhic_serve', 'db_host')
# Connect to the mongo db
connect(MONGO_DATABASE_NAME, alias=MONGO_DATABASE_NAME, tz_aware=True,
        host=MONGO_DATABASE_HOST)
register_connection('default', MONGO_DATABASE_NAME, host=MONGO_DATABASE_HOST)

# Custom test runner to work with Mongo
TEST_RUNNER = 'rhic_serve.common.tests.MongoTestRunner'

LOGIN_URL = '/ui/'

MIDDLEWARE_CLASSES = \
    MIDDLEWARE_CLASSES + \
    ('rhic_serve.common.middleware.RestLoginMiddleware',)

ROOT_URLCONF = 'rhic_serve.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'rhic_serve.wsgi.application'
from splice.common.models import SpliceServer, ConsumerIdentity,  ProductUsage
from rhic_serve.rhic_rest.models import RHIC


# Logging config
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# DB Setup
# We need to connect to the checkin_service db and the rhic_serve db since
# RHIC's live in rhic_serve.
MONGO_RCS_DATABASE_NAME = 'checkin_service'
mongoengine.connect(MONGO_RCS_DATABASE_NAME, alias=MONGO_RCS_DATABASE_NAME)
register_connection('default', MONGO_RCS_DATABASE_NAME)
MONGO_RHIC_DATABASE_NAME = 'rhic_serve'
mongoengine.connect(MONGO_RHIC_DATABASE_NAME, alias=MONGO_RHIC_DATABASE_NAME)

# Facts for instances
fact1 = {"memory_dot_memtotal": "604836", "lscpu_dot_cpu_socket(s)": "1", "lscpu_dot_cpu(s)": "1"}
fact2 = {"memory_dot_memtotal": "9048360", "lscpu_dot_cpu_socket(s)": "2", "lscpu_dot_cpu(s)": "2"}
fact3 = {"memory_dot_memtotal": "16048360", "lscpu_dot_cpu_socket(s)": "4", "lscpu_dot_cpu(s)": "4"}
facts = [fact1, fact2, fact3]

# Each mac address pattern below will be used by the
# instances assigned to 1 rhic.
#
# Since the pattern only allows for the last 2 digits of the mac address to be
# used by the RHIC to generate unique mac addresses, this limits
# each rhic to 100 instances.  We can change this if needed.
Beispiel #13
0
    from .prod import *

elif MODERATION_PANEL_ENV == "prodpp":
    from .prodpp import *

elif MODERATION_PANEL_ENV == "pp":
    from .pp import *

elif MODERATION_PANEL_ENV == "dev":
    from .dev import *


from mongoengine import connection

for alias, conn_settings in MONGODB_DATABASES.items():
    connection.register_connection(alias, **conn_settings)

# import redis
# from rediscluster import RedisCluster
# from rediscluster.connection import ClusterConnectionPool
#
#
# class RedisConnection(object):
#     startup_nodes = [{"host": REDIS_CONFIG["host"], "port": REDIS_CONFIG["port"]}]
#
#     if PLATO_ADMIN_ENV not in ["prod", "prodpp"]:
#         connection_pool = redis.ConnectionPool(host=REDIS_CONFIG["host"],
#             port=REDIS_CONFIG["port"], db=REDIS_CONFIG["db"])
#     else:
#         connection_pool = ClusterConnectionPool(startup_nodes=startup_nodes,
#             skip_full_coverage_check=True)
Beispiel #14
0
 def setup(self):
     self._parse_config()
     for alias, mongo_uri in self.aliases.items():
         register_connection(alias=alias, host=mongo_uri)
Beispiel #15
0
 def setUp(self):
     register_connection('default', name='default')
Beispiel #16
0
from notes.models import *
from mongoengine.connection import connect, disconnect, register_connection

MONGODB_NAME = "testdb"

MONGO_DATABASE_OPTIONS = {
    "host": '127.0.0.1',
    "port": 27017,
    "username": '******',
    "password": '******',
}

register_connection('default', MONGODB_NAME, **MONGO_DATABASE_OPTIONS)

disconnect()
connect('testdb')

comment_1 = {"author": "Author1",
             "email": "*****@*****.**",
             "text": "First comment of Author1"}

comment_2 = {"author": "Author1",
             "email": "*****@*****.**",
             "text": "Second comment of Author1"}

comment_3 = {"author": "Author2",
             "email": "*****@*****.**",
             "text": "First comment of Author2"}

comment_4 = {"author": "Author2",
             "email": "*****@*****.**",
Beispiel #17
0
MONGO_DATABASE_NAME = config.CONFIG.get('report_server', 'db_name')
MONGO_DATABASE_HOST = config.CONFIG.get('report_server', 'db_host')
MONGO_DATABASE_NAME_CHECKIN = config.CONFIG.get('server', 'db_name')
MONGO_DATABASE_HOST_CHECKIN = config.CONFIG.get('server', 'db_host')
MONGO_DATABASE_NAME_RHICSERVE = config.CONFIG.get('rhic_serve', 'db_name')
MONGO_DATABASE_HOST_RHICSERVE = config.CONFIG.get('rhic_serve', 'db_host')

# Connect to the mongo databases.
connect(MONGO_DATABASE_NAME_CHECKIN, alias='checkin', tz_aware=True,
        host=MONGO_DATABASE_HOST_CHECKIN)
connect(MONGO_DATABASE_NAME_RHICSERVE, alias='rhic_serve', tz_aware=True,
        host=MONGO_DATABASE_HOST_RHICSERVE)
connect(MONGO_DATABASE_NAME, alias='results', tz_aware=True,
        host=MONGO_DATABASE_HOST)
register_connection('default', MONGO_DATABASE_NAME_RHICSERVE,
                    host=MONGO_DATABASE_HOST_RHICSERVE)      

# Custom test runner to work with Mongo
TEST_RUNNER = 'rhic_serve.common.tests.MongoTestRunner'

# Business Rules initialization
r = Rules()
r.init()
r.list_rules()

LOGIN_URL = '/ui/'

ROOT_URLCONF = 'report_server.splice_reports.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'splice_reports.wsgi.application'
 def spider_opened(self, spider):
     for alias, conn_settings in self.mongodb.items():
         connection.register_connection(alias, **conn_settings)