File size: 5,484 Bytes
56c063a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

# MCP AI Data Scientist Deployment Script
# Usage: ./scripts/deploy.sh [environment] [action]
# Environment: dev, staging, prod
# Action: build, up, down, restart, logs, health

set -e

# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
ENVIRONMENT=${1:-dev}
ACTION=${2:-up}

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Logging functions
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Validate environment
validate_environment() {
    case $ENVIRONMENT in
        dev|staging|prod)
            log_info "Deploying to $ENVIRONMENT environment"
            ;;
        *)
            log_error "Invalid environment: $ENVIRONMENT. Use dev, staging, or prod"
            exit 1
            ;;
    esac
}

# Check prerequisites
check_prerequisites() {
    log_info "Checking prerequisites..."
    
    # Check Docker
    if ! command -v docker &> /dev/null; then
        log_error "Docker is not installed"
        exit 1
    fi
    
    # Check Docker Compose
    if ! command -v docker-compose &> /dev/null; then
        log_error "Docker Compose is not installed"
        exit 1
    fi
    
    # Check .env file
    if [ ! -f "$PROJECT_DIR/.env" ]; then
        log_error ".env file not found. Please create it from .env.example"
        exit 1
    fi
    
    log_success "Prerequisites check passed"
}

# Build images
build_images() {
    log_info "Building Docker images..."
    
    cd "$PROJECT_DIR"
    
    if [ "$ENVIRONMENT" = "dev" ]; then
        docker-compose -f docker-compose.yml -f docker-compose.dev.yml build
    elif [ "$ENVIRONMENT" = "prod" ]; then
        docker-compose -f docker-compose.yml -f docker-compose.prod.yml build
    else
        docker-compose build
    fi
    
    log_success "Images built successfully"
}

# Start services
start_services() {
    log_info "Starting services..."
    
    cd "$PROJECT_DIR"
    
    if [ "$ENVIRONMENT" = "dev" ]; then
        docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
    elif [ "$ENVIRONMENT" = "prod" ]; then
        docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
    else
        docker-compose up -d
    fi
    
    log_success "Services started successfully"
}

# Stop services
stop_services() {
    log_info "Stopping services..."
    
    cd "$PROJECT_DIR"
    
    if [ "$ENVIRONMENT" = "dev" ]; then
        docker-compose -f docker-compose.yml -f docker-compose.dev.yml down
    elif [ "$ENVIRONMENT" = "prod" ]; then
        docker-compose -f docker-compose.yml -f docker-compose.prod.yml down
    else
        docker-compose down
    fi
    
    log_success "Services stopped successfully"
}

# Restart services
restart_services() {
    log_info "Restarting services..."
    stop_services
    start_services
    log_success "Services restarted successfully"
}

# Show logs
show_logs() {
    log_info "Showing logs..."
    
    cd "$PROJECT_DIR"
    
    if [ "$ENVIRONMENT" = "dev" ]; then
        docker-compose -f docker-compose.yml -f docker-compose.dev.yml logs -f
    elif [ "$ENVIRONMENT" = "prod" ]; then
        docker-compose -f docker-compose.yml -f docker-compose.prod.yml logs -f
    else
        docker-compose logs -f
    fi
}

# Health check
health_check() {
    log_info "Performing health check..."
    
    # Wait for services to start
    sleep 10
    
    # Check orchestrator
    if curl -f http://localhost:8000/health > /dev/null 2>&1; then
        log_success "Orchestrator is healthy"
    else
        log_error "Orchestrator health check failed"
        return 1
    fi
    
    # Check Streamlit
    if curl -f http://localhost:8501/_stcore/health > /dev/null 2>&1; then
        log_success "Streamlit UI is healthy"
    else
        log_error "Streamlit UI health check failed"
        return 1
    fi
    
    # Check MCP servers
    local mcp_ports=(3001 3002 3003 3004 3005 3006)
    local mcp_names=("data-cleaning" "hypothesis-testing" "preprocessing" "feature-engineering" "model-training" "action-generation")
    
    for i in "${!mcp_ports[@]}"; do
        local port=${mcp_ports[$i]}
        local name=${mcp_names[$i]}
        
        if python3 -c "import socket; s=socket.socket(); s.connect(('localhost', $port)); s.close()" 2>/dev/null; then
            log_success "$name MCP server is healthy"
        else
            log_error "$name MCP server health check failed"
            return 1
        fi
    done
    
    log_success "All services are healthy"
}

# Main execution
main() {
    validate_environment
    check_prerequisites
    
    cd "$PROJECT_DIR"
    
    case $ACTION in
        build)
            build_images
            ;;
        up)
            build_images
            start_services
            health_check
            ;;
        down)
            stop_services
            ;;
        restart)
            restart_services
            health_check
            ;;
        logs)
            show_logs
            ;;
        health)
            health_check
            ;;
        *)
            log_error "Invalid action: $ACTION. Use build, up, down, restart, logs, or health"
            exit 1
            ;;
    esac
}

# Run main function
main "$@"