Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2013 The LUCI Authors. All rights reserved. | 1 # Copyright 2013 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed by the Apache v2.0 license that can be | 2 # Use of this source code is governed by the Apache v2.0 license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Instance specific settings.""" | 5 """Instance specific settings.""" |
| 6 | 6 |
| 7 from google.appengine.ext import ndb | 7 import logging |
| 8 import posixpath | |
| 8 | 9 |
| 9 from components.datastore_utils import config | 10 from components import config |
| 11 from components import gitiles | |
| 12 from components import net | |
| 13 from components import utils | |
| 14 from components.config import validation | |
| 15 | |
| 16 from proto import config_pb2 | |
| 17 | |
| 18 SETTINGS_CFG_FILENAME = 'settings.cfg' | |
| 10 | 19 |
| 11 | 20 |
| 12 class GlobalConfig(config.GlobalConfig): | 21 ConfigApi = config.ConfigApi |
| 13 """Application wide settings.""" | |
| 14 # id to inject into pages if applicable. | |
| 15 google_analytics = ndb.StringProperty(indexed=False, default='') | |
| 16 | |
| 17 # The number of seconds an old task can be deduped from. | |
| 18 reusable_task_age_secs = ndb.IntegerProperty( | |
| 19 indexed=False, default=7*24*60*60) | |
| 20 | |
| 21 # The amount of time that has to pass before a machine is considered dead. | |
| 22 bot_death_timeout_secs = ndb.IntegerProperty(default=10*60) | |
| 23 | |
| 24 # Enable ts_mon based monitoring. | |
| 25 enable_ts_monitoring = ndb.BooleanProperty(indexed=False, default=False) | |
| 26 | 22 |
| 27 | 23 |
| 28 def settings(fresh=False): | 24 @validation.self_rule(SETTINGS_CFG_FILENAME, config_pb2.SettingsCfg) |
| 29 """Loads GlobalConfig or a default one if not present. | 25 def validate_settings(_cfg, _ctx): |
| 26 """Validates settings.cfg file against proto message schema.""" | |
| 30 | 27 |
| 31 If fresh=True, a full fetch from NDB is done. | 28 |
| 29 @utils.memcache('config:get_configs_url', time=300) | |
|
M-A Ruel
2016/04/27 17:50:45
5 minutes is a long time. 1 minute would be more s
nodir
2016/04/27 18:37:21
This value almost never changes.
M-A Ruel
2016/04/27 18:51:19
but when it does, we don't want to wait. Furthermo
nodir
2016/04/27 19:08:52
changed to 1min
| |
| 30 def _get_configs_url(): | |
| 31 """Returns URL where luci-config fetches configs from.""" | |
| 32 url = None | |
| 33 try: | |
| 34 url = config.get_config_set_location(config.self_config_set()) | |
| 35 except net.Error: | |
| 36 logging.exception('Could not get configs URL') | |
| 37 return url or 'about:blank' | |
| 38 | |
| 39 | |
| 40 def _gitiles_url(configs_url, rev, path): | |
| 41 """URL to a directory in gitiles -> URL to a file at concrete revision.""" | |
| 42 try: | |
| 43 loc = gitiles.Location.parse(configs_url) | |
| 44 return str(loc._replace( | |
| 45 treeish=rev or loc.treeish, | |
| 46 path=posixpath.join(loc.path, path))) | |
| 47 except ValueError: | |
| 48 # Not a gitiles URL, return as is. | |
| 49 return configs_url | |
| 50 | |
| 51 | |
| 52 def _get_settings(): | |
| 53 """Returns (rev, cfg) where cfg is a parsed SettingsCfg message. | |
| 54 | |
| 55 The config is cached in the datastore. | |
| 32 """ | 56 """ |
| 33 if fresh: | 57 rev = None |
| 34 GlobalConfig.clear_cache() | 58 cfg = None |
| 35 return GlobalConfig.cached() | 59 try: |
| 60 # store_last_good=True tells config component to update the config file | |
| 61 # in a cron job. Here we just read from datastore. | |
| 62 rev, cfg = config.get_self_config( | |
| 63 SETTINGS_CFG_FILENAME, config_pb2.SettingsCfg, store_last_good=True) | |
| 64 except config.CannotLoadConfigError: | |
| 65 logging.exception('Could not load settings.cfg; using defaults') | |
| 66 if not cfg: | |
| 67 cfg = config_pb2.SettingsCfg( | |
| 68 reusable_task_age_secs=7*24*60*60, | |
| 69 bot_death_timeout_secs=10*60, | |
| 70 ) | |
|
M-A Ruel
2016/04/27 17:50:45
This doesn't match the code style of this project.
nodir
2016/04/27 18:37:21
you mean that the closing bracket must be on the s
M-A Ruel
2016/04/27 18:51:19
yes
| |
| 71 return rev, cfg | |
| 72 | |
| 73 | |
| 74 def settings_info(): | |
| 75 """Returns (settings, rev, rev_url) of the settings file. | |
| 76 | |
| 77 Where settings is a parsed SettingsCfg message, rev is the revision of the | |
| 78 config and rev_url is a URL of a human-consumable page that displays the | |
| 79 config. | |
| 80 """ | |
| 81 rev, cfg = _get_settings() | |
| 82 rev_url = _gitiles_url(_get_configs_url(), rev, SETTINGS_CFG_FILENAME) | |
| 83 return cfg, rev, rev_url | |
| 84 | |
| 85 | |
| 86 def settings(): | |
| 87 """Loads settings from an NDB-based cache or a default one if not present.""" | |
| 88 return _get_settings()[1] | |
| OLD | NEW |