File size: 6,719 Bytes
18c9405
5854e13
18c9405
 
 
 
5854e13
 
 
18c9405
 
 
5854e13
18c9405
 
 
 
 
 
 
 
 
 
 
 
 
5854e13
 
18c9405
 
 
 
 
 
 
 
 
 
5854e13
18c9405
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ada5d3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18c9405
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71ad4a3
5854e13
18c9405
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
"""
Environment Variable Setup Script for Enflow

This script helps users set up their .env file for the Enflow backend.
It will copy env.example to .env if it doesn't exist, and prompt for values.
"""

import os
import secrets
import shutil
import getpass

def main():
    """Main function to set up environment variables"""
    print("Enflow Environment Setup Script")
    print("===============================")
    
    # Check if .env already exists
    env_file = os.path.join(os.path.dirname(__file__), '.env')
    env_example = os.path.join(os.path.dirname(__file__), 'env.example')
    
    if os.path.exists(env_file):
        overwrite = input(".env file already exists. Do you want to overwrite it? (y/n): ").lower()
        if overwrite != 'y':
            print("Setup cancelled. Your .env file was not modified.")
            return
    
    # Copy env.example to .env if it doesn't exist or user wants to overwrite
    if not os.path.exists(env_file) or overwrite == 'y':
        if os.path.exists(env_example):
            shutil.copy(env_example, env_file)
            print(f"Created .env file from env.example")
        else:
            # Create a new .env file with basic structure
            with open(env_file, 'w') as f:
                f.write("# Enflow Environment Variables\n\n")
            print(f"Created new .env file")
    
    # Read current .env file
    env_vars = {}
    try:
        with open(env_file, 'r') as f:
            for line in f:
                line = line.strip()
                if line and not line.startswith('#') and '=' in line:
                    key, value = line.split('=', 1)
                    env_vars[key] = value
    except Exception as e:
        print(f"Error reading .env file: {str(e)}")
        return
    
    # Prompt for values
    print("\nPlease provide values for the following environment variables:")
    print("(Press Enter to keep the current value or use the default)\n")
    
    # MongoDB URI
    mongo_uri = input(f"MongoDB URI [current: {env_vars.get('MONGO_URI', 'not set')}]: ")
    if mongo_uri:
        env_vars['MONGO_URI'] = mongo_uri
    elif 'MONGO_URI' not in env_vars:
        env_vars['MONGO_URI'] = "mongodb+srv://username:password@cluster.mongodb.net/enflow"
        print("Using example MongoDB URI. You must update this before running the application.")
    
    # JWT Secret
    jwt_secret = input(f"JWT Secret [current: {env_vars.get('JWT_SECRET', 'not set')}]: ")
    if jwt_secret:
        env_vars['JWT_SECRET'] = jwt_secret
    elif 'JWT_SECRET' not in env_vars:
        env_vars['JWT_SECRET'] = secrets.token_hex(32)
        print(f"Generated random JWT secret: {env_vars['JWT_SECRET']}")
    
    # Cloudinary configuration
    print("\nCloudinary Configuration (for file storage):")
    cloud_name = input(f"Cloudinary Cloud Name [current: {env_vars.get('CLOUDINARY_CLOUD_NAME', 'not set')}]: ")
    if cloud_name:
        env_vars['CLOUDINARY_CLOUD_NAME'] = cloud_name
    elif 'CLOUDINARY_CLOUD_NAME' not in env_vars:
        env_vars['CLOUDINARY_CLOUD_NAME'] = "your_cloud_name"
    
    api_key = input(f"Cloudinary API Key [current: {env_vars.get('CLOUDINARY_API_KEY', 'not set')}]: ")
    if api_key:
        env_vars['CLOUDINARY_API_KEY'] = api_key
    elif 'CLOUDINARY_API_KEY' not in env_vars:
        env_vars['CLOUDINARY_API_KEY'] = "your_api_key"
    
    api_secret = getpass.getpass(f"Cloudinary API Secret [leave empty to keep current]: ")
    if api_secret:
        env_vars['CLOUDINARY_API_SECRET'] = api_secret
    elif 'CLOUDINARY_API_SECRET' not in env_vars:
        env_vars['CLOUDINARY_API_SECRET'] = "your_api_secret"
    
    # Redis Configuration
    print("\nRedis Configuration (for task queue):")
    print("Option 1: Configure individual components (recommended)")
    print("Option 2: Provide full Redis URL")
    redis_option = input("Which option do you prefer? (1/2) [default: 1]: ")
    
    if redis_option == "2":
        # Redis URL
        redis_url = input(f"Redis URL [current: {env_vars.get('REDIS_URL', 'redis://localhost:6379/0')}]: ")
        if redis_url:
            env_vars['REDIS_URL'] = redis_url
        elif 'REDIS_URL' not in env_vars:
            env_vars['REDIS_URL'] = "redis://localhost:6379/0"
        
        # Remove individual components if they exist
        for key in ['REDIS_HOST', 'REDIS_PORT', 'REDIS_PASSWORD']:
            if key in env_vars:
                del env_vars[key]
    else:
        # Redis Host
        redis_host = input(f"Redis Host [current: {env_vars.get('REDIS_HOST', 'localhost')}]: ")
        if redis_host:
            env_vars['REDIS_HOST'] = redis_host
        elif 'REDIS_HOST' not in env_vars:
            env_vars['REDIS_HOST'] = "localhost"
        
        # Redis Port
        redis_port = input(f"Redis Port [current: {env_vars.get('REDIS_PORT', '6379')}]: ")
        if redis_port:
            env_vars['REDIS_PORT'] = redis_port
        elif 'REDIS_PORT' not in env_vars:
            env_vars['REDIS_PORT'] = "6379"
        
        # Redis Password
        redis_password = getpass.getpass(f"Redis Password [leave empty to keep current]: ")
        if redis_password:
            env_vars['REDIS_PASSWORD'] = redis_password
        elif 'REDIS_PASSWORD' not in env_vars:
            env_vars['REDIS_PASSWORD'] = ""
            print("Warning: No Redis password set. This should only be used for local development.")
        
        # Remove full URL if it exists
        if 'REDIS_URL' in env_vars:
            del env_vars['REDIS_URL']
    
    # OpenAI API Key
    openai_key = getpass.getpass(f"OpenAI API Key [leave empty to keep current]: ")
    if openai_key:
        env_vars['OPENAI_API_KEY'] = openai_key
    elif 'OPENAI_API_KEY' not in env_vars:
        env_vars['OPENAI_API_KEY'] = "your_openai_api_key"
    
    # Flask Environment
    flask_env = input(f"Flask Environment (development/production) [current: {env_vars.get('FLASK_ENV', 'development')}]: ")
    if flask_env:
        env_vars['FLASK_ENV'] = flask_env
    elif 'FLASK_ENV' not in env_vars:
        env_vars['FLASK_ENV'] = "development"
    
    # Write updated .env file
    try:
        with open(env_file, 'w') as f:
            f.write("# Enflow Environment Variables\n\n")
            for key, value in env_vars.items():
                f.write(f"{key}={value}\n")
        print("\nEnvironment variables successfully saved to .env file.")
        print("Remember to NEVER commit this file to version control!")
    except Exception as e:
        print(f"Error writing .env file: {str(e)}")
    
    print("\nSetup complete! You can now run the application.")

if __name__ == "__main__":
    main()