def _check_replica_set(conn): """Checks if the replSet option was enabled either through the command line option or config file and if it matches the one provided by bigchaindb configuration. Note: The setting we are looking for will have a different name depending if it was set by the config file (`replSetName`) or by command line arguments (`replSet`). Raise: :exc:`~ConfigurationError`: If mongod was not started with the replSet option. """ options = conn.admin.command('getCmdLineOpts') try: repl_opts = options['parsed']['replication'] repl_set_name = repl_opts.get('replSetName', repl_opts.get('replSet')) except KeyError: raise ConfigurationError('mongod was not started with' ' the replSet option.') bdb_repl_set_name = bigchaindb.config['database']['replicaset'] if repl_set_name != bdb_repl_set_name: raise ConfigurationError('The replicaset configuration of ' 'bigchaindb (`{}`) needs to match ' 'the replica set name from MongoDB' ' (`{}`)'.format(bdb_repl_set_name, repl_set_name))
def connect(backend=None, host=None, port=None, name=None, max_tries=None, connection_timeout=None, replicaset=None): """Create a new connection to the database backend. All arguments default to the current configuration's values if not given. Args: backend (str): the name of the backend to use. host (str): the host to connect to. port (int): the port to connect to. name (str): the name of the database to use. replicaset (str): the name of the replica set (only relevant for MongoDB connections). Returns: An instance of :class:`~bigchaindb.backend.connection.Connection` based on the given (or defaulted) :attr:`backend`. Raises: :exc:`~ConnectionError`: If the connection to the database fails. :exc:`~ConfigurationError`: If the given (or defaulted) :attr:`backend` is not supported or could not be loaded. """ backend = backend or bigchaindb.config['database']['backend'] host = host or bigchaindb.config['database']['host'] port = port or bigchaindb.config['database']['port'] dbname = name or bigchaindb.config['database']['name'] # Not sure how to handle this here. This setting is only relevant for # mongodb. # I added **kwargs for both RethinkDBConnection and MongoDBConnection # to handle these these additional args. In case of RethinkDBConnection # it just does not do anything with it. replicaset = replicaset or bigchaindb.config['database'].get('replicaset') try: module_name, _, class_name = BACKENDS[backend].rpartition('.') Class = getattr(import_module(module_name), class_name) except KeyError: raise ConfigurationError('Backend `{}` is not supported. ' 'BigchainDB currently supports {}'.format( backend, BACKENDS.keys())) except (ImportError, AttributeError) as exc: raise ConfigurationError( 'Error loading backend `{}`'.format(backend)) from exc logger.debug('Connection: {}'.format(Class)) return Class(host=host, port=port, dbname=dbname, max_tries=max_tries, connection_timeout=connection_timeout, replicaset=replicaset)
def connect(backend=None, host=None, port=None, name=None): """Create a new connection to the database backend. All arguments default to the current configuration's values if not given. Args: backend (str): the name of the backend to use. host (str): the host to connect to. port (int): the port to connect to. name (str): the name of the database to use. Returns: An instance of :class:`~bigchaindb.backend.connection.Connection` based on the given (or defaulted) :attr:`backend`. Raises: :exc:`~ConfigurationError`: If the given (or defaulted) :attr:`backend` is not supported or could not be loaded. """ backend = backend or bigchaindb.config['database']['backend'] host = host or bigchaindb.config['database']['host'] port = port or bigchaindb.config['database']['port'] dbname = name or bigchaindb.config['database']['name'] try: module_name, _, class_name = BACKENDS[backend].rpartition('.') Class = getattr(import_module(module_name), class_name) except KeyError: raise ConfigurationError('Backend `{}` is not supported. ' 'BigchainDB currently supports {}'.format( backend, BACKENDS.keys())) except (ImportError, AttributeError) as exc: raise ConfigurationError( 'Error loading backend `{}`'.format(backend)) from exc logger.debug('Connection: {}'.format(Class)) return Class(host, port, dbname)
def connect(backend=None, host=None, port=None, name=None, max_tries=None, connection_timeout=None, replicaset=None, ssl=None, login=None, password=None, ca_cert=None, certfile=None, keyfile=None, keyfile_passphrase=None, crlfile=None): """Create a new connection to the database backend. All arguments default to the current configuration's values if not given. Args: backend (str): the name of the backend to use. host (str): the host to connect to. port (int): the port to connect to. name (str): the name of the database to use. replicaset (str): the name of the replica set (only relevant for MongoDB connections). Returns: An instance of :class:`~bigchaindb.backend.connection.Connection` based on the given (or defaulted) :attr:`backend`. Raises: :exc:`~ConnectionError`: If the connection to the database fails. :exc:`~ConfigurationError`: If the given (or defaulted) :attr:`backend` is not supported or could not be loaded. :exc:`~AuthenticationError`: If there is a OperationFailure due to Authentication failure after connecting to the database. """ backend = backend or get_bigchaindb_config_value_or_key_error('backend') host = host or get_bigchaindb_config_value_or_key_error('host') port = port or get_bigchaindb_config_value_or_key_error('port') dbname = name or get_bigchaindb_config_value_or_key_error('name') # Not sure how to handle this here. This setting is only relevant for # mongodb. # I added **kwargs for both RethinkDBConnection and MongoDBConnection # to handle these these additional args. In case of RethinkDBConnection # it just does not do anything with it. # # UPD: RethinkDBConnection is not here anymore cause we no longer support RethinkDB. # The problem described above might be reconsidered next time we introduce a backend, # if it ever happens. replicaset = replicaset or get_bigchaindb_config_value('replicaset') ssl = ssl if ssl is not None else get_bigchaindb_config_value('ssl', False) login = login or get_bigchaindb_config_value('login') password = password or get_bigchaindb_config_value('password') ca_cert = ca_cert or get_bigchaindb_config_value('ca_cert') certfile = certfile or get_bigchaindb_config_value('certfile') keyfile = keyfile or get_bigchaindb_config_value('keyfile') keyfile_passphrase = keyfile_passphrase or get_bigchaindb_config_value( 'keyfile_passphrase', None) crlfile = crlfile or get_bigchaindb_config_value('crlfile') try: module_name, _, class_name = BACKENDS[backend].rpartition('.') Class = getattr(import_module(module_name), class_name) except KeyError: raise ConfigurationError('Backend `{}` is not supported. ' 'BigchainDB currently supports {}'.format( backend, BACKENDS.keys())) except (ImportError, AttributeError) as exc: raise ConfigurationError( 'Error loading backend `{}`'.format(backend)) from exc logger.debug('Connection: {}'.format(Class)) return Class(host=host, port=port, dbname=dbname, max_tries=max_tries, connection_timeout=connection_timeout, replicaset=replicaset, ssl=ssl, login=login, password=password, ca_cert=ca_cert, certfile=certfile, keyfile=keyfile, keyfile_passphrase=keyfile_passphrase, crlfile=crlfile)
def _normalize_log_level(level): try: return level.upper() except AttributeError as exc: raise ConfigurationError('Log level must be a string!') from exc