From 3a87e8b24c61251b4aec8e4f868241354fdf377d Mon Sep 17 00:00:00 2001 From: Gertjan Franken Date: Fri, 16 Feb 2024 17:01:19 +0000 Subject: [PATCH] Improve database configuration handling --- .gitignore | 1 + README.md | 4 ++-- bci/configuration.py | 16 +++++++++++----- bci/distribution/worker_manager.py | 2 +- bci/master.py | 2 +- docker-compose.yml | 2 +- 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 4da9853..df2dedc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ test/resources/repositories browser/binaries/ +config/ database/data/ !**/.gitkeep **/node_modules diff --git a/README.md b/README.md index c62816f..5268311 100644 --- a/README.md +++ b/README.md @@ -77,9 +77,9 @@ By default, BugHog uses a MongoDB container. you might want to prefer all data to be stored on your own MongoDB instance. If you prefer storing data in your own MongoDB instance, follow these steps: -1. Create a `.env` file from `.env.example` in the BugHog root directory and fill in the missing values. +1. Create a `.env` file from `.env.example` (both in the `config` folder) and fill in the missing values. -2. Rebuild BugHog and run it. +2. (Re)start BugHog. ### Stopping To stop BugHog, run the following command: diff --git a/bci/configuration.py b/bci/configuration.py index 4747d3f..a6b4655 100644 --- a/bci/configuration.py +++ b/bci/configuration.py @@ -36,11 +36,17 @@ def initialize_folders(): @staticmethod def get_database_connection_params() -> DatabaseConnectionParameters: - if 'BCI_MONGO_HOST' not in os.environ or \ - 'BCI_MONGO_USERNAME' not in os.environ or \ - 'BCI_MONGO_DATABASE' not in os.environ or \ - 'BCI_MONGO_PASSWORD' not in os.environ: - logger.info('Could not find database environment variables, using database container...') + required_database_params = [ + 'BCI_MONGO_HOST', + 'BCI_MONGO_USERNAME', + 'BCI_MONGO_DATABASE', + 'BCI_MONGO_PASSWORD' + ] + missing_database_params = [ + param for param in required_database_params + if os.getenv(param) in ['', None]] + if missing_database_params: + logger.info(f'Could not find database parameters {missing_database_params}, using database container...') return container.run() else: database_params = DatabaseConnectionParameters( diff --git a/bci/distribution/worker_manager.py b/bci/distribution/worker_manager.py index c35f642..8fe7e23 100644 --- a/bci/distribution/worker_manager.py +++ b/bci/distribution/worker_manager.py @@ -75,7 +75,7 @@ def start_container_thread(): labels=['bh_worker'], command=command, volumes=[ - os.path.join(os.getenv('host_pwd'), '.env') + ':/app/.env', + os.path.join(os.getenv('host_pwd'), 'config') + ':/app/config', os.path.join(os.getenv('host_pwd'), 'browser/binaries/chromium/artisanal') + ':/app/browser/binaries/chromium/artisanal', os.path.join(os.getenv('host_pwd'), 'browser/binaries/firefox/artisanal') + ':/app/browser/binaries/firefox/artisanal', os.path.join(os.getenv('host_pwd'), 'experiments') + ':/app/experiments', diff --git a/bci/master.py b/bci/master.py index bca1bf4..5dddc86 100644 --- a/bci/master.py +++ b/bci/master.py @@ -41,7 +41,7 @@ def __init__(self): self.firefox_build = None self.chromium_build = None - load_dotenv() + load_dotenv('/app/config/.env') Global.initialize_folders() self.db_connection_params = Global.get_database_connection_params() diff --git a/docker-compose.yml b/docker-compose.yml index 1adb78d..7a07f61 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,7 +22,7 @@ services: ports: - "5000:5000" volumes: - - .env:/app/.env:ro + - ./config:/app/config:ro - ./browser/binaries/chromium/artisanal:/app/browser/binaries/chromium/artisanal:rw - ./browser/binaries/firefox/artisanal:/app/browser/binaries/firefox/artisanal:rw - ./experiments:/app/experiments:ro