sync-from-remote.ps1 1.2 KB

12345678910111213141516171819202122232425262728
  1. param(
  2. [Parameter(Mandatory=$true)][string]$RemoteSource,
  3. [Parameter(Mandatory=$true)][string]$Destination,
  4. [Parameter(Mandatory=$true)][string]$ExpectedSha256,
  5. [string]$KnownHosts = 'C:\temp\prod2u-thermal\receipts\known_hosts'
  6. )
  7. $ErrorActionPreference='Stop'
  8. if(-not (Test-Path -LiteralPath $KnownHosts -PathType Leaf)){throw 'REMOTE_HOST_KEY_CONFIRMATION_REQUIRED'}
  9. $partial="$Destination.partial"
  10. $delays=2,5,10,20,30
  11. $ssh=@('-o','BatchMode=yes','-o','StrictHostKeyChecking=yes','-o',"UserKnownHostsFile=$KnownHosts",'-o','ConnectTimeout=10','-o','ServerAliveInterval=15','-o','ServerAliveCountMax=4')
  12. for($i=0;$i -lt $delays.Count;$i++){
  13. $savedPreference=$ErrorActionPreference
  14. $ErrorActionPreference='Continue'
  15. & scp @ssh "edk2-uefi-lab:$RemoteSource" $partial
  16. $exitCode=$LASTEXITCODE
  17. $ErrorActionPreference=$savedPreference
  18. if($exitCode -eq 0){
  19. $sha=(Get-FileHash -Algorithm SHA256 -LiteralPath $partial).Hash.ToLowerInvariant()
  20. if($sha -eq $ExpectedSha256.ToLowerInvariant()){
  21. Move-Item -LiteralPath $partial -Destination $Destination -Force
  22. Write-Output "TRANSFER_PASS sha256=$sha local=$Destination"
  23. exit 0
  24. }
  25. }
  26. if($i -lt $delays.Count-1){Start-Sleep -Seconds $delays[$i]}
  27. }
  28. throw 'REMOTE_NETWORK_UNSTABLE'