From dfedcc0666fe3a7e655642ec3385ecb3fb6286c0 Mon Sep 17 00:00:00 2001 From: Vadim Shtayura Date: Fri, 14 Sep 2018 19:47:37 +0000 Subject: [PATCH] [cipd] More careful error handling during CIPD bootstrap on Windows. Errors during the download should be reported as such, not as "failed to grab the lock file". R=nodir@chromium.org, iannucci@chromium.org BUG=853722 Change-Id: If24f4587693852eed0608450bcd311cf3bafd2c3 Reviewed-on: https://chromium-review.googlesource.com/1226106 Reviewed-by: Nodir Turakulov Commit-Queue: Vadim Shtayura --- cipd.ps1 | 61 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/cipd.ps1 b/cipd.ps1 index 1154e4ef8..fa6e50679 100644 --- a/cipd.ps1 +++ b/cipd.ps1 @@ -2,6 +2,15 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +# Note: to run this on e.g. OSX for adhoc testing or debugging in case Windows +# is not around: +# +# pwsh cipd.ps1 \ +# -CipdBinary _cipd.exe \ +# -BackendURL https://chrome-infra-packages.appspot.com \ +# -VersionFile ./cipd_client_version +# file _cipd.exe + Param( # Path to download the CIPD binary to. [parameter(Mandatory=$true)][string]$CipdBinary, @@ -82,37 +91,39 @@ $Version = (Get-Content $VersionFile).Trim() $URL = "$BackendURL/client?platform=$Platform&version=$Version" -# Use a lock file to prevent simultaneous processes from stepping on each other. +# Grab a lock to prevent simultaneous processes from stepping on each other. +# This depends on "exclusive write" file sharing mode used by OpenWrite. $CipdLockPath = Join-Path $DepotToolsPath -ChildPath ".cipd_client.lock" -$TmpPath = $CipdBinary + ".tmp" -while ($true) { - $CipdLockFile = $null +$CipdLockFile = $null +while ($CipdLockFile -eq $null) { try { $CipdLockFile = [System.IO.File]::OpenWrite($CipdLockPath) - - echo "Downloading CIPD client for $Platform from $URL..." - $wc = (New-Object System.Net.WebClient) - $wc.Headers.Add("User-Agent", $UserAgent) - $wc.DownloadFile($URL, $TmpPath) - - $ActualSHA256 = Get-Actual-SHA256 $TmpPath - if ($ActualSHA256 -ne $ExpectedSHA256) { - throw "Invalid SHA256 digest: $ActualSHA256 != $ExpectedSHA256" - } - - Move-Item -LiteralPath $TmpPath -Destination $CipdBinary -Force - break - } catch [System.IO.IOException] { echo "CIPD bootstrap lock is held, trying again after delay..." Start-Sleep -s 1 + } +} + +# Fetch the binary now that the lock is ours. +$TmpPath = $CipdBinary + ".tmp" +try { + echo "Downloading CIPD client for $Platform from $URL..." + $wc = (New-Object System.Net.WebClient) + $wc.Headers.Add("User-Agent", $UserAgent) + try { + $wc.DownloadFile($URL, $TmpPath) } catch { - throw # for some reason this is needed to exit while(...) loop on errors - } finally { - Delete-If-Possible $TmpPath - if ($CipdLockFile) { - $CipdLockFile.Close() - Delete-If-Possible $CipdLockPath - } + throw "Failed to download the file, check your network connection" } + + $ActualSHA256 = Get-Actual-SHA256 $TmpPath + if ($ActualSHA256 -ne $ExpectedSHA256) { + throw "Invalid SHA256 digest: $ActualSHA256 != $ExpectedSHA256" + } + + Move-Item -LiteralPath $TmpPath -Destination $CipdBinary -Force +} finally { + $CipdLockFile.Close() + Delete-If-Possible $CipdLockPath + Delete-If-Possible $TmpPath }