simplify env file logic
This commit is contained in:
parent
25d7237a57
commit
382e81d727
|
|
@ -4,8 +4,7 @@ Functionality:
|
|||
- encapsulate persistence of application properties
|
||||
"""
|
||||
|
||||
import os.path
|
||||
from os import environ
|
||||
from os import environ, path
|
||||
|
||||
try:
|
||||
from dotenv import load_dotenv
|
||||
|
|
@ -27,18 +26,19 @@ def get_password_from_file(env_var_name) -> str:
|
|||
if env_var_name_val is not None:
|
||||
return str(env_var_name_val)
|
||||
|
||||
elif env_var_path_val is not None:
|
||||
if os.path.isfile(str(env_var_path_val)):
|
||||
with open(
|
||||
file=str(env_var_path_val), mode="r", encoding="utf-8"
|
||||
) as file:
|
||||
return file.read()
|
||||
else:
|
||||
print(f"{env_var_file} file: {env_var_path_val} does not exist")
|
||||
else:
|
||||
if env_var_path_val is None:
|
||||
print(f"either {env_var_name} or {env_var_file} must be set")
|
||||
return ""
|
||||
|
||||
return ""
|
||||
is_path = path.isfile(env_var_path_val)
|
||||
if not is_path:
|
||||
print(f"{env_var_path_val} is not a path")
|
||||
return ""
|
||||
|
||||
with open(env_var_path_val, "r", encoding="utf-8") as f:
|
||||
file_content = f.read().strip()
|
||||
|
||||
return file_content
|
||||
|
||||
|
||||
class EnvironmentSettings:
|
||||
|
|
|
|||
|
|
@ -61,6 +61,10 @@ EXPECTED_ENV_VARS = [
|
|||
"ES_URL",
|
||||
"TA_HOST",
|
||||
]
|
||||
FILE_FALLBACK = [
|
||||
"ELASTIC_PASSWORD",
|
||||
"TA_PASSWORD",
|
||||
]
|
||||
UNEXPECTED_ENV_VARS = {
|
||||
"TA_UWSGI_PORT": "Has been replaced with 'TA_BACKEND_PORT'",
|
||||
"REDIS_HOST": "Has been replaced with 'REDIS_CON' connection string",
|
||||
|
|
@ -139,14 +143,16 @@ class Command(BaseCommand):
|
|||
self.stdout.write("[1] checking expected env vars")
|
||||
env = os.environ
|
||||
for var in EXPECTED_ENV_VARS:
|
||||
if not env.get(var):
|
||||
if not (var.endswith("_PASSWORD") and env.get(var + "_FILE")):
|
||||
message = (
|
||||
f" 🗙 expected env var {var} not set\n {INST}"
|
||||
)
|
||||
self.stdout.write(self.style.ERROR(message))
|
||||
sleep(60)
|
||||
raise CommandError(message)
|
||||
if var in env:
|
||||
continue
|
||||
|
||||
if var in FILE_FALLBACK and f"{var}_FILE" in env:
|
||||
continue
|
||||
|
||||
message = f" 🗙 expected env var {var} not set\n {INST}"
|
||||
self.stdout.write(self.style.ERROR(message))
|
||||
sleep(60)
|
||||
raise CommandError(message)
|
||||
|
||||
message = " ✓ all expected env vars are set"
|
||||
self.stdout.write(self.style.SUCCESS(message))
|
||||
|
|
|
|||
Loading…
Reference in New Issue