Files
Data-Coupler/build-docker.ps1
T
Alessio Dal Santo 23c2788fcf -Aggiunta configurazione tramite dockerfile per windows e linux
-Aggiunte github actions per la compilazione dei container e l'esposizione
2026-01-16 14:16:15 +01:00

169 lines
5.7 KiB
PowerShell

# PowerShell script per build e test locale dei container Docker
# Utilizzo: .\build-docker.ps1 [-Target <linux|windows|all>] [-Test] [-Clean]
param(
[Parameter(Position=0)]
[ValidateSet("linux", "windows", "all", "clean")]
[string]$Target = "all",
[switch]$Test,
[switch]$Clean
)
$ErrorActionPreference = "Stop"
Write-Host "╔═══════════════════════════════════════════╗" -ForegroundColor Green
Write-Host "║ Data-Coupler Docker Build Script ║" -ForegroundColor Green
Write-Host "╚═══════════════════════════════════════════╝" -ForegroundColor Green
Write-Host ""
# Funzione per build Linux
function Build-Linux {
Write-Host "📦 Building Linux container..." -ForegroundColor Yellow
docker build -t data-coupler:local -f Dockerfile .
if ($LASTEXITCODE -eq 0) {
Write-Host "✅ Linux container built successfully!" -ForegroundColor Green
} else {
Write-Host "❌ Linux container build failed!" -ForegroundColor Red
exit 1
}
Write-Host ""
}
# Funzione per build Windows
function Build-Windows {
Write-Host "📦 Building Windows container..." -ForegroundColor Yellow
docker build -t data-coupler:local-windows -f Dockerfile.windows .
if ($LASTEXITCODE -eq 0) {
Write-Host "✅ Windows container built successfully!" -ForegroundColor Green
} else {
Write-Host "❌ Windows container build failed!" -ForegroundColor Red
exit 1
}
Write-Host ""
}
# Funzione per test container
function Test-Container {
param(
[string]$Image,
[string]$ContainerName
)
Write-Host "🧪 Testing container: $Image" -ForegroundColor Yellow
# Stop e rimuovi container esistente
docker stop $ContainerName 2>$null | Out-Null
docker rm $ContainerName 2>$null | Out-Null
# Avvia container
Write-Host "Starting container..."
docker run -d --name $ContainerName -p 7550:7550 $Image | Out-Null
# Attendi avvio (max 60 secondi)
Write-Host "Waiting for container to be ready..."
$ready = $false
for ($i = 1; $i -le 60; $i++) {
$logs = docker logs $ContainerName 2>&1
if ($logs -match "Now listening on") {
Write-Host "✅ Container started successfully!" -ForegroundColor Green
$ready = $true
break
}
if ($i -eq 60) {
Write-Host "❌ Container failed to start within 60 seconds" -ForegroundColor Red
docker logs $ContainerName
return $false
}
Start-Sleep -Seconds 1
}
if (-not $ready) {
return $false
}
# Test health endpoint
Write-Host "Testing health endpoint..."
Start-Sleep -Seconds 5
try {
$response = Invoke-WebRequest -Uri "http://localhost:7550/health" -UseBasicParsing -TimeoutSec 10
if ($response.StatusCode -eq 200) {
Write-Host "✅ Health check passed!" -ForegroundColor Green
} else {
Write-Host "❌ Health check failed with status: $($response.StatusCode)" -ForegroundColor Red
return $false
}
} catch {
Write-Host "❌ Health check failed: $_" -ForegroundColor Red
docker logs $ContainerName
return $false
}
# Mostra log
Write-Host ""
Write-Host "📋 Container logs:" -ForegroundColor Yellow
docker logs --tail 20 $ContainerName
Write-Host ""
Write-Host "✅ Container test completed successfully!" -ForegroundColor Green
Write-Host "💡 Container is running at: http://localhost:7550" -ForegroundColor Yellow
Write-Host "💡 Stop with: docker stop $ContainerName" -ForegroundColor Yellow
Write-Host ""
return $true
}
# Funzione per cleanup
function Clean-Containers {
Write-Host "🧹 Cleaning up test containers..." -ForegroundColor Yellow
docker stop data-coupler-test 2>$null | Out-Null
docker rm data-coupler-test 2>$null | Out-Null
docker stop data-coupler-test-windows 2>$null | Out-Null
docker rm data-coupler-test-windows 2>$null | Out-Null
Write-Host "✅ Cleanup completed" -ForegroundColor Green
}
# Menu principale
if ($Clean) {
Clean-Containers
exit 0
}
switch ($Target) {
"linux" {
Build-Linux
if ($Test) {
Test-Container -Image "data-coupler:local" -ContainerName "data-coupler-test"
}
}
"windows" {
Build-Windows
if ($Test) {
Test-Container -Image "data-coupler:local-windows" -ContainerName "data-coupler-test-windows"
}
}
"all" {
Build-Linux
Build-Windows
if ($Test) {
Write-Host "Testing Linux container..." -ForegroundColor Cyan
Test-Container -Image "data-coupler:local" -ContainerName "data-coupler-test"
Write-Host ""
Write-Host "Testing Windows container..." -ForegroundColor Cyan
Test-Container -Image "data-coupler:local-windows" -ContainerName "data-coupler-test-windows"
}
}
"clean" {
Clean-Containers
}
}
Write-Host ""
Write-Host "🎉 All done!" -ForegroundColor Green
Write-Host ""
Write-Host "Usage examples:" -ForegroundColor Cyan
Write-Host " .\build-docker.ps1 linux -Test # Build and test Linux container" -ForegroundColor Gray
Write-Host " .\build-docker.ps1 windows -Test # Build and test Windows container" -ForegroundColor Gray
Write-Host " .\build-docker.ps1 all # Build all containers" -ForegroundColor Gray
Write-Host " .\build-docker.ps1 -Clean # Clean up test containers" -ForegroundColor Gray