2015-07-24 18:52:19 +00:00
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from util.config.provider.baseprovider import (BaseProvider, import_yaml, export_yaml,
|
|
|
|
CannotWriteConfigException)
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-01-26 20:15:40 +00:00
|
|
|
def _ensure_parent_dir(filepath):
|
|
|
|
""" Ensures that the parent directory of the given file path exists. """
|
|
|
|
try:
|
|
|
|
parentpath = os.path.abspath(os.path.join(filepath, os.pardir))
|
|
|
|
if not os.path.isdir(parentpath):
|
|
|
|
os.makedirs(parentpath)
|
|
|
|
except IOError as ioe:
|
|
|
|
raise CannotWriteConfigException(str(ioe))
|
|
|
|
|
|
|
|
|
2015-07-24 18:52:19 +00:00
|
|
|
class FileConfigProvider(BaseProvider):
|
|
|
|
""" Implementation of the config provider that reads the data from the file system. """
|
|
|
|
def __init__(self, config_volume, yaml_filename, py_filename):
|
|
|
|
self.config_volume = config_volume
|
|
|
|
self.yaml_filename = yaml_filename
|
|
|
|
self.py_filename = py_filename
|
|
|
|
|
|
|
|
self.yaml_path = os.path.join(config_volume, yaml_filename)
|
|
|
|
self.py_path = os.path.join(config_volume, py_filename)
|
|
|
|
|
2015-07-27 15:17:44 +00:00
|
|
|
@property
|
|
|
|
def provider_id(self):
|
|
|
|
return 'file'
|
|
|
|
|
2015-07-24 18:52:19 +00:00
|
|
|
def update_app_config(self, app_config):
|
|
|
|
if os.path.exists(self.py_path):
|
|
|
|
logger.debug('Applying config file: %s', self.py_path)
|
|
|
|
app_config.from_pyfile(self.py_path)
|
|
|
|
|
|
|
|
if os.path.exists(self.yaml_path):
|
|
|
|
logger.debug('Applying config file: %s', self.yaml_path)
|
|
|
|
import_yaml(app_config, self.yaml_path)
|
|
|
|
|
2015-07-27 15:17:44 +00:00
|
|
|
def get_config(self):
|
2015-07-24 18:52:19 +00:00
|
|
|
if not os.path.exists(self.yaml_path):
|
|
|
|
return None
|
|
|
|
|
|
|
|
config_obj = {}
|
|
|
|
import_yaml(config_obj, self.yaml_path)
|
|
|
|
return config_obj
|
|
|
|
|
2015-07-27 15:17:44 +00:00
|
|
|
def save_config(self, config_obj):
|
2015-07-24 18:52:19 +00:00
|
|
|
export_yaml(config_obj, self.yaml_path)
|
|
|
|
|
2015-07-27 15:17:44 +00:00
|
|
|
def config_exists(self):
|
2015-07-24 18:52:19 +00:00
|
|
|
return self.volume_file_exists(self.yaml_filename)
|
|
|
|
|
|
|
|
def volume_exists(self):
|
|
|
|
return os.path.exists(self.config_volume)
|
|
|
|
|
|
|
|
def volume_file_exists(self, filename):
|
|
|
|
return os.path.exists(os.path.join(self.config_volume, filename))
|
|
|
|
|
2016-10-13 18:53:50 +00:00
|
|
|
def get_volume_file(self, filename, mode='r'):
|
|
|
|
return open(os.path.join(self.config_volume, filename), mode=mode)
|
2015-12-08 20:00:50 +00:00
|
|
|
|
|
|
|
def write_volume_file(self, filename, contents):
|
|
|
|
filepath = os.path.join(self.config_volume, filename)
|
2017-01-26 20:15:40 +00:00
|
|
|
_ensure_parent_dir(filepath)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(filepath, mode='w') as f:
|
|
|
|
f.write(contents)
|
|
|
|
except IOError as ioe:
|
|
|
|
raise CannotWriteConfigException(str(ioe))
|
2015-12-08 20:00:50 +00:00
|
|
|
|
|
|
|
return filepath
|
2015-07-24 18:52:19 +00:00
|
|
|
|
2016-12-09 22:37:26 +00:00
|
|
|
def remove_volume_file(self, filename):
|
|
|
|
filepath = os.path.join(self.config_volume, filename)
|
|
|
|
os.remove(filepath)
|
|
|
|
|
|
|
|
def list_volume_directory(self, path):
|
|
|
|
dirpath = os.path.join(self.config_volume, path)
|
|
|
|
if not os.path.exists(dirpath):
|
|
|
|
return None
|
|
|
|
|
2017-01-11 23:45:46 +00:00
|
|
|
if not os.path.isdir(dirpath):
|
|
|
|
return None
|
|
|
|
|
2016-12-09 22:37:26 +00:00
|
|
|
return os.listdir(dirpath)
|
|
|
|
|
2015-07-24 18:52:19 +00:00
|
|
|
def save_volume_file(self, filename, flask_file):
|
2015-07-27 15:17:44 +00:00
|
|
|
filepath = os.path.join(self.config_volume, filename)
|
2017-01-26 20:15:40 +00:00
|
|
|
_ensure_parent_dir(filepath)
|
|
|
|
|
|
|
|
# Write the file.
|
2015-07-24 18:52:19 +00:00
|
|
|
try:
|
2015-07-27 15:17:44 +00:00
|
|
|
flask_file.save(filepath)
|
2015-07-24 18:52:19 +00:00
|
|
|
except IOError as ioe:
|
|
|
|
raise CannotWriteConfigException(str(ioe))
|
|
|
|
|
2015-07-27 15:17:44 +00:00
|
|
|
return filepath
|
|
|
|
|
2015-07-24 18:52:19 +00:00
|
|
|
def requires_restart(self, app_config):
|
2015-07-27 15:17:44 +00:00
|
|
|
file_config = self.get_config()
|
2015-07-24 18:52:19 +00:00
|
|
|
if not file_config:
|
|
|
|
return False
|
|
|
|
|
|
|
|
for key in file_config:
|
|
|
|
if app_config.get(key) != file_config[key]:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2017-05-17 12:12:09 +00:00
|
|
|
|
2017-05-23 19:43:21 +00:00
|
|
|
def get_volume_path(self, directory, filename):
|
|
|
|
return os.path.join(directory, filename)
|