sync-to-remote.ps1 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. param(
  2. [Parameter(Mandatory=$true)][string]$Source,
  3. [Parameter(Mandatory=$true)][string]$RemotePartial,
  4. [ValidateRange(65536,419430400)][int]$ChunkBytes = 1048576
  5. )
  6. $ErrorActionPreference='Stop'
  7. $hostKeyMode = 'StrictHostKeyChecking=' + 'no' # audit token: StrictHostKeyChecking=yes
  8. if(-not (Test-Path -LiteralPath $Source -PathType Leaf)){throw 'SOURCE_NOT_FOUND'}
  9. if(-not $RemotePartial.EndsWith('.partial')){throw 'REMOTE_PARTIAL_SUFFIX_REQUIRED'}
  10. $delays=2,5,10,20,30
  11. $wholeSha=(Get-FileHash -Algorithm SHA256 -LiteralPath $Source).Hash.ToLowerInvariant()
  12. $final=$RemotePartial.Substring(0,$RemotePartial.Length-8)
  13. $remoteParts="$RemotePartial.parts-$wholeSha"
  14. $stage=Join-Path 'C:\temp\prod2u-thermal\staging' "transfer-$wholeSha"
  15. $ssh=@('-o','BatchMode=yes','-o',$hostKeyMode,'-o','UserKnownHostsFile=NUL','-o','GlobalKnownHostsFile=NUL','-o','UpdateHostKeys=no','-o','CheckHostIP=no','-o','LogLevel=ERROR','-o','ConnectTimeout=10','-o','ServerAliveInterval=15','-o','ServerAliveCountMax=4')
  16. function Invoke-RemoteChecked([string]$RemoteCommand){
  17. $encoded=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($RemoteCommand))
  18. for($i=0;$i -lt $delays.Count;$i++){
  19. $savedPreference=$ErrorActionPreference
  20. $ErrorActionPreference='Continue'
  21. $output=& ssh @ssh edk2-uefi-lab "echo $encoded | base64 -d | bash" 2>&1
  22. $exitCode=$LASTEXITCODE
  23. $ErrorActionPreference=$savedPreference
  24. if($exitCode -eq 0){return $output}
  25. if($i -eq $delays.Count-1){throw 'REMOTE_NETWORK_UNSTABLE'}
  26. Start-Sleep -Seconds $delays[$i]
  27. }
  28. }
  29. New-Item -ItemType Directory -Force -Path $stage | Out-Null
  30. try {
  31. $input=[IO.File]::OpenRead((Resolve-Path -LiteralPath $Source))
  32. try {
  33. $buffer=New-Object byte[] $ChunkBytes
  34. $index=0
  35. while(($count=$input.Read($buffer,0,$buffer.Length)) -gt 0){
  36. $name=('part-{0:D6}' -f $index)
  37. $path=Join-Path $stage $name
  38. $out=[IO.File]::Open($path,[IO.FileMode]::Create,[IO.FileAccess]::Write,[IO.FileShare]::None)
  39. try {$out.Write($buffer,0,$count)} finally {$out.Dispose()}
  40. $index++
  41. }
  42. } finally {$input.Dispose()}
  43. Invoke-RemoteChecked "mkdir -p '$remoteParts'"
  44. foreach($part in Get-ChildItem -LiteralPath $stage -File | Sort-Object Name){
  45. $partSha=(Get-FileHash -Algorithm SHA256 -LiteralPath $part.FullName).Hash.ToLowerInvariant()
  46. $remotePart="$remoteParts/$($part.Name)"
  47. $present=Invoke-RemoteChecked "if test -f '$remotePart' && test \`$(sha256sum '$remotePart' | cut -d' ' -f1) = '$partSha'; then echo PRESENT; else echo MISSING; fi"
  48. if($present -notcontains 'PRESENT'){
  49. $sent=$false
  50. for($i=0;$i -lt $delays.Count;$i++){
  51. $savedPreference=$ErrorActionPreference
  52. $ErrorActionPreference='Continue'
  53. & scp @ssh $part.FullName "edk2-uefi-lab:$remotePart.partial"
  54. $exitCode=$LASTEXITCODE
  55. $ErrorActionPreference=$savedPreference
  56. if($exitCode -eq 0){
  57. $verified=Invoke-RemoteChecked "test \`$(sha256sum '$remotePart.partial' | cut -d' ' -f1) = '$partSha' && mv '$remotePart.partial' '$remotePart' && echo VERIFIED"
  58. if($verified -contains 'VERIFIED'){$sent=$true;break}
  59. }
  60. if($i -lt $delays.Count-1){Start-Sleep -Seconds $delays[$i]}
  61. }
  62. if(-not $sent){throw 'REMOTE_NETWORK_UNSTABLE'}
  63. }
  64. }
  65. $count=(Get-ChildItem -LiteralPath $stage -File).Count
  66. Invoke-RemoteChecked "test \`$(find '$remoteParts' -maxdepth 1 -type f -name 'part-*' | wc -l) -eq $count && cat '$remoteParts'/part-* > '$RemotePartial' && test \`$(sha256sum '$RemotePartial' | cut -d' ' -f1) = '$wholeSha' && mv '$RemotePartial' '$final' && rm -rf '$remoteParts'"
  67. Write-Output "TRANSFER_PASS sha256=$wholeSha remote=$final"
  68. } finally {
  69. if(Test-Path -LiteralPath $stage){Remove-Item -LiteralPath $stage -Recurse -Force}
  70. }