.cipd_impl.ps1 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Copyright (c) 2016 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. # Note: to run this on e.g. OSX for adhoc testing or debugging in case Windows
  5. # is not around:
  6. #
  7. # pwsh
  8. # PS ...> $ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage"
  9. # PS ...> ./.cipd_impl.ps1 -CipdBinary _cipd.exe `
  10. # -BackendURL https://chrome-infra-packages.appspot.com `
  11. # -VersionFile ./cipd_client_version
  12. # file _cipd.exe
  13. param(
  14. # Path to download the CIPD binary to.
  15. [Parameter(Mandatory = $true)]
  16. [string]
  17. $CipdBinary,
  18. # CIPD platform to download the client for.
  19. [string]
  20. $Platform = "windows-amd64",
  21. # E.g. "https://chrome-infra-packages.appspot.com".
  22. [Parameter(Mandatory = $true)]
  23. [string]
  24. $BackendURL,
  25. # Path to the cipd_client_version file with the client version.
  26. [Parameter(Mandatory = $true)]
  27. [string]
  28. $VersionFile
  29. )
  30. # Import PowerShell<=5 Get-Filehash from Microsoft.PowerShell.Utility.
  31. # This prevents loading of incompatible Get-FileHash from PowerShell 6+ $PSModulePath.
  32. # See: crbug.com/1443163.
  33. Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash
  34. $DepotToolsPath = Split-Path $MyInvocation.MyCommand.Path -Parent
  35. # Put depot_tool's git revision into the user agent string.
  36. try {
  37. $DepotToolsVersion = &git -C $DepotToolsPath rev-parse HEAD 2>&1
  38. if ($LastExitCode -eq 0) {
  39. $UserAgent = "depot_tools/$DepotToolsVersion"
  40. } else {
  41. $UserAgent = "depot_tools/???"
  42. }
  43. } catch [System.Management.Automation.CommandNotFoundException] {
  44. $UserAgent = "depot_tools/no_git/???"
  45. }
  46. $Env:CIPD_HTTP_USER_AGENT_PREFIX = $UserAgent
  47. # Returns the expected SHA256 hex digest for the given platform reading it from
  48. # *.digests file.
  49. function Get-Expected-SHA256($platform) {
  50. $digestsFile = $VersionFile+".digests"
  51. foreach ($line in Get-Content $digestsFile) {
  52. if ($line -match "^([0-9a-z\-]+)\s+sha256\s+([0-9a-f]+)$") {
  53. if ($Matches[1] -eq $platform) {
  54. return $Matches[2]
  55. }
  56. }
  57. }
  58. throw "No SHA256 digests for $platform in $digestsFile"
  59. }
  60. # Retry a command with a delay between each.
  61. function Retry-Command {
  62. [CmdletBinding()]
  63. param (
  64. [Parameter(Mandatory = $true)]
  65. [scriptblock]
  66. $Command,
  67. [int]
  68. $MaxAttempts = 3,
  69. [timespan]
  70. $Delay = (New-TimeSpan -Seconds 5)
  71. )
  72. $attempt = 0
  73. while ($attempt -lt $MaxAttempts) {
  74. try {
  75. Invoke-Command -ScriptBlock $Command
  76. return
  77. }
  78. catch {
  79. $attempt += 1
  80. $exception = $_.Exception.InnerException
  81. if ($attempt -lt $MaxAttempts) {
  82. Write-Output "FAILURE: " + $_
  83. Write-Output "Retrying after a short nap..."
  84. Start-Sleep -Seconds $Delay.TotalSeconds
  85. } else {
  86. throw $exception
  87. }
  88. }
  89. }
  90. }
  91. # Check is url in the NO_PROXY of Environment?
  92. function Is-UrlInNoProxy {
  93. [CmdletBinding()]
  94. param (
  95. [Parameter(Mandatory)]
  96. [string]$Url
  97. )
  98. $NoProxy = $Env:NO_PROXY
  99. if ([string]::IsNullOrEmpty($NoProxy)) {
  100. return $false
  101. }
  102. $NoProxyList = $NoProxy -split ',' | ForEach-Object { $_.Trim().ToLower() }
  103. $UriHost = ([uri]$Url).Host.ToLower()
  104. foreach ($entry in $NoProxyList) {
  105. if ($entry.StartsWith('.')) {
  106. if ($UriHost.EndsWith($entry.Substring(1))) {
  107. return $true
  108. }
  109. } elseif ($entry -eq $UriHost) {
  110. return $true
  111. }
  112. }
  113. return $false
  114. }
  115. $ExpectedSHA256 = Get-Expected-SHA256 $Platform
  116. $Version = (Get-Content $VersionFile).Trim()
  117. $URL = "$BackendURL/client?platform=$Platform&version=$Version"
  118. # Fetch the binary now that the lock is ours.
  119. $TmpPath = $CipdBinary + ".tmp." + $PID
  120. try {
  121. Write-Output "Downloading CIPD client for $Platform from $URL..."
  122. $Parameters = @{
  123. UserAgent = $UserAgent
  124. Uri = $URL
  125. OutFile = $TmpPath
  126. }
  127. if ($Env:HTTPS_PROXY) {
  128. $Proxy = $Env:HTTPS_PROXY
  129. } elseif ($Env:HTTP_PROXY) {
  130. $Proxy = $Env:HTTP_PROXY
  131. } elseif ($Env:ALL_PROXY) {
  132. $Proxy = $Env:ALL_PROXY
  133. } else {
  134. $Proxy = $null
  135. }
  136. $UrlNotInNoProxy = -not (Is-UrlInNoProxy -Url $URL)
  137. if ($UrlNotInNoProxy -and $Proxy) {
  138. Write-Output "Using Proxy $Proxy..."
  139. $Parameters.Proxy = $Proxy
  140. }
  141. Retry-Command {
  142. $ProgressPreference = "SilentlyContinue"
  143. Invoke-WebRequest @Parameters
  144. }
  145. $ActualSHA256 = (Get-FileHash -Path $TmpPath -Algorithm "SHA256").Hash.toLower()
  146. if ($ActualSHA256 -ne $ExpectedSHA256) {
  147. throw "Invalid SHA256 digest: $ActualSHA256 != $ExpectedSHA256"
  148. }
  149. Move-Item -LiteralPath $TmpPath -Destination $CipdBinary -Force
  150. } catch {
  151. Remove-Item -Path $TmpPath -ErrorAction Ignore
  152. throw # Re raise any error
  153. }