From 36d254fe1e2c2d0a274854ce627add1f622459b7 Mon Sep 17 00:00:00 2001 From: Bryan McLellan Date: Sun, 5 Apr 2020 20:23:31 -0400 Subject: Download and verify the Ruby installer, then move into place This should reduce or remove race conditions while downloading the same file on multiple container instances. Signed-off-by: Bryan McLellan --- scripts/bk_tests/bk_win_functional.ps1 | 47 ++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/scripts/bk_tests/bk_win_functional.ps1 b/scripts/bk_tests/bk_win_functional.ps1 index 5eb17769d4..9a9d50a63b 100644 --- a/scripts/bk_tests/bk_win_functional.ps1 +++ b/scripts/bk_tests/bk_win_functional.ps1 @@ -12,16 +12,39 @@ $RubyS3Path = "s3://public-cd-buildkite-cache/$RubyFilename" Function DownloadRuby { + $RandDigits = Get-Random echo "Downloading Ruby + DevKit" - aws s3 cp $RubyS3Path $RubyPath | Out-Null # Out-Null is a hack to wait for the process to complete - if ($LASTEXITCODE -ne 0) - { + aws s3 cp "$RubyS3Path" "$RubyPath.$RandDigits" | Out-Null # Out-Null is a hack to wait for the process to complete + + if ($LASTEXITCODE -ne 0) { echo "aws s3 download failed: $LASTEXITCODE" exit $LASTEXITCODE } - $DownloadedHash = (Get-FileHash $RubyPath -Algorithm SHA256).Hash - echo "Downloaded SHA256: $DownloadedHash" + + $FileHash = (Get-FileHash "$RubyPath.$RandDigits" -Algorithm SHA256).Hash + If ($FileHash -eq $RubySHA256) { + echo "Downloaded SHA256 matches: $FileHash" + } Else { + echo "Downloaded file hash $FileHash does not match desired $RubySHA256" + exit 1 + } + + # On a shared filesystem, sometimes a good file appears while we are downloading + If (Test-Path $RubyPath) { + $FileHash = (Get-FileHash "$RubyPath" -Algorithm SHA256).Hash + If ($FileHash -eq $RubySHA256) { + echo "A matching file appeared while downloading, using it." + Remove-Item "$RubyPath.$RandDigits" -Force + Return + } Else { + echo "Existing file does not match, bad hash: $FileHash" + Remove-Item $RubyPath -Force + } + } + + echo "Moving file installer into place" + Rename-Item -Path "$RubyPath.$RandDigits" -NewName $RubyFilename } Function InstallRuby @@ -29,16 +52,12 @@ Function InstallRuby If (Test-Path $RubyPath) { echo "$RubyPath already exists" - $ExistingRubyHash = (Get-FileHash $RubyPath -Algorithm SHA256).Hash - - echo "Verifying file SHA256 hash $ExistingRubyHash to desired hash $RubySHA256" - - If ($ExistingRubyHash -ne $RubySHA256) { - echo "SHA256 hash mismatch, attempting to remove and re-download" - Remove-Item $RubyPath -Force - DownloadRuby - } Else { + $FileHash = (Get-FileHash "$RubyPath" -Algorithm SHA256).Hash + If ($FileHash -eq $RubySHA256) { echo "Found matching Ruby + DevKit on disk" + } Else { + echo "SHA256 hash mismatch, re-downloading" + DownloadRuby } } Else { echo "No Ruby found at $RubyPath, downloading" -- cgit v1.2.1