-
Notifications
You must be signed in to change notification settings - Fork 5
/
sitecore-searchstax-connector.ps1
377 lines (330 loc) · 15.3 KB
/
sitecore-searchstax-connector.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
366
367
368
369
370
371
372
373
374
375
376
377
Import-Module powershell-yaml
$configPath=".\config.yml"
$xpConfigPath=".\Configs\xp\solr_config-"
$xConnectConfigFolderPath=".\Configs\xconnect\"
$xConnectConfigPath=".\Configs\xconnect\xconnect-config-"
$start_time = Get-Date
$collectionsXM = @("_master_index","_core_index","_web_index")
$collectionsMarketing = @("_marketingdefinitions_master","_marketingdefinitions_web","_marketing_asset_index_master","_marketing_asset_index_web","_testing_index","_suggested_test_index","_fxm_master_index","_fxm_web_index" )
$collections93Marketing = @("_marketingdefinitions_master","_marketingdefinitions_web","_marketing_asset_index_master","_marketing_asset_index_web","_testing_index","_suggested_test_index","_fxm_master_index","_fxm_web_index","_personalization_index" )
$collectionsXConnect = @("xdb_internal", "xdb_rebuild_internal")
$collectionsSXA = @("sitecore_sxa_master_index", "sitecore_sxa_web_index")
$searchstaxUrl = 'https://app.searchstax.com'
$searchstaxStaticUrl = 'https://static.searchstax.com'
$authUrl = -join($searchstaxUrl, '/api/rest/v1/obtain-auth-token/')
# DEFAULT VALUES AS SUGGESTED BY SITECORE
#Max return rows from solr
$searchMaxResults="500"
#Max items in a batch
$batchSize="500"
# DEFAULT VALUES AS SUGGESTED BY SITECORE - END
function Init {
[string[]]$fileContent = Get-Content $configPath
$content = ''
foreach ($line in $fileContent) {
$content = $content + "`n" + $line
}
$yaml = ConvertFrom-YAML $content
# Get base values
$global:accountName=$yaml.settings.accountName
$global:deploymentUid=$yaml.settings.deploymentUid
$global:sitecorePrefix=$yaml.settings.sitecorePrefix
$global:solrUsername=$yaml.settings.solrUsername
$global:solrPassword=$yaml.settings.solrPassword
$global:sitecoreVersion=$yaml.settings.sitecoreVersion
$global:isUniqueConfigs=Get-BooleanValue $yaml.settings.isUniqueConfigs
$global:isSxa=Get-BooleanValue $yaml.settings.isSxa
$global:customIndexes=$yaml.settings.customIndexes
$global:collectionsXM = $collectionsXM
$global:switchOnRebuildSufix=$yaml.settings.switchOnRebuild.sufix
$global:switchOnRebuildMainAlias=$yaml.settings.switchOnRebuild.mainAlias
$global:switchOnRebuildAlias=$yaml.settings.switchOnRebuild.rebuildAlias
$global:switchOnRebuildSitecorePrefix=$yaml.settings.switchOnRebuild.sitecorePrefix
$global:switchOnRebuildEnableForPlatformIndexes=Get-BooleanValue $yaml.settings.switchOnRebuild.enableForPlatformIndexes
$global:switchOnRebuildEnableForMarketingIndexes=Get-BooleanValue $yaml.settings.switchOnRebuild.enableForMarketingIndexes
$global:switchOnRebuildEnableForSXA=Get-BooleanValue $yaml.settings.switchOnRebuild.enableForSXA
$global:switchOnRebuildCollections = @()
# Get configuration mode
$global:configurationMode=$yaml.settings.configurationMode
$configurationModeArray=$configurationMode.split("|")
$global:isConfigureXM=$false
$global:isConfigureXP=$false
$global:isConfigureXConnect=$false
foreach($instMode in $configurationModeArray){
if($instMode.ToUpper() -eq "XM"){
$global:isConfigureXM = $true
break
} Elseif($instMode.ToUpper() -eq "XP"){
$global:isConfigureXP=$true
} Elseif ($instMode.ToUpper() -eq "XCONNECT") {
$global:isConfigureXConnect=$true
} else {
Write-Error -Message "Invalid Configuration mode" -ErrorAction Stop
}
}
if (-Not $isConfigureXM -And -Not $isConfigureXP -And -Not $isConfigureXConnect){
Write-Error -Message "Please select at least 1 Configuration mode" -ErrorAction Stop
}
# Configure internal variables
$global:deploymentReadUrl = -join($searchstaxUrl,'/api/rest/v2/account/',$accountName,'/deployment/',$deploymentUid,'/')
$global:configUploadUrl = -join($searchstaxUrl,'/api/rest/v2/account/',$accountName,'/deployment/',$deploymentUid,'/zookeeper-config/')
$global:xConnectCollectionAlias = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$global:xConnectCollectionAlias.Add("xdb_internal", "xdb")
$global:xConnectCollectionAlias.Add("xdb_rebuild_internal", "xdb_rebuild")
}
function Get-BooleanValue($val){
if ($val -eq "true") {
return $true
} Elseif ($val -eq "false") {
return $false
} else {
Write-Error -Message "Invalid value provided for boolean. [true/false]" -ErrorAction Stop
}
}
function Get-Token {
# "Please provide authentication information."
$uname = Read-Host -Prompt 'Username - '
$password = Read-Host -AsSecureString -Prompt 'Password - '
$password = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
Write-Host "Asking for an authorization token for $uname..."
Write-Host
$body = @{
username=$uname
password=$password
}
$body = $body | ConvertTo-Json
try {
$token = Invoke-RestMethod -uri "https://app.searchstax.com/api/rest/v2/obtain-auth-token/" -Method Post -Body $body -ContentType 'application/json'
$token = $token.token
Remove-Variable PASSWORD
Remove-Variable body
Write-Host "Obtained token" $token
Write-Host
return $token
} catch {
if ($_.ErrorDetails.Message.Contains("tfa_token_required")) {
$tfa_token = Read-Host -Prompt '2FA Token - '
$body = @{
username=$uname
password=$password
tfa_token=$tfa_token
}
$body = $body | ConvertTo-Json
$token = Invoke-RestMethod -uri "https://app.searchstax.com/api/rest/v2/obtain-auth-token/" -Method Post -Body $body -ContentType 'application/json'
$token = $token.token
Remove-Variable PASSWORD
Remove-Variable body
Write-Host "Obtained token" $token
Write-Host
return $token
}
else {
Write-Error -Message "Unable to get Auth Token. Error was: $_" -ErrorAction Stop
}
}
}
function Check-DeploymentExist($token) {
try {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Token $token")
$result = Invoke-WebRequest -Method Get -Headers $headers -uri $deploymentReadUrl
if ($result.statuscode -eq 200) {
Write-Host "Deployment found. Continuing."
} else {
Write-Error "Could not find deployment. Exiting." -ErrorAction Stop
}
} catch {
Write-Error -Message "Unable to verify if deployment exists. Error was: $_" -ErrorAction Stop
}
}
function Get-Node-Count($token) {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Token $token")
$result = Invoke-RestMethod -Method Get -ContentType 'application/json' -Headers $headers -uri $deploymentReadUrl
return [int]$result.num_nodes_default + [int]$result.num_additional_app_nodes
}
function Get-SolrUrl($token) {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Token $token")
$result = Invoke-RestMethod -Method Get -ContentType 'application/json' -Headers $headers -uri $deploymentReadUrl
return $result.http_endpoint
}
function Update-XML($path, $xpath, $attributeKey, $attributeValue){
if (Test-Path -LiteralPath $path) {
$xml = New-Object XML
$xml.Load($path)
$node = $xml.SelectSingleNode($xpath)
$node.SetAttribute($attributeKey,$attributeValue)
$xml.Save($path)
}
else {
Write-Error -Message "Could not find $path File"
}
}
if (!($PSVersionTable.PSVersion.Major -ge 6)){
Write-Host "This script is only compatible with Powershell Core v6 and above."
Write-Host
Write-Host "You can install Powershell Core v6 using following command - "
Write-Host "iex `"& { `$(irm https://aka.ms/install-powershell.ps1) } -UseMSI`""
Write-Host
Write-Host "Please restart this script using Powershell Core v6"
Write-Error -Message "" -ErrorAction Stop
}
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process pwsh.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
# Initializing Script
Init
. "src\searchstax-sitecore-xp.ps1"
. "src\searchstax-sitecore-xconnect.ps1"
. "src\searchstax-sitecore-sxa.ps1"
# Initializing Script Ends
if ($global:isConfigureXM -eq "true") {
$global:coll += $collectionsXM
}
elseif($global:isConfigureXP -eq "true") {
if ($sitecoreVersion -eq "9.0.2") {
$solrVersion = "6"
$global:isUniqueConfigs= $true
$global:collectionsMarketing = $collectionsMarketing
} elseif ($sitecoreVersion -eq "9.1.1") {
$solrVersion = "7.2.1"
$global:collectionsMarketing = $collectionsMarketing
} elseif ($sitecoreVersion -eq "9.2.0") {
$solrVersion = "7.5.0"
$global:collectionsMarketing = $collectionsMarketing
} elseif ($sitecoreVersion -eq "9.3.0") {
$solrVersion = "8.1.1"
$global:collectionsMarketing = $collections93Marketing
} elseif ($sitecoreVersion -like "10.0.*") {
$solrVersion = "8.4.0"
$global:collectionsMarketing = $collections93Marketing
} elseif ($sitecoreVersion -like "10.1.*") {
$solrVersion = "8.4.0"
$global:collectionsMarketing = $collections93Marketing
} elseif ($sitecoreVersion -like "10.2.*") {
$solrVersion = "8.8.2"
$global:collectionsMarketing = $collections93Marketing
} elseif ($sitecoreVersion -like "10.3.*") {
$solrVersion = "8.11.2"
$global:collectionsMarketing = $collections93Marketing
} elseif ($sitecoreVersion -like "10.4.*") {
$solrVersion = "8.11.2"
$global:collectionsMarketing = $collections93Marketing
}
else {
Write-Error -Message "Unsupported sitecore version specified. Supported versions are 9.0.2, 9.1.1, 9.2.0, 9.3.0, 10.0.*, 10.1.*, 10.2.*, 10.3.*, 10.4.*" -ErrorAction Stop
}
$global:coll = $collectionsXM + $global:collectionsMarketing
}
if ($global:isSxa -eq "true") {
$global:sxaColl = $collectionsSXA
}
if($global:switchOnRebuildEnableForPlatformIndexes) {
$global:switchOnRebuildCollections += $collectionsXM
}
if($global:switchOnRebuildEnableForMarketingIndexes) {
$global:switchOnRebuildCollections += $global:collectionsMarketing
}
Write-Host "Sitecore Version - $sitecoreVersion"
Write-Host "Solr Version - $solrVersion"
Write-Host "Configuration Mode - $configurationMode"
Write-Host
$token = Get-Token
Check-DeploymentExist($token)
Write-Host "Getting live node count ..."
$nodeCount = Get-Node-Count $token
Write-Host "Getting live node count ... DONE"
Write-Host "Number of nodes - $nodeCount"
$solr = Get-SolrUrl $token
if ($isConfigureXM -Or $isConfigureXP){
Upload-Configs $solrVersion $token
Create-Collections $solr $nodeCount
if ($global:switchOnRebuildEnableForPlatformIndexes -or $global:switchOnRebuildEnableForMarketingIndexes) {
Create-SwitchOnRebuildCollections $solr $nodeCount
Create-SwitchOnRebuildAliases $solr
}
}
if($global:customIndexes.Count -gt 0) {
Upload-CustomConfigs $solrVersion $token
Create-CustomCollections $solr $nodeCount
}
if ($global:isSxa -eq "true") {
Upload-SXA-Config $solrVersion $token
Create-SXA-Collections $solr $nodeCount
}
if ($isConfigureXConnect){
Upload-XConnect-Config $solrVersion $token
Create-XConnect-Collections $solr $nodeCount
Create-XConnect-Alias $solr $nodeCount
Update-XConnectSchema $solr $token $solrVersion
}
Write-Host "****************************************************************"
Write-Host "****************************************************************"
Write-Host "****************************************************************"
Write-Host "Solr has been set up"
Write-Host
Write-Host "Please follow these instructions to complete the "
Write-Host "SearchStax - Sitecore integration:"
Write-Host ""
if ($isConfigureXP){
$solrConn = $solr.substring(0,$solr.length-1)
if ($solrUsername.length -gt 0) {
$solrConn = -join("https://",$solrUsername,":",$solrPassword,"@",$solrConn.substring(8,$solrConn.length-8))
}
$solrConn = -join($solrConn,";solrCloud=true")
Write-Host "****************************************************************"
Write-Host "Update web.config file:"
Write-Host "1. Navigate to your Sitecore installation"
Write-Host "2. Open web.config"
Write-Host "3. Locate the <AppSettings> section and change the value"
Write-Host " of the search:define setting to Solr"
Write-Host
Write-Host " <AppSettings>"
Write-Host " <add key=`"role:define`" value=`"ContentDelivery`"/>"
Write-Host " <add key=`"search:define`" value=`"Solr`"/>"
Write-Host " </AppSettings>"
Write-Host
Write-Host "****************************************************************"
Write-Host "Configuring the Solr endpoint:"
Write-Host "1. Navigate to App_Config/ConnectionStrings.config"
Write-Host "2. Locate <add> tag with an attribute `"solr.search`" "
Write-Host " and change the value of connectionString."
Write-Host
Write-Host "<add name=`"solr.search`" connectionString=`"$solrConn`"/>"
Write-Host
Write-Host "****************************************************************"
}
if ($isConfigureXConnect){
$solrConn = $solr.substring(0,$solr.length-1)
if ($solrUsername.length -gt 0) {
$solrConn = -join("https://",$solrUsername,":",$solrPassword,"@",$solrConn.substring(8,$solrConn.length-8))
}
$solrConn = -join($solrConn,"/xdb;solrcloud=true")
Write-Host "****************************************************************"
Write-Host "Update the connection string for xConnect:"
Write-Host "1. Navigate to App_Config/ConnectionStrings.config for xConnect"
Write-Host "2. Locate <add> tag with an attribute `"solrCore`" "
Write-Host " and change the value of connectionString."
Write-Host
Write-Host "<add name=`"solrCore`" connectionString=`"$solrConn`" />"
Write-Host
Write-Host "3. Navigate to App_Data\jobs\continuous\IndexWorker\"
Write-Host " App_Config\ConnectionStrings.config for xConnect"
Write-Host "4. Locate <add> tag with an attribute `"solrCore`" "
Write-Host " and change the value of connectionString."
Write-Host
Write-Host "<add name=`"solrCore`" connectionString=`"$solrConn`" />"
Write-Host
Write-Host "****************************************************************"
Write-Host "Update xConnect Schema:"
Write-Host "1. Navigate to App_Data\solrcommands\schema.json"
Write-Host "2. Use the Schema API and upload the above file to "
Write-Host " both XDB Collections"
Write-Host " https://lucene.apache.org/solr/guide/8_6/schema-api.html"
Write-Host
Write-Host "****************************************************************"
}
Write-Host "Restart Sitecore"
Write-Host "****************************************************************"
Write-Host "****************************************************************"
Write-Output "Time taken: $((Get-Date).Subtract($start_time))"
Write-Host "FINISHED"