blob: 78efcd635b35ca35763bef9428a3173ce90476c2 [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
5$myPath = Split-Path $MyInvocation.MyCommand.Path -Parent
6
7function GetEnvVar([string] $key, [scriptblock] $defaultFn) {
8 if (Test-Path "Env:\$key") {
9 return Get-ChildItem $Env $key
10 }
11 return $defaultFn.Invoke()
12}
13
14$cipdClientVer = GetEnvVar "CIPD_CLIENT_VER" {
Robert Iannuccibe3daff2016-12-14 18:42:52 -080015 Get-Content (Join-Path $myPath -ChildPath 'cipd_client_version') -TotalCount 1
Robert Iannucci2188fe92016-12-02 11:15:57 -080016}
17$cipdClientSrv = GetEnvVar "CIPD_CLIENT_SRV" {
18 "https://chrome-infra-packages.appspot.com"
19}
20
21$plat="windows"
22if ([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 Iannucci78628da2017-04-24 18:21:39 -070031try {
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 Iannucci2188fe92016-12-02 11:15:57 -080040}
41
42$Env:CIPD_HTTP_USER_AGENT_PREFIX = $user_agent
Dan Jacques7c2e05b2017-07-06 10:03:30 -070043
44# Use a lock fle to prevent simultaneous processes from stepping on each other.
45$cipd_lock = Join-Path $myPath -ChildPath '.cipd_client.lock'
46while ($true) {
47 $cipd_lock_file = $false
48 try {
49 $cipd_lock_file = [IO.File]::OpenWrite($cipd_lock)
50
51 if (!(Test-Path $client)) {
Daniel Jacques9f9d82a2017-07-27 14:21:21 +000052 echo "Bootstrapping cipd client for $plat-$arch..."
53 echo "From $url"
54
Dan Jacques7c2e05b2017-07-06 10:03:30 -070055 # TODO(iannucci): It would be really nice if there was a way to get this to
56 # show progress without also completely destroying the download speed, but
57 # I can't seem to find a way to do it. Patches welcome :)
58 $wc = (New-Object System.Net.WebClient)
59 $wc.Headers.add('User-Agent', $user_agent)
60 $wc.DownloadFile($url, $client)
61 }
62 break
63 } catch {
Daniel Jacques9f9d82a2017-07-27 14:21:21 +000064 echo "CIPD lock is held, trying again after delay..."
Dan Jacques7c2e05b2017-07-06 10:03:30 -070065 Start-Sleep -s 1
66 } finally {
67 if ($cipd_lock_file) {
68 $cipd_lock_file.close()
69 }
70 }
Daniel Jacques9f9d82a2017-07-27 14:21:21 +000071}