$ErrorActionPreference = 'Stop' $project = Split-Path -Parent $PSScriptRoot Start-Transcript -Path (Join-Path $project 'runtime/startup-api-transfer.log') -Force | Out-Null $binary = Join-Path $project 'build/linux-arm64/tera-clone-demo-api-startup' $hash = (Get-FileHash -LiteralPath $binary -Algorithm SHA256).Hash.ToLowerInvariant() $script = @' #!/bin/sh set -eu stage=/tmp/tera-startup-api-20260909 mkdir -p "$stage" wget -q -O "$stage/api" http://192.168.0.100:9001/api echo '__HASH__ '"$stage/api" | sha256sum -c - backup=/userdata/tera-backups/startup-api-$(date +%Y%m%d-%H%M%S) mkdir -p "$backup" cp -p /usr/bin/tera-clone-demo-api "$backup/tera-clone-demo-api" echo "BACKUP=$backup" cp "$stage/api" /usr/bin/tera-clone-demo-api.next chmod 0755 /usr/bin/tera-clone-demo-api.next rollback() { trap - EXIT /etc/init.d/S51tera-clone-demo-api stop || true cp "$backup/tera-clone-demo-api" /usr/bin/tera-clone-demo-api.restore chmod 0755 /usr/bin/tera-clone-demo-api.restore mv /usr/bin/tera-clone-demo-api.restore /usr/bin/tera-clone-demo-api /etc/init.d/S51tera-clone-demo-api start echo STARTUP_API_ROLLED_BACK } trap rollback EXIT /etc/init.d/S51tera-clone-demo-api stop sleep 2 mv /usr/bin/tera-clone-demo-api.next /usr/bin/tera-clone-demo-api /etc/init.d/S51tera-clone-demo-api start ready=0 for n in $(seq 1 30); do if wget -q -T 2 -O "$stage/status" http://127.0.0.1:8084/api/v1/auth/login-policy; then ready=1; break; fi sleep 1 done [ "$ready" = 1 ] sleep 3 /etc/init.d/S51tera-clone-demo-api status wget -q -T 3 -O "$stage/status" http://127.0.0.1:8084/api/v1/auth/login-policy cat "$stage/status" sha256sum /usr/bin/tera-clone-demo-api sync trap - EXIT echo STARTUP_API_DEPLOY_OK '@ $script = $script.Replace('__HASH__', $hash).Replace("`r`n", "`n") + "`n" $rule = 'TERA-startup-api-' + [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-startup-api.sh http://192.168.0.100:9001/deploy.sh && sh /tmp/deploy-startup-api.sh') $deadline = [DateTime]::UtcNow.AddSeconds(90) $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) } '/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('STARTUP_API_DEPLOY_OK')) { break } Start-Sleep -Milliseconds 100 } if (-not $output.Contains('STARTUP_API_DEPLOY_OK')) { throw 'Deployment did not report success; inspect device console.' } if (-not $output.Contains($hash)) { 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 }