HPC.ACM.psm1

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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
# TODO: need login?
# NOTE: This function is called as a script block by Start-ThreadJob AND THUS
# CAN NOT CALL ANY OTHER INTERNAL FUNCTIONS DIRECTLY. This may need to be FIXED.
function Add-AcmVm {
  param(
    [Parameter(Mandatory = $true)]
    [object] $vm,

    [Parameter(Mandatory = $true)]
    [string] $storageAccountName,

    [Parameter(Mandatory = $true)]
    [string] $storageAccountRG,

    [string] $linuxExtensionUrl,
    [string] $windowsExtensionUrl
  )

  $devMode = $linuxExtensionUrl -or $windowsExtensionUrl

  Write-Host "Enable MSI for VM $($vm.Name)"
  if ($vm.Identity -eq $null -or !($vm.Identity.Type -contains "SystemAssigned")) {
    Write-Host "Executing for VM $($vm.Name)"
    Update-AzVM -ResourceGroupName $vm.ResourceGroupName -VM $vm -IdentityType "SystemAssigned"
  }
  else {
    Write-Host "The VM $($vm.Name) already has an System Assigned Identity"
  }

  # Update $vm for new $vm.Identity.PrincipalId
  $vm = Get-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName

  Write-Host "Add role 'reader' to VM $($vm.Name)"
  try {
    New-AzRoleAssignment -ObjectId $vm.Identity.PrincipalId -RoleDefinitionName "Reader" -ResourceGroupName $vm.ResourceGroupName
  }
  catch {
    if ($_ -contains 'already exists') {
      Write-Host "The VM $($vm.Name) already has role 'Reader' on resouce group $($vm.ResourceGroupName)"
    }
    else {
      throw
    }
  }

  Write-Host "Add role 'Storage Account Contributor' to VM $($vm.Name)"
  try {
    New-AzRoleAssignment -ObjectId $vm.Identity.PrincipalId -RoleDefinitionName "Storage Account Contributor" -ResourceName $storageAccountName -ResourceType "Microsoft.Storage/storageAccounts" -ResourceGroupName $storageAccountRG
  }
  catch {
    Write-Host $_
    if ($_ -contains 'already exists') {
      Write-Host "The VM $($vm.Name) already has role 'Storage Account Contributor' on storage account $($storageAccountName)"
    }
    else {
      throw
    }
  }

  $extName = 'HpcAcmAgent'
  $devExtName = 'HpcAcmAgentDev'
  $knownExts = @($extName, $devExtName)
  $foundExts = @()
  foreach ($ext in $vm.Extensions) {
    foreach ($name in $knownExts) {
      if ($ext.Name -eq $name) {
        $foundExts += $name
      }
    }
  }
  Write-Host "Found $($foundExts.Count) VM Extensions for VM $($vm.Name): $($foundExts -join ', ')"

  foreach ($ext in $foundExts) {
    Write-Host "Remove Extension $ext from VM $($vm.Name)"
    if ($ext -eq $devExtName) {
      if ($vm.OSProfile.LinuxConfiguration) {
        $settings = @{
          timestamp = $(Get-Date).Ticks
          fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/uninstall.sh')
          commandToExecute = "./uninstall.sh"
          skipDos2Unix = $true
        }
        Set-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Location $vm.Location -ExtensionType CustomScript -Publisher Microsoft.Azure.Extensions -TypeHandlerVersion 2.0 -Name $ext -Settings $settings
      }
      else {
        $settings = @{
          timestamp = $(Get-Date).Ticks
          fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/uninstall.ps1')
          commandToExecute = "powershell -ExecutionPolicy Unrestricted -File uninstall.ps1"
        }
        Set-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Location $vm.Location -ExtensionType CustomScriptExtension -Publisher Microsoft.Compute -TypeHandlerVersion 1.9 -Name $ext -Settings $settings
      }
    }
    else {
      Remove-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Name $ext -Force
    }
  }

  if ($devMode) {
    $ext = $devExtName
  }
  else {
    $ext = $extName
  }
  Write-Host "Install $ext for VM $($vm.Name)"

  if ($devMode) {
    if ($vm.OSProfile.LinuxConfiguration) {
      $settings = @{
        timestamp = $(Get-Date).Ticks
        fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/install.sh')
        commandToExecute = "./install.sh $linuxExtensionUrl"
        skipDos2Unix = $true
      }
      $result = Set-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Location $vm.Location -ExtensionType CustomScript -Publisher Microsoft.Azure.Extensions -TypeHandlerVersion 2.0 -Name $ext -Settings $settings
    }
    else {
      $settings = @{
        timestamp = $(Get-Date).Ticks
        fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/install.ps1')
        commandToExecute = "powershell -ExecutionPolicy Unrestricted -File install.ps1 $windowsExtensionUrl"
      }
      $result = Set-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Location $vm.Location -ExtensionType CustomScriptExtension -Publisher Microsoft.Compute -TypeHandlerVersion 1.9 -Name $ext -Settings $settings
    }
  }
  else {
    if ($vm.OSProfile.LinuxConfiguration) {
      $extesionType = "HpcAcmAgent"
    }
    else {
      # Suppose there're only Linux and Windows
      $extesionType = "HpcAcmAgentWin"
    }
    $result = Set-AzVMExtension -Publisher "Microsoft.HpcPack" -ExtensionType $extesionType -ResourceGroupName $vm.ResourceGroupName `
      -TypeHandlerVersion 1.0 -VMName $vm.Name -Location $vm.Location -Name $ext
  }
  if (!$result.IsSuccessStatusCode) {
    throw "Failed installing $ext for VM $($vm.Name)."
  }
}

function Remove-AcmVm {
  param(
    [Parameter(Mandatory = $true)]
    [object] $vm,

    [Parameter(Mandatory = $true)]
    [string] $storageAccountName,

    [Parameter(Mandatory = $true)]
    [string] $storageAccountRG
  )

  $extName = 'HpcAcmAgent'
  $devExtName = 'HpcAcmAgentDev'
  $knownExts = @($extName, $devExtName)
  $foundExts = @()
  foreach ($ext in $vm.Extensions) {
    foreach ($name in $knownExts) {
      if ($ext.Name -eq $name) {
        $foundExts += $name
      }
    }
  }
  Write-Host "Found $($foundExts.Count) VM Extensions for VM $($vm.Name): $($foundExts -join ', ')"

  foreach ($ext in $foundExts) {
    Write-Host "Remove Extension $ext from VM $($vm.Name)"
    if ($ext -eq $devExtName) {
      if ($vm.OSProfile.LinuxConfiguration) {
        $settings = @{
          timestamp = $(Get-Date).Ticks
          fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/uninstall.sh')
          commandToExecute = "./uninstall.sh"
          skipDos2Unix = $true
        }
        Set-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Location $vm.Location -ExtensionType CustomScript -Publisher Microsoft.Azure.Extensions -TypeHandlerVersion 2.0 -Name $ext -Settings $settings
      }
      else {
        $settings = @{
          timestamp = $(Get-Date).Ticks
          fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/uninstall.ps1')
          commandToExecute = "powershell -ExecutionPolicy Unrestricted -File uninstall.ps1"
        }
        Set-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Location $vm.Location -ExtensionType CustomScriptExtension -Publisher Microsoft.Compute -TypeHandlerVersion 1.9 -Name $ext -Settings $settings
      }
    }
    Remove-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Name $ext -Force
  }

  Write-Host "Remove role 'Storage Account Contributor' from VM $($vm.Name)"
  try {
    Remove-AzRoleAssignment -ObjectId $vm.Identity.PrincipalId -RoleDefinitionName "Storage Account Contributor" -ResourceName $storageAccountName -ResourceType "Microsoft.Storage/storageAccounts" -ResourceGroupName $storageAccountRG
  }
  catch {
    Write-Host "Caught exception: $($_)"
  }

  Write-Host "Remove role 'reader' from VM $($vm.Name)"
  try {
    Remove-AzRoleAssignment -ObjectId $vm.Identity.PrincipalId -RoleDefinitionName "Reader" -ResourceGroupName $vm.ResourceGroupName
  }
  catch {
    Write-Host "Caught exception: $($_)"
  }

  Write-Host "Disable MSI for VM $($vm.Name)"
  if ($vm.Identity -and $vm.Identity.Type -contains "SystemAssigned") {
    try {
      Update-AzVM -ResourceGroupName $vm.ResourceGroupName -VM $vm -IdentityType "None"
    }
    catch {
      Write-Host "Caught exception: $($_)"
    }
  }
}

function Add-AcmVmScaleSet {
  param(
    [Parameter(Mandatory = $true)]
    [object] $vmss,

    [Parameter(Mandatory = $true)]
    [string] $storageAccountName,

    [Parameter(Mandatory = $true)]
    [string] $storageAccountRG,

    [string] $linuxExtensionUrl,
    [string] $windowsExtensionUrl
  )

  $devMode = $linuxExtensionUrl -or $windowsExtensionUrl

  Write-Host "Enable MSI for VM Scale Set $($vmss.Name)"
  if ($vmss.Identity -eq $null -or !($vmss.Identity.Type -contains "SystemAssigned")) {
    Write-Host "Executing for VMSS $($vmss.Name)"
    Update-AzVmss -ResourceGroupName $vmss.resourceGroupName -VMScaleSetName $vmss.Name -IdentityType "SystemAssigned"
  }
  else {
    Write-Host "The VMSS $($vmss.Name) already has an System Assigned Identity"
  }

  # Update $vmss for new $vmss.Identity.PrincipalId
  $vmss = Get-AzVmss -Name $vmss.Name -ResourceGroupName $vmss.ResourceGroupName

  Write-Host "Add role 'reader' to VMSS $($vmss.Name)"
  try {
    New-AzRoleAssignment -ObjectId $vmss.Identity.PrincipalId -RoleDefinitionName "Reader" -ResourceGroupName $vmss.ResourceGroupName
  }
  catch {
    if ($_ -contains 'already exists') {
      Write-Host "The VMSS $($vmss.Name) already has role 'Reader' on resouce group $($vmss.ResourceGroupName)"
    }
    else {
      throw
    }
  }

  Write-Host "Add role 'Storage Account Contributor' to VMSS $($vmss.Name)"
  try {
    New-AzRoleAssignment -ObjectId $vmss.Identity.PrincipalId -RoleDefinitionName "Storage Account Contributor" -ResourceName $storageAccountName -ResourceType "Microsoft.Storage/storageAccounts" -ResourceGroupName $storageAccountRG
  }
  catch {
    if ($_ -contains 'already exists') {
      Write-Host "The VMSS $($vmss.Name) already has role 'Storage Account Contributor' on storage account $($storageAccountName)"
    }
    else {
      throw
    }
  }

  $extName = 'HpcAcmAgent'
  $devExtName = 'HpcAcmAgentDev'
  $knownExts = @($extName, $devExtName)
  $foundExts = @()
  foreach ($ext in $vmss.VirtualMachineProfile.ExtensionProfile.Extensions) {
    foreach ($name in $knownExts) {
      if ($ext.Name -eq $name) {
        $foundExts += $name
      }
    }
  }
  Write-Host "Found $($foundExts.Count) VM Extensions for VM scale set $($vmss.Name): $($foundExts -join ', ')"

  foreach ($ext in $foundExts) {
    Write-Host "Remove Extension $ext from VM scale set $($vmss.Name)"

    # Remove-AzVmssExtension simply removes the element of extension by name from
    # array of $vmss.VirtualMachineProfile.ExtensionProfile.Extensions, while
    # Update-AzVmss does the effective work. Remember: only one custom script
    # extension is allowed on one VM/VM scale set.
    Remove-AzVmssExtension -VirtualMachineScaleSet $vmss -Name $ext

    if ($ext -eq $devExtName) {
      if ($vmss.VirtualMachineProfile.OsProfile.LinuxConfiguration) {
        $settings = @{
          timestamp = $(Get-Date).Ticks
          fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/uninstall.sh')
          commandToExecute = "./uninstall.sh"
          skipDos2Unix = $true
        }
        Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name $ext -Setting $settings `
          -Type CustomScript -Publisher Microsoft.Azure.Extensions -TypeHandlerVersion 2.0
      }
      else {
        $settings = @{
          timestamp = $(Get-Date).Ticks
          fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/uninstall.ps1')
          commandToExecute = "powershell -ExecutionPolicy Unrestricted -File uninstall.ps1"
        }
        Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name $ext -Setting $settings `
          -Type CustomScriptExtension -Publisher Microsoft.Compute -TypeHandlerVersion 1.9
      }
    }
  }
  Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -VMScaleSetName $vmss.Name -VirtualMachineScaleSet $vmss
  if ($vmss.UpgradePolicy.Mode -eq 'Manual') {
    Update-AzVmssInstance -ResourceGroupName $vmss.ResourceGroupName -VMScaleSetName $vmss.Name -InstanceId "*"
  }

  if ($devMode) {
    $ext = $devExtName
  }
  else {
    $ext = $extName
  }
  Write-Host "Install $ext for VM Scale Set $($vmss.Name)"

  if ($devMode) {
    Remove-AzVmssExtension -VirtualMachineScaleSet $vmss -Name $ext
    if ($vmss.VirtualMachineProfile.OsProfile.LinuxConfiguration) {
      $settings = @{
        timestamp = $(Get-Date).Ticks
        fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/install.sh')
        commandToExecute = "./install.sh $linuxExtensionUrl"
        skipDos2Unix = $true
      }
      Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name $ext -Setting $settings `
        -Type CustomScript -Publisher Microsoft.Azure.Extensions -TypeHandlerVersion 2.0
    }
    else {
      $settings = @{
        timestamp = $(Get-Date).Ticks
        fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/install.ps1')
        commandToExecute = "powershell -ExecutionPolicy Unrestricted -File install.ps1 $windowsExtensionUrl"
      }
      Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name $ext -Setting $settings `
        -Type CustomScriptExtension -Publisher Microsoft.Compute -TypeHandlerVersion 1.9
    }
  }
  else {
    if ($vmss.VirtualMachineProfile.OsProfile.LinuxConfiguration) {
      $extesionType = "HpcAcmAgent"
    }
    else {
      # Suppose there're only Linux and Windows
      $extesionType = "HpcAcmAgentWin"
    }
    Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name $ext -Publisher "Microsoft.HpcPack" `
      -Type $extesionType -TypeHandlerVersion 1.0
  }
  Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -VMScaleSetName $vmss.Name -VirtualMachineScaleSet $vmss
  if ($vmss.UpgradePolicy.Mode -eq 'Manual') {
    $result = Update-AzVmssInstance -ResourceGroupName $vmss.ResourceGroupName -VMScaleSetName $vmss.Name -InstanceId "*"
    if ($result.Status -ne 'Succeeded') {
      throw "Failed installing $ext for VM scale set $($vmss.Name)."
    }
  }
}

