45 lines
2.6 KiB
PowerShell
45 lines
2.6 KiB
PowerShell
# Windows PowerShell version of scripts/restart.sh (persistence hook, lab7): restart LB, ALL API replicas, workers,
|
|
# PostgreSQL, Redis, S3, NATS JetStream, OpenBao (+ operator unseal) and observability keeping volumes. State before/after -> evidence\restart-hook.txt
|
|
$ErrorActionPreference = 'Continue'
|
|
$root = Split-Path -Parent $PSScriptRoot
|
|
Set-Location $root
|
|
$envFile = if ($env:ENV_FILE) { $env:ENV_FILE } else { '.env' }
|
|
$log = Join-Path $root 'evidence\restart-hook.txt'
|
|
function Dc { & docker compose --env-file $envFile @args 2>$null | Out-Null; if ($LASTEXITCODE -ne 0) { throw ('docker compose ' + ($args -join ' ') + ' failed') } }
|
|
function Get-State {
|
|
foreach ($cid in (& docker compose --env-file $envFile ps -a -q)) {
|
|
& docker inspect -f '{{index .Config.Labels "com.docker.compose.service"}} {{.Name}} status={{.State.Status}} started={{.State.StartedAt}} pid={{.State.Pid}}' $cid
|
|
}
|
|
& docker volume ls --filter label=com.docker.compose.project=mtusi-lab7 --format 'volume {{.Name}}'
|
|
}
|
|
try {
|
|
Add-Content -Path $log -Encoding UTF8 -Value ('== ' + (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ') + ' before restart')
|
|
Add-Content -Path $log -Encoding UTF8 -Value (Get-State)
|
|
$ttl = @(& docker compose --env-file $envFile ps -q api-ttl 2>$null).Count -gt 0
|
|
$workers = @(& docker compose --env-file $envFile ps -q worker 2>$null).Count -gt 0
|
|
Dc stop lb api worker
|
|
if ($ttl) { Dc stop api-ttl }
|
|
Dc restart postgres redis s3 nats openbao otel-gw prometheus-gw loki-gw tempo-gw
|
|
Dc restart otel-collector prometheus loki tempo grafana
|
|
$env:NO_PROXY = '127.0.0.1,localhost'
|
|
& python scripts/bao_admin.py unseal *>> $log # operator: 2 of 3 shares (values are not printed)
|
|
# Wait for healthy storages (S3: gateway + master leader + writes; NATS: JetStream) before API/worker/LB.
|
|
foreach ($svc in 'postgres', 'redis', 's3', 'nats', 'openbao', 'prometheus') {
|
|
$cid = (& docker compose --env-file $envFile ps -q $svc | Select-Object -First 1)
|
|
for ($i = 0; $i -lt 120; $i++) {
|
|
if ((& docker inspect -f '{{.State.Health.Status}}' $cid) -eq 'healthy') { break }
|
|
Start-Sleep -Milliseconds 500
|
|
}
|
|
}
|
|
Dc start api
|
|
if ($ttl) { Dc start api-ttl }
|
|
if ($workers) { Dc start worker }
|
|
Dc start lb
|
|
Add-Content -Path $log -Encoding UTF8 -Value ('== ' + (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ') + ' after restart')
|
|
Add-Content -Path $log -Encoding UTF8 -Value (Get-State)
|
|
exit 0
|
|
} catch {
|
|
[Console]::Error.WriteLine($_.Exception.Message)
|
|
exit 1
|
|
}
|