forked from mtusi2026/mtusi2026lab7
7 — Т2302 — ебедев — на 5
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
# Windows PowerShell version of scripts/stop-one.sh (failover hook). Stops EXACTLY ONE API replica whose
|
||||
# X-Instance-Id is in $env:TARGET_INSTANCE_ID and leaves it stopped. DB/Redis/LB/volumes untouched.
|
||||
# Restore afterwards: scripts\start-replicas.ps1
|
||||
$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\failover-hook.txt'
|
||||
if (-not $env:TARGET_INSTANCE_ID) { [Console]::Error.WriteLine('TARGET_INSTANCE_ID is required'); exit 2 }
|
||||
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
|
||||
}
|
||||
}
|
||||
$target = $null
|
||||
foreach ($cid in (& docker compose --env-file $envFile ps -q api)) {
|
||||
$id = (& docker exec $cid cat /tmp/instance-id 2>$null)
|
||||
if ($id -and $id.Trim() -eq $env:TARGET_INSTANCE_ID) { $target = $cid }
|
||||
}
|
||||
if (-not $target) { [Console]::Error.WriteLine('no running api replica with this id'); exit 1 }
|
||||
$name = (& docker inspect -f '{{.Name}}' $target).TrimStart('/')
|
||||
$stamp = (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
|
||||
Add-Content -Path $log -Encoding UTF8 -Value ("== $stamp stop-one target=" + $env:TARGET_INSTANCE_ID + " container=$name")
|
||||
Add-Content -Path $log -Encoding UTF8 -Value (Get-State)
|
||||
& docker stop -t 15 $target | Out-Null
|
||||
if ($LASTEXITCODE -ne 0) { exit 1 }
|
||||
$stamp = (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
|
||||
Add-Content -Path $log -Encoding UTF8 -Value ("== $stamp after stop")
|
||||
Add-Content -Path $log -Encoding UTF8 -Value (Get-State)
|
||||
exit 0
|
||||
Reference in New Issue
Block a user