sync-to-remote.ps1 3.7 KB

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