config: move env expansion to common code for EE usage
This commit is contained in:
parent
bfaa4b9416
commit
ab6095b38c
2 changed files with 39 additions and 37 deletions
|
|
@ -85,3 +85,38 @@ def load_pyramid_environment(global_config, settings):
|
|||
|
||||
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
|
||||
|
|
@ -38,7 +38,7 @@ from pyramid.renderers import render_to_response
|
|||
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, **settings):
|
|||
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, **settings):
|
|||
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, default, lower=True, default_when_empty=Fals
|
|||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue