Пример #1
0
def file_config(filename=None):
    """Returns the config values found in a configuration file.

    Args:
        filename (str): the JSON file with the configuration values.
            If ``None``, CONFIG_DEFAULT_PATH will be used.

    Returns:
        dict: The config values in the specified config file (or the
              file at CONFIG_DEFAULT_PATH, if filename == None)
    """
    logger.debug('On entry into file_config(), filename = {}'.format(filename))

    if filename is None:
        filename = CONFIG_DEFAULT_PATH

    logger.debug('file_config() will try to open `{}`'.format(filename))
    with open(filename) as f:
        try:
            config = json.load(f)
        except ValueError as err:
            raise exceptions.ConfigurationError(
                'Failed to parse the JSON configuration from `{}`, {}'.format(
                    filename, err))

        logger.info('Configuration loaded from `{}`'.format(filename))

    return config
Пример #2
0
def _get_replica_set_name(conn):
    """Checks if the replSet option was enabled either through the command
       line option or config file.

       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`).

       Returns:
           The replica set name if enabled.

        Raise:
            :exc:`~ConfigurationError`: If mongod was not started with the
            replSet option.
    """
    options = conn.conn.admin.command('getCmdLineOpts')
    try:
        repl_opts = options['parsed']['replication']
        return repl_opts.get('replSetName', None) or repl_opts['replSet']
    except KeyError:
        raise exceptions.ConfigurationError('mongod was not started with'
                                            ' the replSet option.')