forked from inveniosoftware/invenio-app-rdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: granular env-based solution for connection strings
* build db uri * build redis url * build mq url partially closes: inveniosoftware/helm-invenio#112
- Loading branch information
Showing
3 changed files
with
220 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# Copyright (C) 2024 KTH Royal Institute of Technology. | ||
# | ||
# Invenio App RDM is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""Utilities for building connection strings.""" | ||
|
||
import os | ||
|
||
from click import secho | ||
|
||
|
||
def build_db_uri(): | ||
"""Build the database URI.""" | ||
DEFAULT_URI = "postgresql+psycopg2://invenio-app-rdm:invenio-app-rdm@localhost/invenio-app-rdm" | ||
params = { | ||
k: os.environ.get(f"DB_{k.upper()}") | ||
for k in ["user", "password", "host", "port", "name"] | ||
} | ||
|
||
if all(params.values()): | ||
uri = f"postgresql+psycopg2://{params['user']}:{params['password']}@{params['host']}:{params['port']}/{params['name']}" | ||
secho( | ||
f"Constructed database URI: '{params['user']}:***@{params['host']}:{params['port']}/{params['name']}'", | ||
fg="blue", | ||
) | ||
return uri | ||
|
||
uri = os.environ.get("SQLALCHEMY_DATABASE_URI") | ||
if uri: | ||
secho(f"Using SQLALCHEMY_DATABASE_URI: '{uri}'", fg="blue") | ||
return uri | ||
|
||
secho(f"Falling back to the default URI: '{DEFAULT_URI}'", fg="blue") | ||
return DEFAULT_URI | ||
|
||
|
||
def build_broker_url(): | ||
"""Build the broker URL.""" | ||
DEFAULT_BROKER_URL = "amqp://guest:guest@localhost:5672/" | ||
params = { | ||
k: os.environ.get(f"BROKER_{k.upper()}") | ||
for k in ["user", "password", "host", "port"] | ||
} | ||
|
||
if all(params.values()): | ||
uri = f"amqp://{params['user']}:{params['password']}@{params['host']}:{params['port']}/" | ||
secho( | ||
f"Constructed AMQP URL: '{params['user']}:***@{params['host']}:{params['port']}/'", | ||
fg="blue", | ||
) | ||
return uri | ||
|
||
uri = os.environ.get("BROKER_URL") | ||
if uri: | ||
secho(f"AMQP URI: '{uri}'", fg="blue") | ||
return uri | ||
|
||
secho(f"Falling back to the default URI: '{DEFAULT_BROKER_URL}'", fg="blue") | ||
return DEFAULT_BROKER_URL | ||
|
||
|
||
def build_redis_url(db=None): | ||
"""Build the Redis broker URL.""" | ||
redis_host = os.environ.get("REDIS_HOST") | ||
redis_port = os.environ.get("REDIS_PORT") | ||
redis_password = os.environ.get("REDIS_PASSWORD") | ||
db = db if db is not None else 0 | ||
DEFAULT_BROKER_URL = f"redis://localhost:6379/{db}" | ||
|
||
if redis_host and redis_port: | ||
password = f":{redis_password}@" if redis_password else "" | ||
uri = f"redis://{password}{redis_host}:{redis_port}/{db}" | ||
secho(f"Constructed Redis URL: '{uri}'", fg="blue") | ||
return uri | ||
|
||
uri = os.environ.get("BROKER_URL") | ||
if uri and uri.startswith(("redis://", "rediss://", "unix://")): | ||
secho(f"Using Redis BROKER_URL: '{uri}'", fg="blue") | ||
return uri | ||
|
||
secho(f"Falling back to the default Redis URL: '{DEFAULT_BROKER_URL}'", fg="blue") | ||
return DEFAULT_BROKER_URL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters