Spaces:
Running

WC / scripts /manage_environments.sh
Dzung Doan
create separate pipelines for dev / staging / prod
e251b13
Raw History Blame Contribute Delete
8.25 kB
#!/bin/bash
# Environment Management Script for World Cup AI Assistant
# Helps manage different environments and their configurations
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_info() {
echo -e "${BLUE}ℹ️ $1${NC}"
}
print_success() {
echo -e "${GREEN}βœ… $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}❌ $1${NC}"
}
# Check if we're in the right directory
if [ ! -f "environments.py" ]; then
print_error "Please run this script from the worldcup-ui-app root directory"
exit 1
fi
# Function to show current environment status
show_current_status() {
print_info "πŸ” Current Environment Status:"
echo ""
if [ -f ".env" ]; then
echo "πŸ“ .env file found:"
grep -E "^(ENVIRONMENT|AGENT_URL|MCP_SERVER_URL)" .env 2>/dev/null || echo " No environment variables found"
else
echo "πŸ“ No .env file found"
fi
echo ""
echo "🐍 Python environment check:"
python3 -c "
import os
from environments import get_environment, get_current_config
try:
env = get_environment()
config = get_current_config()
print(f' Current environment: {env}')
print(f' Agent URL: {config[\"agent_url\"]}')
print(f' MCP Server URL: {config[\"mcp_server_url\"]}')
print(f' Requires API Key: {config[\"requires_api_key\"]}')
except Exception as e:
print(f' Error: {e}')
"
print_success "Environment $env updated with:"
echo " Agent IP: $agent_ip"
echo " MCP Server IP: $mcp_ip"
}
# Function to update environment configuration
update_environment() {
local env=$1
local agent_ip=$2
local mcp_ip=$3
if [ -z "$env" ] || [ -z "$agent_ip" ] || [ -z "$mcp_ip" ]; then
print_error "Usage: $0 update-env <environment> <agent-ip> <mcp-ip>"
print_error "Example: $0 update-env staging 10.0.1.100 10.0.1.101"
exit 1
fi
print_info "πŸ”„ Updating environment configuration for: $env"
# Create backup
cp environments.py environments.py.backup
print_success "Backup created: environments.py.backup"
# Update the configuration
python3 -c "
import re
# Read the file
with open('environments.py', 'r') as f:
content = f.read()
# Update staging or prod URLs
if '$env' == 'staging':
content = re.sub(
r'\"agent_url\": \"http://staging-agent-ip:8001\"',
'\"agent_url\": \"http://$agent_ip:8001\"',
content
)
content = re.sub(
r'\"mcp_server_url\": \"http://staging-mcp-ip:8000\"',
'\"mcp_server_url\": \"http://$mcp_ip:8000\"',
content
)
elif '$env' == 'prod':
content = re.sub(
r'\"agent_url\": \"http://prod-agent-ip:8001\"',
'\"agent_url\": \"http://$agent_ip:8001\"',
content
)
content = re.sub(
r'\"mcp_server_url\": \"http://prod-mcp-ip:8000\"',
'\"mcp_server_url\": \"http://$mcp_ip:8000\"',
content
)
# Write back
with open('environments.py', 'w') as f:
f.write(content)
print('βœ… Environment configuration updated successfully!')
"
print_success "Environment $env updated with:"
echo " Agent IP: $agent_ip"
echo " MCP Server IP: $mcp_ip"
}
# Function to show environment info
show_environment_info() {
local env=$1
if [ -z "$env" ]; then
print_error "Usage: $0 show-env <environment>"
print_error "Example: $0 show-env staging"
exit 1
fi
print_info "πŸ” Environment Information for: $env"
echo ""
python3 -c "
from environments import get_config, get_environment_display_info
try:
config = get_config('$env')
info = get_environment_display_info('$env')
print(f'Name: {info[\"name\"]}')
print(f'Description: {info[\"description\"]}')
print(f'Icon: {info[\"icon\"]}')
print(f'Color: {info[\"color\"]}')
print(f'Agent URL: {config[\"agent_url\"]}')
print(f'MCP Server URL: {config[\"mcp_server_url\"]}')
print(f'Requires API Key: {config[\"requires_api_key\"]}')
except Exception as e:
print(f'Error: {e}')
"
}
# Function to list all environments
list_environments() {
print_info "πŸ“‹ Available Environments:"
echo ""
python3 -c "
from environments import list_environments, get_environment_display_info
try:
for env in list_environments():
info = get_environment_display_info(env)
print(f'{info[\"icon\"]} {info[\"name\"]} ({env})')
print(f' {info[\"description\"]}')
print()
except Exception as e:
print(f'Error: {e}')
"
}
# Function to create environment-specific files
create_env_files() {
local env=$1
if [ -z "$env" ]; then
print_error "Usage: $0 create-env <environment>"
print_error "Example: $0 create-env staging"
exit 1
fi
print_info "πŸ“ Creating environment-specific files for: $env"
# Create .env file
cat > ".env.$env" << EOF
# Environment: $env
ENVIRONMENT=$env
AGENT_API_KEY=<your-$env-agent-api-key>
EOF
print_success "Created .env.$env file"
# Create requirements file if it doesn't exist
if [ ! -f "requirements.txt" ]; then
cat > "requirements.txt" << EOF
streamlit
requests
python-dotenv
EOF
print_success "Created requirements.txt file"
fi
print_info "Next steps:"
echo "1. Update .env.$env with your actual API key"
echo "2. Update environments.py with actual IP addresses"
echo "3. Deploy to HF Space: worldcup-qa-$env"
}
# Function to show deployment commands
show_deployment_commands() {
local env=$1
if [ -z "$env" ]; then
print_error "Usage: $0 deploy-cmds <environment>"
print_error "Example: $0 deploy-cmds staging"
exit 1
fi
print_info "πŸš€ Deployment Commands for: $env"
echo ""
echo "1. Create HF Space:"
echo " - Go to https://huggingface.co/spaces"
echo " - Create new space: worldcup-qa-$env"
echo " - SDK: Streamlit, License: Apache 2.0"
echo ""
echo "2. Set Environment Variables in HF Space:"
echo " ENVIRONMENT=$env"
echo " AGENT_API_KEY=<your-$env-agent-api-key>"
echo ""
echo "3. Deploy Code:"
echo " Option A - Git Integration:"
echo " git checkout -b $env"
echo " git push origin $env"
echo ""
echo " Option B - Manual Upload:"
echo " - Upload code directly to HF Space"
echo " - Set environment variables in secrets"
echo ""
echo "4. Test Connection:"
echo " - Check environment badge shows: $env"
echo " - Test API calls to AWS $env backend"
}
# Main script logic
case "${1:-help}" in
"status")
show_current_status
;;
"update-env")
update_environment "$2" "$3" "$4"
;;
"show-env")
show_environment_info "$2"
;;
"list")
list_environments
;;
"create-env")
create_env_files "$2"
;;
"deploy-cmds")
show_deployment_commands "$2"
;;
"help"|*)
echo "πŸš€ World Cup AI Assistant - Environment Management Script"
echo ""
echo "Usage: $0 <command> [options]"
echo ""
echo "Commands:"
echo " status - Show current environment status"
echo " list - List all available environments"
echo " show-env <env> - Show information for specific environment"
echo " create-env <env> - Create environment-specific files"
echo " update-env <env> <agent-ip> <mcp-ip> - Update environment IPs"
echo " deploy-cmds <env> - Show deployment commands for environment"
echo " help - Show this help message"
echo ""
echo "Examples:"
echo " $0 status"
echo " $0 list"
echo " $0 show-env staging"
echo " $0 create-env staging"
echo " $0 update-env staging 10.0.1.100 10.0.1.101"
echo " $0 deploy-cmds staging"
;;
esac