-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadtest.ps1
560 lines (461 loc) · 17 KB
/
loadtest.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
Set-StrictMode -v latest
$ErrorActionPreference = "Stop"
[string] $logfolder = (pwd).Path
function Main($mainargs)
{
[Diagnostics.Stopwatch] $watch = [Diagnostics.Stopwatch]::StartNew()
if (!$mainargs -or $mainargs.Count -ne 3)
{
Log ("Usage: pwsh loadtest.ps1 <subscription> <resourcegroup> <storageaccount>") Red
exit 1
}
[string] $subscriptionName = $mainargs[0]
[string] $resourceGroupName = $mainargs[1]
[string] $storageAccountName = $mainargs[2]
[string] $location = "West Europe"
[string] $templateFile = "template.json"
[string] $parametersFile = "parameters.json"
if (!(Test-Path $templateFile))
{
Log ("Template file missing: '" + $templateFile + "'") Red
exit 1
}
if (!(Test-Path $parametersFile))
{
Log ("Parameters file missing: '" + $parametersFile + "'") Red
exit 1
}
[string] $payloadFolder = "payload"
if (Test-Path $payloadFolder)
{
Log ("Deleting payload folder: '" + $payloadFolder + "'")
rd -Recurse -Force $payloadFolder
}
[string] $payloadFile = "payload.7z"
if (Test-Path $payloadFile)
{
Log ("Deleting payload file: '" + $payloadFile + "'")
del $payloadFile
}
Load-Dependencies
[string] $artilleryYml = Get-ArtilleryYml
Log ("Retrieving public ip address.")
[string] $ipaddress = (Invoke-RestMethod -Uri "https://api.ipify.org?format=json").ip
Log ("Got public ip address: " + $ipaddress)
[string] $username = "loadadmin"
Log ("Generating vm password.")
[string] $password = Generate-AlphanumericPassword 24
Prepare-Beat $payloadFolder "metricbeat" $env:MetricbeatYml
Prepare-Beat $payloadFolder "filebeat" $env:FilebeatYml
Log ("Generating payload password.")
[string] $zipPassword = Generate-AlphanumericPassword 24
Prepare-Payload $payloadFolder $payloadFile $zipPassword $artilleryYml
'Stop-AzVM -ResourceGroupName "' + $resourceGroupName + '" -Name "vm-loadazure" -Force' | Set-Content "ShutdownVM.ps1"
Login $subscriptionName
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
if (!$resourceGroup)
{
Log ("Creating resource group: '" + $resourceGroupName + "' at '" + $location + "'")
New-AzResourceGroup -Name $resourceGroupName -Location $location
}
else
{
Log ("Using existing resource group: '" + $resourceGroupName + "'")
}
[DateTime] $now = Get-Date
$sasurls = Upload-Payload $payloadFile $resourceGroupName $location $storageAccountName $now
Update-ParametersFile $parametersFile $username $password $ipaddress $zipPassword $sasurls
Log ("Deploying: '" + $resourceGroupName + "' '" + $templateFile + "' '" + $parametersFile + "'")
try
{
Log-TCTime "LoadTestCreateResourceGroup" { New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $templateFile -TemplateParameterFile $parametersFile }
}
catch
{
Log ("Couldn't deploy resources: " + $_.Exception.ToString()) Yellow
}
try
{
Download-Result $resourceGroupName $storageAccountName $sasurls["blobUrl"] $sasurls["containerName"] "result.7z" $zipPassword
}
catch
{
Log ("Couldn't download result: " + $_.Exception.ToString()) Yellow
}
if (!$env:DontDelete)
{
Log ("Deleting resource group: '" + $resourceGroupName + "'")
try
{
Log-TCTime "LoadTestRemoveResourceGroup" { Remove-AzResourceGroup $resourceGroupName -Force }
}
catch
{
Log ("Couldn't delete resource group: " + $_.Exception.ToString()) Yellow
}
}
Log ("Done: " + $watch.Elapsed)
}
function Load-Dependencies()
{
[string] $nugetpkg = "https://www.nuget.org/api/v2/package/Newtonsoft.Json/12.0.2"
[string] $tmpfolder = [IO.Path]::GetTempPath()
[string] $zipfile = Join-Path $tmpfolder "json.zip"
[string] $dllfolder = Join-Path $tmpfolder "jsondll"
[string] $dllfile = Join-Path $tmpfolder "jsondll" "lib" "netstandard2.0" "Newtonsoft.Json.dll"
if (Test-Path $dllfile)
{
Log ("File already downloaded: '" + $dllfile + "'")
}
else
{
Log ("Downloading: '" + $nugetpkg + "' -> '" + $zipfile + "'")
Invoke-WebRequest -UseBasicParsing $nugetpkg -OutFile $zipfile
if (!(Test-Path $zipfile))
{
Log ("Couldn't download: '" + $zipfile + "'") Red
exit 1
}
Log ("Extracting: '" + $zipfile + "' -> '" + $dllfolder + "'")
Expand-Archive $zipfile $dllfolder
if (!(Test-Path $dllfile))
{
Log ("Couldn't extract: '" + $dllfile + "'") Red
exit 1
}
Log ("Deleting file: '" + $zipfile + "'")
del $zipfile
}
Log ("Loading assembly: '" + $dllfile + "'")
[Reflection.Assembly]::LoadFile($dllfile) | Out-Null
}
function Login([string] $subscriptionName)
{
[string] $tenantId = $env:AzureTenantId
[string] $clientId = $env:AzureClientId
[string] $clientSecret = $env:AzureClientSecret
Log ("Logging in: '" + $subscriptionName + "'")
if ($tenantId -and $clientId -and $clientSecret)
{
$ss = $clientSecret | ConvertTo-SecureString -Force -AsPlainText
$creds = New-Object PSCredential -ArgumentList $clientId, $ss
Connect-AzAccount -Subscription $subscriptionName -ServicePrincipal -Tenant $tenantId -Credential $creds
}
else
{
Connect-AzAccount -Subscription $subscriptionName | Out-Null
}
}
function Get-ArtilleryYml()
{
$artilleryYml = $null
if (!$env:ArtilleryYml)
{
Log ("Environment variable ArtilleryYml not set.") Red
exit 1
}
else
{
[string] $artilleryYml = $env:ArtilleryYml
if (!$artilleryYml.Contains("config:") -or !$artilleryYml.Contains("scenarios:"))
{
Log ("Environment variable ArtilleryYml not a valid Artillery configuration!") Red
exit 1
}
}
return $artilleryYml
}
function Prepare-Beat([string] $payloadFolder, [string] $beatName, [string] $beatYml, [string] $beatFolder)
{
if (!$beatYml)
{
Log ("Ignoring " + $beatName + ".") Yellow
return
}
if (!(Test-Path $payloadFolder))
{
Log ("Creating payload folder: '" + $payloadFolder + "'")
md $payloadFolder | Out-Null
}
[string] $beatFile = Join-Path $payloadFolder ($beatName + ".yml")
Log ("Saving " + $beatName + " file: '" + $beatFile + "'")
Set-Content $beatFile $beatYml
}
function Prepare-Payload([string] $payloadFolder, [string] $payloadFile, [string] $zipPassword, [string] $artilleryYml)
{
if (!(Test-Path $payloadFolder))
{
Log ("Creating payload folder: '" + $payloadFolder + "'")
md $payloadFolder | Out-Null
}
[string] $zipfile = Join-Path ".." $payloadFile
[string] $filename = Join-Path $payloadFolder "artillery.yml"
Set-Content $filename $artilleryYml
cd $payloadFolder
Log ("Current dir: '" + (pwd).Path + "'")
Log ("Zipping: . -> '" + $zipfile + "'")
7z a -mx9 $zipfile -mhe ("-p" + $zipPassword)
if (!$? -or (!(Test-Path $zipfile)) -or (dir $zipfile).Length -lt 1)
{
cd ..
Log ("Couldn't zip.") Red
exit 1
}
cd ..
}
function Upload-Payload([string] $payloadFile, [string] $resourceGroupName, [string] $location, [string] $storageAccountName, [DateTime] $now)
{
[string] $sevenZip = "sevenzip.zip"
[string] $storageKind = "BlobStorage"
[string] $containerName = [Guid]::NewGuid().ToString()
[string] $type = "Standard_LRS"
[string] $accessTier = "Hot"
Log ("Creating storage account: '" + $storageAccountName + "' in '" + $resourceGroupName + "' at '" + $location + "'")
$storageAccount = $null
try
{
$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName -Location $location -Type $type -Kind $storageKind -AccessTier $accessTier -EnableHttpsTrafficOnly $true
}
catch
{
Log $_.Exception Yellow
Log ("Retrieving deployment storage account")
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName
}
Log ("Retrieving deployment storage account key")
$storageAccountKey = Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccount.StorageAccountName
$storageKey = $storageAccountKey[0].Value
Log ("Creating deployment storage context")
$storageContext = New-AzStorageContext -StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $storageKey
Run-Robust {
Log ("Creating deployment storage container (try " + ($i+1) + "): '" + $containerName + "'")
New-AzStorageContainer -Name $containerName -Context $storageContext | Out-Null
} { Clear-DnsClientCache } 12 5
[DateTime] $endtime = $now.AddHours(1.0)
if ((Test-Path "setup.ps1") -and (Test-Path $sevenZip))
{
[string] $setupScript = "setup.ps1"
Log ("Windows, using setup script: '" + $setupScript + "'")
[string[]] $keys = "setupScriptUrl", "sevenZipUrl", "payloadZipUrl"
[string[]] $files = $setupScript, $sevenZip, $payloadFile
}
elseif ((Test-Path "setup.sh"))
{
[string] $setupScript = "setup.sh"
Log ("Linux, using setup script: '" + $setupScript + "'")
[string[]] $keys = "setupScriptUrl", "payloadZipUrl"
[string[]] $files = $setupScript, $payloadFile
}
else
{
Log ("Unknown environment, setup.ps1/setup.sh missing.") Red
exit 1
}
if (!(Test-Path $payloadFile))
{
Log ("Missing payload file: '" + $payloadFile + "'") Red
exit 1
}
$urls = @{}
Log ("Uploading files...")
for ([int] $i=0; $i -lt $keys.Count; $i++)
{
[string] $key = $keys[$i]
[string] $filename = $files[$i]
$urls[$key] = Upload-Blob $storageContext $containerName $filename $now $endtime
}
$urls["blobUrl"] = $storageContext.BlobEndPoint + $containerName
$urls["storageKey"] = $storageKey
$urls["containerName"] = $containerName
return $urls
}
function Run-Robust([ScriptBlock] $main, [ScriptBlock] $cleanup, [int] $retries, [int] $sleepSeconds)
{
for ([int] $i=0; $i -lt $retries; $i++)
{
try
{
&$main
break
}
catch
{
Log $_.Exception
if ($i -eq ($retries-1))
{
throw
}
if ($cleanup)
{
&$cleanup
}
Start-Sleep $sleepSeconds
}
}
}
function Upload-Blob($storageContext, [string] $containerName, [string] $filename, [DateTime] $now, [DateTime] $endtime)
{
[string] $blobName = Split-Path -Leaf $filename
[string] $url = $storageContext.BlobEndPoint + $containerName + "/" + $blobName
Log ("Uploading '" + $filename + "' to '" + $url + "'")
Set-AzStorageBlobContent -Container $containerName -File $filename -Blob $blobName -Context $storageContext -BlobType "Block" -Force | Out-Null
[string] $sas = New-AzStorageBlobSASToken -Container $containerName -Blob $blobName -Context $storageContext -Permission r -StartTime $now -ExpiryTime $endtime
[string] $sasurl = $url + $sas
Log ("Got sasurl: '" + $sasurl + "'")
return $sasurl
}
function Update-ParametersFile([string] $parametersFile, [string] $username, [string] $password, [string] $ipaddress, [string] $zipPassword, $sasurls)
{
$replaceValues = @{}
$replaceValues["adminUsername"] = $username
$replaceValues["adminPassword"] = $password
$replaceValues["sourceAddressPrefix"] = $ipaddress
$replaceValues["zipPassword"] = $zipPassword
$sasurls.Keys | % {
$replaceValues[$_] = $sasurls[$_]
}
[string] $filename = Join-Path (pwd).Path $parametersFile
Log ("Reading parameters file: '" + $filename + "'")
[string] $content = [IO.File]::ReadAllText($filename)
$json = [Newtonsoft.Json.Linq.JToken]::Parse($content)
if (!$json.parameters)
{
Log ("Couldn't find any parameters in file: '" + $filename + "'") Red
exit 1
}
[bool] $changed = $false
foreach ($elementName in ($replaceValues.Keys | sort))
{
$elements = @($json.parameters.Descendants() | ? { ($_.GetType().Name -eq "JProperty") -and ($_.Name -eq $elementName) })
if (!$elements)
{
Log ("Couldn't find element: '" + $elementName + "'") Yellow
continue
}
foreach ($element in $elements)
{
[string] $newvalue = $replaceValues[$elementName]
if ($element.children().children())
{
if ($element.value.value.value -ne $newvalue)
{
Log ("Updating .value '" + $filename + "', " + $element.Name + ": '" + $element.value.value.value + "' -> '" + (Obfuscate-String $newvalue $element.Name) + "'")
$element.value.value = $newvalue
$changed = $true
}
}
else
{
if ($element.value -ne $newvalue)
{
Log ("Updating '" + $filename + "', " + $element.Name + ": '" + $element.value + "' -> '" + (Obfuscate-String $newvalue $element.Name) + "'")
$element.value.value = $newvalue
$changed = $true
}
}
}
}
if ($changed)
{
[string] $pretty = $json.ToString([Newtonsoft.Json.Formatting]::Indented)
Log ("Saving parameters file: '" + $filename + "'")
[IO.File]::WriteAllText($filename, $pretty)
}
}
function Download-Result([string] $resourceGroupName, [string] $storageAccountName, [string] $blobUrl, [string] $containerName, [string] $zipfile, [string] $zipPassword)
{
Log ("Retrieving result storage account")
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName
Log ("Retrieving deployment storage account key")
$storageAccountKey = Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccount.StorageAccountName
$storageKey = $storageAccountKey[0].Value
Log ("Creating deployment storage context")
$storageContext = New-AzStorageContext -StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $storageKey
[string] $url = $blobUrl + "/" + $zipfile
[string] $zipfile = "result.7z"
[string] $blobName = "result.7z"
if (Test-Path $zipfile)
{
Log ("Deleting zipfile: '" + $zipfile + "'")
del $zipfile
}
[string] $jsonfile = "result.json"
if (Test-Path $jsonfile)
{
Log ("Deleting jsonfile: '" + $jsonfile + "'")
del $jsonfile
}
[string] $htmlfile = "result.html"
if (Test-Path $htmlfile)
{
Log ("Deleting htmlfile: '" + $htmlfile + "'")
del $htmlfile
}
[string] $stdfile = "stdout"
if (Test-Path $stdfile)
{
Log ("Deleting stdfile: '" + $stdfile + "'")
del $stdfile
}
[string] $errfile = "errout"
if (Test-Path $errfile)
{
Log ("Deleting errfile: '" + $errfile + "'")
del $errfile
}
Log ("Downloading '" + $url + "' to '" + $zipfile + "'")
Get-AzStorageBlobContent -Container $containerName -Blob $blobName -Context $storageContext | Out-Null
7z x $zipfile ("-p" + $zipPassword)
}
function Obfuscate-String([string] $text, [string] $textname)
{
if ($textname.ToLower().Contains("password") -or $textname.ToLower().EndsWith("key"))
{
return "*" * $text.Length
}
else
{
return $text
}
}
function Generate-AlphanumericPassword([int] $numberOfChars)
{
[char[]] $validChars = 'a'..'z' + 'A'..'Z' + [char]'0'..[char]'9'
[string] $password = ""
do
{
[string] $password = (1..$numberOfChars | % { $validChars[(Get-Random -Maximum $validChars.Length)] }) -join ""
}
while (
!($password | ? { ($_.ToCharArray() | ? { [Char]::IsUpper($_) }) }) -or
!($password | ? { ($_.ToCharArray() | ? { [Char]::IsLower($_) }) }) -or
!($password | ? { ($_.ToCharArray() | ? { [Char]::IsDigit($_) }) }));
return $password
}
function Log-TCTime([string] $key, [ScriptBlock] $script)
{
[Diagnostics.Stopwatch] $watch = [Diagnostics.Stopwatch]::StartNew()
try
{
&$script
}
finally
{
Log("##teamcity[buildStatisticValue key='" + $key + "' value='" + [int]$watch.Elapsed.TotalMilliseconds + "']") Magenta
}
}
function Log([string] $message, $color)
{
[string] $logfile = Join-Path $logfolder "create.log"
[string] $annotatedMessage = [DateTime]::UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + ": " + $message
$annotatedMessage | Add-Content $logfile
if ($color)
{
Write-Host $annotatedMessage -f $color
}
else
{
Write-Host $annotatedMessage -f Green
}
}
Main $args