function Remove-AcmVmScaleSet {
  param(
    [Parameter(Mandatory = $true)]
    [object] $vmss,

    [Parameter(Mandatory = $true)]
    [string] $storageAccountName,

    [Parameter(Mandatory = $true)]
    [string] $storageAccountRG
  )

  $extName = 'HpcAcmAgent'
  $devExtName = 'HpcAcmAgentDev'
  $knownExts = @($extName, $devExtName)
  $foundExts = @()
  foreach ($ext in $vmss.VirtualMachineProfile.ExtensionProfile.Extensions) {
    foreach ($name in $knownExts) {
      if ($ext.Name -eq $name) {
        $foundExts += $name
      }
    }
  }
  Write-Host "Found $($foundExts.Count) VM Extensions for VM scale set $($vmss.Name): $($foundExts -join ', ')"

  foreach ($ext in $foundExts) {
    Write-Host "Remove Extension $ext from VM scale set $($vmss.Name)"
    Remove-AzVmssExtension -VirtualMachineScaleSet $vmss -Name $ext
    if ($ext -eq $devExtName) {
      if ($vmss.VirtualMachineProfile.OsProfile.LinuxConfiguration) {
        $settings = @{
          timestamp = $(Get-Date).Ticks
          fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/uninstall.sh')
          commandToExecute = "./uninstall.sh"
          skipDos2Unix = $true
        }
        Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name $ext -Setting $settings `
          -Type CustomScript -Publisher Microsoft.Azure.Extensions -TypeHandlerVersion 2.0
      }
      else {
        $settings = @{
          timestamp = $(Get-Date).Ticks
          fileUris = @('https://raw.githubusercontent.com/coin8086/node-manager-deployer/master/uninstall.ps1')
          commandToExecute = "powershell -ExecutionPolicy Unrestricted -File uninstall.ps1"
        }
        Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name $ext -Setting $settings `
          -Type CustomScriptExtension -Publisher Microsoft.Compute -TypeHandlerVersion 1.9
      }
    }
  }
  Update-AzVmss -ResourceGroupName $vmss.ResourceGroupName -VMScaleSetName $vmss.Name -VirtualMachineScaleSet $vmss
  if ($vmss.UpgradePolicy.Mode -eq 'Manual') {
    Update-AzVmssInstance -ResourceGroupName $vmss.ResourceGroupName -VMScaleSetName $vmss.Name -InstanceId "*"
  }

  Write-Host "Remove role 'Storage Account Contributor' from VMSS $($vmss.Name)"
  try {
    Remove-AzRoleAssignment -ObjectId $vmss.Identity.PrincipalId -RoleDefinitionName "Storage Account Contributor" -ResourceName $storageAccountName -ResourceType "Microsoft.Storage/storageAccounts" -ResourceGroupName $storageAccountRG
  }
  catch {
    Write-Host "Caught exception: $($_)"
  }

  Write-Host "Remove role 'reader' from VMSS $($vmss.Name)"
  try {
    Remove-AzRoleAssignment -ObjectId $vmss.Identity.PrincipalId -RoleDefinitionName "Reader" -ResourceGroupName $vmss.ResourceGroupName
  }
  catch {
    Write-Host "Caught exception: $($_)"
  }

  Write-Host "Disable MSI for VMSS $($vmss.Name)"
  if ($vmss.Identity -and $vmss.Identity.Type -contains "SystemAssigned") {
    try {
      Update-AzVmss -ResourceGroupName $vmss.resourceGroupName -VMScaleSetName $vmss.Name -IdentityType "None"
    }
    catch {
      Write-Host "Caught exception: $($_)"
    }
  }
}

