Files
mtusi2026lab2/scripts/restart.ps1

45 lines
1.9 KiB
PowerShell

$ErrorActionPreference = 'Stop'
Set-Location (Split-Path -Parent $PSScriptRoot)
function Read-Containers {
$result = @()
foreach ($service in @('api', 'postgres', 'redis')) {
$containerID = docker compose ps -q $service
if ($LASTEXITCODE -ne 0 -or -not $containerID) { throw "Cannot find $service container" }
$json = docker inspect --format '{{json .}}' $containerID
if ($LASTEXITCODE -ne 0) { throw "Cannot inspect $service container" }
$container = $json | ConvertFrom-Json
# Only retain safe lifecycle fields; never write Config.Env or secrets.
$result += [pscustomobject]@{
service = $service
id = $container.Id
started_at = $container.State.StartedAt
status = $container.State.Status
data_volumes = @($container.Mounts | Where-Object Type -eq 'volume' | ForEach-Object Name)
}
}
return $result
}
$before = @(Read-Containers)
docker compose stop api postgres redis
if ($LASTEXITCODE -ne 0) { throw 'Stop failed' }
docker compose up -d --wait postgres redis
if ($LASTEXITCODE -ne 0) { throw 'PostgreSQL restart failed' }
docker compose up -d --wait api
if ($LASTEXITCODE -ne 0) { throw 'API restart failed' }
$after = @(Read-Containers)
foreach ($old in $before) {
$new = $after | Where-Object service -eq $old.service
if ($new.started_at -eq $old.started_at -or $new.status -ne 'running') {
throw "Restart not confirmed for $($old.service)"
}
if (($new.data_volumes -join ',') -ne ($old.data_volumes -join ',')) {
throw "Volume changed for $($old.service)"
}
}
$evidence = [ordered]@{ before = $before; after = $after; volumes_preserved = $true }
$path = Join-Path $PWD 'evidence/restart-all-containers.json'
[System.IO.File]::WriteAllText($path, ($evidence | ConvertTo-Json -Depth 5), [System.Text.UTF8Encoding]::new($false))
Write-Output 'PASS: API, PostgreSQL and Redis restarted; named volumes preserved.'