From cb28298ce7074fb2c0b22f6f9bd6901f2ebebf6d Mon Sep 17 00:00:00 2001 From: EliseCastle23 <109446148+EliseCastle23@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:12:44 -0700 Subject: [PATCH 1/2] updating job export to work with Helm --- job_export.py | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/job_export.py b/job_export.py index 09df724..d7908b9 100644 --- a/job_export.py +++ b/job_export.py @@ -50,14 +50,25 @@ filters = json.dumps({"filter": input_data.get("filter", {})}) case_ids = gql.execute(filters=filters) - with open("/peregrine-creds.json") as pelican_creds_file: - peregrine_creds = json.load(pelican_creds_file) - - DB_URL = "jdbc:postgresql://{}/{}".format( - peregrine_creds["db_host"], peregrine_creds["db_database"] - ) - DB_USER = peregrine_creds["db_username"] - DB_PASS = peregrine_creds["db_password"] + try: + with open("/peregrine-creds.json") as pelican_creds_file: + peregrine_creds = json.load(pelican_creds_file) + except (FileNotFoundError, json.JSONDecodeError) as e: + print(f"Failed to load credentials file: {e}") + peregrine_creds = {} + + # Set variables, prioritizing environment variables + DB_HOST = os.getenv("DB_HOST", peregrine_creds.get("db_host")) + DB_DATABASE = os.getenv("DB_DATABASE", peregrine_creds.get("db_database")) + DB_USER = os.getenv("DB_USER", peregrine_creds.get("db_username")) + DB_PASS = os.getenv("DB_PASS", peregrine_creds.get("db_password")) + + # Construct the database URL if possible + if DB_HOST and DB_DATABASE: + DB_URL = f"jdbc:postgresql://{DB_HOST}/{DB_DATABASE}" + else: + DB_URL = None + print("DB_HOST or DB_DATABASE is missing. DB_URL cannot be constructed.") dictionary_url = os.environ["DICTIONARY_URL"] dictionary, model = init_dictionary(url=dictionary_url) @@ -170,8 +181,17 @@ COMMONS = "https://" + hostname + "/" # try sending to indexd - with open("/indexd-creds.json") as indexd_creds_file: - indexd_creds = json.load(indexd_creds_file) + try: + with open("/indexd-creds.json") as indexd_creds_file: + indexd_creds = json.load(indexd_creds_file) + gdcapi_credential = indexd_creds["user_db"]["gdcapi"] + except (FileNotFoundError, json.JSONDecodeError, KeyError) as e: + print(f"Failed to load indexd credentials file or missing keys: {e}") + indexd_creds = {} + gdcapi_credential = None + + # Load the indexd credential (fallback to SHEEPDOG env variable) + indexd_creds = os.getenv("SHEEPDOG", gdcapi_credential) s3_url = "s3://" + pelican_creds["manifest_bucket_name"] + "/" + avro_filename @@ -191,7 +211,7 @@ indexd_record = indexd_submit( COMMONS, - indexd_creds["user_db"]["gdcapi"], + indexd_creds, avro_filename, os.stat(fname).st_size, [s3_url], From 6bfb73cf9052a3f1db9bb09bd3774beaa6f923b7 Mon Sep 17 00:00:00 2001 From: EliseCastle23 <109446148+EliseCastle23@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:28:02 -0700 Subject: [PATCH 2/2] change from print statement to runtime error --- job_export.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/job_export.py b/job_export.py index 78afb76..5c073d2 100644 --- a/job_export.py +++ b/job_export.py @@ -68,7 +68,9 @@ DB_URL = f"jdbc:postgresql://{DB_HOST}/{DB_DATABASE}" else: DB_URL = None - print("DB_HOST or DB_DATABASE is missing. DB_URL cannot be constructed.") + raise RuntimeError( + "DB_HOST or DB_DATABASE is missing. DB_URL cannot be constructed." + ) dictionary_url = os.environ["DICTIONARY_URL"] dictionary, model = init_dictionary(url=dictionary_url)