2018-06-28 17:45:26 +00:00
|
|
|
import os
|
2018-08-15 21:17:41 +00:00
|
|
|
|
2018-08-27 21:05:53 +00:00
|
|
|
from shutil import copytree
|
2018-06-28 17:45:26 +00:00
|
|
|
from backports.tempfile import TemporaryDirectory
|
|
|
|
|
|
|
|
from config_app.config_util.config.fileprovider import FileConfigProvider
|
2018-08-08 18:22:28 +00:00
|
|
|
|
2018-08-27 21:05:53 +00:00
|
|
|
OLD_CONFIG_SUBDIR = 'old/'
|
2018-06-28 17:45:26 +00:00
|
|
|
|
|
|
|
class TransientDirectoryProvider(FileConfigProvider):
|
2018-08-15 19:32:24 +00:00
|
|
|
""" Implementation of the config provider that reads and writes the data
|
|
|
|
from/to the file system, only using temporary directories,
|
|
|
|
deleting old dirs and creating new ones as requested.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, config_volume, yaml_filename, py_filename):
|
|
|
|
# Create a temp directory that will be cleaned up when we change the config path
|
|
|
|
# This should ensure we have no "pollution" of different configs:
|
|
|
|
# no uploaded config should ever affect subsequent config modifications/creations
|
|
|
|
temp_dir = TemporaryDirectory()
|
|
|
|
self.temp_dir = temp_dir
|
2018-08-27 21:05:53 +00:00
|
|
|
self.old_config_dir = None
|
2018-08-15 19:32:24 +00:00
|
|
|
super(TransientDirectoryProvider, self).__init__(temp_dir.name, yaml_filename, py_filename)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def provider_id(self):
|
|
|
|
return 'transient'
|
|
|
|
|
|
|
|
def new_config_dir(self):
|
2018-06-28 17:45:26 +00:00
|
|
|
"""
|
2018-08-15 19:32:24 +00:00
|
|
|
Update the path with a new temporary directory, deleting the old one in the process
|
|
|
|
"""
|
|
|
|
self.temp_dir.cleanup()
|
|
|
|
temp_dir = TemporaryDirectory()
|
|
|
|
|
|
|
|
self.config_volume = temp_dir.name
|
|
|
|
self.temp_dir = temp_dir
|
|
|
|
self.yaml_path = os.path.join(temp_dir.name, self.yaml_filename)
|
|
|
|
|
2018-08-27 21:05:53 +00:00
|
|
|
def create_copy_of_config_dir(self):
|
|
|
|
"""
|
|
|
|
Create a directory to store loaded/populated configuration (for rollback if necessary)
|
|
|
|
"""
|
|
|
|
if self.old_config_dir is not None:
|
|
|
|
self.old_config_dir.cleanup()
|
2018-08-15 21:17:41 +00:00
|
|
|
|
2018-08-27 21:05:53 +00:00
|
|
|
temp_dir = TemporaryDirectory()
|
|
|
|
self.old_config_dir = temp_dir
|
2018-08-15 19:32:24 +00:00
|
|
|
|
2018-08-27 21:05:53 +00:00
|
|
|
# Python 2.7's shutil.copy() doesn't allow for copying to existing directories,
|
|
|
|
# so when copying/reading to the old saved config, we have to talk to a subdirectory,
|
|
|
|
# and use the shutil.copytree() function
|
|
|
|
copytree(self.config_volume, os.path.join(temp_dir.name, OLD_CONFIG_SUBDIR))
|
2018-08-15 21:17:41 +00:00
|
|
|
|
2018-08-27 21:05:53 +00:00
|
|
|
def get_config_dir_path(self):
|
|
|
|
return self.config_volume
|
2018-08-15 21:17:41 +00:00
|
|
|
|
2018-08-27 21:05:53 +00:00
|
|
|
def get_old_config_dir(self):
|
|
|
|
if self.old_config_dir is None:
|
|
|
|
raise Exception('Cannot return a configuration that was no old configuration')
|
2018-08-15 19:32:24 +00:00
|
|
|
|
2018-08-27 21:05:53 +00:00
|
|
|
return os.path.join(self.old_config_dir.name, OLD_CONFIG_SUBDIR)
|