2015-07-24 18:52:19 +00:00
|
|
|
import logging
|
2016-10-10 20:23:42 +00:00
|
|
|
import yaml
|
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
from six import add_metaclass
|
|
|
|
|
2017-12-01 15:46:39 +00:00
|
|
|
from jsonschema import validate, ValidationError
|
|
|
|
|
|
|
|
from util.config.schema import CONFIG_SCHEMA
|
2015-10-30 19:48:29 +00:00
|
|
|
|
2015-07-24 18:52:19 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-10-10 20:23:42 +00:00
|
|
|
|
2015-07-24 18:52:19 +00:00
|
|
|
class CannotWriteConfigException(Exception):
|
|
|
|
""" Exception raised when the config cannot be written. """
|
|
|
|
pass
|
|
|
|
|
2016-10-10 20:23:42 +00:00
|
|
|
|
2015-10-30 19:48:29 +00:00
|
|
|
class SetupIncompleteException(Exception):
|
|
|
|
""" Exception raised when attempting to verify config that has not yet been setup. """
|
|
|
|
pass
|
|
|
|
|
2016-10-10 20:23:42 +00:00
|
|
|
|
2015-07-24 18:52:19 +00:00
|
|
|
def import_yaml(config_obj, config_file):
|
|
|
|
with open(config_file) as f:
|
|
|
|
c = yaml.safe_load(f)
|
|
|
|
if not c:
|
|
|
|
logger.debug('Empty YAML config file')
|
|
|
|
return
|
|
|
|
|
|
|
|
if isinstance(c, str):
|
|
|
|
raise Exception('Invalid YAML config file: ' + str(c))
|
|
|
|
|
|
|
|
for key in c.iterkeys():
|
|
|
|
if key.isupper():
|
|
|
|
config_obj[key] = c[key]
|
|
|
|
|
2017-12-01 15:46:39 +00:00
|
|
|
if config_obj.get('SETUP_COMPLETE', True):
|
|
|
|
try:
|
|
|
|
validate(config_obj, CONFIG_SCHEMA)
|
|
|
|
except ValidationError:
|
|
|
|
# TODO: Change this into a real error
|
|
|
|
logger.exception('Could not validate config schema')
|
|
|
|
else:
|
|
|
|
logger.debug('Skipping config schema validation because setup is not complete')
|
|
|
|
|
2015-07-24 18:52:19 +00:00
|
|
|
return config_obj
|
|
|
|
|
|
|
|
|
2015-07-27 15:17:44 +00:00
|
|
|
def get_yaml(config_obj):
|
|
|
|
return yaml.safe_dump(config_obj, encoding='utf-8', allow_unicode=True)
|
|
|
|
|
2016-10-10 20:23:42 +00:00
|
|
|
|
2015-07-24 18:52:19 +00:00
|
|
|
def export_yaml(config_obj, config_file):
|
|
|
|
try:
|
|
|
|
with open(config_file, 'w') as f:
|
2015-07-27 15:17:44 +00:00
|
|
|
f.write(get_yaml(config_obj))
|
2015-07-24 18:52:19 +00:00
|
|
|
except IOError as ioe:
|
|
|
|
raise CannotWriteConfigException(str(ioe))
|
|
|
|
|
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@add_metaclass(ABCMeta)
|
2015-07-24 18:52:19 +00:00
|
|
|
class BaseProvider(object):
|
|
|
|
""" A configuration provider helps to load, save, and handle config override in the application.
|
|
|
|
"""
|
|
|
|
|
2015-07-27 15:17:44 +00:00
|
|
|
@property
|
|
|
|
def provider_id(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2015-07-24 18:52:19 +00:00
|
|
|
def update_app_config(self, app_config):
|
|
|
|
""" Updates the given application config object with the loaded override config. """
|
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2015-07-27 15:17:44 +00:00
|
|
|
def get_config(self):
|
|
|
|
""" Returns the contents of the config override file, or None if none. """
|
2015-07-24 18:52:19 +00:00
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2015-07-27 15:17:44 +00:00
|
|
|
def save_config(self, config_object):
|
|
|
|
""" Updates the contents of the config override file to those given. """
|
2015-07-24 18:52:19 +00:00
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2015-07-27 15:17:44 +00:00
|
|
|
def config_exists(self):
|
|
|
|
""" Returns true if a config override file exists in the config volume. """
|
2015-07-24 18:52:19 +00:00
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2015-07-24 18:52:19 +00:00
|
|
|
def volume_exists(self):
|
|
|
|
""" Returns whether the config override volume exists. """
|
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2018-05-10 13:44:18 +00:00
|
|
|
def volume_file_exists(self, relative_file_path):
|
|
|
|
""" Returns whether the file with the given relative path exists under the config override
|
|
|
|
volume. """
|
2015-07-24 18:52:19 +00:00
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2018-05-10 13:44:18 +00:00
|
|
|
def get_volume_file(self, relative_file_path, mode='r'):
|
|
|
|
""" Returns a Python file referring to the given path under the config override volume. """
|
2015-12-08 20:00:50 +00:00
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2018-05-10 13:44:18 +00:00
|
|
|
def remove_volume_file(self, relative_file_path):
|
|
|
|
""" Removes the config override volume file with the given path. """
|
2016-12-09 22:37:26 +00:00
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2016-12-09 22:37:26 +00:00
|
|
|
def list_volume_directory(self, path):
|
|
|
|
""" Returns a list of strings representing the names of the files found in the config override
|
|
|
|
directory under the given path. If the path doesn't exist, returns None.
|
|
|
|
"""
|
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2018-05-10 13:44:18 +00:00
|
|
|
def save_volume_file(self, flask_file, relative_file_path):
|
2015-07-24 18:52:19 +00:00
|
|
|
""" Saves the given flask file to the config override volume, with the given
|
2018-05-10 13:44:18 +00:00
|
|
|
relative path.
|
2015-07-24 18:52:19 +00:00
|
|
|
"""
|
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2015-07-24 18:52:19 +00:00
|
|
|
def requires_restart(self, app_config):
|
|
|
|
""" If true, the configuration loaded into memory for the app does not match that on disk,
|
|
|
|
indicating that this container requires a restart.
|
|
|
|
"""
|
2017-12-01 15:46:39 +00:00
|
|
|
|
2018-04-06 21:23:50 +00:00
|
|
|
@abstractmethod
|
2017-05-23 19:43:21 +00:00
|
|
|
def get_volume_path(self, directory, filename):
|
2018-05-10 13:44:18 +00:00
|
|
|
""" Helper for constructing relative file paths, which may differ between providers.
|
|
|
|
For example, kubernetes can't have subfolders in configmaps """
|
2018-05-29 17:50:51 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_config_root(self):
|
|
|
|
""" Returns the config root directory. """
|
|
|
|
|