File size: 4,784 Bytes
6069ba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76bf02c
 
 
 
6069ba8
 
 
 
76bf02c
 
 
 
 
 
 
 
6069ba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
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)