You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

119 lines
5.4 KiB

$ErrorActionPreference = 'Stop'
$project = Split-Path -Parent $PSScriptRoot
Start-Transcript -Path (Join-Path $project 'runtime/login-ready-transfer.log') -Force | Out-Null
$binary = Join-Path $project 'build/linux-arm64/tera-clone-demo-api-login-ready'
$webBinary = Join-Path $project 'build/linux-arm64/tera-clone-demo-web-login-ready'
$webHash = (Get-FileHash -LiteralPath $webBinary -Algorithm SHA256).Hash.ToLowerInvariant()
$hash = (Get-FileHash -LiteralPath $binary -Algorithm SHA256).Hash.ToLowerInvariant()
$script = @'
#!/bin/sh
set -eu
stage=/tmp/tera-login-ready-20260909
mkdir -p "$stage"
wget -q -T 20 -O "$stage/api" http://192.168.0.100:9001/api
wget -q -T 20 -O "$stage/web" http://192.168.0.100:9001/web
echo '__HASH__ '"$stage/api" | sha256sum -c -
echo '__WEBHASH__ '"$stage/web" | sha256sum -c -
backup=/userdata/tera-backups/login-ready-$(date +%Y%m%d-%H%M%S)
mkdir -p "$backup"
cp -p /usr/bin/tera-clone-demo-api "$backup/api"
cp -p /usr/bin/tera-clone-demo-web "$backup/web"
echo "BACKUP=$backup"
cp "$stage/api" /usr/bin/tera-clone-demo-api.next
cp "$stage/web" /usr/bin/tera-clone-demo-web.next
chmod 0755 /usr/bin/tera-clone-demo-api.next /usr/bin/tera-clone-demo-web.next
rollback() {
trap - EXIT
/etc/init.d/S61tera-clone-demo-web stop || true
/etc/init.d/S51tera-clone-demo-api stop || true
sleep 2
cp "$backup/api" /usr/bin/tera-clone-demo-api.restore
cp "$backup/web" /usr/bin/tera-clone-demo-web.restore
chmod 0755 /usr/bin/tera-clone-demo-api.restore /usr/bin/tera-clone-demo-web.restore
mv /usr/bin/tera-clone-demo-api.restore /usr/bin/tera-clone-demo-api
mv /usr/bin/tera-clone-demo-web.restore /usr/bin/tera-clone-demo-web
/etc/init.d/S51tera-clone-demo-api start
/etc/init.d/S61tera-clone-demo-web start
echo LOGIN_READY_ROLLED_BACK
}
trap rollback EXIT
/etc/init.d/S61tera-clone-demo-web stop
/etc/init.d/S51tera-clone-demo-api stop
sleep 2
mv /usr/bin/tera-clone-demo-api.next /usr/bin/tera-clone-demo-api
mv /usr/bin/tera-clone-demo-web.next /usr/bin/tera-clone-demo-web
(
set -a
. /etc/tera/tera-clone-demo-api.env
set +a
start-stop-daemon -S -q -b -m -p /run/tera-clone-demo-api.pid -c tera:tera -x /bin/sh -- -c 'exec /usr/bin/tera-clone-demo-api > /run/tera-clone-demo-api/login-ready-startup.log 2>&1'
)
/etc/init.d/S61tera-clone-demo-web start
ready=0
for n in $(seq 1 45); do
if wget -q -T 2 -O "$stage/status" http://127.0.0.1:8083/login/backend-status; then
if grep -q '"ready":true' "$stage/status"; then ready=1; break; fi
if grep -q '"state":"failed"' "$stage/status"; then cat "$stage/status"; grep -E "startup|reconciliation" /run/tera-clone-demo-api/login-ready-startup.log; exit 1; fi
fi
sleep 1
done
cat "$stage/status"
[ "$ready" = 1 ]
wget -q -T 3 -O "$stage/login.js" http://127.0.0.1:8083/static/js/login.js
grep -q 'function showStartup' "$stage/login.js"
sha256sum /usr/bin/tera-clone-demo-api /usr/bin/tera-clone-demo-web
sync
trap - EXIT
echo LOGIN_READY_DEPLOY_OK
'@
$script = $script.Replace('__HASH__', $hash).Replace('__WEBHASH__', $webHash).Replace("`r`n", "`n") + "`n"
$rule = 'TERA-login-ready-' + [guid]::NewGuid().ToString('N')
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add('http://192.168.0.100:9001/')
$serial = New-Object System.IO.Ports.SerialPort 'COM5',115200,'None',8,'One'
try {
New-NetFirewallRule -DisplayName $rule -Direction Inbound -Action Allow -Protocol TCP -LocalPort 9001 -RemoteAddress 192.168.0.223 | Out-Null
$listener.Start()
$serial.Open()
$serial.WriteLine('wget -q -O /tmp/deploy-login-ready.sh http://192.168.0.100:9001/deploy.sh && sh /tmp/deploy-login-ready.sh')
$deadline = [DateTime]::UtcNow.AddSeconds(180)
$output = ''
$pending = $listener.GetContextAsync()
while ([DateTime]::UtcNow -lt $deadline) {
if ($pending.IsCompleted) {
$context = $pending.GetAwaiter().GetResult()
if ($context.Request.RemoteEndPoint.Address.ToString() -ne '192.168.0.223') {
$context.Response.StatusCode = 403
$context.Response.Close()
} else {
$bytes = $null
switch ($context.Request.Url.AbsolutePath) {
'/deploy.sh' { $bytes = [Text.Encoding]::UTF8.GetBytes($script) }
'/web' { $bytes = [IO.File]::ReadAllBytes($webBinary) }
'/api' { $bytes = [IO.File]::ReadAllBytes($binary) }
default { $context.Response.StatusCode = 404 }
}
if ($null -ne $bytes) {
$context.Response.ContentLength64 = $bytes.Length
$context.Response.OutputStream.Write($bytes, 0, $bytes.Length)
}
$context.Response.Close()
}
$pending = $listener.GetContextAsync()
}
$chunk = $serial.ReadExisting()
if ($chunk) { $output += $chunk; Write-Host -NoNewline $chunk }
if ($output.Contains('LOGIN_READY_ROLLED_BACK')) { throw 'Readiness failed; old pair restored.' }
if ($output.Contains('LOGIN_READY_DEPLOY_OK')) { break }
Start-Sleep -Milliseconds 100
}
if (-not $output.Contains('LOGIN_READY_DEPLOY_OK')) { throw 'Deployment did not report success; inspect device console.' }
if (-not $output.Contains($hash) -or -not $output.Contains($webHash)) { throw 'Deployed hash missing from console output.' }
Write-Output "VERIFIED deployed SHA256=$hash and API login-policy endpoint"
} finally {
if ($serial.IsOpen) { $serial.Close() }
$serial.Dispose()
$listener.Close()
Remove-NetFirewallRule -DisplayName $rule -ErrorAction SilentlyContinue
Stop-Transcript | Out-Null
}