diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/leap/soledad/server/_config.py | 74 | 
1 files changed, 44 insertions, 30 deletions
| diff --git a/src/leap/soledad/server/_config.py b/src/leap/soledad/server/_config.py index 0d37879e..7fa01b38 100644 --- a/src/leap/soledad/server/_config.py +++ b/src/leap/soledad/server/_config.py @@ -19,9 +19,13 @@  import configparser  import os +from twisted.logger import Logger +  __all__ = ['get_config'] +logger = Logger() +  # make sure to update documentation if this default is changed.  DEFAULT_CONFIG_FILE = '/etc/soledad/soledad-server.conf'  CONFIG_DEFAULTS = { @@ -44,45 +48,55 @@ CONFIG_DEFAULTS = {  } -_config = None - - -def get_config(section='soledad-server'): -    global _config -    if not _config: -        fname = os.environ.get( -            'SOLEDAD_SERVER_CONFIG_FILE', DEFAULT_CONFIG_FILE) -        _config = _load_config(fname) -    return _config[section] - - -def _load_config(file_path): -    """ -    Load server configuration from file. - -    @param file_path: The path to the configuration file. -    @type file_path: str - -    @return: A dictionary with the configuration. -    @rtype: dict -    """ +def _load_from_file(file_path): +    logger.info('Loading configuration from %s' % file_path)      conf = dict(CONFIG_DEFAULTS) -    config = configparser.SafeConfigParser() -    config.read(file_path) +    parsed = configparser.SafeConfigParser() +    parsed.read(file_path)      for section in conf: -        if not config.has_section(section): +        if not parsed.has_section(section):              continue          for key, value in conf[section].items(): -            if not config.has_option(section, key): +            if not parsed.has_option(section, key):                  continue              elif type(value) == bool: -                conf[section][key] = config.getboolean(section, key) +                conf[section][key] = parsed.getboolean(section, key)              elif type(value) == list: -                values = config.get(section, key).split(',') +                values = parsed.get(section, key).split(',')                  values = [v.strip() for v in values]                  conf[section][key] = values              else: -                conf[section][key] = config.get(section, key) +                conf[section][key] = parsed.get(section, key)      # TODO: implement basic parsing/sanitization of options comming from -    # config file. +    # parsed file. +    return conf + + +def _reflect_environment(conf): +    from_environment = ['couch_url'] +    for option in from_environment: +        name = 'SOLEDAD_%s' % option.upper() +        value = os.environ.get(name) +        if value: +            logger.info('Using %s=%s because of %s environment variable.' +                        % (option, value, name)) +            conf['soledad-server'][option] = value      return conf + + +def _load_config(file_path): +    conf = _load_from_file(file_path) +    conf = _reflect_environment(conf) +    return conf + + +_config = None + + +def get_config(section='soledad-server'): +    global _config +    if not _config: +        fname = os.environ.get( +            'SOLEDAD_SERVER_CONFIG_FILE', DEFAULT_CONFIG_FILE) +        _config = _load_config(fname) +    return _config[section] | 
