Spaces:
Running

File size: 8,247 Bytes
e251b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/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