ai_data_scientist / scripts /deploy.ps1
VAH6002's picture
Upload 264 files
56c063a verified
Raw History Blame Contribute Delete
7.82 kB
# 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