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.
 
 
 
 
 
 

56 lines
3.1 KiB

$ErrorActionPreference = 'Stop'
$project = Split-Path -Parent $PSScriptRoot
Start-Transcript -Path (Join-Path $project 'runtime/config-read-transfer.log') -Force | Out-Null
$assets = @{'/api'=(Join-Path $project 'build/linux-arm64/tera-clone-demo-api'); '/web'=(Join-Path $project 'build/linux-arm64/tera-clone-demo-web')}
$hashes = @{}
foreach ($key in $assets.Keys) {$hashes[$key]=(Get-FileHash $assets[$key] -Algorithm SHA256).Hash.ToLowerInvariant()}
$script=[IO.File]::ReadAllText((Join-Path $PSScriptRoot 'deploy-config-read.sh')).Replace('__APIHASH__',$hashes['/api']).Replace('__WEBHASH__',$hashes['/web']).Replace("`r`n","`n")
$rule = 'TERA-replacement-default-port-' + [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 'COM4',115200,'None',8,'One'
$serial.Encoding = [Text.Encoding]::UTF8
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-replacement-default-port.sh http://192.168.0.100:9001/deploy.sh && sh /tmp/deploy-replacement-default-port.sh')
$deadline = [DateTime]::UtcNow.AddSeconds(360)
$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
$route = $context.Request.Url.AbsolutePath
if ($route -eq '/deploy.sh') { $bytes = [Text.Encoding]::UTF8.GetBytes($script) }
elseif ($assets.ContainsKey($route)) { $bytes = [IO.File]::ReadAllBytes($assets[$route]) }
else { $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('REPLACEMENT_DEFAULT_PORT_ROLLED_BACK')) { throw 'Readiness failed; old pair restored.' }
if ($output.Contains('REPLACEMENT_DEFAULT_PORT_DEPLOY_OK')) { break }
Start-Sleep -Milliseconds 100
}
if (-not $output.Contains('REPLACEMENT_DEFAULT_PORT_DEPLOY_OK')) { throw 'Deployment did not report success; inspect device console.' }
foreach ($hash in $hashes.Values) { if (-not $output.Contains($hash)) { throw 'A deployed hash was not verified.' } }
Write-Output 'VERIFIED replacement device deployment on HTTP port 80'
} finally {
if ($serial.IsOpen) { $serial.Close() }
$serial.Dispose()
$listener.Close()
Remove-NetFirewallRule -DisplayName $rule -ErrorAction SilentlyContinue
Stop-Transcript | Out-Null
}