-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdeploy.ps1
365 lines (303 loc) · 15 KB
/
deploy.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Write-Host "Ensuring Azure dependencies are installed."
if (!(Get-Module -Name Az)) {
Write-Host "Installing Az PowerShell..."
Install-Module -Name Az
Import-Module -Name Az
}
if (!(Get-Module -Name Az.Search)) {
Write-Host "Installing Az.Search PowerShell..."
Install-Module -Name Az.Search
Import-Module -Name Az.Search
}
Write-Host @"
------------------------------------------------------------
Guidance for choosing parameters for resource deployment:
uniqueName: Choose a name that is globally unique and less than 12 characters. This name will be used as a prefix for the resources created and the resultant name must not confict with any other Azure resource.
Ex: FabrikamTestPilot1
resourceGroup: Please create a resource group in your Azure account and retreive it's resource group name.
Ex: testpilotresourcegroup
subscriptionId: Your subscription id.
Ex: 123456-7890-1234-5678-9012345678
------------------------------------------------------------
"@
function Deploy
{
# Read parameters from user.
Write-Host "Press enter to use [default] value."
Write-Host "For uniqueName, please enter a string with 10 or less characters."
while (!($uniqueName = Read-Host "uniqueName")) { Write-Host "You must provide a uniqueName."; }
while (!($resourceGroupName = Read-Host "resourceGroupName")) { Write-Host "You must provide a resourceGroupName."; }
while (!($subscriptionId = Read-Host "subscriptionId")) { Write-Host "You must provide a subscriptionId."; }
$defaultLocation = "usgovvirginia"
if (!($location = Read-Host "location [$defaultLocation]")) { $location = $defaultLocation }
$defaultSearchSku = "basic"
if (!($searchSku = Read-Host "searchSku [$defaultSearchSku]")) { $searchSku = $defaultSearchSku }
# Generate derivative parameters.
$searchServiceName = $uniqueName + "search";
$webappname = $uniqueName + "app";
$cogServicesName = $uniqueName + "cog";
$appInsightsName = $uniqueName + "insights";
$storageAccountName = $uniqueName + "str";
$storageContainerName = "documents";
$dataSourceName = $uniqueName + "-datasource";
$skillsetName = $uniqueName + "-skillset";
$indexName = $uniqueName + "-index";
$indexerName = $uniqueName + "-indexer";
# These values are extracted by this process automatically. Do not set values here.
$global:storageAccountKey = "";
$global:searchServiceKey = "";
$global:storageConnectionString = "";
$global:cogServicesKey = "";
function ValidateParameters
{
Write-Host "------------------------------------------------------------";
Write-Host "Here are the values of all parameters:";
Write-Host "uniqueName: '$uniqueName'";
Write-Host "resourceGroupName: '$resourceGroupName'";
Write-Host "subscriptionId: '$subscriptionId'";
Write-Host "location: '$location'";
Write-Host "searchSku: '$searchSku'";
Write-Host "searchServiceName: '$searchServiceName'";
Write-Host "webappname: '$webappname'";
Write-Host "cogServicesName: '$cogServicesName'";
Write-Host "appInsightsName: '$appInsightsName'";
Write-Host "storageAccountName: '$storageAccountName'";
Write-Host "storageContainerName: '$storageContainerName'";
Write-Host "dataSourceName: '$dataSourceName'";
Write-Host "skillsetName: '$skillsetName'";
Write-Host "indexName: '$indexName'";
Write-Host "indexerName: '$indexerName'";
Write-Host "------------------------------------------------------------";
}
ValidateParameters;
function Signin
{
# Sign in
Write-Host "Logging in for '$subscriptionId'";
Connect-AzAccount -EnvironmentName AzureUSGovernment;
# Select subscription
Write-Host "Selecting subscription '$subscriptionId'";
Select-AzSubscription -SubscriptionID $subscriptionId;
}
Signin;
function PrepareSubscription
{
# Register RPs
$resourceProviders = @("microsoft.cognitiveservices", "microsoft.insights", "microsoft.search", "microsoft.storage");
if ($resourceProviders.length) {
Write-Host "Registering resource providers"
foreach ($resourceProvider in $resourceProviders) {
Register-AzResourceProvider -ProviderNamespace $resourceProvider;
}
}
}
PrepareSubscription;
function FindOrCreateResourceGroup
{
# Create or check for existing resource group
$resourceGroup = Get-AzResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
if (!$resourceGroup) {
Write-Host "Resource group '$resourceGroupName' does not exist.";
if (!$location) {
$location = Read-Host "please enter a location:";
}
Write-Host "Creating resource group '$resourceGroupName' in location '$location'";
New-AzResourceGroup -Name $resourceGroupName -Location $location
}
else {
Write-Host "Using existing resource group '$resourceGroupName'";
}
}
FindOrCreateResourceGroup;
function CreateStorageAccountAndContainer
{
# Create a new storage account
Write-Host "Creating Storage Account";
# Create the resource using the API
$storageAccount = New-AzStorageAccount `
-ResourceGroupName $resourceGroupName `
-Name $storageAccountName `
-Location $location `
-SkuName Standard_LRS `
-Kind StorageV2
$global:storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName)[0].Value
$global:storageConnectionString = 'DefaultEndpointsProtocol=https;AccountName=' + $storageAccountName + ';AccountKey=' + $global:storageAccountKey + ';EndpointSuffix=core.usgovcloudapi.net'
Write-Host "Storage Account Key: '$global:storageAccountKey'";
$storageContext = New-AzStorageContext `
-StorageAccountName $storageAccountName `
-StorageAccountKey $global:storageAccountKey
Write-Host "Creating Storage Container";
$storageContainer = New-AzStorageContainer `
-Name $storageContainerName `
-Context $storageContext `
-Permission Off
Write-Host "Uploading sample documents directory";
$filepath= "../sample_documents"
foreach($file in Get-ChildItem $filepath)
{
Set-AzStorageBlobContent -File $file.FullName -Container $storageContainerName -Properties @{"ContentType" = [System.Web.MimeMapping]::GetMimeMapping($file.FullName);} -Context $storageContext -Force
}
}
CreateStorageAccountAndContainer;
function CreateSearchServices
{
# Create a cognitive services resource
Write-Host "Creating Cognitive Services";
$cogServices = New-AzCognitiveServicesAccount `
-ResourceGroupName $resourceGroupName `
-Name $cogServicesName `
-Location $location `
-SkuName S0 `
-Type CognitiveServices
$global:cogServicesKey = (Get-AzCognitiveServicesAccountKey -ResourceGroupName $resourceGroupName -name $cogServicesName).Key1
Write-Host "Cognitive Services Key: '$global:cogServicesKey'";
# Create a new search service
# Alternatively, you can now use the Az.Search module: https://docs.microsoft.com/en-us/azure/search/search-manage-powershell
Write-Host "Creating Search Service";
$searchService = New-AzSearchService `
-ResourceGroupName $resourceGroupName `
-Name $searchServiceName `
-Sku $searchSku -Location $location `
-PartitionCount 1 `
-ReplicaCount 1
$global:searchServiceKey = (Get-AzSearchAdminKeyPair -ResourceGroupName $resourceGroupName -ServiceName $searchServiceName).Primary
Write-Host "Search Service Key: '$global:searchServiceKey'";
}
CreateSearchServices;
function CreateSearchIndex
{
Write-Host "Creating Search Index";
function CallSearchAPI
{
param (
[string]$url,
[string]$body
)
$headers = @{
'api-key' = $global:searchServiceKey
'Content-Type' = 'application/json'
'Accept' = 'application/json'
}
$baseSearchUrl = "https://"+$searchServiceName+".search.azure.us"
$fullUrl = $baseSearchUrl + $url
Write-Host "Calling api: '"$fullUrl"'";
Invoke-RestMethod -Uri $fullUrl -Headers $headers -Method Put -Body $body | ConvertTo-Json
};
# Create the datasource
$dataSourceBody = Get-Content -Path .\templates\base-datasource.json
$dataSourceBody = $dataSourceBody -replace "{{env_storage_connection_string}}", $global:storageConnectionString
$dataSourceBody = $dataSourceBody -replace "{{env_storage_container}}", $storageContainerName
CallSearchAPI -url ("/datasources/"+$dataSourceName+"?api-version=2019-05-06") -body $dataSourceBody
# Create the skillset
$skillBody = Get-Content -Path .\templates\base-skills.json
$skillBody = $skillBody -replace "{{cog_services_key}}", $global:cogServicesKey
CallSearchAPI -url ("/skillsets/"+$skillsetName+"?api-version=2019-05-06") -body $skillBody
# Create the index
$indexBody = Get-Content -Path .\templates\base-index.json
CallSearchAPI -url ("/indexes/"+$indexName+"?api-version=2019-05-06") -body $indexBody
# Create the indexer
$indexerBody = Get-Content -Path .\templates\base-indexer.json
$indexerBody = $indexerBody -replace "{{datasource_name}}", $dataSourceName
$indexerBody = $indexerBody -replace "{{skillset_name}}", $skillsetName
$indexerBody = $indexerBody -replace "{{index_name}}", $indexName
CallSearchAPI -url ("/indexers/"+$indexerName+"?api-version=2019-05-06") -body $indexerBody
}
CreateSearchIndex;
function CreateWebApp
{
# Create an App Service plan
Write-Host "Creating App Service Plan";
$appService = New-AzAppServicePlan `
-Name $webappname `
-Location $location `
-ResourceGroupName $resourceGroupName `
-Tier Standard
# Create a web app.
Write-Host "Creating Web App";
$webApp = New-AzWebApp `
-Name $webappname `
-Location $location `
-AppServicePlan $webappname `
-ResourceGroupName $resourceGroupName `
-WarningAction SilentlyContinue
# Create an application insights instance
Write-Host "Creating App Insights";
$appInsights = New-AzResource `
-ResourceName $appInsightsName `
-ResourceGroupName $resourceGroupName `
-Tag @{ applicationType = "web"; applicationName = $webappname } `
-ResourceType "Microsoft.Insights/components" `
-Location $location `
-PropertyObject @{"Application_Type" = "web" } `
-Force
# Setting App Insights Key in Web app
Write-Host "Configuring the Web App";
$appSetting = @{'APPINSIGHTS_INSTRUMENTATIONKEY' = $appInsights.Properties.InstrumentationKey }
$updateSettings = Set-AzWebApp `
-Name $webappname `
-ResourceGroupName $resourceGroupName `
-AppSettings $appSetting
}
CreateWebApp;
function PrintAppsettings
{
Write-Host "Copy and paste the following values to update the appsettings.json file described in the next folder:";
Write-Host "------------------------------------------------------------";
Write-Host "SearchServiceName: '$searchServiceName'";
Write-Host "SearchApiKey: '$global:searchServiceKey'";
Write-Host "SearchIndexName: '$indexName'";
Write-Host "SearchIndexerName: '$indexerName'";
Write-Host "StorageAccountName: '$storageAccountName'";
Write-Host "StorageAccountKey: '$global:storageAccountKey'";
$StorageContainerAddress = ("https://"+$storageAccountName+".blob.core.usgovcloudapi.net/"+$storageContainerName)
Write-Host "StorageContainerAddress: '$StorageContainerAddress'";
Write-Host "StorageContainerAddress2: '$StorageContainerAddress2'";
Write-Host "StorageContainerAddress3: '$StorageContainerAddress3'";
Write-Host "KeyField: '$KeyField'";
Write-Host "IsPathBase64Encoded: '$IsPathBase64Encoded'";
Write-Host "SearchApiVersion: '$SearchApiVersion'";
Write-Host "InstrumentationKey: '$InstrumentationKey'";
Write-Host "AzureMapsSubscriptionKey: '$AzureMapsSubscriptionKey'";
Write-Host "GraphFacet: '$GraphFacet'";
Write-Host "Customizable: '$Customizable'";
Write-Host "OrganizationName: '$OrganizationName'";
Write-Host "OrganizationLogo: '$OrganizationLogo'";
Write-Host "OrganizationWebSiteUrl: '$OrganizationWebSiteUrl'";
Write-Host "------------------------------------------------------------";
}
PrintAppsettings;
function DeployWebUICode
{
Set-Location "..\02 - Web UI Template\CognitiveSearch.UI"
dotnet publish
Set-Location ".\bin\Debug\netcoreapp3.1\publish\"
$json = Get-Content .\appsettings.json | ConvertFrom-Json
$json.SearchServiceName="$searchServiceName"
$json.SearchApiKey="$global:searchServiceKey"
$json.SearchIndexName="$indexName"
$json.SearchIndexerName="$indexerName"
$json.StorageAccountName="$storageAccountName"
$json.StorageAccountKey="$global:storageAccountKey"
$StorageContainerAddress = ("https://"+$storageAccountName+".blob.core.usgovcloudapi.net/"+$storageContainerName)
$json.StorageContainerAddress="$StorageContainerAddress"
$json.StorageContainerAddress2="https://{storage-account-name}.blob.core.usgovcloudapi.net/{container-name}"
$json.StorageContainerAddress3="https://{storage-account-name}.blob.core.usgovcloudapi.net/{container-name}"
$json.KeyField="metadata_storage_path"
$json.SearchApiVersion="2020-06-30"
$json.InstrumentationKey="$InstrumentationKey"
$json.AzureMapsSubscriptionKey="$AzureMapsSubscriptionKey"
$json.GraphFacet="$GraphFacet"
$json.Customizable="true"
$json.OrganizationName="Microsoft"
$json.OrganizationLogo="~/images/logo.png"
$json.OrganizationWebSiteUrl="https://www.microsoft.com"
$json | ConvertTo-Json | Set-Content .\appsettings.json
Compress-Archive * ..\..\CognativeSearchUI.zip -Force
Set-location "..\.."
Publish-AzWebApp -ResourceGroupName $resourceGroupName -Name $webappname -ArchivePath $pwd\CognativeSearchUI.zip
}
DeployWebUICode;
}
Deploy;