blob: 511ed84d144e56127b62e1834e703e3a841aaa74 [file] [log] [blame]
Robert Iannucci2188fe92016-12-02 11:15:57 -08001# 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 Shtayuradfedcc02018-09-14 19:47:37 +00005# Note: to run this on e.g. OSX for adhoc testing or debugging in case Windows
6# is not around:
7#
Robert Iannucci71ab1b72023-05-04 21:06:10 +00008# 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 Shtayuradfedcc02018-09-14 19:47:37 +000013# file _cipd.exe
14
Vadim Shtayura4f3b3222023-01-12 00:11:03 +000015param(
16 # Path to download the CIPD binary to.
17 [Parameter(Mandatory = $true)]
18 [string]
19 $CipdBinary,
Robert Iannucci71ab1b72023-05-04 21:06:10 +000020
Vadim Shtayura4f3b3222023-01-12 00:11:03 +000021 # 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 Shtayura95fb6dc2018-08-16 19:01:27 +000034)
Robert Iannucci2188fe92016-12-02 11:15:57 -080035
Tushar Singh02838e62023-05-23 19:46:55 +000036# 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.
39Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash
40
Vadim Shtayura95fb6dc2018-08-16 19:01:27 +000041$DepotToolsPath = Split-Path $MyInvocation.MyCommand.Path -Parent
Robert Iannucci2188fe92016-12-02 11:15:57 -080042
Vadim Shtayura95fb6dc2018-08-16 19:01:27 +000043# Put depot_tool's git revision into the user agent string.
Robert Iannucci78628da2017-04-24 18:21:39 -070044try {
Vadim Shtayura95fb6dc2018-08-16 19:01:27 +000045 $DepotToolsVersion = &git -C $DepotToolsPath rev-parse HEAD 2>&1
Robert Iannucci78628da2017-04-24 18:21:39 -070046 if ($LastExitCode -eq 0) {
Vadim Shtayura95fb6dc2018-08-16 19:01:27 +000047 $UserAgent = "depot_tools/$DepotToolsVersion"
Robert Iannucci78628da2017-04-24 18:21:39 -070048 } else {
Vadim Shtayura95fb6dc2018-08-16 19:01:27 +000049 $UserAgent = "depot_tools/???"
Robert Iannucci78628da2017-04-24 18:21:39 -070050 }
51} catch [System.Management.Automation.CommandNotFoundException] {
Vadim Shtayura95fb6dc2018-08-16 19:01:27 +000052 $UserAgent = "depot_tools/no_git/???"
53}
54$Env:CIPD_HTTP_USER_AGENT_PREFIX = $UserAgent
55
Vadim Shtayura95fb6dc2018-08-16 19:01:27 +000056# Returns the expected SHA256 hex digest for the given platform reading it from
57# *.digests file.
58function 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 Kotsenas22775d82020-11-05 20:44:46 +000070# Retry a command with a delay between each.
71function 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 Iannucci71ab1b72023-05-04 21:06:10 +000088 Invoke-Command -ScriptBlock $Command
Matt Kotsenas22775d82020-11-05 20:44:46 +000089 return
90 }
91 catch {
92 $attempt += 1
93 $exception = $_.Exception.InnerException
94 if ($attempt -lt $MaxAttempts) {
Robert Iannucci71ab1b72023-05-04 21:06:10 +000095 Write-Output "FAILURE: " + $_
96 Write-Output "Retrying after a short nap..."
Matt Kotsenas22775d82020-11-05 20:44:46 +000097 Start-Sleep -Seconds $Delay.TotalSeconds
98 } else {
99 throw $exception
100 }
101 }
102 }
103}
104
Vadim Shtayura95fb6dc2018-08-16 19:01:27 +0000105$ExpectedSHA256 = Get-Expected-SHA256 $Platform
106$Version = (Get-Content $VersionFile).Trim()
107$URL = "$BackendURL/client?platform=$Platform&version=$Version"
108
Vadim Shtayuradfedcc02018-09-14 19:47:37 +0000109# Fetch the binary now that the lock is ours.
Robert Iannucci71ab1b72023-05-04 21:06:10 +0000110$TmpPath = $CipdBinary + ".tmp." + $PID
Vadim Shtayuradfedcc02018-09-14 19:47:37 +0000111try {
Robert Iannucci71ab1b72023-05-04 21:06:10 +0000112 Write-Output "Downloading CIPD client for $Platform from $URL..."
113 Retry-Command {
Aleksey Khoroshilov42515352023-05-15 17:37:34 +0000114 $ProgressPreference = "SilentlyContinue"
Robert Iannucci71ab1b72023-05-04 21:06:10 +0000115 Invoke-WebRequest -UserAgent $UserAgent -Uri $URL -OutFile $TmpPath
Vadim Shtayuradfedcc02018-09-14 19:47:37 +0000116 }
117
Robert Iannucci71ab1b72023-05-04 21:06:10 +0000118 $ActualSHA256 = (Get-FileHash -Path $TmpPath -Algorithm "SHA256").Hash.toLower()
Vadim Shtayuradfedcc02018-09-14 19:47:37 +0000119 if ($ActualSHA256 -ne $ExpectedSHA256) {
120 throw "Invalid SHA256 digest: $ActualSHA256 != $ExpectedSHA256"
121 }
122
123 Move-Item -LiteralPath $TmpPath -Destination $CipdBinary -Force
Robert Iannucci71ab1b72023-05-04 21:06:10 +0000124} catch {
125 Remove-Item -Path $TmpPath -ErrorAction Ignore
126 throw # Re raise any error
Vadim Shtayuradfedcc02018-09-14 19:47:37 +0000127}