158 lines
6.0 KiB
PowerShell
158 lines
6.0 KiB
PowerShell
# Windows twin of scripts/deploy.sh, stop-one.sh, restore.sh, restart.sh and
|
|
# rolling-update.sh. The public resilience test launches an action script with
|
|
# Popen([script]) without a shell; on native Windows a .cmd wrapper is required.
|
|
#
|
|
# X-Instance-Id is the Swarm task container hostname, so a target is resolved by
|
|
# inspecting running task containers, not by trusting the header alone.
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[ValidateSet('deploy', 'stop-one', 'restore', 'restart', 'rollout')]
|
|
[string]$Mode,
|
|
[string]$Image = ''
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
|
|
Set-Location $root
|
|
$stack = if ($env:STACK_NAME) { $env:STACK_NAME } else { 'lab3' }
|
|
$service = "$stack" + '_api'
|
|
$port = if ($env:APP_PORT) { $env:APP_PORT } else { '8080' }
|
|
$resolveImage = if ($env:RESOLVE_IMAGE) { $env:RESOLVE_IMAGE } else { 'never' }
|
|
if (-not $Image) {
|
|
$Image = if ($env:API_IMAGE) { $env:API_IMAGE } else { 'mtusi-messenger-lab3:3.0.0' }
|
|
}
|
|
|
|
function Invoke-Docker {
|
|
param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Arguments)
|
|
& docker @Arguments
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw "docker $($Arguments -join ' ') failed with exit code $LASTEXITCODE"
|
|
}
|
|
}
|
|
|
|
function Test-Ready {
|
|
try {
|
|
$response = Invoke-WebRequest -UseBasicParsing -Uri "http://127.0.0.1:$port/health/ready" -TimeoutSec 3
|
|
return $response.StatusCode -eq 200
|
|
} catch {
|
|
return $false
|
|
}
|
|
}
|
|
|
|
function Wait-Ready {
|
|
param([int]$Seconds = 180)
|
|
$deadline = (Get-Date).AddSeconds($Seconds)
|
|
while ((Get-Date) -lt $deadline) {
|
|
if (Test-Ready) { return }
|
|
Start-Sleep -Seconds 2
|
|
}
|
|
throw "stack did not become ready within $Seconds seconds"
|
|
}
|
|
|
|
function Get-RunningTasks {
|
|
return @(& docker service ps -q $service --filter desired-state=running)
|
|
}
|
|
|
|
function Find-TargetContainer {
|
|
param([string]$Target)
|
|
foreach ($task in (Get-RunningTasks)) {
|
|
$container = (& docker inspect -f '{{.Status.ContainerStatus.ContainerID}}' $task 2>$null)
|
|
if (-not $container) { continue }
|
|
$hostname = (& docker inspect -f '{{.Config.Hostname}}' $container 2>$null)
|
|
if ($hostname -eq $Target) { return $container }
|
|
}
|
|
return $null
|
|
}
|
|
|
|
function New-HexSecret {
|
|
$bytes = New-Object byte[] 32
|
|
[System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
|
|
return (-join ($bytes | ForEach-Object { $_.ToString('x2') }))
|
|
}
|
|
|
|
function Ensure-SecretFiles {
|
|
New-Item -ItemType Directory -Force -Path 'secrets' | Out-Null
|
|
$passwordPath = Join-Path 'secrets' 'postgres_password'
|
|
$password = if ((Test-Path $passwordPath) -and (Get-Item $passwordPath).Length -gt 0) {
|
|
(Get-Content $passwordPath -Raw).Trim()
|
|
} else {
|
|
New-HexSecret
|
|
}
|
|
$defaults = [ordered]@{
|
|
postgres_password = $password
|
|
cursor_secret = (New-HexSecret)
|
|
cache_url = 'redis://valkey:6379/0'
|
|
database_url = "postgresql+asyncpg://messenger:$password@postgres:5432/messenger"
|
|
}
|
|
foreach ($name in $defaults.Keys) {
|
|
$path = Join-Path 'secrets' $name
|
|
if (-not (Test-Path $path) -or (Get-Item $path).Length -eq 0) {
|
|
Set-Content -NoNewline -Encoding ascii -Path $path -Value $defaults[$name]
|
|
}
|
|
}
|
|
}
|
|
|
|
function Ensure-Secrets {
|
|
$names = @('database_url', 'cache_url', 'postgres_password', 'cursor_secret')
|
|
$existing = @(& docker secret ls --format '{{.Name}}')
|
|
foreach ($name in $names) {
|
|
$secret = "$stack" + '_' + $name
|
|
if ($existing -notcontains $secret) {
|
|
Invoke-Docker secret create $secret (Join-Path 'secrets' $name) | Out-Null
|
|
}
|
|
}
|
|
}
|
|
|
|
switch ($Mode) {
|
|
'deploy' {
|
|
$state = (& docker info --format '{{.Swarm.LocalNodeState}}').Trim()
|
|
if ($state -ne 'active') { Invoke-Docker swarm init | Out-Null }
|
|
Ensure-SecretFiles
|
|
Ensure-Secrets
|
|
Invoke-Docker build -t $Image . | Out-Null
|
|
$env:API_IMAGE = $Image
|
|
Invoke-Docker stack deploy --resolve-image $resolveImage -c stack.yaml $stack | Out-Null
|
|
Wait-Ready
|
|
& docker stack services $stack
|
|
}
|
|
'stop-one' {
|
|
$target = $env:TARGET_INSTANCE_ID
|
|
if (-not $target) { throw 'TARGET_INSTANCE_ID is required' }
|
|
$container = Find-TargetContainer $target
|
|
if (-not $container) { throw "target instance $target not found among running tasks" }
|
|
Write-Output "stopping instance $target container $container"
|
|
Invoke-Docker stop --time 15 $container | Out-Null
|
|
# Swarm may already be creating a replacement task; the target container
|
|
# itself was stopped by id and its X-Instance-Id will not be served again.
|
|
Write-Output "instance $target stopped"
|
|
}
|
|
'restore' {
|
|
Invoke-Docker service update --restart-condition any --replicas 2 $service | Out-Null
|
|
$running = @(Get-RunningTasks).Count
|
|
if ($running -lt 2) { Invoke-Docker service update --force $service | Out-Null }
|
|
$deadline = (Get-Date).AddSeconds(120)
|
|
while ((Get-Date) -lt $deadline) {
|
|
$running = @(Get-RunningTasks).Count
|
|
if ($running -ge 2 -and (Test-Ready)) {
|
|
Write-Output "api restored: $running running tasks and readiness OK"
|
|
exit 0
|
|
}
|
|
Start-Sleep -Seconds 2
|
|
}
|
|
throw 'api did not recover within 120s'
|
|
}
|
|
'restart' {
|
|
foreach ($name in @('postgres', 'valkey', 'api')) {
|
|
$svc = $stack + '_' + $name
|
|
Write-Output "restarting $svc"
|
|
Invoke-Docker service update --force $svc | Out-Null
|
|
}
|
|
Wait-Ready
|
|
Write-Output 'stack ready after restart'
|
|
}
|
|
'rollout' {
|
|
Invoke-Docker build --build-arg APP_REVISION=$Image -t $Image . | Out-Null
|
|
Invoke-Docker service update --image $Image --update-order start-first --update-parallelism 1 --update-delay 5s --update-failure-action rollback $service | Out-Null
|
|
& docker service ps $service --no-trunc
|
|
}
|
|
} |