Spaces:
Sleeping
Sleeping
File size: 4,611 Bytes
f6278c5 | 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 | #!/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() |