function Set-AcmClusterTag {
  param(
    [Parameter(Mandatory = $true)]
    [string] $ResourceGroup,

    [Parameter(Mandatory = $true)]
    [string] $StorageAccountName,

    [Parameter(Mandatory = $true)]
    [string] $StorageAccountRG
  )

  $rg = Get-AzResourceGroup -Name $ResourceGroup
  $tags = $rg.Tags
  $key = "StorageConfiguration"
  $value = "{ `"AccountName`": `"$($StorageAccountName)`", `"ResourceGroup`":`"$($StorageAccountRG)`" }"
  if ($tags -eq $null) {
    $tags = @{ "$key" = "$value" }
  }
  else {
    $tags[$key] = $value
  }
  Set-AzResourceGroup -Tags $tags -Name $ResourceGroup
}

function Reset-AcmClusterTag {
  param(
    [Parameter(Mandatory = $true)]
    [string] $ResourceGroup
  )

  $rg = Get-AzResourceGroup -Name $ResourceGroup
  $tags = $rg.Tags
  if ($tags) {
    $key = "StorageConfiguration"
    $tags.Remove($key)
    Set-AzResourceGroup -Tags $tags -Name $ResourceGroup
  }
}

function Login {
  $ErrorActionPreference = 'Stop'

  $needLogin = $true
  Try {
    $content = Get-AzContext
    if ($content) {
      $needLogin = ([string]::IsNullOrEmpty($content.Account))
    }
  }
  Catch {
    if ($_ -like "*Login-AzAccount to login*") {
      $needLogin = $true
    }
    else {
      throw
    }
  }

  if ($needLogin) {
    Login-AzAccount
  }
}

function Prepare-AcmAzureCtx {
  param($SubscriptionId)
  Login
  Select-AzSubscription -SubscriptionId $SubscriptionId
}

function Log {
  param(
    [Parameter(Mandatory = $true)]
    $activity,

    $status,
    $op
  )
  $now = Get-Date
  $ts = $now.ToString('yyyy-MM-dd HH:mm:ss')
  $msg = "[$($ts)][$($activity)]"
  if ($status) {
    $msg += "[$($status)]"
  }
  if ($op) {
    $msg += "[$($op)]"
  }
  Write-Host $msg
}

function ShowProgress {
  param(
    [Parameter(Mandatory = $true)]
    $startTime,

    [Parameter(Mandatory = $true)]
    $timeout,

    [Parameter(Mandatory = $true)]
    $activity,

    $status,
    $op,
    $id,
    $pid,

    [switch] $nolog
  )

  $now = Get-Date
  $elapsed = ($now - $startTime).TotalSeconds
  $percent = $elapsed * 100 / $timeout
  if ($percent -gt 100) {
    $percent = 100
  }
  $args = @{
    Activity = $activity
    PercentComplete = $percent
    SecondsRemaining = $timeout - $elapsed
  }
  if ($id) {
    $args['Id'] = $id
  }
  if ($pid) {
    $args['ParentId'] = $pid
  }
  if ($status) {
    $args['Status'] = $status
  }
  if ($op) {
    $args['CurrentOperation'] = $op
  }
  Write-Progress @args

  if (!$nolog) {
    Log $activity $status $op
  }
}

function HideProgress {
  param($id)
  Write-Progress -Activity "END" -Completed -Id $id
}

function Wait-AcmJob {
  param($jobs, $startTime, $timeout, $activity, $progId)

  $status = "Waiting jobs to finish..."
  $pargs = @{
    startTime = $startTime
    timeout = $timeout
    activity = $activity
    status = $status
    id = $progId
  }
  $ids = $jobs.foreach('id')
  $preCount = $null

  while ($true) {
    # TODO: Optimize counting?
    $doneJobCount = $(Get-Job -Id $ids).where({ $_.state -in 'Completed', 'Failed', 'Stopped' }).Count
    $pargs['op'] = "Completed jobs: $($doneJobCount)/$($jobs.Count)"

    if ($doneJobCount -eq $ids.Count) {
      ShowProgress @pargs
      break
    }

    $elapsed = ($(Get-Date) - $startTime).TotalSeconds
    if ($elapsed -ge $Timeout) {
      ShowProgress @pargs
      Log $activity $status 'Timed out!'
      break
    }

    $output = Receive-Job $jobs
    if ($output) {
      ShowProgress @pargs
      Write-Host $output
    }
    else {
      if ($preCount -ne $doneJobCount) {
        ShowProgress @pargs
      }
      else {
        ShowProgress @pargs -nolog
      }
    }
    $preCount = $doneJobCount
    Start-Sleep 1
  }

  $output = Receive-Job $jobs
  if ($output) {
    Write-Host $output
  }
}

function Remove-AcmJob {
  param($ids)

  # Remove-Job somtimes don't return even with "-Force". So do it in another job and forget it.
  Start-ThreadJob -ScriptBlock {
    param($ids)
    Stop-Job -Id $ids
    Remove-Job -Force -Id $ids
  } -ArgumentList $ids | Out-Null
}

function CollectResult {
  param($names, $jobs)

  $result = @()
  for ($idx = 1; $idx -lt $names.Length; $idx++) {
    $result += [PSCustomObject]@{
      Name = $names[$idx]
      JobState = $jobs[$idx].State
      JobId = $jobs[$idx].Id
    }
  }
  return $result
}

function OutputResult {
  param($result)

  $result |
    Sort-Object -Property JobState, Name |
    Format-Table -Property @{Name = 'VM/VM Scale Set'; Expression = {$_.Name}}, JobState, JobId -Wrap |
    Out-Default

  if ($result.Count -gt 0) {
    $completed = $result.where({ $_.JobState -eq 'Completed' }).Count
    $summary = [PSCustomObject]@{
      Total = $result.Count
      Completed = $completed
      Percent = "$('{0:0.00}' -f ($completed * 100 / $result.Count))%"
    }
    $summary | Format-Table -Property Total, Completed, Percent -Wrap | Out-Default
  }
}

function Initialize-AcmCluster {
  param(
    [Parameter(Mandatory = $true)]
    [string] $SubscriptionId,

    [Parameter(Mandatory = $true)]
    [string] $ResourceGroup,

    [Parameter(Mandatory = $true)]
    [string] $AcmResourceGroup,

    [int] $Timeout,

    # NOTE: Do not change the default value and do not provide a bigger one,
    # as Start-ThreadJob won't accept a value > 50 and will raise an error.
    [int] $ConcurrentLimit = 50,

    [switch] $RetainJobs,

    [switch] $Return,

    [switch] $Uninitialize,

    [string] $LinuxExtensionUrl,

    [string] $WindowsExtensionUrl
  )

  $startTime = Get-Date
  if ($Uninitialize) {
    $activity = 'Removing cluster from ACM service...'
  }
  else {
    $activity = 'Adding cluster to ACM service...'
  }

  $basetime = 360 # Max time to add one VM/VM scale set
  if (!$Timeout) {
    # timelimit will be recomputed later based on number of vms
    $timelimit = $basetime
  }
  else {
    $timelimit = $Timeout
  }

  ShowProgress $startTime $timelimit $activity -Status "Login to Azure..." -id 1
  Prepare-AcmAzureCtx $SubscriptionId | Out-Null

  ShowProgress $startTime $timelimit $activity -Status "Preparing for jobs..." -id 1

  $jobs = @()
  $names = @($null)
  $acmRg = Get-AzResourceGroup -Name $AcmResourceGroup
  $storageAccount = (Get-AzStorageAccount -ResourceGroupName $acmRg.ResourceGroupName)[0]
  # TODO: Filter out only running vms, but what about VM scale set?
  $vms = Get-AzVm -ResourceGroupName $ResourceGroup
  $vmssSet = Get-AzVmss -ResourceGroupName $ResourceGroup

  if (!$Timeout) {
    $total = $vms.Count + $vmssSet.Count
    $timelimit = $basetime * ([math]::Truncate($total / $ConcurrentLimit))
    if (($total % $ConcurrentLimit) -gt 0) {
      $timelimit += $basetime
    }
  }

  ShowProgress $startTime $timelimit $activity -Status "Starting jobs..." -id 1

  # Configure storage information for the resource group
  if ($Uninitialize) {
    $jobs += Start-ThreadJob -ScriptBlock ${function:Reset-AcmClusterTag} -ArgumentList $ResourceGroup
  }
  else {
    $jobs += Start-ThreadJob -ScriptBlock ${function:Set-AcmClusterTag} `
      -ArgumentList $ResourceGroup, $storageAccount.StorageAccountName, $storageAccount.ResourceGroupName
  }

  # Register each vm and vm scale set to ACM
  foreach ($vm in $vms) {
    $args = $vm, $storageAccount.StorageAccountName, $storageAccount.ResourceGroupName
    if ($Uninitialize) {
      $func = ${function:Remove-AcmVm}
    }
    else {
      $func = ${function:Add-AcmVm}
      $args += @($LinuxExtensionUrl, $WindowsExtensionUrl)
    }
    $jobs += Start-ThreadJob -ThrottleLimit $ConcurrentLimit -ScriptBlock $func -ArgumentList $args
    $names += $vm.Name
  }
  foreach ($vmss in $vmssSet) {
    $args = $vmss, $storageAccount.StorageAccountName, $storageAccount.ResourceGroupName
    if ($Uninitialize) {
      $func = ${function:Remove-AcmVmScaleSet}
    }
    else {
      $func = ${function:Add-AcmVmScaleSet}
      $args += @($LinuxExtensionUrl, $WindowsExtensionUrl)
    }
    $jobs += Start-ThreadJob -ThrottleLimit $ConcurrentLimit -ScriptBlock $func -ArgumentList $args
    $names += $vmss.Name
  }

  Wait-AcmJob $jobs $startTime $timelimit $activity -ProgId 1

  if (!$Uninitialize) {
    # Wait for some time for ACM agents to register itself to ACM
    $timeLeft = ($(Get-Date) - $startTime).TotalSeconds
    if (!$Timeout -or $timeLeft -lt $timelimit) {
      $time = 120
      if (!$Timeout) {
        $timelimit += $time
      }
      else {
        if ($timeLeft -lt $time) {
          $time = $timeLeft
        }
      }

      $status = "Waiting for $($time) seconds for nodes to register itself to ACM..."
      for ($i = 0; $i -lt $time; $i++) {
        if ($i -eq 0) {
          ShowProgress $startTime $timelimit $activity -Status $status -id 1
        }
        else {
          ShowProgress $startTime $timelimit $activity -Status $status -id 1 -nolog
        }
        Start-Sleep 1
      }
    }
  }

  if (!$RetainJobs) {
    ShowProgress $startTime $timelimit $activity -Status "Cleaning jobs..." -id 1
    $ids = $jobs.foreach('Id')
    Remove-AcmJob $ids
  }
  HideProgress 1

  ShowProgress $startTime $timelimit $activity -Status "Ending..." -id 1
  $result = CollectResult $names $jobs
  OutputResult $result
  if ($Return) {
    return $result
  }
}

