Show More
@@ -85,3 +85,38 b' def load_pyramid_environment(global_conf' | |||||
85 |
|
85 | |||
86 | if vcs_server_enabled: |
|
86 | if vcs_server_enabled: | |
87 | connect_vcs(vcs_server_uri, utils.get_vcs_server_protocol(settings)) |
|
87 | connect_vcs(vcs_server_uri, utils.get_vcs_server_protocol(settings)) | |
|
88 | ||||
|
89 | ||||
|
90 | def get_rc_env_settings(prefix='ENV_{}'): | |||
|
91 | return {'ENV_{}'.format(key): value for key, value in os.environ.items()} | |||
|
92 | ||||
|
93 | ||||
|
94 | def substitute_values(mapping, substitutions): | |||
|
95 | result = {} | |||
|
96 | ||||
|
97 | try: | |||
|
98 | for key, value in mapping.items(): | |||
|
99 | # initialize without substitution first | |||
|
100 | result[key] = value | |||
|
101 | ||||
|
102 | # Note: Cannot use regular replacements, since they would clash | |||
|
103 | # with the implementation of ConfigParser. Using "format" instead. | |||
|
104 | try: | |||
|
105 | result[key] = value.format(**substitutions) | |||
|
106 | except KeyError as e: | |||
|
107 | env_var = '{}'.format(e.args[0]) | |||
|
108 | ||||
|
109 | msg = 'Failed to substitute: `{key}={{{var}}}` with environment entry. ' \ | |||
|
110 | 'Make sure your environment has {var} set, or remove this ' \ | |||
|
111 | 'variable from config file'.format(key=key, var=env_var) | |||
|
112 | ||||
|
113 | if env_var.startswith('ENV_'): | |||
|
114 | raise ValueError(msg) | |||
|
115 | else: | |||
|
116 | log.warning(msg) | |||
|
117 | ||||
|
118 | except ValueError as e: | |||
|
119 | log.warning('Failed to substitute ENV variable: %s', e) | |||
|
120 | result = mapping | |||
|
121 | ||||
|
122 | return result No newline at end of file |
@@ -38,7 +38,7 b' from pyramid.renderers import render_to_' | |||||
38 | from rhodecode.model import meta |
|
38 | from rhodecode.model import meta | |
39 | from rhodecode.config import patches |
|
39 | from rhodecode.config import patches | |
40 | from rhodecode.config import utils as config_utils |
|
40 | from rhodecode.config import utils as config_utils | |
41 | from rhodecode.config.environment import load_pyramid_environment |
|
41 | from rhodecode.config.environment import load_pyramid_environment, substitute_values, get_rc_env_settings | |
42 |
|
42 | |||
43 | import rhodecode.events |
|
43 | import rhodecode.events | |
44 | from rhodecode.lib.middleware.vcs import VCSMiddleware |
|
44 | from rhodecode.lib.middleware.vcs import VCSMiddleware | |
@@ -93,6 +93,9 b' def make_pyramid_app(global_config, **se' | |||||
93 | start_time = time.time() |
|
93 | start_time = time.time() | |
94 | log.info('Pyramid app config starting') |
|
94 | log.info('Pyramid app config starting') | |
95 |
|
95 | |||
|
96 | global_config = substitute_values(global_config, get_rc_env_settings()) | |||
|
97 | settings = substitute_values(settings, get_rc_env_settings()) | |||
|
98 | ||||
96 | # init and bootstrap StatsdClient |
|
99 | # init and bootstrap StatsdClient | |
97 | StatsdClient.setup(settings) |
|
100 | StatsdClient.setup(settings) | |
98 |
|
101 | |||
@@ -100,11 +103,6 b' def make_pyramid_app(global_config, **se' | |||||
100 | if debug: |
|
103 | if debug: | |
101 | enable_debug() |
|
104 | enable_debug() | |
102 |
|
105 | |||
103 | environ = {'ENV_{}'.format(key): value for key, value in os.environ.items()} |
|
|||
104 |
|
||||
105 | global_config = _substitute_values(global_config, environ) |
|
|||
106 | settings = _substitute_values(settings, environ) |
|
|||
107 |
|
||||
108 | sanitize_settings_and_apply_defaults(global_config, settings) |
|
106 | sanitize_settings_and_apply_defaults(global_config, settings) | |
109 |
|
107 | |||
110 | config = Configurator(settings=settings) |
|
108 | config = Configurator(settings=settings) | |
@@ -761,34 +759,3 b' def _string_setting(settings, name, defa' | |||||
761 | value = value.lower() |
|
759 | value = value.lower() | |
762 | settings[name] = value |
|
760 | settings[name] = value | |
763 | return settings[name] |
|
761 | return settings[name] | |
764 |
|
||||
765 |
|
||||
766 | def _substitute_values(mapping, substitutions): |
|
|||
767 | result = {} |
|
|||
768 |
|
||||
769 | try: |
|
|||
770 | for key, value in mapping.items(): |
|
|||
771 | # initialize without substitution first |
|
|||
772 | result[key] = value |
|
|||
773 |
|
||||
774 | # Note: Cannot use regular replacements, since they would clash |
|
|||
775 | # with the implementation of ConfigParser. Using "format" instead. |
|
|||
776 | try: |
|
|||
777 | result[key] = value.format(**substitutions) |
|
|||
778 | except KeyError as e: |
|
|||
779 | env_var = '{}'.format(e.args[0]) |
|
|||
780 |
|
||||
781 | msg = 'Failed to substitute: `{key}={{{var}}}` with environment entry. ' \ |
|
|||
782 | 'Make sure your environment has {var} set, or remove this ' \ |
|
|||
783 | 'variable from config file'.format(key=key, var=env_var) |
|
|||
784 |
|
||||
785 | if env_var.startswith('ENV_'): |
|
|||
786 | raise ValueError(msg) |
|
|||
787 | else: |
|
|||
788 | log.warning(msg) |
|
|||
789 |
|
||||
790 | except ValueError as e: |
|
|||
791 | log.warning('Failed to substitute ENV variable: %s', e) |
|
|||
792 | result = mapping |
|
|||
793 |
|
||||
794 | return result |
|
General Comments 0
You need to be logged in to leave comments.
Login now