pms_server / utils /environment.py
Noel-Teens's picture
fix(env): support FROM_MAIL as alternative for FROM_EMAIL
76bf02c
Raw History Blame Contribute Delete
4.78 kB
"""
Environment validation utilities for PMS Server
"""
import os
import warnings
from typing import Dict, List, Optional
def validate_environment_variables() -> Dict[str, bool]:
"""
Validate required environment variables for the application.
Returns:
Dict[str, bool]: Dictionary with validation status for each service
"""
validation_results = {}
# Backblaze B2 Storage
b2_vars = ['B2_KEY_ID', 'B2_APP_KEY', 'B2_BUCKET_NAME', 'B2_ENDPOINT_URL']
b2_configured = all(os.getenv(var) for var in b2_vars)
validation_results['backblaze_b2'] = b2_configured
if not b2_configured:
missing_vars = [var for var in b2_vars if not os.getenv(var)]
warnings.warn(
f"Backblaze B2 storage not fully configured. Missing: {', '.join(missing_vars)}. "
"Files will be stored locally.",
RuntimeWarning
)
# Aiven MySQL Database
mysql_vars = ['AIVEN_DB_NAME', 'AIVEN_DB_USER', 'AIVEN_DB_PASSWORD', 'AIVEN_DB_HOST']
mysql_configured = all(os.getenv(var) for var in mysql_vars)
validation_results['aiven_mysql'] = mysql_configured
if not mysql_configured:
missing_vars = [var for var in mysql_vars if not os.getenv(var)]
warnings.warn(
f"Aiven MySQL database not fully configured. Missing: {', '.join(missing_vars)}. "
"Application will fall back to SQLite for development.",
RuntimeWarning
)
elif not os.getenv('AIVEN_CA_CERT'):
warnings.warn(
"AIVEN_CA_CERT not provided. SSL connection may fail. "
"For production, please provide the SSL certificate.",
RuntimeWarning
)
# Email Services (SendGrid)
sendgrid_vars = ['SENDGRID_API_KEY']
# Check for either FROM_EMAIL or FROM_MAIL (both are valid)
from_email_configured = bool(os.getenv('FROM_EMAIL') or os.getenv('FROM_MAIL'))
sendgrid_configured = all(os.getenv(var) for var in sendgrid_vars) and from_email_configured
validation_results['sendgrid'] = sendgrid_configured
if not sendgrid_configured:
missing_vars = [var for var in sendgrid_vars if not os.getenv(var)]
if not from_email_configured:
missing_vars.append('FROM_EMAIL (or FROM_MAIL)')
if missing_vars:
warnings.warn(
f"SendGrid email service not configured. Missing: {', '.join(missing_vars)}. "
"Email invitations will not work.",
RuntimeWarning
)
# Firebase
firebase_configured = bool(os.getenv('FIREBASE_SERVICE_KEY'))
validation_results['firebase'] = firebase_configured
if not firebase_configured:
warnings.warn(
"Firebase not configured. FIREBASE_SERVICE_KEY missing. "
"Firebase authentication will not work.",
RuntimeWarning
)
return validation_results
def get_storage_status() -> str:
"""
Get current storage configuration status.
Returns:
str: Description of current storage configuration
"""
b2_vars = ['B2_KEY_ID', 'B2_APP_KEY', 'B2_BUCKET_NAME', 'B2_ENDPOINT_URL']
if all(os.getenv(var) for var in b2_vars):
return "Backblaze B2 Cloud Storage (Configured)"
else:
return "Local File Storage (Backblaze B2 not configured)"
def get_database_status() -> str:
"""
Get current database configuration status.
Returns:
str: Description of current database configuration
"""
mysql_vars = ['AIVEN_DB_NAME', 'AIVEN_DB_USER', 'AIVEN_DB_PASSWORD', 'AIVEN_DB_HOST']
if all(os.getenv(var) for var in mysql_vars):
ssl_status = "with SSL" if os.getenv('AIVEN_CA_CERT') else "without SSL"
return f"Aiven MySQL Database (Configured {ssl_status} using PyMySQL)"
else:
return "SQLite Database (Fallback - MySQL not configured)"
def print_configuration_summary():
"""Print a summary of the current configuration."""
print("=" * 50)
print("PMS Server Configuration Summary")
print("=" * 50)
validation_results = validate_environment_variables()
print(f"Storage: {get_storage_status()}")
print(f"Database: {get_database_status()}")
print(f"Email (SendGrid): {'Configured' if validation_results['sendgrid'] else 'Not Configured'}")
print(f"Firebase: {'Configured' if validation_results['firebase'] else 'Not Configured'}")
if not all(validation_results.values()):
print("\n⚠️ Some services are not fully configured.")
print("Please check the warnings above and set the missing environment variables.")
else:
print("\n✅ All services are properly configured!")
print("=" * 50)