function Add-AcmCluster {
<#
.SYNOPSIS
Add an Azure cluster of VMs/VM scale sets to ACM.

.PARAMETER ResourceGroup
The name of an Azure resource group containing the VMs/VM scale sets to test.

.PARAMETER AcmResourceGroup
The name of an Azure resource group containing the ACM service.

.PARAMETER SubscriptionId
The ID of an Azure subscription containing both the ResourceGroup and AcmResourceGroup.

.PARAMETER Timeout
The timeout value for adding cluster to Acm. By default, an estimated value will be set based on the number of VMs/VM scale sets in a cluster. A value shorter than necesssary will fail the setup procedure. You could specify a larger value to ensure the success of setup.

.PARAMETER RetainJobs
Do not remove PowerShell jobs after. This is for checking the job state for debug purpose.

.PARAMETER Return
Return the result. By default, the function returns nothing.

.PARAMETER LinuxExtensionUrl
URL for the Linux VM Extension. The file name in the URL must match pattern "^.+\-\d+\.\d+\.\d+\.\d+\.zip$". This option is internal for development.

.PARAMETER WindowsExtensionUrl
URL for the Windows VM Extension. The file name in the URL must match pattern "^.+\-\d+\.\d+\.\d+\.\d+\.zip$". This option is internal for development.

.NOTES
The command will log a lot to screen. So it's better to redirect them to files. Do it like
Add-AcmCluster ... 2>err_log 6>info_log
It writes errors to file err_log and other information to file info_log.

.EXAMPLE
Add-AcmCluster -SubscriptionId a486e243-747b-42de-8c4c-379f8295a746 -ResourceGroup 'my-cluster-1' -AcmResourceGroup 'my-acm-cluster' 2>err_log 6>info_log
Add a cluster of VMs/VM scale sets to ACM.
#>

  param(
    [Parameter(Mandatory = $true)]
    [string] $SubscriptionId,

    [Parameter(Mandatory = $true)]
    [string] $ResourceGroup,

    [Parameter(Mandatory = $true)]
    [string] $AcmResourceGroup,

    [int] $Timeout,

    [switch] $RetainJobs,

    [switch] $Return,

    [string] $LinuxExtensionUrl,

    [string] $WindowsExtensionUrl
  )
  Initialize-AcmCluster @PSBoundParameters
}

