#!/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()