forked from lucabol/DTLCustomImagesLab
-
Notifications
You must be signed in to change notification settings - Fork 2
/
New-EmptyLabs.ps1
100 lines (76 loc) · 4.83 KB
/
New-EmptyLabs.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
param
(
[Parameter(Mandatory=$false, HelpMessage="Configuration File, see example in directory")]
[ValidateNotNullOrEmpty()]
[string] $ConfigFile = "config.csv",
[Parameter(Mandatory=$false, HelpMessage="How many seconds to wait before starting the next parallel lab creation")]
[int] $SecondsBetweenLoop = 15,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory=$false, HelpMessage="Custom Role to add users to")]
[string] $CustomRole = "No VM Creation User"
)
$ErrorActionPreference = "Stop"
# Common setup for scripts
. "./Utils.ps1" # Import all our utilities
$config = Import-ConfigFile -ConfigFile $ConfigFile # Import all the lab settings from the config file
$config | ForEach-Object {
# Create any/all the resource groups
# The SilentlyContinue bit is to suppress the error that otherwise this generates.
$existingRg = Get-AzResourceGroup -Name $_.ResourceGroupName -Location $_.LabRegion -ErrorAction SilentlyContinue
if(-not $existingRg) {
Write-Host "Creating Resource Group '$($_.ResourceGroupName)' ..." -ForegroundColor Green
New-AzResourceGroup -Name $_.ResourceGroupName -Location $_.LabRegion | Out-Null
}
# If specified create any/all the resource groups where VMs should be created
if ($_.VmCreationResourceGroupName) {
$existingVmCreationRg = Get-AzResourceGroup -Name $_.VmCreationResourceGroupName -Location $_.LabRegion -ErrorAction SilentlyContinue
if(-not $existingVmCreationRg) {
Write-Host "Creating VM Creation Resource Group '$($_.VmCreationResourceGroupName)' for lab '$($_.DevTestLabName)' ..." -ForegroundColor Green
New-AzResourceGroup -Name $_.VmCreationResourceGroupName -Location $_.LabRegion | Out-Null
}
}
}
$configCount = ($config | Measure-Object).Count
Write-Host "---------------------------------" -ForegroundColor Green
Write-Host "Creating $configCount labs..." -ForegroundColor Green
$LabCreateSB = {
param($labConfig, $customRole, $utilities)
# Make sure we stop for errors
$ErrorActionPreference = "Stop"
# Reimport the utilities
if (-not (Get-Command -Name "Set-LabAccessControl" -ErrorAction SilentlyContinue)) {
. $utilities # Import all our utilities if needed
}
Write-Output "Creating Lab $($labConfig.DevTestLabName) in Resource group $($labConfig.ResourceGroupName)"
$lab = $labConfig | New-AzDtlLab -VmCreationSubnetPrefix "10.0.0.0/21" -VmCreationResourceGroupName $labConfig.VmCreationResourceGroupName
Write-Output " Updating shutdown policy for lab $($labConfig.DevTestLabName)"
$lab = $labConfig | Set-AzDtlLabShutdown
Write-Output " Connecting Shared Image Gallery to lab $($labConfig.DevTestLabName)"
$SharedImageGallery = Get-AzGallery -Name $labConfig.SharedImageGalleryName
if (-not $SharedImageGallery) {
Throw "Unable to update lab '$($labConfig.DevTestLabName)', '$($labConfig.SharedImageGalleryName)' shared image gallery does not exist."
}
$sharedImageGallery = $labConfig | Get-AzDtlLab | Set-AzDtlLabSharedImageGallery -Name $labConfig.SharedImageGalleryName -ResourceId $SharedImageGallery.Id
Write-Output " Updating IP policy to $($labConfig.IpConfig) for lab $($labConfig.DevTestLabName)"
$result = Set-AzDtlLabIpPolicy -Lab $labConfig -IpConfig $labConfig.IpConfig
Write-Output " Adding owners & users for lab $($labConfig.DevTestLabName)"
$result = Set-LabAccessControl $labConfig.DevTestLabName $labConfig.ResourceGroupName $CustomRole $labConfig.LabOwners $labConfig.LabUsers
Write-Output "Completed creating lab $($labConfig.DevTestLabName) in Resource group $($labConfig.ResourceGroupName)"
}
$labCreateJobs = @()
$config | ForEach-Object {
$labCreateJobs += Start-RSJob -Name "$($_.DevTestLabName)-JobId$(Get-Random)" -ScriptBlock $LabCreateSB -ArgumentList $_, $CustomRole, ((Resolve-Path ".\Utils.ps1").Path) -ModulesToImport $AzDtlModulePath
Start-Sleep -Seconds $SecondsBetweenLoop
}
# We wait additional hour for every 10 jobs, starting at 4 hours
$timeout = 4 + [int] ($configCount / 10)
Wait-RSJobWithProgress -secTimeout ($timeout*60*60) -jobs $labCreateJobs
$configBastion = [Array] ($config | Where-Object { $_.BastionEnabled })
if (($configBastion | Measure-Object).Count -gt 0) {
# Deploy the Azure Bastion hosts to the labs
Write-Host "---------------------------------" -ForegroundColor Green
Write-Host "Deploying $(($configBastion | Measure-Object).Count) Bastion hosts to the labs..." -ForegroundColor Green
# Currently use Leave strategy for existing Bastions
"./Deploy-Bastion.ps1" | Invoke-RSForEachLab -ConfigFile $ConfigFile -SecondsBetweenLoop $SecondsBetweenLoop -SecTimeout (8 * 60 * 60) -CustomRole $null -ModulesToImport $AzDtlModulePath
}
Write-Host "Completed creating labs!" -ForegroundColor Green