function Remove-AcmCluster {
<#
.SYNOPSIS
Remove an Azure cluster of VMs/VM scale sets from ACM.

.PARAMETER ResourceGroup
The name of an Azure resource group containing the VMs/VM scale sets to test.

.PARAMETER AcmResourceGroup
The name of an Azure resource group containing the ACM service.

.PARAMETER SubscriptionId
The ID of an Azure subscription containing both the ResourceGroup and AcmResourceGroup.

.PARAMETER Timeout
The timeout value for adding cluster to Acm. By default, an estimated value will be set based on the number of VMs/VM scale sets in a cluster. A value shorter than necesssary will fail the setup procedure. You could specify a larger value to ensure the success of setup.

.PARAMETER RetainJobs
Do not remove PowerShell jobs after. This is for checking the job state for debug purpose.

.PARAMETER Return
Return the result. By default, the function returns nothing.

.EXAMPLE
Remove-AcmCluster -SubscriptionId a486e243-747b-42de-8c4c-379f8295a746 -ResourceGroup 'my-cluster-1' -AcmResourceGroup 'my-acm-cluster'
Remove a cluster of VMs/VM scale sets from ACM.
#>

  param(
    [Parameter(Mandatory = $true)]
    [string] $SubscriptionId,

    [Parameter(Mandatory = $true)]
    [string] $ResourceGroup,

    [Parameter(Mandatory = $true)]
    [string] $AcmResourceGroup,

    [int] $Timeout,

    [switch] $RetainJobs,

    [switch] $Return
  )
  Initialize-AcmCluster @PSBoundParameters -Uninitialize
}

