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
|
$infraOk = 0
|
||||||
$infraFail = 0
|
$infraFail = 0
|
||||||
foreach ($svc in $infraContainers) {
|
foreach ($svc in $infraContainers) {
|
||||||
$status = docker inspect --format='{{.State.Health.Status}}' $svc.Container 2>$null
|
$running = docker inspect -f '{{.State.Running}}' $svc.Container 2>$null
|
||||||
if ($status -eq "healthy") {
|
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
|
Write-Host " [OK] $($svc.Name)" -ForegroundColor Green
|
||||||
$infraOk++
|
$infraOk++
|
||||||
} elseif ($status -eq "running") {
|
} else {
|
||||||
Write-Host " [WARN] $($svc.Name) (running, no healthcheck)" -ForegroundColor Yellow
|
Write-Host " [WARN] $($svc.Name) (running, no healthcheck)" -ForegroundColor Yellow
|
||||||
$infraOk++
|
$infraOk++
|
||||||
} else {
|
|
||||||
Write-Host " [FAIL] $($svc.Name) (status=$status)" -ForegroundColor Red
|
|
||||||
$infraFail++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,22 +36,28 @@ if (-not $SkipInfraCheck) {
|
|||||||
@{Name="Jaeger"; Container="edu-jaeger"}
|
@{Name="Jaeger"; Container="edu-jaeger"}
|
||||||
)
|
)
|
||||||
$allHealthy = $true
|
$allHealthy = $true
|
||||||
|
$prevEAP = $ErrorActionPreference
|
||||||
|
$ErrorActionPreference = "Continue"
|
||||||
foreach ($svc in $infraServices) {
|
foreach ($svc in $infraServices) {
|
||||||
$status = docker inspect --format='{{.State.Health.Status}}' $svc.Container 2>$null
|
$running = docker inspect -f '{{.State.Running}}' $svc.Container 2>$null
|
||||||
if ($status -eq "healthy") {
|
if ($running -ne "true") {
|
||||||
Write-Host " [OK] $($svc.Name) ($($svc.Container))" -ForegroundColor Green
|
Write-Host " [FAIL] $($svc.Name) not running" -ForegroundColor Red
|
||||||
} 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
|
|
||||||
$allHealthy = $false
|
$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) {
|
if (-not $allHealthy) {
|
||||||
Write-Host ""
|
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
|
exit 1
|
||||||
}
|
}
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
|
|||||||
Reference in New Issue
Block a user