fix(infra): use conditional docker template to avoid Health key error
Use {{if .State.Health}}...{{end}} instead of direct {{.State.Health.Status}}.
This commit is contained in:
@@ -34,16 +34,20 @@ $infraContainers = @(
|
||||
$infraOk = 0
|
||||
$infraFail = 0
|
||||
foreach ($svc in $infraContainers) {
|
||||
$status = docker inspect --format='{{.State.Health.Status}}' $svc.Container 2>$null
|
||||
if ($status -eq "healthy") {
|
||||
$running = docker inspect -f '{{.State.Running}}' $svc.Container 2>$null
|
||||
if ($running -ne "true") {
|
||||
Write-Host " [FAIL] $($svc.Name) not running" -ForegroundColor Red
|
||||
$infraFail++
|
||||
continue
|
||||
}
|
||||
# 条件模板:Health 不存在时返回空字符串,不报错
|
||||
$health = docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{end}}' $svc.Container 2>$null
|
||||
if ($health -eq "healthy") {
|
||||
Write-Host " [OK] $($svc.Name)" -ForegroundColor Green
|
||||
$infraOk++
|
||||
} elseif ($status -eq "running") {
|
||||
} else {
|
||||
Write-Host " [WARN] $($svc.Name) (running, no healthcheck)" -ForegroundColor Yellow
|
||||
$infraOk++
|
||||
} else {
|
||||
Write-Host " [FAIL] $($svc.Name) (status=$status)" -ForegroundColor Red
|
||||
$infraFail++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,22 +36,28 @@ if (-not $SkipInfraCheck) {
|
||||
@{Name="Jaeger"; Container="edu-jaeger"}
|
||||
)
|
||||
$allHealthy = $true
|
||||
$prevEAP = $ErrorActionPreference
|
||||
$ErrorActionPreference = "Continue"
|
||||
foreach ($svc in $infraServices) {
|
||||
$status = docker inspect --format='{{.State.Health.Status}}' $svc.Container 2>$null
|
||||
if ($status -eq "healthy") {
|
||||
Write-Host " [OK] $($svc.Name) ($($svc.Container))" -ForegroundColor Green
|
||||
} elseif ($status -eq "running") {
|
||||
Write-Host " [WARN] $($svc.Name) running but no healthcheck" -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host " [FAIL] $($svc.Name) not started or unhealthy (status=$status)" -ForegroundColor Red
|
||||
Write-Host " Please start infrastructure first:" -ForegroundColor Red
|
||||
Write-Host " docker compose -f infra/docker-compose.yml --profile p6 --profile observability up -d" -ForegroundColor Red
|
||||
$running = docker inspect -f '{{.State.Running}}' $svc.Container 2>$null
|
||||
if ($running -ne "true") {
|
||||
Write-Host " [FAIL] $($svc.Name) not running" -ForegroundColor Red
|
||||
$allHealthy = $false
|
||||
continue
|
||||
}
|
||||
# 条件模板:Health 不存在时返回空字符串,不报错
|
||||
$health = docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{end}}' $svc.Container 2>$null
|
||||
if ($health -eq "healthy") {
|
||||
Write-Host " [OK] $($svc.Name) ($($svc.Container))" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host " [WARN] $($svc.Name) running (no healthcheck)" -ForegroundColor Yellow
|
||||
}
|
||||
}
|
||||
$ErrorActionPreference = $prevEAP
|
||||
if (-not $allHealthy) {
|
||||
Write-Host ""
|
||||
Write-Host "Infrastructure not ready. Start it first." -ForegroundColor Red
|
||||
Write-Host "Infrastructure not ready. Start it first:" -ForegroundColor Red
|
||||
Write-Host " docker compose -f infra/docker-compose.yml --profile p6 --profile observability up -d" -ForegroundColor White
|
||||
exit 1
|
||||
}
|
||||
Write-Host ""
|
||||
|
||||
Reference in New Issue
Block a user