-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.ps1
209 lines (196 loc) · 7.53 KB
/
functions.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
function Get-AADAccessToken () {
# Log in with Azure CLI (if not logged in yet)
Login-Az
Write-Debug "az account get-access-token --resource 499b84ac-1321-427f-aa17-267ca6975798"
az account get-access-token --resource 499b84ac-1321-427f-aa17-267ca6975798 `
--query "accessToken" `
--output tsv `
| Set-Variable aadToken
if (!$aadToken) {
Write-Warning "Could not obtain AAD token"
return $null
}
Write-Debug "AAD Token: $($aadToken -replace '.','*')"
return $aadToken
}
function Get-AccessToken (
[parameter(Mandatory=$false)]
[string]
$Token
) {
if ($Token) {
Write-Verbose "Using Token parameter"
} elseif ($env:AZURE_DEVOPS_EXT_PAT) {
Write-Verbose "Using AZURE_DEVOPS_EXT_PAT environment variable"
$Token = $env:AZURE_DEVOPS_EXT_PAT
} elseif ($env:AZDO_PERSONAL_ACCESS_TOKEN) {
Write-Verbose "Using AZDO_PERSONAL_ACCESS_TOKEN environment variable"
$Token = $env:AZDO_PERSONAL_ACCESS_TOKEN
} elseif (Get-Command az -ErrorAction SilentlyContinue) {
Write-Verbose "Using Azure CLI"
$Token = (Get-AADAccessToken)
} elseif ($env:SYSTEM_ACCESSTOKEN) {
Write-Verbose "Using SYSTEM_ACCESSTOKEN environment variable"
$Token = $env:SYSTEM_ACCESSTOKEN
}
if ($Token) {
Write-Verbose "Access token: $($token -replace '.','*')"
Write-Debug "Access token: ${token}"
return $Token
} else {
Write-Warning "No access token found, exiting"
exit 1
}
}
function Get-AgentPackageUrl (
[parameter(Mandatory=$false)][switch]
$ExcludeEoLNode,
[parameter(Mandatory=$false)][string]
[ValidateSet("Previous", "Current", "Prerelease")]
$VersionPreference="Current",
[parameter(Mandatory=$false)]
[ValidateSet(2, 3, 4)]
[int]
$MajorVersion=4
) {
(Invoke-RestMethod -Uri https://api.github.com/repos/microsoft/azure-pipelines-agent/releases) `
| Where-Object {!$_.draft} `
| Where-Object {$_.name -match "^v${MajorVersion}"} `
| Sort-Object -Property @{Expression = "created_at"; Descending = $true} `
| Set-Variable releases
switch ($VersionPreference) {
"Previous" {
$releases | Where-Object {!$_.prerelease} `
| Select-Object -Skip 1 -First 1 `
| Set-Variable release
break
}
default { # Latest
$releases | Where-Object {!$_.prerelease} `
| Select-Object -Skip 0 -First 1 `
| Set-Variable release
break
}
"Prerelease" {
$releases | Select-Object -Skip 0 -First 1 `
| Set-Variable release
break
}
}
if (!$release) {
Write-Warning "Agent VersionPreference '${VersionPreference}' release v${MajorVersion} not found, exiting"
exit
}
$release | Format-List | Out-String | Write-Debug
$release.name -replace "^v","" | Set-Variable agentVersion
if ($IsWindows) {
$os = "Windows"
$osString = [Environment]::Is64BitProcess ? "win-x64" : "win-x86"
$extension = "zip"
}
if ($IsMacOS) {
$os = "macOS"
$osString = (($PSVersionTable.OS -imatch "ARM64") -and $MajorVersion -ge 3) ? "osx-arm64" : "osx-x64"
$extension = "tar.gz"
}
if ($IsLinux) {
$os = "Linux"
$osString = "linux-"
$arch = $(uname -m)
if ($arch -in @("arm", "arm64")) {
$osString += $arch
} elseif ($arch -eq "x86_64") {
$osString += "x64"
} else {
Write-Warning "Unknown architecture '${arch}', defaulting to x64"
$osString += "x64"
}
$extension = "tar.gz"
}
$packagePrefix = $ExcludeEoLNode ? "pipelines" : "vsts"
"{0}-agent-{1}-{2}.{3}" -f $packagePrefix, $osString, $agentVersion, $extension | Set-Variable agentPackage
"https://download.agent.dev.azure.com/agent/{0}/{1}" -f $agentVersion, $agentPackage | Set-Variable packageUrl
try {
Write-Verbose "Validating whether package exists at '${packageUrl}'..."
Invoke-WebRequest -method HEAD $packageUrl | Set-Variable packageResponse
$packageResponse | Format-List | Out-String | Write-Debug
"Agent package for '{0}' ({1},{2}):`n{3}" -f $PSVersionTable.OS, `
$VersionPreference, `
($ExcludeEoLNode ? "ExcludeEoLNode" : "IncludeNode6"), `
$packageUrl `
| Write-Verbose
return $packageUrl
} catch {
throw "Could not access agent package for ${os} ($($VersionPreference.ToLower())): ${packageUrl}`n$($_.Exception.Message)"
}
}
function Invoke-AgentConfig(
[parameter(Mandatory=$true)][string]$Command
) {
if (!$Command) {
throw "No Command specified"
}
$Command -replace "\s+"," " | Set-Variable Command
"Runnnning: $Command" -replace "--token +(\S+)","--token ***" | Write-Debug
Invoke-Expression -Command $Command
}
function Login-Az (
[parameter(Mandatory=$false)]
[guid]
$TenantId#=($env:ARM_TENANT_ID ?? $env:AZURE_TENANT_ID)
) {
if (!(Get-Command az)) {
Write-Error "Azure CLI is not installed, get it at http://aka.ms/azure-cli"
exit 1
}
# Are we logged in? If so, is it the right tenant?
$azureAccount = $null
az account show 2>$null | ConvertFrom-Json | Set-Variable azureAccount
if ($azureAccount -and `
($TenantId -and ($TenantId -ne [guid]::Empty)) -and `
($azureAccount.tenantId -ine $TenantId)) {
Write-Warning "Logged into tenant $($azureAccount.tenant_id) instead of ${TenantId}"
$azureAccount = $null
}
if (-not $azureAccount) {
if ($env:CODESPACES -ieq "true") {
$azLoginSwitches = "--use-device-code "
}
if ($TenantId -and ($TenantId -ne [guid]::Empty)) {
Write-Debug "az login -t ${TenantId} --allow-no-subscriptions $($azLoginSwitches)"
az login -t $TenantId -o none --allow-no-subscriptions $($azLoginSwitches)
} else {
Write-Debug "az login $($azLoginSwitches)"
az login $($azLoginSwitches) -o none
az account show 2>$null | ConvertFrom-Json | Set-Variable azureAccount
}
}
}
function Remove-Directory (
[parameter(Mandatory=$true)][string]$Path,
[parameter(Mandatory=$false)][int]$Interval=5,
[parameter(Mandatory=$false)][int]$MaxTries=25
) {
$tries = 0
Write-Host "Removing agent directory '${Path}'" -NoNewLine
while ((Test-Path($Path)) -and ($tries -le $MaxTries)) {
Write-Host "." -NoNewLine
if (Get-Command rm -ErrorAction SilentlyContinue) {
rm -r $Path 2>$null
} else {
Remove-Item -LiteralPath $Path -Recurse -Force -ErrorAction SilentlyContinue
# Get-ChildItem -Path $Path -Include * -Recurse | Remove-Item -Force
}
if (Test-Path($Path)) {
Write-Verbose "File locked, trying again in ${Interval} seconds..."
Start-Sleep -seconds $Interval
}
$tries++
}
if (Test-Path($Path)) {
Write-Host "✗"
Write-Warning "Could not remove directory '${Path}' after ${tries} tries"
} else {
Write-Host "✓"
}
}