-
Notifications
You must be signed in to change notification settings - Fork 237
/
Download-Artifacts.ps1
289 lines (271 loc) · 13.6 KB
/
Download-Artifacts.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<#
.Synopsis
Download Artifacts
.Description
Download artifacts from artifacts storage
.Parameter artifactUrl
Url for artifact to use.
.Parameter includePlatform
Add this switch to include the platform artifact in the download
.Parameter force
Add this switch to force download artifacts even though they already exists
.Parameter forceRedirection
Add this switch to force download redirection artifacts even though they already exists
.Parameter basePath
Load the artifacts into a file structure below this path. (default is c:\bcartifacts.cache)
.Parameter timeout
Timeout in seconds for each file download.
.Example
$artifactPaths = Download-Artifacts -artifactUrl $artifactUrl -includePlatform
$appArtifactPath = $artifactPaths[0]
$platformArtifactPath = $artifactPaths[1]
.Example
$appArtifactPath = Download-Artifacts -artifactUrl $artifactUrl
#>
function Download-Artifacts {
Param (
[Parameter(Mandatory=$true)]
[string] $artifactUrl,
[switch] $includePlatform,
[switch] $force,
[switch] $forceRedirection,
[string] $basePath = "",
[int] $timeout = $bccontainerHelperConfig.artifactDownloadTimeout
)
function DownloadPackage {
Param(
[string] $artifactUrl,
[string] $destinationPath,
[int] $timeout = 300
)
$tmpFolder = Join-Path ([System.IO.Path]::GetDirectoryName($destinationPath)) ([System.IO.Path]::GetRandomFileName())
$zipFile = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString()).zip"
$retry = $false
do {
Download-File -sourceUrl $artifactUrl -destinationFile $zipFile -timeout $timeout
Write-Host "Unpacking artifact to tmp folder " -NoNewline
try {
Expand-7zipArchive -Path $zipFile -DestinationPath $tmpFolder -use7zipIfAvailable:(!$retry)
$retry = $false
}
catch {
Remove-Item -path $zipFile -force
if (Test-Path $tmpFolder) {
Remove-Item $tmpFolder -Recurse -Force
}
if ($retry) {
throw "Error trying to unpack artifact, downloaded package is corrupt"
}
else {
if ($artifactUrl -match '^https:\/\/(.+)\.azureedge\.net\/(.*)$') {
Write-Host "Error unpacking platform artifact downloaded from CDN, retrying download from direct download URL"
$artifactUrl = "https://$($Matches[1]).blob.core.windows.net/$($Matches[2])"
$retry = $true
}
}
}
} while ($retry)
$result = $false
try {
$attempts = 0
while (!(Test-Path $destinationPath)) {
try {
Rename-Item -Path $tmpFolder -NewName ([System.IO.Path]::GetFileName($destinationPath)) -Force
$result = $true
}
catch {
if ($attempts++ -eq 5) {
throw
}
$waittime = 5*$attempts
Write-Host "Could not rename '$tmpFolder' retrying in $waittime seconds."
Start-Sleep -Seconds $waittime
Write-Host "Retrying..."
}
}
}
finally {
if (Test-Path $zipFile) {
Remove-Item -path $zipFile -force
}
if (Test-Path $tmpFolder) {
Remove-Item $tmpFolder -Recurse -Force
}
}
return $result
}
$telemetryScope = InitTelemetryScope -name $MyInvocation.InvocationName -parameterValues $PSBoundParameters -includeParameters @("artifactUrl","includePlatform")
try {
if ($basePath -eq "") {
$basePath = $bcContainerHelperConfig.bcartifactsCacheFolder
}
if (-not (Test-Path $basePath)) {
New-Item $basePath -ItemType Directory | Out-Null
}
$appMutexName = "dl-$($artifactUrl.Split('?')[0].Substring(8).Replace('/','_'))"
$appMutex = New-Object System.Threading.Mutex($false, $appMutexName)
try {
try {
if (!$appMutex.WaitOne(1000)) {
Write-Host "Waiting for other process downloading artifact '$($artifactUrl.Split('?')[0])'"
$appMutex.WaitOne() | Out-Null
Write-Host "Other process completed download"
}
}
catch [System.Threading.AbandonedMutexException] {
Write-Host "Other process terminated abnormally"
}
do {
$redir = $false
$appUri = [Uri]::new($artifactUrl)
$appArtifactPath = Join-Path $basePath $appUri.AbsolutePath
$exists = Test-Path $appArtifactPath
if ($exists -and $force) {
Remove-Item $appArtifactPath -Recurse -Force
$exists = $false
}
if ($exists -and $forceRedirection) {
$appManifestPath = Join-Path $appArtifactPath "manifest.json"
$appManifest = Get-Content $appManifestPath | ConvertFrom-Json
if ($appManifest.PSObject.Properties.name -eq "applicationUrl") {
# redirect artifacts are always downloaded
Remove-Item $appArtifactPath -Recurse -Force
$exists = $false
}
}
if (-not $exists) {
Write-Host "Downloading artifact $($appUri.AbsolutePath)"
DownloadPackage -artifactUrl $artifactUrl -destinationPath $appArtifactPath -timeout $timeout | Out-Null
}
try { [System.IO.File]::WriteAllText((Join-Path $appArtifactPath 'lastused'), "$([datetime]::UtcNow.Ticks)") } catch {}
$appManifestPath = Join-Path $appArtifactPath "manifest.json"
if (Test-Path $appManifestPath) {
$appManifest = Get-Content $appManifestPath | ConvertFrom-Json
# Patch wrong license file in ONPREM AU version 20.5.45456.45889
if ($artifactUrl -like '*/onprem/20.5.45456.45889/au') {
Write-Host "INFO: Patching wrong license file in ONPREM AU version 20.5.45456.45889"
Download-File -sourceUrl 'https://bcartifacts.blob.core.windows.net/prerequisites/21demolicense/au/3048953.flf' -destinationFile (Join-Path $appArtifactPath 'database/Cronus.flf')
}
$cuFixMapping = @{
'11.0.48794.0' = 'cu53';
'11.0.48962.0' = 'cu54';
'11.0.49061.0' = 'cu55';
'11.0.49175.0' = 'cu56';
'11.0.49240.0' = 'cu57';
'11.0.49345.0' = 'cu58';
'11.0.49497.0' = 'cu59';
'11.0.49618.0' = 'cu60';
}
if ($appManifest.version -in $cuFixMapping.Keys) {
$appManifest.cu = $cuFixMapping[$appManifest.version]
$appManifest | ConvertTo-Json | Set-Content -Path $appManifestPath
}
if ($appManifest.PSObject.Properties.name -eq "applicationUrl") {
$redir = $true
$artifactUrl = $appManifest.ApplicationUrl
if ($artifactUrl -notlike 'https://*') {
$artifactUrl = "https://$($appUri.Host)/$artifactUrl$($appUri.Query)"
}
}
}
} while ($redir)
$appArtifactPath
if ($includePlatform) {
if ($appManifest.PSObject.Properties.name -eq "platformUrl") {
$platformUrl = $appManifest.platformUrl
}
else {
$platformUrl = "$($appUri.AbsolutePath.Substring(0,$appUri.AbsolutePath.LastIndexOf('/')))/platform".TrimStart('/')
}
if ($platformUrl -notlike 'https://*') {
$platformUrl = "https://$($appUri.Host.TrimEnd('/'))/$platformUrl$($appUri.Query)"
}
$platformUri = [Uri]::new($platformUrl)
$PlatformMutexName = "dl-$($platformUrl.Split('?')[0].Substring(8).Replace('/','_'))"
$PlatformMutex = New-Object System.Threading.Mutex($false, $PlatformMutexName)
try {
try {
if (!$PlatformMutex.WaitOne(1000)) {
Write-Host "Waiting for other process downloading platform artifact '$($platformUrl.Split('?')[0])'"
$PlatformMutex.WaitOne() | Out-Null
Write-Host "Other process completed download"
}
}
catch [System.Threading.AbandonedMutexException] {
Write-Host "Other process terminated abnormally"
}
$platformArtifactPath = Join-Path $basePath $platformUri.AbsolutePath
$exists = Test-Path $platformArtifactPath
if ($exists -and $force) {
Remove-Item $platformArtifactPath -Recurse -Force
$exists = $false
}
if (-not $exists) {
Write-Host "Downloading platform artifact $($platformUri.AbsolutePath)"
$downloadprereqs = DownLoadPackage -ArtifactUrl $platformUrl -DestinationPath $platformArtifactPath -timeout $timeout
if ($downloadprereqs) {
$prerequisiteComponentsFile = Join-Path $platformArtifactPath "Prerequisite Components.json"
if (Test-Path $prerequisiteComponentsFile) {
$prerequisiteComponents = Get-Content $prerequisiteComponentsFile | ConvertFrom-Json
Write-Host "Downloading Prerequisite Components"
$prerequisiteComponents.PSObject.Properties | % {
$skip = ($_.Name -eq "Prerequisite Components\Open XML SDK 2.5 for Microsoft Office\OpenXMLSDKv25.msi") -and (([System.Version]$appManifest.Version).Major -ge 21)
$path = Join-Path $platformArtifactPath $_.Name
if ((-not $skip) -and (-not (Test-Path $path))) {
$dirName = [System.IO.Path]::GetDirectoryName($path)
if (-not (Test-Path $dirName)) {
New-Item -Path $dirName -ItemType Directory | Out-Null
}
$url = $_.Value
Download-File -sourceUrl $url -destinationFile $path -timeout $timeout
}
}
$dotnetCoreFolder = Join-Path $platformArtifactPath "Prerequisite Components\DotNetCore"
if (!(Test-Path $dotnetCoreFolder)) {
New-Item $dotnetCoreFolder -ItemType Directory | Out-Null
Download-File -sourceUrl "https://go.microsoft.com/fwlink/?LinkID=844461" -destinationFile (Join-Path $dotnetCoreFolder "DotNetCore.1.0.4_1.1.1-WindowsHosting.exe") -timeout $timeout
}
}
# Patch potential wrong version of NewtonSoft.json.DLL
$newtonSoftDllPath = Join-Path $platformArtifactPath 'ServiceTier\program files\Microsoft Dynamics NAV\210\Service\Newtonsoft.json.dll'
if (Test-Path $newtonSoftDllPath) {
'Applications\testframework\TestRunner\Internal\Newtonsoft.json.dll','Test Assemblies\Newtonsoft.json.dll' | ForEach-Object {
$dstFile = Join-Path $platformArtifactPath $_
$file = Get-item -Path $dstFile -ErrorAction SilentlyContinue
if ($file -and $file.Length -eq 686000) {
Write-Host "INFO: Patching wrong version of NewtonSoft.json.DLL in $dstFile"
Copy-Item -Path $newtonSoftDllPath -Destination $dstFile -Force
}
}
}
$ad1DLL = Join-Path $platformArtifactPath 'Applications\testframework\TestRunner\Internal\Microsoft.IdentityModel.Clients.ActiveDirectory.dll'
$ad2DLL = Join-Path $platformArtifactPath 'ServiceTier\program files\Microsoft Dynamics NAV\*\Service\Management\Microsoft.IdentityModel.Clients.ActiveDirectory.dll'
if ((Test-Path $ad1DLL) -and (Test-Path $ad2DLL)) {
if ((Get-Item $ad1DLL).Length -ne (Get-Item $ad2DLL).Length) {
Write-Host "INFO: Patching wrong version of Microsoft.IdentityModel.Clients.ActiveDirectory.dll in $ad1DLL"
}
Copy-Item -Path $ad2DLL -Destination $ad1DLL -Force
}
}
}
try { [System.IO.File]::WriteAllText((Join-Path $platformArtifactPath 'lastused'), "$([datetime]::UtcNow.Ticks)") } catch {}
$platformArtifactPath
}
finally {
$platformMutex.ReleaseMutex()
}
}
}
finally {
$appMutex.ReleaseMutex()
}
}
catch {
TrackException -telemetryScope $telemetryScope -errorRecord $_
throw
}
finally {
TrackTrace -telemetryScope $telemetryScope
}
}
Export-ModuleMember -Function Download-Artifacts