##// END OF EJS Templates
env: made configuration completly overridable by env variables
super-admin -
r1023:28e26155 default
parent child Browse files
Show More
@@ -215,6 +215,10 b' core.binary_dir = ""'
215 215
216 216 ; configure logging automatically at server startup set to false
217 217 ; to use the below custom logging config.
218 ; RC_LOGGING_FORMATTER
219 ; RC_LOGGING_LEVEL
220 ; env variables can control the settings for logging in case of autoconfigure
221
218 222 #logging.autoconfigure = true
219 223
220 224 ; specify your own custom logging config file to configure logging
@@ -223,6 +227,7 b' core.binary_dir = ""'
223 227 ; #####################
224 228 ; LOGGING CONFIGURATION
225 229 ; #####################
230
226 231 #[loggers]
227 232 #keys = root, vcsserver
228 233
@@ -178,6 +178,10 b' core.binary_dir = ""'
178 178
179 179 ; configure logging automatically at server startup set to false
180 180 ; to use the below custom logging config.
181 ; RC_LOGGING_FORMATTER
182 ; RC_LOGGING_LEVEL
183 ; env variables can control the settings for logging in case of autoconfigure
184
181 185 #logging.autoconfigure = true
182 186
183 187 ; specify your own custom logging config file to configure logging
@@ -186,6 +190,7 b' core.binary_dir = ""'
186 190 ; #####################
187 191 ; LOGGING CONFIGURATION
188 192 ; #####################
193
189 194 #[loggers]
190 195 #keys = root, vcsserver
191 196
@@ -27,6 +27,11 b' import tempfile'
27 27 import logging.config
28 28 log = logging.getLogger(__name__)
29 29
30 # skip keys, that are set here, so we don't double process those
31 set_keys = {
32 '__file__': ''
33 }
34
30 35
31 36 def str2bool(_str):
32 37 """
@@ -119,6 +124,27 b' class SettingsMaker(object):'
119 124 def _key_transformator(cls, key):
120 125 return "{}_{}".format('RC'.upper(), key.upper().replace('.', '_').replace('-', '_'))
121 126
127 def maybe_env_key(self, key):
128 # now maybe we have this KEY in env, search and use the value with higher priority.
129 transformed_key = self._key_transformator(key)
130 envvar_value = os.environ.get(transformed_key)
131 if envvar_value:
132 log.debug('using `%s` key instead of `%s` key for config', transformed_key, key)
133
134 return envvar_value
135
136 def env_expand(self):
137 replaced = {}
138 for k, v in self.settings.items():
139 if k not in set_keys:
140 envvar_value = self.maybe_env_key(k)
141 if envvar_value:
142 replaced[k] = envvar_value
143 set_keys[k] = envvar_value
144
145 # replace ALL keys updated
146 self.settings.update(replaced)
147
122 148 def enable_logging(self, logging_conf=None, level='INFO', formatter='generic'):
123 149 """
124 150 Helper to enable debug on running instance
@@ -133,7 +159,8 b' class SettingsMaker(object):'
133 159 logging_conf = self.settings.get('logging.logging_conf_file') or ''
134 160
135 161 if not os.path.isfile(logging_conf):
136 log.error('Unable to setup logging based on %s, file does not exist...', logging_conf)
162 log.error('Unable to setup logging based on %s, '
163 'file does not exist.... specify path using logging.logging_conf_file= config setting. ', logging_conf)
137 164 return
138 165
139 166 with open(logging_conf, 'rb') as f:
@@ -143,14 +170,6 b' class SettingsMaker(object):'
143 170 RC_LOGGING_FORMATTER=os.environ.get('RC_LOGGING_FORMATTER', '') or formatter
144 171 )
145 172
146
147 with open(logging_conf, 'rb') as f:
148 ini_template = textwrap.dedent(f.read())
149 ini_template = string.Template(ini_template).safe_substitute(
150 RC_LOGGING_LEVEL=os.environ.get('RC_LOGGING_LEVEL', '') or 'INFO',
151 RC_LOGGING_FORMATTER=os.environ.get('RC_LOGGING_FORMATTER', '') or 'generic'
152 )
153
154 173 with tempfile.NamedTemporaryFile(prefix='rc_logging_', suffix='.ini', delete=False) as f:
155 174 log.info('Saved Temporary LOGGING config at %s', f.name)
156 175 f.write(ini_template)
@@ -159,7 +178,6 b' class SettingsMaker(object):'
159 178 os.remove(f.name)
160 179
161 180 def make_setting(self, key, default, lower=False, default_when_empty=False, parser=None):
162
163 181 input_val = self.settings.get(key, default)
164 182
165 183 if default_when_empty and not input_val:
@@ -180,11 +198,10 b' class SettingsMaker(object):'
180 198 None: lambda i: i
181 199 }[parser]
182 200
183 # now maybe we have this KEY in env, search and use the value with higher priority.
184 transformed_key = self._key_transformator(key)
185 envvar_value = os.environ.get(transformed_key)
201 envvar_value = self.maybe_env_key(key)
186 202 if envvar_value:
187 log.debug('using `%s` key instead of `%s` key for config', transformed_key, key)
188 203 input_val = envvar_value
204 set_keys[key] = input_val
205
189 206 self.settings[key] = parser_func(input_val)
190 207 return self.settings[key]
@@ -645,35 +645,21 b' def sanitize_settings_and_apply_defaults'
645 645 global_settings_maker = SettingsMaker(global_config)
646 646 settings_maker = SettingsMaker(settings)
647 647
648 settings_maker.make_setting(
649 'logging.autoconfigure',
650 default=True,
651 parser='bool')
648 settings_maker.make_setting('logging.autoconfigure', True, parser='bool')
652 649
653 650 logging_conf = os.path.join(os.path.dirname(global_config.get('__file__')), 'logging.ini')
654 651 settings_maker.enable_logging(logging_conf)
655 652
656 653 # Default includes, possible to change as a user
657 654 pyramid_includes = settings_maker.make_setting('pyramid.includes', [], parser='list:newline')
658 log.debug(
659 "Using the following pyramid.includes: %s",
660 pyramid_includes)
655 log.debug("Using the following pyramid.includes: %s", pyramid_includes)
661 656
662 657 settings_maker.make_setting('__file__', global_config.get('__file__'))
663 658
664 settings_maker.make_setting(
665 'pyramid.default_locale_name',
666 default='en',
667 parser='string')
668 settings_maker.make_setting(
669 'locale',
670 default='en_US.UTF-8',
671 parser='string')
659 settings_maker.make_setting('pyramid.default_locale_name', 'en')
660 settings_maker.make_setting('locale', 'en_US.UTF-8')
672 661
673 settings_maker.make_setting(
674 'core.binary_dir',
675 default='',
676 parser='string')
662 settings_maker.make_setting('core.binary_dir', '')
677 663
678 664 temp_store = tempfile.gettempdir()
679 665 default_cache_dir = os.path.join(temp_store, 'rc_cache')
@@ -705,26 +691,13 b' def sanitize_settings_and_apply_defaults'
705 691 parser='string')
706 692
707 693 # statsd
708 settings_maker. make_setting(
709 'statsd.enabled',
710 default=False,
711 parser='bool')
712 settings_maker. make_setting(
713 'statsd.statsd_host',
714 default='statsd-exporter',
715 parser='string')
716 settings_maker. make_setting(
717 'statsd.statsd_port',
718 default=9125,
719 parser='int')
720 settings_maker. make_setting(
721 'statsd.statsd_prefix',
722 default='',
723 parser='string')
724 settings_maker. make_setting(
725 'statsd.statsd_ipv6',
726 default=False,
727 parser='bool')
694 settings_maker.make_setting('statsd.enabled', False, parser='bool')
695 settings_maker.make_setting('statsd.statsd_host', 'statsd-exporter', parser='string')
696 settings_maker.make_setting('statsd.statsd_port', 9125, parser='int')
697 settings_maker.make_setting('statsd.statsd_prefix', '')
698 settings_maker.make_setting('statsd.statsd_ipv6', False, parser='bool')
699
700 settings_maker.env_expand()
728 701
729 702
730 703 def main(global_config, **settings):
General Comments 0
You need to be logged in to leave comments. Login now