Robert Iannucci | 2188fe9 | 2016-12-02 11:15:57 -0800 | [diff] [blame] | 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 |
|
Vadim Shtayura | dfedcc0 | 2018-09-14 19:47:37 +0000 | [diff] [blame] | 5 | # Note: to run this on e.g. OSX for adhoc testing or debugging in case Windows
|
| 6 | # is not around:
|
| 7 | #
|
Robert Iannucci | 71ab1b7 | 2023-05-04 21:06:10 +0000 | [diff] [blame] | 8 | # pwsh
|
| 9 | # PS ...> $ExecutionContext.SessionState.LanguageMode = "ConstrainedLanguage"
|
| 10 | # PS ...> ./.cipd_impl.ps1 -CipdBinary _cipd.exe `
|
| 11 | # -BackendURL https://chrome-infra-packages.appspot.com `
|
| 12 | # -VersionFile ./cipd_client_version
|
Vadim Shtayura | dfedcc0 | 2018-09-14 19:47:37 +0000 | [diff] [blame] | 13 | # file _cipd.exe
|
| 14 |
|
Vadim Shtayura | 4f3b322 | 2023-01-12 00:11:03 +0000 | [diff] [blame] | 15 | param(
|
| 16 | # Path to download the CIPD binary to.
|
| 17 | [Parameter(Mandatory = $true)]
|
| 18 | [string]
|
| 19 | $CipdBinary,
|
Robert Iannucci | 71ab1b7 | 2023-05-04 21:06:10 +0000 | [diff] [blame] | 20 |
|
Vadim Shtayura | 4f3b322 | 2023-01-12 00:11:03 +0000 | [diff] [blame] | 21 | # CIPD platform to download the client for.
|
| 22 | [string]
|
| 23 | $Platform = "windows-amd64",
|
| 24 |
|
| 25 | # E.g. "https://chrome-infra-packages.appspot.com".
|
| 26 | [Parameter(Mandatory = $true)]
|
| 27 | [string]
|
| 28 | $BackendURL,
|
| 29 |
|
| 30 | # Path to the cipd_client_version file with the client version.
|
| 31 | [Parameter(Mandatory = $true)]
|
| 32 | [string]
|
| 33 | $VersionFile
|
Vadim Shtayura | 95fb6dc | 2018-08-16 19:01:27 +0000 | [diff] [blame] | 34 | )
|
Robert Iannucci | 2188fe9 | 2016-12-02 11:15:57 -0800 | [diff] [blame] | 35 |
|
Tushar Singh | 02838e6 | 2023-05-23 19:46:55 +0000 | [diff] [blame] | 36 | # Import PowerShell<=5 Get-Filehash from Microsoft.PowerShell.Utility.
|
| 37 | # This prevents loading of incompatible Get-FileHash from PowerShell 6+ $PSModulePath.
|
| 38 | # See: crbug.com/1443163.
|
| 39 | Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash
|
| 40 |
|
Vadim Shtayura | 95fb6dc | 2018-08-16 19:01:27 +0000 | [diff] [blame] | 41 | $DepotToolsPath = Split-Path $MyInvocation.MyCommand.Path -Parent
|
Robert Iannucci | 2188fe9 | 2016-12-02 11:15:57 -0800 | [diff] [blame] | 42 |
|
Vadim Shtayura | 95fb6dc | 2018-08-16 19:01:27 +0000 | [diff] [blame] | 43 | # Put depot_tool's git revision into the user agent string.
|
Robert Iannucci | 78628da | 2017-04-24 18:21:39 -0700 | [diff] [blame] | 44 | try {
|
Vadim Shtayura | 95fb6dc | 2018-08-16 19:01:27 +0000 | [diff] [blame] | 45 | $DepotToolsVersion = &git -C $DepotToolsPath rev-parse HEAD 2>&1
|
Robert Iannucci | 78628da | 2017-04-24 18:21:39 -0700 | [diff] [blame] | 46 | if ($LastExitCode -eq 0) {
|
Vadim Shtayura | 95fb6dc | 2018-08-16 19:01:27 +0000 | [diff] [blame] | 47 | $UserAgent = "depot_tools/$DepotToolsVersion"
|
Robert Iannucci | 78628da | 2017-04-24 18:21:39 -0700 | [diff] [blame] | 48 | } else {
|
Vadim Shtayura | 95fb6dc | 2018-08-16 19:01:27 +0000 | [diff] [blame] | 49 | $UserAgent = "depot_tools/???"
|
Robert Iannucci | 78628da | 2017-04-24 18:21:39 -0700 | [diff] [blame] | 50 | }
|
| 51 | } catch [System.Management.Automation.CommandNotFoundException] {
|
Vadim Shtayura | 95fb6dc | 2018-08-16 19:01:27 +0000 | [diff] [blame] | 52 | $UserAgent = "depot_tools/no_git/???"
|
| 53 | }
|
| 54 | $Env:CIPD_HTTP_USER_AGENT_PREFIX = $UserAgent
|
| 55 |
|
Vadim Shtayura | 95fb6dc | 2018-08-16 19:01:27 +0000 | [diff] [blame] | 56 | # Returns the expected SHA256 hex digest for the given platform reading it from
|
| 57 | # *.digests file.
|
| 58 | function Get-Expected-SHA256($platform) {
|
| 59 | $digestsFile = $VersionFile+".digests"
|
| 60 | foreach ($line in Get-Content $digestsFile) {
|
| 61 | if ($line -match "^([0-9a-z\-]+)\s+sha256\s+([0-9a-f]+)$") {
|
| 62 | if ($Matches[1] -eq $platform) {
|
| 63 | return $Matches[2]
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 | throw "No SHA256 digests for $platform in $digestsFile"
|
| 68 | }
|
| 69 |
|
Matt Kotsenas | 22775d8 | 2020-11-05 20:44:46 +0000 | [diff] [blame] | 70 | # Retry a command with a delay between each.
|
| 71 | function Retry-Command {
|
| 72 | [CmdletBinding()]
|
| 73 | param (
|
| 74 | [Parameter(Mandatory = $true)]
|
| 75 | [scriptblock]
|
| 76 | $Command,
|
| 77 |
|
| 78 | [int]
|
| 79 | $MaxAttempts = 3,
|
| 80 |
|
| 81 | [timespan]
|
| 82 | $Delay = (New-TimeSpan -Seconds 5)
|
| 83 | )
|
| 84 |
|
| 85 | $attempt = 0
|
| 86 | while ($attempt -lt $MaxAttempts) {
|
| 87 | try {
|
Robert Iannucci | 71ab1b7 | 2023-05-04 21:06:10 +0000 | [diff] [blame] | 88 | Invoke-Command -ScriptBlock $Command
|
Matt Kotsenas | 22775d8 | 2020-11-05 20:44:46 +0000 | [diff] [blame] | 89 | return
|
| 90 | }
|
| 91 | catch {
|
| 92 | $attempt += 1
|
| 93 | $exception = $_.Exception.InnerException
|
| 94 | if ($attempt -lt $MaxAttempts) {
|
Robert Iannucci | 71ab1b7 | 2023-05-04 21:06:10 +0000 | [diff] [blame] | 95 | Write-Output "FAILURE: " + $_
|
| 96 | Write-Output "Retrying after a short nap..."
|
Matt Kotsenas | 22775d8 | 2020-11-05 20:44:46 +0000 | [diff] [blame] | 97 | Start-Sleep -Seconds $Delay.TotalSeconds
|
| 98 | } else {
|
| 99 | throw $exception
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|
Vadim Shtayura | 95fb6dc | 2018-08-16 19:01:27 +0000 | [diff] [blame] | 105 | $ExpectedSHA256 = Get-Expected-SHA256 $Platform
|
| 106 | $Version = (Get-Content $VersionFile).Trim()
|
| 107 | $URL = "$BackendURL/client?platform=$Platform&version=$Version"
|
| 108 |
|
Vadim Shtayura | dfedcc0 | 2018-09-14 19:47:37 +0000 | [diff] [blame] | 109 | # Fetch the binary now that the lock is ours.
|
Robert Iannucci | 71ab1b7 | 2023-05-04 21:06:10 +0000 | [diff] [blame] | 110 | $TmpPath = $CipdBinary + ".tmp." + $PID
|
Vadim Shtayura | dfedcc0 | 2018-09-14 19:47:37 +0000 | [diff] [blame] | 111 | try {
|
Robert Iannucci | 71ab1b7 | 2023-05-04 21:06:10 +0000 | [diff] [blame] | 112 | Write-Output "Downloading CIPD client for $Platform from $URL..."
|
| 113 | Retry-Command {
|
Aleksey Khoroshilov | 4251535 | 2023-05-15 17:37:34 +0000 | [diff] [blame] | 114 | $ProgressPreference = "SilentlyContinue"
|
Robert Iannucci | 71ab1b7 | 2023-05-04 21:06:10 +0000 | [diff] [blame] | 115 | Invoke-WebRequest -UserAgent $UserAgent -Uri $URL -OutFile $TmpPath
|
Vadim Shtayura | dfedcc0 | 2018-09-14 19:47:37 +0000 | [diff] [blame] | 116 | }
|
| 117 |
|
Robert Iannucci | 71ab1b7 | 2023-05-04 21:06:10 +0000 | [diff] [blame] | 118 | $ActualSHA256 = (Get-FileHash -Path $TmpPath -Algorithm "SHA256").Hash.toLower()
|
Vadim Shtayura | dfedcc0 | 2018-09-14 19:47:37 +0000 | [diff] [blame] | 119 | if ($ActualSHA256 -ne $ExpectedSHA256) {
|
| 120 | throw "Invalid SHA256 digest: $ActualSHA256 != $ExpectedSHA256"
|
| 121 | }
|
| 122 |
|
| 123 | Move-Item -LiteralPath $TmpPath -Destination $CipdBinary -Force
|
Robert Iannucci | 71ab1b7 | 2023-05-04 21:06:10 +0000 | [diff] [blame] | 124 | } catch {
|
| 125 | Remove-Item -Path $TmpPath -ErrorAction Ignore
|
| 126 | throw # Re raise any error
|
Vadim Shtayura | dfedcc0 | 2018-09-14 19:47:37 +0000 | [diff] [blame] | 127 | }
|