From d6711c6f53c4a15a153b581be50d2ef0b7b02f6b Mon Sep 17 00:00:00 2001 From: Damian Borowiecki Date: Sun, 23 Jun 2024 12:35:20 +0200 Subject: [PATCH] Enable .env config for data import --- .gitignore | 3 ++- sigeca_data_import_microservice/main.py | 29 ++++++++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 144de6c..0dc3762 100644 --- a/.gitignore +++ b/.gitignore @@ -164,4 +164,5 @@ cython_debug/ sigeca_data_export_microservice/config.json sigeca_data_import_microservice/config.json sigeca_data_import_microservice/mocked_facilities.json -sigeca_data_import_microservice/private_key \ No newline at end of file +sigeca_data_import_microservice/private_key +*/settings.env \ No newline at end of file diff --git a/sigeca_data_import_microservice/main.py b/sigeca_data_import_microservice/main.py index fef4715..81c6f38 100644 --- a/sigeca_data_import_microservice/main.py +++ b/sigeca_data_import_microservice/main.py @@ -14,11 +14,20 @@ ) from app.infrastructure.jdbc_reader import JDBCReader import argparse +from dotenv import dotenv_values -def load_config(config_path): - with open(config_path, "r") as config_file: - config = json.load(config_file) - return config + + +def load_config(from_env=False): + if from_env: + config = dotenv_values('./settings.env') + if 'sigeca_import_config' not in config.keys(): + raise KeyError("Provided settings.env missing `sigeca_import_config` key.") + return json.loads(config['sigeca_import_config']) + else: + with open('config.json', "r") as config_file: + config = json.load(config_file) + return config def _run_scheduler(sync_service, sync_interval_minutes): @@ -34,8 +43,14 @@ def _run_scheduler(sync_service, sync_interval_minutes): if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Data synchronization service") + parser.add_argument("--run-mode", choices=["continuous", "one-time"], required=True, help="Run mode: 'continuous' to start the scheduler or 'one-time' to execute one-time integration") + parser.add_argument("--env-config", required=False, action='store_true', help="Env Config: use stringified config comming form env instead of .json file") + args = parser.parse_args() + logging.basicConfig(level=logging.INFO) - config = load_config("./config.json") + + config = load_config(args.env_config) engine = get_engine(config["database"]) lmis_client = OpenLmisApiClient(config["open_lmis_api"]) @@ -57,9 +72,7 @@ def _run_scheduler(sync_service, sync_interval_minutes): jdbc_reader.setup_ssh_tunnel() lmis_client.login() - parser = argparse.ArgumentParser(description="Data synchronization service") - parser.add_argument("--run-mode", choices=["continuous", "one-time"], required=True, help="Run mode: 'continuous' to start the scheduler or 'one-time' to execute one-time integration") - args = parser.parse_args() + if args.run_mode == "continuous": sync_interval_minutes = config["sync"]["interval_minutes"]