-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_azure.sh
235 lines (215 loc) · 9.71 KB
/
setup_azure.sh
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
#!/bin/bash
# Variable block
location="West Europe"
shortLocation="westeurope"
resourceGroup="localink-rg"
tag="localink"
account="localink-account-cosmos" #needs to be lower case
database="main"
user_container="users"
partitionKey="/id"
searchName='localink-search'
searchDataSourceName='localink-datasource'
searchIndexName='localink-search-index'
searchIndexerName='localink-indexer'
storageAccountName="localinkstorage"
functionAuthName="localink-auth"
functionGenerateEmbeddingsName="localink-generate-embeddings"
functionMatchName="localink-match"
functionMeetName="localink-meet"
functionQueryName="localink-query"
functionSyncPositionName="localink-sync-position"
usage() { echo "Usage: $0 [-u] [-f]" 1>&2; exit 1; }
while getopts "uf" o; do
case "${o}" in
u)
echo "Deleting resources..."
az group delete --name $resourceGroup
exit 0
;;
f)
echo "Creating function auth"
az storage account create --name "$storageAccountName" --location "$location" --resource-group "$resourceGroup" --sku Standard_LRS --allow-blob-public-access false
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionAuthName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
echo "Creating function generateEmbeddings"
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionGenerateEmbeddingsName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
echo "Creating function match"
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionMatchName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
echo "Creating function meet"
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionMeetName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
echo "Creating function query"
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionQueryName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
echo "Creating function syncPosition"
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionSyncPositionName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
exit 0
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
echo "Configuring required Azure services..."
# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag
# Cosmos DB
echo "Configuring Cosmos DB resources..."
# Create a Cosmos account for SQL API
echo "Creating Cosmos DB account"
az cosmosdb create --name $account --resource-group $resourceGroup --default-consistency-level Eventual --locations regionName="$location" failoverPriority=0 isZoneRedundant=False --capabilities EnableServerless
# Create a SQL API database
echo "Creating $database database"
az cosmosdb sql database create --account-name $account --resource-group $resourceGroup --name $database
# Create a SQL API user_container
echo "Creating $user_container with $partitionKey"
az cosmosdb sql container create --account-name $account --resource-group $resourceGroup --database-name $database --name $user_container --partition-key-path $partitionKey
# Azure Cognitive Search
echo "Creating search service"
az search service create --name $searchName --resource-group $resourceGroup --sku Free
# Fetch generated primary admin key
echo "Fetching the admin key for subsequent data calls"
adminKey=$(az search admin-key show --resource-group $resourceGroup --service-name $searchName --query primaryKey --out tsv)
adminKey="${adminKey%$'\r'}"
echo "Creating search index"
jsonBody=$(cat <<EOF
{
"name": "$searchIndexName",
"fields": [{
"name": "id",
"type": "Edm.String",
"key": true,
"searchable": false
},
{
"name": "description",
"type": "Edm.String",
"filterable": false,
"searchable": true,
"sortable": false,
"facetable": false,
"retrievable": true
},
{
"name": "name",
"type": "Edm.String",
"filterable": false,
"searchable": true,
"sortable": false,
"facetable": false,
"retrievable": true
},
{
"name": "description_embeddings",
"type": "Collection(Edm.Single)",
"searchable": true,
"filterable": false,
"retrievable": false,
"sortable": false,
"facetable": false,
"key": false,
"indexAnalyzer": null,
"searchAnalyzer": null,
"analyzer": null,
"synonymMaps": [],
"dimensions": 1536,
"vectorSearchProfile": "default-vector-profile"
},
{
"name": "location",
"type": "Edm.GeographyPoint",
"searchable": false,
"filterable": true,
"sortable": true,
"facetable": false,
"retrievable": true
}
],
"vectorSearch": {
"algorithms": [
{
"name": "hnsw-main",
"kind": "hnsw",
"hnswParameters": {
"m": 4,
"efConstruction": 400,
"efSearch": 500,
"metric": "cosine"
}
}
],
"profiles": [
{
"name": "default-vector-profile",
"algorithm": "hnsw-main"
}
]
}
}
EOF
)
curl -H "Content-Type: application/json" -H "api-key: $adminKey" --request PUT --data "$jsonBody" "https://$searchName.search.windows.net/indexes('$searchIndexName')?allowIndexDowntime=False&api-version=2023-10-01-Preview"
echo "Creating function auth"
az storage account create --name "$storageAccountName" --location "$location" --resource-group "$resourceGroup" --sku Standard_LRS --allow-blob-public-access false
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionAuthName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
echo "Creating function generateEmbeddings"
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionGenerateEmbeddingsName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
echo "Creating function match"
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionMatchName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
echo "Creating function meet"
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionMeetName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
echo "Creating function query"
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionQueryName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
echo "Creating function syncPosition"
az functionapp create --resource-group "$resourceGroup" --consumption-plan-location "$shortLocation" --functions-version 4 --name "$functionSyncPositionName" --storage-account "$storageAccountName" --os-type linux --runtime custom --disable-app-insights
# Currently disabled - a push strategy seems better for our usecase (otherwise the delay would be minimum 3 minutes), but in future batching might be better
#echo "Creating data source"
#cosmosKey=$(az cosmosdb keys list --name $account --resource-group $resourceGroup --type keys --query primaryMasterKey --out tsv)
#cosmosKey="${cosmosKey%$'\r'}"
# todo filter data
#jsonBody=$(cat <<EOF
#{
# "name": "$searchDataSourceName",
# "type": "cosmosdb",
# "credentials": {
# "connectionString": "AccountEndpoint=https://$account.documents.azure.com;AccountKey=$cosmosKey;Database=$database"
# },
# "user_container": {
# "name": "$user_container",
# "query": null
# },
# "dataChangeDetectionPolicy": {
# "@odata.type": "#Microsoft.Azure.Search.HighWaterMarkChangeDetectionPolicy",
# "highWaterMarkColumnName": "_ts"
# }
#}
#EOF
#)
#
#curl -H "Content-Type: application/json" -H "api-key: $adminKey" --request POST --data "$jsonBody" https://$searchName.search.windows.net/datasources?api-version=2020-06-30
#
#
#echo "Creating indexer"
#jsonBody=$(cat <<EOF
#{
# "name" : "$searchIndexerName",
# "dataSourceName" : "$searchDataSourceName",
# "targetIndexName" : "$searchIndexName",
# "disabled": null,
# "schedule": null,
# "parameters": {
# "batchSize": null,
# "maxFailedItems": 0,
# "maxFailedItemsPerBatch": 0,
# "base64EncodeKeys": false,
# "configuration": {}
# },
# "fieldMappings": [],
# "encryptionKey": null
#}
#EOF
#)
#
#curl -H "Content-Type: application/json" -H "api-key: $adminKey" --request POST --data "$jsonBody" https://$searchName.search.windows.net/indexers?api-version=2020-06-30
#
# todo check indexer status