cc: add data_dir_abs_path property to EnvironmentConfig
EnvironmentConfig needs to handle environment variables and '~' in its `data_dir` property. Other components that consume `data_dir` need environment variables and '~' resolved to an absolute path. Add a property called `data_dir_abs_path` that calculates the absolute path from `data_dir`. Since `data_dir` remains unchanged, the EnvironmentConfig can be saved to file without modifying the `data_dir` option in the file.
This commit is contained in:
parent
20a3d31852
commit
e1209dcb4c
|
@ -50,6 +50,10 @@ class EnvironmentConfig:
|
||||||
self.aws = aws
|
self.aws = aws
|
||||||
self.data_dir = data_dir
|
self.data_dir = data_dir
|
||||||
|
|
||||||
|
@property
|
||||||
|
def data_dir_abs_path(self):
|
||||||
|
return os.path.abspath(os.path.expanduser(os.path.expandvars(self.data_dir)))
|
||||||
|
|
||||||
def save_to_file(self):
|
def save_to_file(self):
|
||||||
with open(self._server_config_path, "w") as f:
|
with open(self._server_config_path, "w") as f:
|
||||||
f.write(json.dumps(self.to_dict(), indent=2))
|
f.write(json.dumps(self.to_dict(), indent=2))
|
||||||
|
|
|
@ -24,7 +24,7 @@ STANDARD_WITH_CREDENTIALS = os.path.join(
|
||||||
TEST_RESOURCES_DIR, "server_config_standard_with_credentials.json"
|
TEST_RESOURCES_DIR, "server_config_standard_with_credentials.json"
|
||||||
)
|
)
|
||||||
WITH_DATA_DIR = os.path.join(TEST_RESOURCES_DIR, "server_config_with_data_dir.json")
|
WITH_DATA_DIR = os.path.join(TEST_RESOURCES_DIR, "server_config_with_data_dir.json")
|
||||||
|
WITH_DATA_DIR_HOME = os.path.join(TEST_RESOURCES_DIR, "server_config_with_data_dir_home.json")
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def config_file(tmpdir):
|
def config_file(tmpdir):
|
||||||
|
@ -124,3 +124,14 @@ def test_generate_default_file(config_file):
|
||||||
def test_data_dir():
|
def test_data_dir():
|
||||||
environment_config = EnvironmentConfig(WITH_DATA_DIR)
|
environment_config = EnvironmentConfig(WITH_DATA_DIR)
|
||||||
assert environment_config.data_dir == "/test/data/dir"
|
assert environment_config.data_dir == "/test/data/dir"
|
||||||
|
|
||||||
|
|
||||||
|
def set_home_env(monkeypatch, tmpdir):
|
||||||
|
monkeypatch.setenv("HOME", str(tmpdir))
|
||||||
|
|
||||||
|
|
||||||
|
def test_data_dir_abs_path_from_file(monkeypatch, tmpdir):
|
||||||
|
set_home_env(monkeypatch, tmpdir)
|
||||||
|
|
||||||
|
config = EnvironmentConfig(WITH_DATA_DIR_HOME)
|
||||||
|
assert config.data_dir_abs_path == os.path.join(tmpdir, "data_dir")
|
||||||
|
|
|
@ -35,7 +35,7 @@ MINIMUM_MONGO_DB_VERSION_REQUIRED = "4.2.0"
|
||||||
def main(should_setup_only=False, server_config_filename=DEFAULT_SERVER_CONFIG_PATH):
|
def main(should_setup_only=False, server_config_filename=DEFAULT_SERVER_CONFIG_PATH):
|
||||||
logger.info("Starting bootloader server")
|
logger.info("Starting bootloader server")
|
||||||
env_singleton.initialize_from_file(server_config_filename)
|
env_singleton.initialize_from_file(server_config_filename)
|
||||||
initialize_encryptor(env_singleton.config.data_dir)
|
initialize_encryptor(env_singleton.config.data_dir_abs_path)
|
||||||
|
|
||||||
mongo_url = os.environ.get('MONGO_URL', env_singleton.env.get_mongo_url())
|
mongo_url = os.environ.get('MONGO_URL', env_singleton.env.get_mongo_url())
|
||||||
bootloader_server_thread = Thread(target=BootloaderHttpServer(mongo_url).serve_forever, daemon=True)
|
bootloader_server_thread = Thread(target=BootloaderHttpServer(mongo_url).serve_forever, daemon=True)
|
||||||
|
|
|
@ -16,9 +16,7 @@ class Encryptor:
|
||||||
_PASSWORD_FILENAME = "mongo_key.bin"
|
_PASSWORD_FILENAME = "mongo_key.bin"
|
||||||
|
|
||||||
def __init__(self, password_file_dir):
|
def __init__(self, password_file_dir):
|
||||||
password_file = os.path.expanduser(
|
password_file = os.path.join(password_file_dir, self._PASSWORD_FILENAME)
|
||||||
os.path.join(password_file_dir, self._PASSWORD_FILENAME)
|
|
||||||
)
|
|
||||||
|
|
||||||
if os.path.exists(password_file):
|
if os.path.exists(password_file):
|
||||||
self._load_existing_key(password_file)
|
self._load_existing_key(password_file)
|
||||||
|
|
|
@ -33,11 +33,3 @@ def test_create_new_password_file(tmpdir):
|
||||||
initialize_encryptor(tmpdir)
|
initialize_encryptor(tmpdir)
|
||||||
|
|
||||||
assert os.path.isfile(os.path.join(tmpdir, PASSWORD_FILENAME))
|
assert os.path.isfile(os.path.join(tmpdir, PASSWORD_FILENAME))
|
||||||
|
|
||||||
|
|
||||||
def test_expand_home(monkeypatch, tmpdir):
|
|
||||||
monkeypatch.setenv("HOME", str(tmpdir))
|
|
||||||
|
|
||||||
initialize_encryptor("~/")
|
|
||||||
|
|
||||||
assert os.path.isfile(os.path.join(tmpdir, "mongo_key.bin"))
|
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"server_config": "password",
|
||||||
|
"deployment": "develop",
|
||||||
|
"user": "test",
|
||||||
|
"password_hash": "abcdef",
|
||||||
|
"data_dir": "~/data_dir"
|
||||||
|
}
|
Loading…
Reference in New Issue