-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_azure_resources.ps1
300 lines (260 loc) · 10.2 KB
/
create_azure_resources.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
param(
[Parameter(Mandatory=$True)]
[string]
$prefix,
[Parameter(Mandatory=$True)]
[string]
$location = "East US",
[string]
$outputPath = "..\..\src\TestConfig",
[int]
$testDatabaseCount = 10,
[Parameter()]
[string[]]
[ValidateSet("AKS", "ContainerApp", "Batch", "ACI", "All", "None")]
$deploy = "All",
[Parameter()]
[string[]]
[ValidateSet("AKS", "ContainerApp", "Batch", "ACI", "All", "None")]
$settingsFile = ""
)
#############################################
# Set variable defaults to $false
#############################################
$deployAks = $false
$deployBatch = $false
$build = $false
$deployContainerAppEnv = $false
$deployContainerRegistry = $false
$settingsFileAks = $false
$settingsFileBatch = $false
$settingsFileContainerApp = $false
$settingsFileAci = $false
$shouldDeploy = $false
$shouldDeploy = (-Not $deploy.Contains("None"))
if($shouldDeploy)
{
if($deploy.Contains("AKS") -or $deploy.Contains("All"))
{
$deployAks = $true
$deployContainerRegistry = $true
if(-Not $settingsFile.Contains("None")) { $settingsFileAks = $true}
}
if($deploy.Contains("Batch") -or $deploy.Contains("All"))
{
$deployBatch = $true
$build = $true
if(-Not $settingsFile.Contains("None")) { $settingsFileBatch = $true}
}
if($deploy.Contains("ContainerApp") -or $deploy.Contains("All"))
{
$deployContainerAppEnv = $true
$deployContainerRegistry = $true
if(-Not $settingsFile.Contains("None")) { $settingsFileContainerApp = $true}
}
if($deploy.Contains("ACI") -or $deploy.Contains("All"))
{
$deployContainerRegistry = $true
if(-Not $settingsFile.Contains("None")) { $settingsFileAci = $true}
}
}
if(-Not $settingsFile.Contains("None"))
{
if($settingsFile.Contains("AKS") -or $settingsFile.Contains("All"))
{
$settingsFileAks = $true
}
if($settingsFile.Contains("Batch") -or $settingsFile.Contains("All"))
{
$settingsFileBatch = $true
}
if($settingsFile.Contains("ContainerApp") -or $settingsFile.Contains("All"))
{
$settingsFileContainerApp = $true
}
if($settingsFile.Contains("ACI") -or $settingsFile.Contains("All"))
{
$settingsFileAci = $true
}
}
#############################################
# Get set resource name variables from prefix
#############################################
. ./prefix_resource_names.ps1 -prefix $prefix
. ./key_file_names.ps1 -prefix $prefix -path $outputPath
$targetFramework = .\get_targetframework.ps1
if($false -eq (Test-Path $outputPath))
{
New-Item -Path $outputPath -ItemType Directory
}
$outputPath = Resolve-Path $outputPath
Write-Host "Will be saving output files to $outputPath" -ForegroundColor Green
if($shouldDeploy)
{
################
# Resource Group
################
Write-Host "Creating Resourcegroup: $ResourceGroupName" -ForegroundColor Cyan
az group create --name $resourceGroupName --location $location -o table
############################################################################################
# Storage Account, Event Hub and Service Bus Topic, Key Vault, Identity and RBAC Assignments
############################################################################################
$ipAddress = (Invoke-WebRequest https://api.ipify.org/?format=text).Content.Trim()
Write-Host "Using IP Address: $ipAddress" -ForegroundColor Green
$userIdGuid = az ad signed-in-user show -o tsv --query id
Write-Host "Using User Id GUID: $userIdGuid" -ForegroundColor Green
if("" -eq $sqlUserName -or "" -eq $sqlPassword -or $null -eq $sqlUserName -or $null -eq $sqlPassword)
{
if($testDatabaseCount -ge 0) {
Write-Host "SQL Username or Password is empty. Canceling deployment" -ForegroundColor Red
Exit
}
}
Write-Host "Deplpoying Azure resources: Virtual Network, Subnets, Storage Account, Event Hub, Service Bus Topic, Key Vault, Identity and RBAC Assignments" -ForegroundColor Cyan
if($deployAks) {Write-Host "Deploying AKS Cluster" -ForegroundColor Cyan} else {Write-Host "Skipping AKS deployment" -ForegroundColor DarkBlue}
if($deployBatch) {Write-Host "Deploying Batch Account" -ForegroundColor Cyan} else {Write-Host "Skipping Batch deployment" -ForegroundColor DarkBlue}
if($deployContainerRegistry) {Write-Host "Deploying Container Registry" -ForegroundColor Cyan} else {Write-Host "Skipping Container Registry deployment" -ForegroundColor DarkBlue}
if($deployContainerAppEnv) {Write-Host "Deploying Container App Env" -ForegroundColor Cyan} else {Write-Host "Skipping Container App Env deployment" -ForegroundColor DarkBlue}
if($testDatabaseCount -ge 0) {Write-Host "Deploying $testDatabaseCount Test Databases" -ForegroundColor Cyan} else {Write-Host "Skipping Test database deployment" -ForegroundColor DarkBlue}
$deployStatus = az deployment group create --resource-group $resourceGroupName --template-file azuredeploy_main.bicep `
--parameters `
namePrefix="$prefix" `
currentIpAddress=$ipAddress `
userIdGuid=$userIdGuid `
sqladminname=$sqlUserName `
sqladminpassword=$sqlPassword `
deployBatchAccount=$deployBatch `
deployContainerRegistry=$deployContainerRegistry `
deployContainerAppEnv=$deployContainerAppEnv `
deployAks=$deployAks `
testDbCountPerServer=$testDatabaseCount `
if($LASTEXITCODE){
Write-Host "Deployment failed with status: $($deployStatus)" -ForegroundColor Red
exit
}else
{
Write-Host "Azure resource deployment succeeded" -ForegroundColor Green
}
}
else {
Write-Host "Skipping Azure resource deployment" -ForegroundColor DarkBlue
}
########################################
# Container Registry and Container Build
########################################
if($deployContainerRegistry)
{
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
.$scriptDir/ContainerRegistry/build_container_registry_image_fromprefix.ps1 -resourceGroupName $resourceGroupName -prefix $prefix -wait $false -path $outputPath
}
else
{
Write-Host "Skipping Container Build" -ForegroundColor DarkBlue
}
#################
# AKS
#################
if($deployAks)
{
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
.$scriptDir/Kubernetes/create_aks_cluster.ps1 -prefix $prefix -resourceGroupName $resourceGroupName -includeContainerRegistry $deployContainerRegistry -path $outputPath
}
else
{
Write-Host "Skipping AKS deployment" -ForegroundColor DarkBlue
}
#########################
# Build Code?
#########################
if($build -and $deployBatch)
{
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
.$scriptDir/Batch/build_and_upload_batch_fromprefix.ps1 -resourceGroupName $resourceGroupName -prefix $prefix -path $outputPath -action BuildAndUpload
}
else
{
Write-Host "Skipping code build" -ForegroundColor DarkBlue
}
#############################################################
# Save the settings files and config files for the unit tests
#############################################################
$sbmExe = (Resolve-Path "..\..\src\SqlBuildManager.Console\bin\Debug\$targetFramework\sbm.exe").Path
##########################
# Add Secrets to Key Vault
##########################
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
.$scriptDir/KeyVault/add_secrets_to_keyvault_fromprefix.ps1 -path $outputPath -resourceGroupName $resourceGroupName -prefix $prefix
#########################
# Database override files
#########################
if($testDatabaseCount -gt 0)
{
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
.$scriptDir/Database/create_database_override_files.ps1 -prefix $prefix -path $outputPath -resourceGroupName $resourceGroupName
}
if($settingsFileAks)
{
##############################
# Create AKS Settings files
##############################
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
.$scriptDir/kubernetes/create_aks_settingsfile_fromprefix.ps1 -path $outputPath -resourceGroupName $resourceGroupName -prefix $prefix
}
else
{
Write-Host "Skipping AKS settings files" -ForegroundColor DarkBlue
}
#################################
# Settings File for Container App
#################################
if($settingsFileContainerApp)
{
# Create test file referencing the
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
.$scriptDir/ContainerApp/create_containerapp_settingsfile_fromprefix_all.ps1 -prefix $prefix
}
else
{
Write-Host "Skipping Container App Environment settings files" -ForegroundColor DarkBlue
}
#########################
# Settings File for Batch
#########################
if($settingsFileBatch)
{
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
.$scriptDir/Batch/create_batch_settingsfiles_fromprefix.ps1 -sbmExe $sbmExe -path $outputPath -resourceGroupName $resourceGroupName -prefix $prefix
}
else
{
Write-Host "Skipping Batch Account settings files" -ForegroundColor DarkBlue
}
#######################
# Settings File for ACI
#######################
if($settingsFileAci)
{
$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path
.$scriptDir/aci/create_aci_settingsfile_fromprefix.ps1 -sbmExe $sbmExe -path $outputPath -resourceGroupName $resourceGroupName -prefix $prefix
}
else
{
Write-Host "Skipping ACI settings files" -ForegroundColor DarkBlue
}
###########################################
# Save to settings files to a prefix folder
###########################################
if(-Not $settingsFile.Contains("None"))
{
$prefixFolder = Join-Path $outputPath $prefix
if($false -eq (Test-Path $prefixFolder))
{
New-Item -Path $prefixFolder -ItemType Directory
}
$prefixFolder = Resolve-Path $prefixFolder
Write-Host "Copying settings and config files to $prefixFolder" -ForegroundColor DarkCyan
Copy-Item -Path "$outputPath\*.json" -Destination $prefixFolder -Force
Copy-Item -Path "$outputPath\*.cfg" -Destination $prefixFolder -Force
Copy-Item -Path "$outputPath\*.txt" -Destination $prefixFolder -Force
}
Write-Host "COMPLETED! - Azure resources have been created." -ForegroundColor DarkCyan