function Wait-AcmDiagnosticJob {
  param($job, $conn, $startTime, $timeout, $activity, $status, $progId)

  $pargs = @{
    startTime = $startTime
    timeout = $timeout
    activity = $activity
    status = $status
    id = $progId
  }
  $jobState = $null

  while ($true) {
    $pargs['op'] = "Job state: $($job.State)"
    if ($job.State -in "Finished", "Failed", "Canceled") {
      ShowProgress @pargs
      break
    }

    $elapsed = ($(Get-Date) - $startTime).TotalSeconds
    if ($elapsed -ge $Timeout) {
      ShowProgress @pargs
      Log $activity $status 'Timed out!'
      break
    }

    if ($jobState -ne $job.State) {
      ShowProgress @pargs
    }
    else {
      ShowProgress @pargs -nolog
    }
    $jobState = $job.State
    $job = Get-AcmDiagnosticJob -Id $job.Id -Connection $conn
    Start-Sleep 1
  }
  return $job
}

<#
.SYNOPSIS
Test Azure cluster of VMs/VM scale sets in ACM.

.PARAMETER ApiBasePoint
The URL of ACM web service. The value can be found by the result of Get-AcmAppInfo.

.PARAMETER IssuerUrl
The issuer URL of ACM web service, may be empty if the ACM web service is not protected by Azure AD. The value can be found by the result of Get-AcmAppInfo.

.PARAMETER ClientId
The client id of ACM web service, may be empty if the ACM web service is not protected by Azure AD. The value can be found by the result of Get-AcmAppInfo.

.PARAMETER ClientSecret
The client secret of ACM web service, may be empty if the ACM web service is not protected by Azure AD. The value can be found by the result of Get-AcmAppInfo.

