##// 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 ; configure logging automatically at server startup set to false
216 ; configure logging automatically at server startup set to false
217 ; to use the below custom logging config.
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 #logging.autoconfigure = true
222 #logging.autoconfigure = true
219
223
220 ; specify your own custom logging config file to configure logging
224 ; specify your own custom logging config file to configure logging
@@ -223,6 +227,7 b' core.binary_dir = ""'
223 ; #####################
227 ; #####################
224 ; LOGGING CONFIGURATION
228 ; LOGGING CONFIGURATION
225 ; #####################
229 ; #####################
230
226 #[loggers]
231 #[loggers]
227 #keys = root, vcsserver
232 #keys = root, vcsserver
228
233
@@ -178,6 +178,10 b' core.binary_dir = ""'
178
178
179 ; configure logging automatically at server startup set to false
179 ; configure logging automatically at server startup set to false
180 ; to use the below custom logging config.
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 #logging.autoconfigure = true
185 #logging.autoconfigure = true
182
186
183 ; specify your own custom logging config file to configure logging
187 ; specify your own custom logging config file to configure logging
@@ -186,6 +190,7 b' core.binary_dir = ""'
186 ; #####################
190 ; #####################
187 ; LOGGING CONFIGURATION
191 ; LOGGING CONFIGURATION
188 ; #####################
192 ; #####################
193
189 #[loggers]
194 #[loggers]
190 #keys = root, vcsserver
195 #keys = root, vcsserver
191
196
@@ -27,6 +27,11 b' import tempfile'
27 import logging.config
27 import logging.config
28 log = logging.getLogger(__name__)
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 def str2bool(_str):
36 def str2bool(_str):
32 """
37 """
@@ -119,6 +124,27 b' class SettingsMaker(object):'
119 def _key_transformator(cls, key):
124 def _key_transformator(cls, key):
120 return "{}_{}".format('RC'.upper(), key.upper().replace('.', '_').replace('-', '_'))
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 def enable_logging(self, logging_conf=None, level='INFO', formatter='generic'):
148 def enable_logging(self, logging_conf=None, level='INFO', formatter='generic'):
123 """
149 """
124 Helper to enable debug on running instance
150 Helper to enable debug on running instance
@@ -133,7 +159,8 b' class SettingsMaker(object):'
133 logging_conf = self.settings.get('logging.logging_conf_file') or ''
159 logging_conf = self.settings.get('logging.logging_conf_file') or ''
134
160
135 if not os.path.isfile(logging_conf):
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 return
164 return
138
165
139 with open(logging_conf, 'rb') as f:
166 with open(logging_conf, 'rb') as f:
@@ -143,14 +170,6 b' class SettingsMaker(object):'
143 RC_LOGGING_FORMATTER=os.environ.get('RC_LOGGING_FORMATTER', '') or formatter
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 with tempfile.NamedTemporaryFile(prefix='rc_logging_', suffix='.ini', delete=False) as f:
173 with tempfile.NamedTemporaryFile(prefix='rc_logging_', suffix='.ini', delete=False) as f:
155 log.info('Saved Temporary LOGGING config at %s', f.name)
174 log.info('Saved Temporary LOGGING config at %s', f.name)
156 f.write(ini_template)
175 f.write(ini_template)
@@ -159,7 +178,6 b' class SettingsMaker(object):'
159 os.remove(f.name)
178 os.remove(f.name)
160
179
161 def make_setting(self, key, default, lower=False, default_when_empty=False, parser=None):
180 def make_setting(self, key, default, lower=False, default_when_empty=False, parser=None):
162
163 input_val = self.settings.get(key, default)
181 input_val = self.settings.get(key, default)
164
182
165 if default_when_empty and not input_val:
183 if default_when_empty and not input_val:
@@ -180,11 +198,10 b' class SettingsMaker(object):'
180 None: lambda i: i
198 None: lambda i: i
181 }[parser]
199 }[parser]
182
200
183 # now maybe we have this KEY in env, search and use the value with higher priority.
201 envvar_value = self.maybe_env_key(key)
184 transformed_key = self._key_transformator(key)
185 envvar_value = os.environ.get(transformed_key)
186 if envvar_value:
202 if envvar_value:
187 log.debug('using `%s` key instead of `%s` key for config', transformed_key, key)
188 input_val = envvar_value
203 input_val = envvar_value
204 set_keys[key] = input_val
205
189 self.settings[key] = parser_func(input_val)
206 self.settings[key] = parser_func(input_val)
190 return self.settings[key]
207 return self.settings[key]
@@ -645,35 +645,21 b' def sanitize_settings_and_apply_defaults'
645 global_settings_maker = SettingsMaker(global_config)
645 global_settings_maker = SettingsMaker(global_config)
646 settings_maker = SettingsMaker(settings)
646 settings_maker = SettingsMaker(settings)
647
647
648 settings_maker.make_setting(
648 settings_maker.make_setting('logging.autoconfigure', True, parser='bool')
649 'logging.autoconfigure',
650 default=True,
651 parser='bool')
652
649
653 logging_conf = os.path.join(os.path.dirname(global_config.get('__file__')), 'logging.ini')
650 logging_conf = os.path.join(os.path.dirname(global_config.get('__file__')), 'logging.ini')
654 settings_maker.enable_logging(logging_conf)
651 settings_maker.enable_logging(logging_conf)
655
652
656 # Default includes, possible to change as a user
653 # Default includes, possible to change as a user
657 pyramid_includes = settings_maker.make_setting('pyramid.includes', [], parser='list:newline')
654 pyramid_includes = settings_maker.make_setting('pyramid.includes', [], parser='list:newline')
658 log.debug(
655 log.debug("Using the following pyramid.includes: %s", pyramid_includes)
659 "Using the following pyramid.includes: %s",
660 pyramid_includes)
661
656
662 settings_maker.make_setting('__file__', global_config.get('__file__'))
657 settings_maker.make_setting('__file__', global_config.get('__file__'))
663
658
664 settings_maker.make_setting(
659 settings_maker.make_setting('pyramid.default_locale_name', 'en')
665 'pyramid.default_locale_name',
660 settings_maker.make_setting('locale', 'en_US.UTF-8')
666 default='en',
667 parser='string')
668 settings_maker.make_setting(
669 'locale',
670 default='en_US.UTF-8',
671 parser='string')
672
661
673 settings_maker.make_setting(
662 settings_maker.make_setting('core.binary_dir', '')
674 'core.binary_dir',
675 default='',
676 parser='string')
677
663
678 temp_store = tempfile.gettempdir()
664 temp_store = tempfile.gettempdir()
679 default_cache_dir = os.path.join(temp_store, 'rc_cache')
665 default_cache_dir = os.path.join(temp_store, 'rc_cache')
@@ -699,32 +685,19 b' def sanitize_settings_and_apply_defaults'
699 'rc_cache.repo_object.expiration_time',
685 'rc_cache.repo_object.expiration_time',
700 default=30 * 24 * 60 * 60, # 30days
686 default=30 * 24 * 60 * 60, # 30days
701 parser='int')
687 parser='int')
702 settings_maker. make_setting(
688 settings_maker.make_setting(
703 'rc_cache.repo_object.arguments.filename',
689 'rc_cache.repo_object.arguments.filename',
704 default=os.path.join(default_cache_dir, 'vcsserver_cache_repo_object.db'),
690 default=os.path.join(default_cache_dir, 'vcsserver_cache_repo_object.db'),
705 parser='string')
691 parser='string')
706
692
707 # statsd
693 # statsd
708 settings_maker. make_setting(
694 settings_maker.make_setting('statsd.enabled', False, parser='bool')
709 'statsd.enabled',
695 settings_maker.make_setting('statsd.statsd_host', 'statsd-exporter', parser='string')
710 default=False,
696 settings_maker.make_setting('statsd.statsd_port', 9125, parser='int')
711 parser='bool')
697 settings_maker.make_setting('statsd.statsd_prefix', '')
712 settings_maker. make_setting(
698 settings_maker.make_setting('statsd.statsd_ipv6', False, parser='bool')
713 'statsd.statsd_host',
699
714 default='statsd-exporter',
700 settings_maker.env_expand()
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')
728
701
729
702
730 def main(global_config, **settings):
703 def main(global_config, **settings):
General Comments 0
You need to be logged in to leave comments. Login now