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 |
|
| 5 | $myPath = Split-Path $MyInvocation.MyCommand.Path -Parent
|
| 6 |
|
| 7 | function GetEnvVar([string] $key, [scriptblock] $defaultFn) {
|
| 8 | if (Test-Path "Env:\$key") {
|
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 9 | return (Get-ChildItem "Env:\$key").Value
|
Robert Iannucci | 2188fe9 | 2016-12-02 11:15:57 -0800 | [diff] [blame] | 10 | }
|
| 11 | return $defaultFn.Invoke()
|
| 12 | }
|
| 13 |
|
| 14 | $cipdClientVer = GetEnvVar "CIPD_CLIENT_VER" {
|
Robert Iannucci | be3daff | 2016-12-14 18:42:52 -0800 | [diff] [blame] | 15 | Get-Content (Join-Path $myPath -ChildPath 'cipd_client_version') -TotalCount 1
|
Robert Iannucci | 2188fe9 | 2016-12-02 11:15:57 -0800 | [diff] [blame] | 16 | }
|
| 17 | $cipdClientSrv = GetEnvVar "CIPD_CLIENT_SRV" {
|
| 18 | "https://chrome-infra-packages.appspot.com"
|
| 19 | }
|
| 20 |
|
| 21 | $plat="windows"
|
| 22 | if ([environment]::Is64BitOperatingSystem) {
|
| 23 | $arch="amd64"
|
| 24 | } else {
|
| 25 | $arch="386"
|
| 26 | }
|
| 27 |
|
| 28 | $url = "$cipdClientSrv/client?platform=$plat-$arch&version=$cipdClientVer"
|
| 29 | $client = Join-Path $myPath -ChildPath ".cipd_client.exe"
|
| 30 |
|
Robert Iannucci | 78628da | 2017-04-24 18:21:39 -0700 | [diff] [blame] | 31 | try {
|
| 32 | $depot_tools_version = &git -C $myPath rev-parse HEAD 2>&1
|
| 33 | if ($LastExitCode -eq 0) {
|
| 34 | $user_agent = "depot_tools/$depot_tools_version"
|
| 35 | } else {
|
| 36 | $user_agent = "depot_tools/???"
|
| 37 | }
|
| 38 | } catch [System.Management.Automation.CommandNotFoundException] {
|
| 39 | $user_agent = "depot_tools/no_git/???"
|
Robert Iannucci | 2188fe9 | 2016-12-02 11:15:57 -0800 | [diff] [blame] | 40 | }
|
| 41 |
|
| 42 | $Env:CIPD_HTTP_USER_AGENT_PREFIX = $user_agent
|
Dan Jacques | 7c2e05b | 2017-07-06 10:03:30 -0700 | [diff] [blame] | 43 |
|
| 44 | # Use a lock fle to prevent simultaneous processes from stepping on each other.
|
| 45 | $cipd_lock = Join-Path $myPath -ChildPath '.cipd_client.lock'
|
| 46 | while ($true) {
|
| 47 | $cipd_lock_file = $false
|
| 48 | try {
|
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 49 | $cipd_lock_file = [System.IO.File]::OpenWrite($cipd_lock)
|
Dan Jacques | 7c2e05b | 2017-07-06 10:03:30 -0700 | [diff] [blame] | 50 |
|
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 51 | echo "Bootstrapping cipd client for $plat-$arch from $url..."
|
| 52 | $wc = (New-Object System.Net.WebClient)
|
| 53 | $wc.Headers.add('User-Agent', $user_agent)
|
| 54 | $wc.DownloadFile($url, $client)
|
Dan Jacques | 7c2e05b | 2017-07-06 10:03:30 -0700 | [diff] [blame] | 55 | break
|
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 56 |
|
| 57 | } catch [System.IO.IOException] {
|
| 58 | echo "CIPD bootstrap lock is held, trying again after delay..."
|
Dan Jacques | 7c2e05b | 2017-07-06 10:03:30 -0700 | [diff] [blame] | 59 | Start-Sleep -s 1
|
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 60 | } catch {
|
| 61 | $err = $_.Exception.Message
|
| 62 | echo "CIPD bootstrap failed: $err"
|
| 63 | throw $err
|
Dan Jacques | 7c2e05b | 2017-07-06 10:03:30 -0700 | [diff] [blame] | 64 | } finally {
|
| 65 | if ($cipd_lock_file) {
|
Vadim Shtayura | 7e50ee3 | 2018-07-26 17:43:51 +0000 | [diff] [blame] | 66 | $cipd_lock_file.Close()
|
| 67 | $cipd_lock_file = $false
|
| 68 | try {
|
| 69 | [System.IO.File]::Delete($cipd_lock)
|
| 70 | } catch {}
|
Dan Jacques | 7c2e05b | 2017-07-06 10:03:30 -0700 | [diff] [blame] | 71 | }
|
| 72 | }
|
Dan Jacques | eb1feb9 | 2017-07-28 13:04:28 +0200 | [diff] [blame] | 73 | }
|