diff --git a/rhodecode/config/environment.py b/rhodecode/config/environment.py --- a/rhodecode/config/environment.py +++ b/rhodecode/config/environment.py @@ -85,3 +85,38 @@ def load_pyramid_environment(global_conf if vcs_server_enabled: connect_vcs(vcs_server_uri, utils.get_vcs_server_protocol(settings)) + + +def get_rc_env_settings(prefix='ENV_{}'): + return {'ENV_{}'.format(key): value for key, value in os.environ.items()} + + +def substitute_values(mapping, substitutions): + result = {} + + try: + for key, value in mapping.items(): + # initialize without substitution first + result[key] = value + + # Note: Cannot use regular replacements, since they would clash + # with the implementation of ConfigParser. Using "format" instead. + try: + result[key] = value.format(**substitutions) + except KeyError as e: + env_var = '{}'.format(e.args[0]) + + msg = 'Failed to substitute: `{key}={{{var}}}` with environment entry. ' \ + 'Make sure your environment has {var} set, or remove this ' \ + 'variable from config file'.format(key=key, var=env_var) + + if env_var.startswith('ENV_'): + raise ValueError(msg) + else: + log.warning(msg) + + except ValueError as e: + log.warning('Failed to substitute ENV variable: %s', e) + result = mapping + + return result \ No newline at end of file diff --git a/rhodecode/config/middleware.py b/rhodecode/config/middleware.py --- a/rhodecode/config/middleware.py +++ b/rhodecode/config/middleware.py @@ -38,7 +38,7 @@ from pyramid.renderers import render_to_ from rhodecode.model import meta from rhodecode.config import patches from rhodecode.config import utils as config_utils -from rhodecode.config.environment import load_pyramid_environment +from rhodecode.config.environment import load_pyramid_environment, substitute_values, get_rc_env_settings import rhodecode.events from rhodecode.lib.middleware.vcs import VCSMiddleware @@ -93,6 +93,9 @@ def make_pyramid_app(global_config, **se start_time = time.time() log.info('Pyramid app config starting') + global_config = substitute_values(global_config, get_rc_env_settings()) + settings = substitute_values(settings, get_rc_env_settings()) + # init and bootstrap StatsdClient StatsdClient.setup(settings) @@ -100,11 +103,6 @@ def make_pyramid_app(global_config, **se if debug: enable_debug() - environ = {'ENV_{}'.format(key): value for key, value in os.environ.items()} - - global_config = _substitute_values(global_config, environ) - settings = _substitute_values(settings, environ) - sanitize_settings_and_apply_defaults(global_config, settings) config = Configurator(settings=settings) @@ -761,34 +759,3 @@ def _string_setting(settings, name, defa value = value.lower() settings[name] = value return settings[name] - - -def _substitute_values(mapping, substitutions): - result = {} - - try: - for key, value in mapping.items(): - # initialize without substitution first - result[key] = value - - # Note: Cannot use regular replacements, since they would clash - # with the implementation of ConfigParser. Using "format" instead. - try: - result[key] = value.format(**substitutions) - except KeyError as e: - env_var = '{}'.format(e.args[0]) - - msg = 'Failed to substitute: `{key}={{{var}}}` with environment entry. ' \ - 'Make sure your environment has {var} set, or remove this ' \ - 'variable from config file'.format(key=key, var=env_var) - - if env_var.startswith('ENV_'): - raise ValueError(msg) - else: - log.warning(msg) - - except ValueError as e: - log.warning('Failed to substitute ENV variable: %s', e) - result = mapping - - return result