-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_aci_settingsfile.ps1
150 lines (130 loc) · 5.68 KB
/
create_aci_settingsfile.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
param
(
[string] $sbmExe = "sbm.exe",
[string] $path,
[string] $resourceGroupName,
[string] $keyVaultName,
[string] $aciName,
[string] $imageTag,
[string] $containerRegistryName,
[string] $storageAccountName,
[string] $eventHubNamespaceName,
[string] $serviceBusNamespaceName,
[string] $identityName,
[string] $identityClientId,
[string] $vnetName = "",
[string] $subnetName = "",
[string] $sqlUserName,
[string] $sqlPassword,
[ValidateSet("Password", "ManagedIdentity", "Both")]
[string] $authType = "Both"
)
Write-Host "Create ACI settings file" -ForegroundColor Cyan
$path = Resolve-Path $path
Write-Host "Output path set to $path" -ForegroundColor DarkGreen
Write-Host "Retrieving keys from resources in $resourceGroupName" -ForegroundColor DarkGreen
if($authType -eq "Both")
{
$authTypes = @("Password", "ManagedIdentity")
}
else
{
$authTypes = @($authType)
}
$haveSqlInfo = $true
if([string]::IsNullOrWhiteSpace($sqlUserName) -or [string]::IsNullOrWhiteSpace($sqlPassword))
{
$haveSqlInfo = $false
}
$tenantId = az account show -o tsv --query tenantId
$storageAcctKey = (az storage account keys list --account-name $storageAccountName -o tsv --query '[].value')[0]
$eventHubName = az eventhubs eventhub list --resource-group $resourceGroupName --namespace-name $eventhubNamespaceName -o tsv --query "[?contains(@.name '$prefix')].name"
$eventHubAuthRuleName = az eventhubs eventhub authorization-rule list --resource-group $resourceGroupName --namespace-name $eventhubNamespaceName --eventhub-name $eventHubName -o tsv --query [].name
$eventHubConnectionString = az eventhubs eventhub authorization-rule keys list --resource-group $resourceGroupName --namespace-name $eventHubNamespaceName --eventhub-name $eventHubName --name $eventHubAuthRuleName -o tsv --query "primaryConnectionString"
$serviceBusTopicAuthRuleName = az servicebus topic authorization-rule list --resource-group $resourceGroupName --namespace-name $serviceBusNamespaceName --topic-name "sqlbuildmanager" -o tsv --query "[].name"
$serviceBusConnectionString = az servicebus topic authorization-rule keys list --resource-group $resourceGroupName --namespace-name $serviceBusNamespaceName --topic-name "sqlbuildmanager" --name $serviceBusTopicAuthRuleName -o tsv --query "primaryConnectionString"
if([string]::IsNullOrWhiteSpace($containerRegistryName))
{
$baseFileName = Join-Path $path "settingsfile-aci-no-registry"
}
else {
$baseFileName = Join-Path $path "settingsfile-aci"
}
$subscriptionId = az account show --query id --output tsv
if([string]::IsNullOrWhiteSpace($containerRegistryName) -eq $false)
{
Write-Host "Getting info for Azure Conainer Registry: $containerRegistryName" -ForegroundColor DarkGreen
$acrUserName = az acr credential show -g $resourceGroupName --name $containerRegistryName -o tsv --query username
$acrPassword = az acr credential show -g $resourceGroupName --name $containerRegistryName -o tsv --query passwords[0].value
$acrServerName = az acr show -g $resourceGroupName --name $containerRegistryName -o tsv --query loginServer
}
$keyFile = Join-Path $path "settingsfilekey.txt"
if($false -eq (Test-Path $keyFile))
{
$AESKey = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($AESKey)
$settingsFileKey = [System.Convert]::ToBase64String($AESKey);
$settingsFileKey | Set-Content -Path $keyFile
}
$sbAndEhArgs =
foreach($auth in $authTypes)
{
$params = @("aci", "savesettings")
if($auth -eq "ManagedIdentity" )
{
$settingsAci =$baseFileName + "-mi.json"
$sbAndEhArgs = @("-sb", $serviceBusNamespaceName)
$sbAndEhArgs += ("-eh","$($eventhubNamespaceName)|$($eventHubName)")
}
else
{
$settingsAci = $baseFileName + ".json"
$sbAndEhArgs = @("-sb", """$serviceBusConnectionString""")
$sbAndEhArgs += ("-eh","""$eventHubConnectionString""")
$params += ("--storageaccountkey",$storageAcctKey)
}
if(Test-Path $settingsAci)
{
Remove-Item $settingsAci
}
Write-Host "Saving settings file to $settingsAci" -ForegroundColor DarkGreen
$params += ("--settingsfile", "$($settingsAci)")
$params += ("--aciname", $aciName)
$params += ("--identityname", $identityName)
$params += ("--clientid", $identityClientId)
$params += ("--idrg", $resourceGroupName)
$params += ("--acirg", $resourceGroupName)
$params += @("--tenantid", $tenantId)
$params += ("-kv", $keyVaultName)
$params += ("--storageaccountname", $storageAccountName)
$params += ("--defaultscripttimeout", "500")
$params += ("--subscriptionid",$subscriptionId)
$params += ("--force")
$params += ("--eventhublogging", "ScriptErrors")
$params += ("--ehrg", $resourceGroupName)
$params += ("--ehsub", $subscriptionId)
if($haveSqlInfo)
{
$params += ("--username", $sqlUserName)
$params += ("--password", $sqlPassword)
}
if([string]::IsNullOrWhiteSpace($containerRegistryName) -eq $false)
{
$params += ("--registryserver", $acrServerName)
$params += ("--registryusername", $acrUserName)
$params += ("--registrypassword", $acrPassword)
}
if([string]::IsNullOrWhiteSpace($imageTag) -eq $false)
{
$params += ("--imagetag", $imageTag)
}
if($vnetName -ne "" -and $subnetName -ne "")
{
$params += ("--vnetname", $vnetName)
$params += ("--subnetname", $subnetName)
$params += ("--vnetrg", $resourceGroupName)
}
$params += ("--authtype", $auth)
Write-Host $params $sbAndEhArgs -ForegroundColor DarkYellow
Start-Process $sbmExe -ArgumentList ($params + $sbAndEhArgs) -Wait -NoNewWindow
}