| OLD | NEW |
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. | 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """Utilities for reading GCE Backend configuration.""" | 5 """Utilities for reading GCE Backend configuration.""" |
| 6 | 6 |
| 7 import collections | 7 import collections |
| 8 import logging | 8 import logging |
| 9 | 9 |
| 10 from components import utils | 10 from components import utils |
| 11 utils.fix_protobuf_package() | 11 utils.fix_protobuf_package() |
| 12 | 12 |
| 13 from google import protobuf | 13 from google import protobuf |
| 14 from google.appengine.ext import ndb | 14 from google.appengine.ext import ndb |
| 15 | 15 |
| 16 import metrics | 16 import metrics |
| 17 | 17 |
| 18 from components import config | 18 from components import config |
| 19 from components import datastore_utils | 19 from components import datastore_utils |
| 20 from components import utils |
| 20 from components.config import validation | 21 from components.config import validation |
| 21 | 22 |
| 22 from proto import config_pb2 | 23 from proto import config_pb2 |
| 23 | 24 |
| 24 | 25 |
| 25 TEMPLATES_CFG_FILENAME = 'templates.cfg' | 26 TEMPLATES_CFG_FILENAME = 'templates.cfg' |
| 26 MANAGERS_CFG_FILENAME = 'managers.cfg' | 27 MANAGERS_CFG_FILENAME = 'managers.cfg' |
| 28 SETTINGS_CFG_FILENAME = 'settings.cfg' |
| 27 | 29 |
| 28 | 30 |
| 29 class Configuration(datastore_utils.config.GlobalConfig): | 31 class Configuration(datastore_utils.config.GlobalConfig): |
| 30 """Configuration for this service.""" | 32 """Configuration for this service.""" |
| 31 # Text-formatted proto.config_pb2.InstanceTemplateConfig. | 33 # Text-formatted proto.config_pb2.InstanceTemplateConfig. |
| 32 template_config = ndb.TextProperty(default='') | 34 template_config = ndb.TextProperty(default='') |
| 33 # Text-formatted proto.config_pb2.InstanceGroupManagerConfig. | 35 # Text-formatted proto.config_pb2.InstanceGroupManagerConfig. |
| 34 manager_config = ndb.TextProperty(default='') | 36 manager_config = ndb.TextProperty(default='') |
| 35 # Revision of the configs. | 37 # Revision of the configs. |
| 36 revision = ndb.StringProperty(default='') | 38 revision = ndb.StringProperty(default='') |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 context.error( | 135 context.error( |
| 134 'zone %s is not unique in template %s.', | 136 'zone %s is not unique in template %s.', |
| 135 manager.zone, | 137 manager.zone, |
| 136 manager.template_base_name, | 138 manager.template_base_name, |
| 137 ) | 139 ) |
| 138 valid = False | 140 valid = False |
| 139 else: | 141 else: |
| 140 zones[manager.template_base_name].add(manager.zone) | 142 zones[manager.template_base_name].add(manager.zone) |
| 141 | 143 |
| 142 metrics.config_valid.set(valid, fields={'config': MANAGERS_CFG_FILENAME}) | 144 metrics.config_valid.set(valid, fields={'config': MANAGERS_CFG_FILENAME}) |
| 145 |
| 146 |
| 147 @validation.self_rule(SETTINGS_CFG_FILENAME, config_pb2.SettingsCfg) |
| 148 def validate_settings_config(config, context): |
| 149 pass |
| 150 |
| 151 |
| 152 def _get_settings(): |
| 153 """Returns (rev, cfg) where cfg is a parsed SettingsCfg message. |
| 154 |
| 155 The config is cached in the datastore. |
| 156 """ |
| 157 rev, cfg = config.get_self_config( |
| 158 SETTINGS_CFG_FILENAME, config_pb2.SettingsCfg, store_last_good=True) |
| 159 cfg = cfg or config_pb2.SettingsCfg() |
| 160 return rev, cfg |
| 161 |
| 162 |
| 163 @utils.cache_with_expiration(60) |
| 164 def settings(): |
| 165 """Loads settings from an NDB-based cache or a default one if not present.""" |
| 166 return _get_settings()[1] |
| OLD | NEW |