pick up env vars for config (not just existing .env files)
This commit is contained in:
parent
7b435a8367
commit
9e4502d07b
1 changed files with 64 additions and 51 deletions
|
|
@ -116,62 +116,75 @@ class EnvManager:
|
||||||
return f"'{escaped_value}'"
|
return f"'{escaped_value}'"
|
||||||
|
|
||||||
def load_existing_env(self) -> bool:
|
def load_existing_env(self) -> bool:
|
||||||
"""Load existing .env file if it exists."""
|
"""Load existing .env file if it exists, or fall back to environment variables."""
|
||||||
if not self.env_file.exists():
|
import os
|
||||||
return False
|
|
||||||
|
# Map env vars to config attributes
|
||||||
|
attr_map = {
|
||||||
|
"OPENAI_API_KEY": "openai_api_key",
|
||||||
|
"ANTHROPIC_API_KEY": "anthropic_api_key",
|
||||||
|
"OLLAMA_ENDPOINT": "ollama_endpoint",
|
||||||
|
"WATSONX_API_KEY": "watsonx_api_key",
|
||||||
|
"WATSONX_ENDPOINT": "watsonx_endpoint",
|
||||||
|
"WATSONX_PROJECT_ID": "watsonx_project_id",
|
||||||
|
"OPENSEARCH_PASSWORD": "opensearch_password",
|
||||||
|
"LANGFLOW_SECRET_KEY": "langflow_secret_key",
|
||||||
|
"LANGFLOW_SUPERUSER": "langflow_superuser",
|
||||||
|
"LANGFLOW_SUPERUSER_PASSWORD": "langflow_superuser_password",
|
||||||
|
"LANGFLOW_CHAT_FLOW_ID": "langflow_chat_flow_id",
|
||||||
|
"LANGFLOW_INGEST_FLOW_ID": "langflow_ingest_flow_id",
|
||||||
|
"LANGFLOW_URL_INGEST_FLOW_ID": "langflow_url_ingest_flow_id",
|
||||||
|
"NUDGES_FLOW_ID": "nudges_flow_id",
|
||||||
|
"GOOGLE_OAUTH_CLIENT_ID": "google_oauth_client_id",
|
||||||
|
"GOOGLE_OAUTH_CLIENT_SECRET": "google_oauth_client_secret",
|
||||||
|
"MICROSOFT_GRAPH_OAUTH_CLIENT_ID": "microsoft_graph_oauth_client_id",
|
||||||
|
"MICROSOFT_GRAPH_OAUTH_CLIENT_SECRET": "microsoft_graph_oauth_client_secret",
|
||||||
|
"WEBHOOK_BASE_URL": "webhook_base_url",
|
||||||
|
"AWS_ACCESS_KEY_ID": "aws_access_key_id",
|
||||||
|
"AWS_SECRET_ACCESS_KEY": "aws_secret_access_key",
|
||||||
|
"LANGFLOW_PUBLIC_URL": "langflow_public_url",
|
||||||
|
"OPENRAG_DOCUMENTS_PATHS": "openrag_documents_paths",
|
||||||
|
"OPENSEARCH_DATA_PATH": "opensearch_data_path",
|
||||||
|
"LANGFLOW_AUTO_LOGIN": "langflow_auto_login",
|
||||||
|
"LANGFLOW_NEW_USER_IS_ACTIVE": "langflow_new_user_is_active",
|
||||||
|
"LANGFLOW_ENABLE_SUPERUSER_CLI": "langflow_enable_superuser_cli",
|
||||||
|
"DISABLE_INGEST_WITH_LANGFLOW": "disable_ingest_with_langflow",
|
||||||
|
}
|
||||||
|
|
||||||
|
loaded_from_file = False
|
||||||
|
|
||||||
|
# Try to load from .env file first
|
||||||
|
if self.env_file.exists():
|
||||||
|
try:
|
||||||
|
with open(self.env_file, "r") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if not line or line.startswith("#"):
|
||||||
|
continue
|
||||||
|
|
||||||
try:
|
if "=" in line:
|
||||||
with open(self.env_file, "r") as f:
|
key, value = line.split("=", 1)
|
||||||
for line in f:
|
key = key.strip()
|
||||||
line = line.strip()
|
value = sanitize_env_value(value)
|
||||||
if not line or line.startswith("#"):
|
|
||||||
continue
|
|
||||||
|
|
||||||
if "=" in line:
|
if key in attr_map:
|
||||||
key, value = line.split("=", 1)
|
setattr(self.config, attr_map[key], value)
|
||||||
key = key.strip()
|
|
||||||
value = sanitize_env_value(value)
|
|
||||||
|
|
||||||
# Map env vars to config attributes
|
loaded_from_file = True
|
||||||
attr_map = {
|
|
||||||
"OPENAI_API_KEY": "openai_api_key",
|
|
||||||
"ANTHROPIC_API_KEY": "anthropic_api_key",
|
|
||||||
"OLLAMA_ENDPOINT": "ollama_endpoint",
|
|
||||||
"WATSONX_API_KEY": "watsonx_api_key",
|
|
||||||
"WATSONX_ENDPOINT": "watsonx_endpoint",
|
|
||||||
"WATSONX_PROJECT_ID": "watsonx_project_id",
|
|
||||||
"OPENSEARCH_PASSWORD": "opensearch_password",
|
|
||||||
"LANGFLOW_SECRET_KEY": "langflow_secret_key",
|
|
||||||
"LANGFLOW_SUPERUSER": "langflow_superuser",
|
|
||||||
"LANGFLOW_SUPERUSER_PASSWORD": "langflow_superuser_password",
|
|
||||||
"LANGFLOW_CHAT_FLOW_ID": "langflow_chat_flow_id",
|
|
||||||
"LANGFLOW_INGEST_FLOW_ID": "langflow_ingest_flow_id",
|
|
||||||
"LANGFLOW_URL_INGEST_FLOW_ID": "langflow_url_ingest_flow_id",
|
|
||||||
"NUDGES_FLOW_ID": "nudges_flow_id",
|
|
||||||
"GOOGLE_OAUTH_CLIENT_ID": "google_oauth_client_id",
|
|
||||||
"GOOGLE_OAUTH_CLIENT_SECRET": "google_oauth_client_secret",
|
|
||||||
"MICROSOFT_GRAPH_OAUTH_CLIENT_ID": "microsoft_graph_oauth_client_id",
|
|
||||||
"MICROSOFT_GRAPH_OAUTH_CLIENT_SECRET": "microsoft_graph_oauth_client_secret",
|
|
||||||
"WEBHOOK_BASE_URL": "webhook_base_url",
|
|
||||||
"AWS_ACCESS_KEY_ID": "aws_access_key_id",
|
|
||||||
"AWS_SECRET_ACCESS_KEY": "aws_secret_access_key",
|
|
||||||
"LANGFLOW_PUBLIC_URL": "langflow_public_url",
|
|
||||||
"OPENRAG_DOCUMENTS_PATHS": "openrag_documents_paths",
|
|
||||||
"OPENSEARCH_DATA_PATH": "opensearch_data_path",
|
|
||||||
"LANGFLOW_AUTO_LOGIN": "langflow_auto_login",
|
|
||||||
"LANGFLOW_NEW_USER_IS_ACTIVE": "langflow_new_user_is_active",
|
|
||||||
"LANGFLOW_ENABLE_SUPERUSER_CLI": "langflow_enable_superuser_cli",
|
|
||||||
"DISABLE_INGEST_WITH_LANGFLOW": "disable_ingest_with_langflow",
|
|
||||||
}
|
|
||||||
|
|
||||||
if key in attr_map:
|
|
||||||
setattr(self.config, attr_map[key], value)
|
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Error loading .env file", error=str(e))
|
||||||
|
|
||||||
|
# Fall back to environment variables if .env file doesn't exist or failed to load
|
||||||
|
if not loaded_from_file:
|
||||||
|
logger.info("No .env file found, loading from environment variables")
|
||||||
|
for env_key, attr_name in attr_map.items():
|
||||||
|
value = os.environ.get(env_key, "")
|
||||||
|
if value:
|
||||||
|
setattr(self.config, attr_name, value)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except Exception as e:
|
return loaded_from_file
|
||||||
logger.error("Error loading .env file", error=str(e))
|
|
||||||
return False
|
|
||||||
|
|
||||||
def setup_secure_defaults(self) -> None:
|
def setup_secure_defaults(self) -> None:
|
||||||
"""Set up secure default values for passwords and keys."""
|
"""Set up secure default values for passwords and keys."""
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue