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