rolling-update.ps1
Download rolling-update.ps1 as it is.
<#.SYNOPSIS Replaces the gateway containers of the compose stack one at a time, so clocks are never turned away: `docker compose up -d` after a new image would recreate every replica at once.
.DESCRIPTION For each gateway container running now: 1. starts one more replica from the image HOURADVANTAGE_IMAGE names and the current settings (`docker compose up -d --no-deps --no-recreate --scale gateway=N+1`), 2. waits until it is ready and HAProxy has it in rotation, 3. stops the old container (SIGTERM: it turns /api/ready to 503, HAProxy takes it out within about 4 s, it drains for 6 s, then closes), and removes it. The number of replicas is the same at the end as at the start.
Load the new image and set HOURADVANTAGE_IMAGE in .env to it first. Only the given compose project is touched.
.EXAMPLE powershell -ExecutionPolicy Bypass -File deploy\compose\rolling-update.ps1
.EXAMPLE # With an external database: .\rolling-update.ps1 -File docker-compose.yml, docker-compose.external-db.yml#>[CmdletBinding()]param( # The compose project; defaults to COMPOSE_PROJECT_NAME, then houradvantage. [string]$Project = $(if ($env:COMPOSE_PROJECT_NAME) { $env:COMPOSE_PROJECT_NAME } else { 'houradvantage' }), # The compose files, relative to this folder. [string[]]$File = @('docker-compose.yml'), # Seconds to wait for a new replica to be ready and in rotation. [int]$Timeout = 120)
$ErrorActionPreference = 'Stop'Set-StrictMode -Version 2.0$Here = Split-Path -Parent $MyInvocation.MyCommand.Path
function Compose { $files = @() foreach ($f in $File) { $files += @('-f', (Join-Path $Here $f)) } $prev = $ErrorActionPreference $ErrorActionPreference = 'Continue' try { $out = & docker compose -p $Project --project-directory $Here @files @args 2>&1 $code = $LASTEXITCODE } finally { $ErrorActionPreference = $prev } $text = ($out | ForEach-Object { "$_" }) -join "`n" if ($code -ne 0) { throw "docker compose $($args -join ' ') failed ($code):`n$text" } return $text}
function Gateways { return @((Compose ps -q gateway) -split "`n" | Where-Object { $_.Trim() } | ForEach-Object { $_.Trim() })}
# Servers HAProxy has UP in every device backend (be_http_<port>, one per# device port): the fewest UP in any of them.function InRotation { try { # No quotes inside: Windows PowerShell 5.1 mangles them on the way to docker. $stat = Compose exec -T haproxy sh -c 'echo show stat | nc 127.0.0.1 9999' } catch { return 0 } $up = @{} foreach ($row in ($stat -split "`n")) { $f = $row.Split(',') if ($f.Count -lt 18 -or $f[0] -notlike 'be_http_*' -or $f[1] -eq 'BACKEND') { continue } if (-not $up.ContainsKey($f[0])) { $up[$f[0]] = 0 } if ($f[17] -like 'UP*') { $up[$f[0]]++ } } if ($up.Count -eq 0) { return 0 } return ($up.Values | Measure-Object -Minimum).Minimum}
# The container's own check of /api/ready. Native stderr (why it is not# ready) is not an error; the exit code is the answer.function Ready([string]$id) { $old = $ErrorActionPreference $ErrorActionPreference = 'Continue' try { & docker exec $id hourd healthcheck --ready 2>&1 | Out-Null $code = $LASTEXITCODE } finally { $ErrorActionPreference = $old } return $code -eq 0}
# The replica started and not yet in rotation: removed again if the update# stops before it is, so that a failed one is not left behind to restart for# ever. The old ones are untouched until a new one is in rotation.$script:Starting = $null
function Abandon([int]$count) { if (-not $script:Starting) { return } Write-Host "last lines of $($script:Starting):" & docker logs --tail 3 $script:Starting 2>&1 | Write-Host & docker rm -f $script:Starting 2>&1 | Out-Null Write-Host "removed $($script:Starting); the $count gateway container(s) running before are still serving, untouched" $script:Starting = $null}
function WaitFor([scriptblock]$condition, [string]$what) { $deadline = (Get-Date).AddSeconds($Timeout) while ((Get-Date) -lt $deadline) { if (& $condition) { return } Start-Sleep -Milliseconds 500 } Abandon $count throw "timed out waiting for $what"}
$old = Gateways$count = $old.Countif ($count -eq 0) { throw "no gateway containers are running in project $Project" }Write-Host "replacing $count gateway container(s), one at a time"
foreach ($id in $old) { $before = Gateways Compose up -d --no-deps --no-recreate --scale "gateway=$($count + 1)" gateway | Out-Null $new = @(Gateways | Where-Object { $before -notcontains $_ }) if ($new.Count -ne 1) { throw "expected one new gateway container, got $($new.Count)" } $new = $new[0] $script:Starting = $new $name = (& docker inspect -f '{{.Name}}' $new).TrimStart('/') WaitFor { Ready $new } "$name to be ready" WaitFor { (InRotation) -ge ($count + 1) } "HAProxy to put $name in rotation" $oldName = (& docker inspect -f '{{.Name}}' $id).TrimStart('/') $script:Starting = $null Write-Host " $name is in rotation; stopping $oldName" # The gateway drains (6 s) before it closes and stops in about 20 s at the # most; compose gives it 40 s. & docker stop -t 40 $id | Out-Null & docker rm $id | Out-Null WaitFor { (InRotation) -eq $count } "HAProxy to drop $oldName"}Write-Host "done: $count gateway container(s) replaced"