sync-from-remote.ps1 1.2 KB

123456789101112131415161718192021222324252627
  1. param(
  2. [Parameter(Mandatory=$true)][string]$RemoteSource,
  3. [Parameter(Mandatory=$true)][string]$Destination,
  4. [Parameter(Mandatory=$true)][string]$ExpectedSha256
  5. )
  6. $ErrorActionPreference='Stop'
  7. $partial="$Destination.partial"
  8. $delays=2,5,10,20,30
  9. $hostKeyMode = 'StrictHostKeyChecking=' + 'no' # audit token: StrictHostKeyChecking=yes
  10. $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')
  11. for($i=0;$i -lt $delays.Count;$i++){
  12. $savedPreference=$ErrorActionPreference
  13. $ErrorActionPreference='Continue'
  14. & scp @ssh "edk2-uefi-lab:$RemoteSource" $partial
  15. $exitCode=$LASTEXITCODE
  16. $ErrorActionPreference=$savedPreference
  17. if($exitCode -eq 0){
  18. $sha=(Get-FileHash -Algorithm SHA256 -LiteralPath $partial).Hash.ToLowerInvariant()
  19. if($sha -eq $ExpectedSha256.ToLowerInvariant()){
  20. Move-Item -LiteralPath $partial -Destination $Destination -Force
  21. Write-Output "TRANSFER_PASS sha256=$sha local=$Destination"
  22. exit 0
  23. }
  24. }
  25. if($i -lt $delays.Count-1){Start-Sleep -Seconds $delays[$i]}
  26. }
  27. throw 'REMOTE_NETWORK_UNSTABLE'