[deploy] Anywhere: the Docker image and compose file (edge and control), the Linux installer and systemd units, the Windows Server installer (Caddy mode, three services), the preview client for Fabrica, and the Windows smoke script

This commit is contained in:
2026-11-17 20:42:04 +01:00
parent 09f6b12676
commit 49c05e2523
12 changed files with 988 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# The Windows twin of scripts/preview.sh (a Fabrica runner on Windows):
#
# scripts\preview.ps1 open -Project shop -Branch fix-42 -Upstream http://10.0.0.5:8080 [-Ttl 86400]
# scripts\preview.ps1 close -Project shop -Branch fix-42
# scripts\preview.ps1 list
#
# $env:IANITOR_URL, $env:IANITOR_PREVIEW_TOKEN (a `--scope previews` token from the Custos team
# vault). An internal (Domi) admin host needs the Domi root in the machine's trusted roots.
param(
[Parameter(Mandatory = $true, Position = 0)][ValidateSet('open', 'close', 'list')][string]$Action,
[string]$Project, [string]$Branch, [string]$Upstream, [int]$Ttl = 0
)
$ErrorActionPreference = 'Stop'
if (-not $env:IANITOR_URL -or -not $env:IANITOR_PREVIEW_TOKEN) { throw 'set IANITOR_URL and IANITOR_PREVIEW_TOKEN' }
$base = $env:IANITOR_URL.TrimEnd('/')
$headers = @{ Authorization = "Bearer $($env:IANITOR_PREVIEW_TOKEN)" }
function Call($method, $path, $body) {
$p = @{ Method = $method; Uri = "$base$path"; Headers = $headers; UseBasicParsing = $true; ContentType = 'application/json' }
if ($body) { $p.Body = [Text.Encoding]::UTF8.GetBytes(($body | ConvertTo-Json -Compress)) }
try {
$r = Invoke-WebRequest @p
return @{ Code = [int]$r.StatusCode; Json = ($r.Content | ConvertFrom-Json) }
} catch [System.Net.WebException] {
$resp = $_.Exception.Response
if (-not $resp) { throw }
$text = (New-Object IO.StreamReader($resp.GetResponseStream())).ReadToEnd()
return @{ Code = [int]$resp.StatusCode; Json = ($text | ConvertFrom-Json) }
}
}
switch ($Action) {
'open' {
$body = @{ project = $Project; branch = $Branch; upstream = $Upstream }
if ($Ttl -gt 0) { $body.ttl = $Ttl }
$r = Call POST '/api/v1/previews' $body
if ($r.Code -ne 202) { throw "preview: $($r.Json.error.message)" }
$host_ = $r.Json.preview.hostname
for ($i = 0; $i -lt 150; $i++) {
$s = Call GET "/api/v1/previews/$host_" $null
if ($s.Code -ne 200) { throw "preview: $($s.Json.error.message)" }
if ($s.Json.preview.state -eq 'live') { Write-Output $r.Json.preview.url; exit 0 }
if ($s.Json.preview.state -eq 'failed') { throw "preview: $($s.Json.preview.problem)" }
Start-Sleep -Seconds 2
}
throw 'preview: not live after 5 minutes'
}
'close' {
$r = Call POST '/api/v1/previews/close' @{ project = $Project; branch = $Branch }
if ($r.Code -eq 202) { 'closing' } elseif ($r.Code -eq 404) { 'no preview' } else { throw "preview: $($r.Json.error.message)" }
}
'list' {
$r = Call GET '/api/v1/previews' $null
$r.Json.items | ForEach-Object { "$($_.url)`t$($_.project)`t$($_.branch)`t$($_.state)`tuntil $($_.expires_at)" }
}
}
+78
View File
@@ -0,0 +1,78 @@
#!/bin/sh
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# A client of Ianitor's preview API (docs/previews.md) for a Fabrica pipeline step or a script:
#
# scripts/preview.sh open <project> <branch> <upstream> [ttl-seconds] prints the preview's URL
# scripts/preview.sh close <project> <branch>
# scripts/preview.sh list
#
# IANITOR_URL Ianitor's admin address, e.g. https://ianitor.example.org
# IANITOR_PREVIEW_TOKEN a token made with `ianitor-control token create --scope previews`
# (kept in the Custos team vault; never in the repository or a log)
# IANITOR_CA optional: a CA file for curl (a Domi root, for an internal admin host)
#
# `open` waits until the preview is live (its apply kept) and fails with Ianitor's reason
# otherwise. Needs curl and jq.
set -eu
: "${IANITOR_URL:?set IANITOR_URL}"
: "${IANITOR_PREVIEW_TOKEN:?set IANITOR_PREVIEW_TOKEN}"
command -v jq >/dev/null || { echo "preview: needs jq" >&2; exit 2; }
api() {
# The token goes in a header read from stdin, so it never shows in `ps`.
method=$1; path=$2; body=${3:-}
set -- -sS -X "$method" -H "Content-Type: application/json" -H @- -w '\n%{http_code}'
[ -n "${IANITOR_CA:-}" ] && set -- "$@" --cacert "$IANITOR_CA"
[ -n "$body" ] && set -- "$@" --data "$body"
printf 'Authorization: Bearer %s\n' "$IANITOR_PREVIEW_TOKEN" | curl "$@" "${IANITOR_URL%/}$path"
}
split() { # sets BODY and CODE from api's output
CODE=$(printf '%s' "$1" | tail -n1)
BODY=$(printf '%s' "$1" | sed '$d')
}
fail() {
echo "preview: $(printf '%s' "$BODY" | jq -r '.error.message // .' 2>/dev/null || printf '%s' "$BODY")" >&2
exit 1
}
case "${1:-}" in
open)
[ $# -ge 4 ] || { echo "usage: $0 open <project> <branch> <upstream> [ttl]" >&2; exit 2; }
body=$(jq -cn --arg p "$2" --arg b "$3" --arg u "$4" --arg t "${5:-}" \
'{project:$p, branch:$b, upstream:$u} + (if $t == "" then {} else {ttl: ($t|tonumber)} end)')
split "$(api POST /api/v1/previews "$body")"
[ "$CODE" = 202 ] || fail
host=$(printf '%s' "$BODY" | jq -r .preview.hostname)
url=$(printf '%s' "$BODY" | jq -r .preview.url)
i=0
while :; do
split "$(api GET "/api/v1/previews/$host")"
[ "$CODE" = 200 ] || fail
state=$(printf '%s' "$BODY" | jq -r .preview.state)
case "$state" in
live) echo "$url"; exit 0 ;;
failed) echo "preview: $(printf '%s' "$BODY" | jq -r '.preview.problem // "the apply failed"')" >&2; exit 1 ;;
esac
i=$((i + 1)); [ $i -le 150 ] || { echo "preview: not live after 5 minutes" >&2; exit 1; }
sleep 2
done
;;
close)
[ $# -eq 3 ] || { echo "usage: $0 close <project> <branch>" >&2; exit 2; }
split "$(api POST /api/v1/previews/close "$(jq -cn --arg p "$2" --arg b "$3" '{project:$p, branch:$b}')")"
case "$CODE" in 202) echo "closing"; ;; 404) echo "no preview"; ;; *) fail ;; esac
;;
list)
split "$(api GET /api/v1/previews)"
[ "$CODE" = 200 ] || fail
printf '%s' "$BODY" | jq -r '.items[] | "\(.url)\t\(.project)\t\(.branch)\t\(.state)\tuntil \(.expires_at)"'
;;
*)
echo "usage: $0 open|close|list ..." >&2; exit 2 ;;
esac
+129
View File
@@ -0,0 +1,129 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# Windows Caddy mode, end to end through the installer (T4). Everything happens in -Work, on
# non-standard ports, with a throw-away Domi root; no service is registered, no certificate is
# trusted, nothing is left running:
#
# 1. deploy\windows\install.ps1 -NoService (the release ianitor-control.exe, a given caddy.exe)
# 2. Caddy and control started in the console, as the services would start them
# 3. the intermediate signed offline with the throw-away root (docs/pki.md), control restarted
# so it adds the admin host
# 4. a previews-only token opens a branch preview with scripts\preview.ps1 (Fabrica's client)
# 5. the preview answers through Caddy with a Domi certificate; the admin host reaches control
# 6. the preview is closed and stops answering
#
# scripts\smoke-windows.ps1 -Control <ianitor-control.exe> -Caddy <caddy.exe> -Work <empty dir>
param(
[Parameter(Mandatory = $true)][string]$Control,
[Parameter(Mandatory = $true)][string]$Caddy,
[Parameter(Mandatory = $true)][string]$Work
)
$ErrorActionPreference = 'Stop'
$repo = Split-Path -Parent $PSScriptRoot
$Http = 18080; $Https = 18443; $Api = 17878; $Admin = 12019; $AppPort = 23456
$procs = @()
function Step($m) { Write-Host "== $m" }
function Check($ok, $m) { if (-not $ok) { throw "FAILED: $m" } ; Write-Host " ok: $m" }
try {
if (Test-Path $Work) { throw "$Work exists; give an empty, new folder" }
New-Item -ItemType Directory $Work | Out-Null
$pkg = Join-Path $Work 'pkg'; New-Item -ItemType Directory $pkg | Out-Null
Copy-Item $Control (Join-Path $pkg 'ianitor-control.exe')
Copy-Item (Join-Path $repo 'deploy\windows\install.ps1') $pkg
$ctl = Join-Path $Work 'bin\ianitor-control.exe'
$root = Join-Path $Work 'root'
$offline = Join-Path $Work 'offline-root'
$pass = Join-Path $Work 'offline-pass.txt'
[IO.File]::WriteAllText($pass, 'smoke-test-offline-passphrase')
Step 'a throw-away Domi root (offline step)'
& (Join-Path $pkg 'ianitor-control.exe') ca init --out $offline --passphrase-file $pass
Check ($LASTEXITCODE -eq 0) 'ca init'
Step 'install.ps1 -NoService'
& (Join-Path $pkg 'install.ps1') -NoService -AdminHost 'ianitor.smoke.test' -Tls domi `
-DomiRoot (Join-Path $offline 'domi-root.crt') -PreviewDomain 'pv.smoke.test' `
-PreviewUpstreams "127.0.0.1:20000-29999" -Root $root -Bin (Join-Path $Work 'bin') `
-HttpPort $Http -HttpsPort $Https -ApiPort $Api -CaddyAdminPort $Admin -Caddy $Caddy
$config = Join-Path $root 'control.toml'
Check (Test-Path $config) 'control.toml written'
function Start-Control {
$env:IANITOR_LOG_FILE = Join-Path $root 'logs\control.log'
$p = Start-Process -PassThru -WindowStyle Hidden -FilePath $ctl -ArgumentList '--config', "`"$config`""
Remove-Item Env:\IANITOR_LOG_FILE
for ($i = 0; $i -lt 60; $i++) {
try { $c = New-Object Net.Sockets.TcpClient('127.0.0.1', $Api); $c.Close(); return $p } catch { Start-Sleep -Milliseconds 500 }
}
throw 'control did not listen'
}
Step 'Caddy and control, as the services start them'
$env:APPDATA = Join-Path $Work 'caddy-appdata'
$procs += Start-Process -PassThru -WindowStyle Hidden -FilePath (Join-Path $Work 'bin\caddy.exe') `
-ArgumentList 'run', '--config', "`"$(Join-Path $root 'caddy\caddy.json')`"" `
-RedirectStandardError (Join-Path $root 'logs\caddy.log') -RedirectStandardOutput (Join-Path $root 'logs\caddy.out')
$p = Start-Control; $procs += $p
$adminTok = (& $ctl token create --name smoke-admin --config $config) | Select-Object -Last 1
$previewTok = (& $ctl token create --name fabrica-previews --scope previews --config $config) | Select-Object -Last 1
$auth = @{ Authorization = "Bearer $adminTok" }
$base = "http://127.0.0.1:$Api"
Step 'the intermediate, signed offline'
$r = Invoke-RestMethod -Method POST -Uri "$base/api/v1/certs/intermediate/renewal" -Headers $auth
$csr = Join-Path $Work 'intermediate.csr'
Invoke-WebRequest -UseBasicParsing -Uri "$base/api/v1/pki-requests/$($r.request_id)/csr" -Headers $auth -OutFile $csr
$crt = Join-Path $Work 'intermediate.crt'
& $ctl ca sign --root $offline --csr $csr --out $crt --yes --passphrase-file $pass
Check ($LASTEXITCODE -eq 0) 'ca sign'
& $ctl pki install $crt --config $config
Check ($LASTEXITCODE -eq 0) 'pki install'
Step 'control again (it adds the admin host now that it can issue its certificate)'
Stop-Process -Id $p.Id -Force; Start-Sleep 1
$procs = @($procs | Where-Object { $_.Id -ne $p.Id })
$p = Start-Control; $procs += $p
$deadline = (Get-Date).AddSeconds(120)
do {
Start-Sleep 2
$hosts = (Invoke-RestMethod -Uri "$base/api/v1/hosts" -Headers $auth).hosts.hostname
} until (($hosts -contains 'ianitor.smoke.test') -or (Get-Date) -gt $deadline)
Check ($hosts -contains 'ianitor.smoke.test') 'admin host in the routes'
Step 'a branch build on 127.0.0.1'
$site = Join-Path $Work 'app'; New-Item -ItemType Directory $site | Out-Null
[IO.File]::WriteAllText((Join-Path $site 'index.html'), 'hello from fix-42')
$procs += Start-Process -PassThru -WindowStyle Hidden -FilePath 'python' -ArgumentList '-m', 'http.server', $AppPort, '--bind', '127.0.0.1', '--directory', "`"$site`""
Start-Sleep 2
Step 'Fabrica opens the preview (scripts\preview.ps1, previews-only token)'
$env:IANITOR_URL = $base; $env:IANITOR_PREVIEW_TOKEN = $previewTok
$url = & (Join-Path $repo 'scripts\preview.ps1') open -Project 'shop' -Branch 'fix-42' -Upstream "http://127.0.0.1:$AppPort" -Ttl 3600
Check ($url -eq 'https://fix-42--shop.pv.smoke.test/') "preview URL $url"
$ca = Join-Path $offline 'domi-root.crt'
$body = & curl.exe -sS --cacert $ca --resolve "fix-42--shop.pv.smoke.test:${Https}:127.0.0.1" "https://fix-42--shop.pv.smoke.test:$Https/"
Check ($body -eq 'hello from fix-42') "the preview answers through Caddy with a Domi certificate ($body)"
$code = & curl.exe -s -o NUL -w '%{http_code}' --cacert $ca --resolve "ianitor.smoke.test:${Https}:127.0.0.1" "https://ianitor.smoke.test:$Https/api/v1/team"
Check ($code -eq '401') "the admin host reaches control and needs a sign-in ($code)"
$deny = & curl.exe -s -o NUL -w '%{http_code}' -H "Authorization: Bearer $previewTok" "$base/api/v1/hosts"
Check ($deny -eq '403') "the preview token cannot read the hosts ($deny)"
$list = & (Join-Path $repo 'scripts\preview.ps1') list
Check ($list -match 'fix-42--shop') 'listed'
Step 'closed on merge'
& (Join-Path $repo 'scripts\preview.ps1') close -Project 'shop' -Branch 'fix-42' | Out-Null
$deadline = (Get-Date).AddSeconds(90)
do {
Start-Sleep 2
$code = & curl.exe -s -o NUL -w '%{http_code}' --cacert $ca --resolve "fix-42--shop.pv.smoke.test:${Https}:127.0.0.1" "https://fix-42--shop.pv.smoke.test:$Https/"
} until (($code -ne '200') -or (Get-Date) -gt $deadline)
Check ($code -ne '200') "gone after close ($code)"
Write-Host 'SMOKE PASSED'
} finally {
foreach ($p in $procs) { try { Stop-Process -Id $p.Id -Force -ErrorAction SilentlyContinue } catch {} }
Remove-Item Env:\IANITOR_PREVIEW_TOKEN -ErrorAction SilentlyContinue
}