cipd.ps1 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 cipd.ps1 \
  8. # -CipdBinary _cipd.exe \
  9. # -BackendURL https://chrome-infra-packages.appspot.com \
  10. # -VersionFile ./cipd_client_version
  11. # file _cipd.exe
  12. Param(
  13. # Path to download the CIPD binary to.
  14. [parameter(Mandatory=$true)][string]$CipdBinary,
  15. # E.g. "https://chrome-infra-packages.appspot.com".
  16. [parameter(Mandatory=$true)][string]$BackendURL,
  17. # Path to the cipd_client_version file with the client version.
  18. [parameter(Mandatory=$true)][string]$VersionFile
  19. )
  20. $DepotToolsPath = Split-Path $MyInvocation.MyCommand.Path -Parent
  21. if ([System.IntPtr]::Size -eq 8) {
  22. $Platform = "windows-amd64"
  23. } else {
  24. $Platform = "windows-386"
  25. }
  26. # Put depot_tool's git revision into the user agent string.
  27. try {
  28. $DepotToolsVersion = &git -C $DepotToolsPath rev-parse HEAD 2>&1
  29. if ($LastExitCode -eq 0) {
  30. $UserAgent = "depot_tools/$DepotToolsVersion"
  31. } else {
  32. $UserAgent = "depot_tools/???"
  33. }
  34. } catch [System.Management.Automation.CommandNotFoundException] {
  35. $UserAgent = "depot_tools/no_git/???"
  36. }
  37. $Env:CIPD_HTTP_USER_AGENT_PREFIX = $UserAgent
  38. # Tries to delete the file, ignoring errors. Used for best-effort cleanups.
  39. function Delete-If-Possible($path) {
  40. try {
  41. [System.IO.File]::Delete($path)
  42. } catch {
  43. $err = $_.Exception.Message
  44. echo "Warning: error when deleting $path - $err. Ignoring."
  45. }
  46. }
  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. # Returns SHA256 hex digest of a binary file at the given path.
  61. function Get-Actual-SHA256($path) {
  62. # Note: we don't use Get-FileHash to be compatible with PowerShell v3.0
  63. $file = [System.IO.File]::Open($path, [System.IO.FileMode]::Open)
  64. try {
  65. $algo = New-Object System.Security.Cryptography.SHA256Managed
  66. $hash = $algo.ComputeHash($file)
  67. } finally {
  68. $file.Close()
  69. }
  70. $hex = ""
  71. foreach ($byte in $hash) {
  72. $hex += $byte.ToString("x2")
  73. }
  74. return $hex
  75. }
  76. $ExpectedSHA256 = Get-Expected-SHA256 $Platform
  77. $Version = (Get-Content $VersionFile).Trim()
  78. $URL = "$BackendURL/client?platform=$Platform&version=$Version"
  79. # Grab a lock to prevent simultaneous processes from stepping on each other.
  80. # This depends on "exclusive write" file sharing mode used by OpenWrite.
  81. $CipdLockPath = Join-Path $DepotToolsPath -ChildPath ".cipd_client.lock"
  82. $CipdLockFile = $null
  83. while ($CipdLockFile -eq $null) {
  84. try {
  85. $CipdLockFile = [System.IO.File]::OpenWrite($CipdLockPath)
  86. } catch [System.IO.IOException] {
  87. echo "CIPD bootstrap lock is held, trying again after delay..."
  88. Start-Sleep -s 1
  89. }
  90. }
  91. # Fetch the binary now that the lock is ours.
  92. $TmpPath = $CipdBinary + ".tmp"
  93. try {
  94. echo "Downloading CIPD client for $Platform from $URL..."
  95. $wc = (New-Object System.Net.WebClient)
  96. $wc.Headers.Add("User-Agent", $UserAgent)
  97. try {
  98. $wc.DownloadFile($URL, $TmpPath)
  99. } catch {
  100. throw "Failed to download the file, check your network connection"
  101. }
  102. $ActualSHA256 = Get-Actual-SHA256 $TmpPath
  103. if ($ActualSHA256 -ne $ExpectedSHA256) {
  104. throw "Invalid SHA256 digest: $ActualSHA256 != $ExpectedSHA256"
  105. }
  106. Move-Item -LiteralPath $TmpPath -Destination $CipdBinary -Force
  107. } finally {
  108. $CipdLockFile.Close()
  109. Delete-If-Possible $CipdLockPath
  110. Delete-If-Possible $TmpPath
  111. }