File size: 7,818 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
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
# MCP AI Data Scientist Deployment Script (PowerShell)
# Usage: .\scripts\deploy.ps1 [environment] [action]
# Environment: dev, staging, prod
# Action: build, up, down, restart, logs, health

param(
    [string]$Environment = "dev",
    [string]$Action = "up"
)

# Configuration
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$ProjectDir = Split-Path -Parent $ScriptDir

# Logging functions
function Write-Info {
    param([string]$Message)
    Write-Host "[INFO] $Message" -ForegroundColor Blue
}

function Write-Success {
    param([string]$Message)
    Write-Host "[SUCCESS] $Message" -ForegroundColor Green
}

function Write-Warning {
    param([string]$Message)
    Write-Host "[WARNING] $Message" -ForegroundColor Yellow
}

function Write-Error {
    param([string]$Message)
    Write-Host "[ERROR] $Message" -ForegroundColor Red
}

# Validate environment
function Test-Environment {
    switch ($Environment) {
        { $_ -in @("dev", "staging", "prod") } {
            Write-Info "Deploying to $Environment environment"
            return $true
        }
        default {
            Write-Error "Invalid environment: $Environment. Use dev, staging, or prod"
            return $false
        }
    }
}

# Check prerequisites
function Test-Prerequisites {
    Write-Info "Checking prerequisites..."
    
    # Check Docker
    if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
        Write-Error "Docker is not installed"
        return $false
    }
    
    # Check Docker Compose
    if (-not (Get-Command docker-compose -ErrorAction SilentlyContinue)) {
        Write-Error "Docker Compose is not installed"
        return $false
    }
    
    # Check .env file
    if (-not (Test-Path "$ProjectDir\.env")) {
        Write-Error ".env file not found. Please create it from .env.example"
        return $false
    }
    
    Write-Success "Prerequisites check passed"
    return $true
}

# Build images
function Build-Images {
    Write-Info "Building Docker images..."
    
    Set-Location $ProjectDir
    
    switch ($Environment) {
        "dev" {
            docker-compose -f docker-compose.yml -f docker-compose.dev.yml build
        }
        "prod" {
            docker-compose -f docker-compose.yml -f docker-compose.prod.yml build
        }
        default {
            docker-compose build
        }
    }
    
    if ($LASTEXITCODE -eq 0) {
        Write-Success "Images built successfully"
        return $true
    } else {
        Write-Error "Failed to build images"
        return $false
    }
}

# Start services
function Start-Services {
    Write-Info "Starting services..."
    
    Set-Location $ProjectDir
    
    switch ($Environment) {
        "dev" {
            docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
        }
        "prod" {
            docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
        }
        default {
            docker-compose up -d
        }
    }
    
    if ($LASTEXITCODE -eq 0) {
        Write-Success "Services started successfully"
        return $true
    } else {
        Write-Error "Failed to start services"
        return $false
    }
}

# Stop services
function Stop-Services {
    Write-Info "Stopping services..."
    
    Set-Location $ProjectDir
    
    switch ($Environment) {
        "dev" {
            docker-compose -f docker-compose.yml -f docker-compose.dev.yml down
        }
        "prod" {
            docker-compose -f docker-compose.yml -f docker-compose.prod.yml down
        }
        default {
            docker-compose down
        }
    }
    
    if ($LASTEXITCODE -eq 0) {
        Write-Success "Services stopped successfully"
        return $true
    } else {
        Write-Error "Failed to stop services"
        return $false
    }
}

# Restart services
function Restart-Services {
    Write-Info "Restarting services..."
    if ((Stop-Services) -and (Start-Services)) {
        Write-Success "Services restarted successfully"
        return $true
    } else {
        Write-Error "Failed to restart services"
        return $false
    }
}

# Show logs
function Show-Logs {
    Write-Info "Showing logs..."
    
    Set-Location $ProjectDir
    
    switch ($Environment) {
        "dev" {
            docker-compose -f docker-compose.yml -f docker-compose.dev.yml logs -f
        }
        "prod" {
            docker-compose -f docker-compose.yml -f docker-compose.prod.yml logs -f
        }
        default {
            docker-compose logs -f
        }
    }
}

# Health check
function Test-Health {
    Write-Info "Performing health check..."
    
    # Wait for services to start
    Start-Sleep -Seconds 10
    
    $allHealthy = $true
    
    # Check orchestrator
    try {
        $response = Invoke-WebRequest -Uri "http://localhost:8000/health" -TimeoutSec 5
        if ($response.StatusCode -eq 200) {
            Write-Success "Orchestrator is healthy"
        } else {
            Write-Error "Orchestrator health check failed"
            $allHealthy = $false
        }
    } catch {
        Write-Error "Orchestrator health check failed: $($_.Exception.Message)"
        $allHealthy = $false
    }
    
    # Check Streamlit
    try {
        $response = Invoke-WebRequest -Uri "http://localhost:8501/_stcore/health" -TimeoutSec 5
        if ($response.StatusCode -eq 200) {
            Write-Success "Streamlit UI is healthy"
        } else {
            Write-Error "Streamlit UI health check failed"
            $allHealthy = $false
        }
    } catch {
        Write-Error "Streamlit UI health check failed: $($_.Exception.Message)"
        $allHealthy = $false
    }
    
    # Check MCP servers
    $mcpPorts = @(3001, 3002, 3003, 3004, 3005, 3006)
    $mcpNames = @("data-cleaning", "hypothesis-testing", "preprocessing", "feature-engineering", "model-training", "action-generation")
    
    for ($i = 0; $i -lt $mcpPorts.Length; $i++) {
        $port = $mcpPorts[$i]
        $name = $mcpNames[$i]
        
        try {
            $tcpClient = New-Object System.Net.Sockets.TcpClient
            $tcpClient.Connect("localhost", $port)
            $tcpClient.Close()
            Write-Success "$name MCP server is healthy"
        } catch {
            Write-Error "$name MCP server health check failed"
            $allHealthy = $false
        }
    }
    
    if ($allHealthy) {
        Write-Success "All services are healthy"
        return $true
    } else {
        Write-Error "Some services are unhealthy"
        return $false
    }
}

# Main execution
function Main {
    if (-not (Test-Environment)) {
        exit 1
    }
    
    if (-not (Test-Prerequisites)) {
        exit 1
    }
    
    Set-Location $ProjectDir
    
    switch ($Action) {
        "build" {
            if (-not (Build-Images)) { exit 1 }
        }
        "up" {
            if (-not (Build-Images)) { exit 1 }
            if (-not (Start-Services)) { exit 1 }
            if (-not (Test-Health)) { exit 1 }
        }
        "down" {
            if (-not (Stop-Services)) { exit 1 }
        }
        "restart" {
            if (-not (Restart-Services)) { exit 1 }
            if (-not (Test-Health)) { exit 1 }
        }
        "logs" {
            Show-Logs
        }
        "health" {
            if (-not (Test-Health)) { exit 1 }
        }
        default {
            Write-Error "Invalid action: $Action. Use build, up, down, restart, logs, or health"
            exit 1
        }
    }
}

# Run main function
Main