.PARAMETER Timeout
The timeout value for performing test on cluster. By default, an estimated value will be set based on the number of nodes in a cluster. A value shorter than necesssary will cause no test result, since the test can't complete without enough time. You could specify a larger value to ensure the test to complete.

.PARAMETER Return
Return the result. By default, the function returns nothing.

.NOTES
The command will log a lot to screen. So it's better to redirect them to files. Do it like
Test-AcmCluster ... 2>err_log 6>info_log
It writes errors to file err_log and other information to file info_log.

.EXAMPLE
  $app = Get-AcmAppInfo -SubscriptionId 'my-id' -ResourceGroup 'my-group'; Test-AcmCluster @app 2>err_log 6>info_log
#>

function Test-AcmCluster {
  param(
    [Parameter(Mandatory = $true)]
    [string] $ApiBasePoint,

    [string] $IssuerUrl,

    [string] $ClientId,

    [string] $ClientSecret,

    [int] $Timeout,

    [switch] $Return
  )

  $startTime = Get-Date
  $activity = "Testing cluster in ACM service..."

  # The meanings of basetime and basesize are:
  # every basesize number of nodes requires basetime to run
  $basetime = 600
  $basesize = 80

  if (!$Timeout) {
    # timelimit will be recomputed later based on number of test nodes
    $timelimit = $basetime
  }
  else {
    $timelimit = $Timeout
  }

  $status = "Connecting to ACM service..."
  ShowProgress $startTime $timelimit $activity -Status $status -id 1

  $args = @{
    ApiBasePoint = $ApiBasePoint
  }
  # Allow unauthenticated access if the ACM service allows.
  if (![string]::IsNullOrEmpty($IssuerUrl)) {
    $args['IssuerUrl'] = $IssuerUrl
    $args['ClientId'] = $ClientId
    $args['ClientSecret'] = $ClientSecret
  }
  $conn = Connect-Acm @args

  $status = "Getting ACM nodes..."
  ShowProgress $startTime $timelimit $activity -Status $status -id 1

  $nodes = Get-AcmNode -Connection $conn -Count 100000
  $nodesInTest = $nodes.where({ $_.Health -eq 'OK' -and $_.State -eq 'Online' })
  $linuxNodeNames = $nodesInTest.where({ $_.NodeRegistrationInfo.DistroInfo -like '*Linux*' }).foreach('Name')
  $winNodeNames = $nodesInTest.where({ $_.NodeRegistrationInfo.DistroInfo -like '*Windows*' }).foreach('Name')
  $names = $linuxNodeNames + $winNodeNames

  Write-Host "There're $($nodes.Count) nodes in the cluster, among which there're $($linuxNodeNames.Count) Linux nodes and $($winNodeNames.Count) Windows nodes good for test."

  if ($names.Count -gt 0) {
    if (!$Timeout) {
      # Recompute timelimit based on node number.
      $timelimit = [Math]::Truncate($names.Count / $basesize) * $basetime
      if ($names.Count % $basesize -gt 0) {
        $timelimit += $basetime
      }
      $timelimit += 60 # Additional time for installation of prerequisites
    }

    # First, install necessary tools
    if ($linuxNodeNames.Count -gt 0) {
      $status = "Installing test prerequisites on Linux nodes..."
      ShowProgress $startTime $timelimit $activity -Status $status -id 1

      $job = Start-AcmDiagnosticJob -Connection $conn -Nodes $linuxNodeNames -Category 'Prerequisite' -Name 'Intel MPI Installation'
      $job = Wait-AcmDiagnosticJob $job $conn $startTime $timelimit $activity $status -progId 1
      if ($job.State -ne 'Finished') {
        throw "Linux prerequisite installation failed."
      }
    }

    if ($winNodeNames.Count -gt 0) {
      $status = "Installing test prerequisites on Windows nodes..."
      ShowProgress $startTime $timelimit $activity -Status $status -id 1

      $job = Start-AcmDiagnosticJob -Connection $conn -Nodes $winNodeNames -Category 'Prerequisite' -Name 'Microsoft MPI Installation'
      $job = Wait-AcmDiagnosticJob $job $conn $startTime $timelimit $activity $status -progId 1
      if ($job.State -ne 'Finished') {
        throw "Windows prerequisite installation timed out."
      }
    }

    # Then, do test
    $status = "Performing test on nodes..."
    ShowProgress $startTime $timelimit $activity -Status $status -id 1

    $job = Start-AcmDiagnosticJob -Connection $conn -Nodes $names -Category 'MPI' -Name 'Pingpong'
    $job = Wait-AcmDiagnosticJob $job $conn $startTime $timelimit $activity $status -progId 1
    if ($job.State -ne 'Finished') {
      throw "Test job failed."
    }

    # Finally, get aggreation result
    $status = "Fetching test aggregation result..."
    ShowProgress $startTime $timelimit $activity -Status $status -id 1

    $testResult = Get-AcmDiagnosticJobAggregationResult -Connection $conn -Id $job.Id
    $testResult = ConvertFrom-JsonNewtonsoft $testResult.ToString()

    # NOTE: Conversion to [string[]] is required, otherwise creating object will fail as
    # it can't find a proper constructor for HashSet type.
    $goodNodes = New-Object -TypeName System.Collections.Generic.HashSet[string] `
      -ArgumentList @(, ($testResult['GoodNodes'] -as [string[]]))
    $goodCount = $goodNodes.Count
  }
  else {
    $goodNodes = $null
    $goodCount = 0
  }

  HideProgress 1

  $status = "Generating result..."
  ShowProgress $startTime $timelimit $activity -Status $status -id 1

  $nodes = $nodes.foreach({
    $val = [ordered]@{
      Name = $_.Name
      InTest = $_.Health -eq 'OK' -and $_.State -eq 'Online'
    }
    if ($goodNodes -ne $null) {
      $val['Good'] = $goodNodes.Contains($_.Name)
    }
    else {
      $val['Good'] = $null
    }
    [PSCustomObject]$val
  })

  $nodes | Sort-Object -Property InTest, Good, Name |
    Format-Table -Wrap -Property `
      @{Name = 'Node'; Expression = {$_.Name}}, `
      @{Name = 'Good for Test'; Expression = {$_.InTest}}, `
      @{Name = 'Good in MPI Pingpong'; Expression = {$_.Good}} | Out-Default

  if ($nodes.Count -gt 0) {
    $summary = [PSCustomObject]@{
      Total = $nodes.Count
      Good = $goodCount
      Percent = "$('{0:0.00}' -f ($goodCount * 100 / $nodes.Count))%"
    }
    $summary | Format-Table -Property Total, Good, Percent -Wrap | Out-Default
  }

  if ($Return) {
    return $nodes
  }
}

# TODO: optional param: app name
function Get-AcmAppInfo {
<#
.SYNOPSIS
Get ACM app/service info for use of Test-AcmCluster.

.PARAMETER ResourceGroup
The name of an Azure resource group containing the ACM service.

.PARAMETER SubscriptionId
The ID of an Azure subscription containing the ResourceGroup.
#>

  param(
    [Parameter(Mandatory = $true)]
    [string] $ResourceGroup,

    [Parameter(Mandatory = $true)]
    [string] $SubscriptionId
  )

  $ErrorActionPreference = 'Stop'

  Prepare-AcmAzureCtx $SubscriptionId | Out-Null

  $app = $(Get-AzWebApp -ResourceGroupName $ResourceGroup)[0]
  $config = Invoke-AzResourceAction -ApiVersion 2016-08-01 -Action list `
    -ResourceGroupName $app.ResourceGroup `
    -ResourceType Microsoft.Web/sites/config `
    -ResourceName "$($app.Name)/authsettings" -Force
  $auth = $config.properties
  return @{
    'IssuerUrl' = $auth.issuer
    'ClientId' = $auth.clientId
    'ClientSecret' = $auth.clientSecret
    'ApiBasePoint' = "https://$($app.DefaultHostName)/v1"
  }
}

function New-AcmTest {
<#
.SYNOPSIS
Add an Azure cluster of VMs/VM scale sets to ACM and perform MPI Pingpong test on it.

.PARAMETER ResourceGroup
The name of an Azure resource group containing the VMs/VM scale sets to test.

.PARAMETER AcmResourceGroup
The name of an Azure resource group containing the ACM service.

.PARAMETER SubscriptionId
The ID of an Azure subscription containing both the ResourceGroup and AcmResourceGroup.

.PARAMETER SetupTimeout
The timeout value for adding cluster to Acm. By default, an estimated value will be set based on the number of VMs/VM scale sets in a cluster. A value shorter than necesssary will fail the setup procedure. You could specify a larger value to ensure the success of setup.

.PARAMETER TestTimeout
The timeout value for performing test on cluster. By default, an estimated value will be set based on the number of nodes in a cluster. A value shorter than necesssary will cause no test result, since the test can't complete without enough time. You could specify a larger value to ensure the test to complete.

.PARAMETER NoSetup
Do not add cluster to ACM but only do test on it. This is for repeated test on a cluster that already has been added to ACM.

.PARAMETER LinuxExtensionUrl
URL for the Linux VM Extension. The file name in the URL must match pattern "^.+\-\d+\.\d+\.\d+\.\d+\.zip$". This option is internal for development.

.PARAMETER WindowsExtensionUrl
URL for the Windows VM Extension. The file name in the URL must match pattern "^.+\-\d+\.\d+\.\d+\.\d+\.zip$". This option is internal for development.

.NOTES
The command will log a lot to screen. So it's better to redirect them to files. Do it like
New-AcmTest ... 2>err_log 6>info_log
It writes errors to file err_log and other information to file info_log.

.EXAMPLE
New-AcmTest -SubscriptionId a486e243-747b-42de-8c4c-379f8295a746 -ResourceGroup 'my-cluster-1' -AcmResourceGroup 'my-acm-cluster' 2>err_log 6>info_log
Perform test on a cluster of VMs/VM scale sets that has not been added to ACM before. It also writes logs to files.

.EXAMPLE
New-AcmTest -SubscriptionId a486e243-747b-42de-8c4c-379f8295a746 -ResourceGroup 'my-cluster-1' -AcmResourceGroup 'my-acm-cluster' -NoSetup 2>err_log 6>info_log
Perform test on a cluster of VMs/VM scale sets that has been added to ACM already.
#>

  param(
    [Parameter(Mandatory = $true)]
    [string] $ResourceGroup,

    [Parameter(Mandatory = $true)]
    [string] $AcmResourceGroup,

    [Parameter(Mandatory = $true)]
    [string] $SubscriptionId,

    [int] $SetupTimeout,

    [int] $TestTimeout,

    [switch] $NoSetup,

    [string] $LinuxExtensionUrl,

    [string] $WindowsExtensionUrl
  )

  if (!$NoSetup) {
    Log "Adding cluster to ACM service..."
    $args = @{
      SubscriptionId = $SubscriptionId
      ResourceGroup = $ResourceGroup
      AcmResourceGroup = $AcmResourceGroup
    }
    if ($SetupTimeout) {
      $args['Timeout'] = $SetupTimeout
    }
    Add-AcmCluster @args
  }

  Log "Getting ACM service app configuration..."
  $app = Get-AcmAppInfo -SubscriptionId $SubscriptionId -ResourceGroup $AcmResourceGroup
  if (!$app['IssuerUrl']) {
    Write-Warning "No authentication configuration is found for the ACM app in $($AcmResourceGroup)!"
  }

  Log "Testing cluster in ACM service..."
  if ($TestTimeout) {
    $app['Timeout'] = $TestTimeout
  }
  Test-AcmCluster @app
}