| 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 import logging | 7 import logging |
| 8 import posixpath | 8 import posixpath |
| 9 | 9 |
| 10 from components import config | 10 from components import config |
| 11 from components import gitiles | 11 from components import gitiles |
| 12 from components import net | 12 from components import net |
| 13 from components import utils | 13 from components import utils |
| 14 from components.config import validation | 14 from components.config import validation |
| 15 | 15 |
| 16 from proto import config_pb2 | 16 from proto import config_pb2 |
| 17 from server import task_request |
| 18 |
| 17 | 19 |
| 18 SETTINGS_CFG_FILENAME = 'settings.cfg' | 20 SETTINGS_CFG_FILENAME = 'settings.cfg' |
| 21 SECONDS_IN_YEAR = 60 * 60 * 24 * 365 |
| 19 | 22 |
| 20 | 23 |
| 21 ConfigApi = config.ConfigApi | 24 ConfigApi = config.ConfigApi |
| 22 | 25 |
| 23 | 26 |
| 27 def validate_isolate_settings(cfg, ctx): |
| 28 if bool(cfg.default_server) != bool(cfg.default_namespace): |
| 29 ctx.error( |
| 30 'either specify both default_server and default_namespace or none') |
| 31 elif cfg.default_server: |
| 32 if not cfg.default_server.startswith(('https://', 'http://')): |
| 33 ctx.error('default_server must start with "https://" or "http://"') |
| 34 |
| 35 if not task_request.NAMESPACE_RE.match(cfg.default_namespace): |
| 36 ctx.error('invalid namespace "%s"', cfg.default_namespace) |
| 37 |
| 38 |
| 24 @validation.self_rule(SETTINGS_CFG_FILENAME, config_pb2.SettingsCfg) | 39 @validation.self_rule(SETTINGS_CFG_FILENAME, config_pb2.SettingsCfg) |
| 25 def validate_settings(cfg, ctx): | 40 def validate_settings(cfg, ctx): |
| 26 """Validates settings.cfg file against proto message schema.""" | 41 """Validates settings.cfg file against proto message schema.""" |
| 27 def within_year(value): | 42 def within_year(value): |
| 28 if value < 0: | 43 if value < 0: |
| 29 ctx.error('cannot be negative') | 44 ctx.error('cannot be negative') |
| 30 elif value > 60 * 60 * 24 * 365: | 45 elif value > SECONDS_IN_YEAR: |
| 31 ctx.error('cannot be more than a year') | 46 ctx.error('cannot be more than a year') |
| 32 | 47 |
| 33 with ctx.prefix('bot_death_timeout_secs '): | 48 with ctx.prefix('bot_death_timeout_secs '): |
| 34 within_year(cfg.bot_death_timeout_secs) | 49 within_year(cfg.bot_death_timeout_secs) |
| 35 with ctx.prefix('reusable_task_age_secs '): | 50 with ctx.prefix('reusable_task_age_secs '): |
| 36 within_year(cfg.reusable_task_age_secs) | 51 within_year(cfg.reusable_task_age_secs) |
| 37 | 52 |
| 53 if cfg.HasField('isolate'): |
| 54 with ctx.prefix('isolate: '): |
| 55 validate_isolate_settings(cfg.isolate, ctx) |
| 56 |
| 38 | 57 |
| 39 @utils.memcache('config:get_configs_url', time=60) | 58 @utils.memcache('config:get_configs_url', time=60) |
| 40 def _get_configs_url(): | 59 def _get_configs_url(): |
| 41 """Returns URL where luci-config fetches configs from.""" | 60 """Returns URL where luci-config fetches configs from.""" |
| 42 url = None | 61 url = None |
| 43 try: | 62 try: |
| 44 url = config.get_config_set_location(config.self_config_set()) | 63 url = config.get_config_set_location(config.self_config_set()) |
| 45 except net.Error: | 64 except net.Error: |
| 46 logging.info( | 65 logging.info( |
| 47 'Could not get configs URL. Possibly config directory for this ' | 66 'Could not get configs URL. Possibly config directory for this ' |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 'config_service_url': ( | 122 'config_service_url': ( |
| 104 'https://%s' % cfg_service_hostname if cfg_service_hostname else '' | 123 'https://%s' % cfg_service_hostname if cfg_service_hostname else '' |
| 105 ), | 124 ), |
| 106 } | 125 } |
| 107 | 126 |
| 108 | 127 |
| 109 @utils.cache_with_expiration(60) | 128 @utils.cache_with_expiration(60) |
| 110 def settings(): | 129 def settings(): |
| 111 """Loads settings from an NDB-based cache or a default one if not present.""" | 130 """Loads settings from an NDB-based cache or a default one if not present.""" |
| 112 return _get_settings()[1] | 131 return _get_settings()[1] |
| OLD | NEW |