Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Deployment configuration and validation script for Chatty application | |
| """ | |
| import os | |
| import sys | |
| import secrets | |
| from config import get_config, validate_environment | |
| def generate_secret_key(): | |
| """Generate a secure secret key""" | |
| return secrets.token_hex(32) | |
| def check_production_readiness(): | |
| """Check if the application is ready for production deployment""" | |
| print("Checking production readiness...") | |
| # Set environment to production for validation | |
| os.environ['FLASK_ENV'] = 'production' | |
| # Validate configuration | |
| if not validate_environment(): | |
| print("β Production configuration validation failed") | |
| return False | |
| # Additional production checks | |
| config_class = get_config('production') | |
| checks = [ | |
| ("SECRET_KEY is set", bool(os.environ.get('SECRET_KEY'))), | |
| ("SECRET_KEY is not default", os.environ.get('SECRET_KEY') != 'dev-secret-key-change-in-production'), | |
| ("SECRET_KEY is long enough", len(os.environ.get('SECRET_KEY', '')) >= 32), | |
| ("MONGODB_URL is set", bool(os.environ.get('MONGODB_URL'))), | |
| ("SESSION_COOKIE_SECURE is enabled", os.environ.get('SESSION_COOKIE_SECURE', '').lower() == 'true'), | |
| ("Debug mode is disabled", not config_class.DEBUG), | |
| ] | |
| all_passed = True | |
| for check_name, passed in checks: | |
| status = "β " if passed else "β" | |
| print(f" {status} {check_name}") | |
| if not passed: | |
| all_passed = False | |
| if all_passed: | |
| print("β Application is ready for production deployment") | |
| else: | |
| print("β Application is NOT ready for production deployment") | |
| return all_passed | |
| def setup_development(): | |
| """Set up development environment""" | |
| print("Setting up development environment...") | |
| env_file = '.env' | |
| if os.path.exists(env_file): | |
| print(f"β {env_file} already exists") | |
| return | |
| # Copy from example | |
| if os.path.exists('.env.example'): | |
| import shutil | |
| shutil.copy('.env.example', env_file) | |
| print(f"β Created {env_file} from .env.example") | |
| print("β οΈ Please edit .env and fill in your actual configuration values") | |
| else: | |
| print("β .env.example not found") | |
| def generate_production_env(): | |
| """Generate production environment template""" | |
| print("Generating production environment configuration...") | |
| secret_key = generate_secret_key() | |
| production_env = f"""# Production Environment Configuration for Chatty | |
| # Generated on {os.popen('date').read().strip()} | |
| # ============================================================================= | |
| # REQUIRED PRODUCTION CONFIGURATION | |
| # ============================================================================= | |
| # Flask Configuration | |
| FLASK_ENV=production | |
| SECRET_KEY={secret_key} | |
| # MongoDB Configuration (REPLACE WITH YOUR VALUES) | |
| MONGODB_URL=mongodb+srv://username:password@cluster.mongodb.net/?retryWrites=true&w=majority&appName=Chatty | |
| MONGODB_DATABASE=Atlas | |
| # Security Configuration | |
| SESSION_COOKIE_SECURE=true | |
| WTF_CSRF_SSL_STRICT=true | |
| SESSION_LIFETIME_HOURS=24 | |
| # Rate Limiting (stricter in production) | |
| MAX_LOGIN_ATTEMPTS=3 | |
| RATE_LIMIT_WINDOW=1800 | |
| # API Configuration | |
| API_URL=https://findEthics-Atlas.hf.space/chat | |
| API_TIMEOUT=30 | |
| # Logging | |
| LOG_LEVEL=INFO | |
| # Deployment | |
| PORT=7860 | |
| # SSL/TLS Configuration | |
| OPENSSL_CONF=/dev/null | |
| """ | |
| with open('.env.production', 'w') as f: | |
| f.write(production_env) | |
| print("β Created .env.production template") | |
| print("β οΈ Please review and update the MongoDB URI and other values as needed") | |
| def main(): | |
| """Main deployment script""" | |
| if len(sys.argv) < 2: | |
| print("Usage: python deploy.py [command]") | |
| print("Commands:") | |
| print(" check-prod - Check production readiness") | |
| print(" setup-dev - Set up development environment") | |
| print(" gen-prod-env - Generate production environment template") | |
| print(" gen-secret - Generate a new secret key") | |
| return | |
| command = sys.argv[1] | |
| if command == 'check-prod': | |
| success = check_production_readiness() | |
| sys.exit(0 if success else 1) | |
| elif command == 'setup-dev': | |
| setup_development() | |
| elif command == 'gen-prod-env': | |
| generate_production_env() | |
| elif command == 'gen-secret': | |
| print("Generated secret key:") | |
| print(generate_secret_key()) | |
| else: | |
| print(f"Unknown command: {command}") | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() |