Last active
July 27, 2021 13:57
-
-
Save BillCacy/b420c4d870e01a24083253b626254606 to your computer and use it in GitHub Desktop.
Solr script to handle the CDCR replication switching during a Blue/Green deployment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using module ".\BlueGreenSolrSettings.psm1" | |
$ErrorActionPreference = "Stop" | |
<# | |
.SYNOPSIS | |
Pushes the specified config and related pieces to the SolrCloud Zookeepers. | |
.PARAMETER solrPath | |
Path on the machine to the Solr installation. | |
EX: C:\solr\solr-6.2.1 | |
.PARAMETER zookeepers | |
Comma separated string list of the Zookeeper IP addresses for the current environment. | |
EX: 10.2.3.22:2181,10.2.3.23:2181,10.2.3.18:2181 | |
.PARAMETER configName | |
Name of the index configuration that is getting updated. | |
EX: sitecore_analytics_live_index | |
.PARAMETER configDir | |
Directory on the Solr server that contains the configuration files for the index. | |
Source config files: | |
solrPath + \server\solr\configsets\sitecore_analytics_live_index_source\conf | |
Target config files: | |
solrPath + \server\solr\configsets\sitecore_analytics_live_index_target\conf | |
#> | |
function Push-ConfigToZookeepers() | |
{ | |
param( | |
[Parameter(Mandatory = $true)] | |
[string]$solrPath, | |
[Parameter(Mandatory = $true)] | |
[string]$zookeepers, | |
[Parameter(Mandatory = $true)] | |
[string]$configName, | |
[Parameter(Mandatory = $true)] | |
[string]$configDir | |
) | |
$cloudScriptDir = Join-Path -Path $solrPath -ChildPath "\server\scripts\cloud-scripts" | |
if(!(Test-Path -Path $cloudScriptDir)) | |
{ | |
throw "$($cloudScriptDir) does not exist." | |
} | |
$zkCmdPath = Join-Path $cloudScriptDir -ChildPath "\zkcli.bat" | |
if (!(Test-Path $zkCmdPath)) { | |
throw "$($zkCmdPath) does not exist." | |
} | |
$cmd = '"' + $zkCmdPath + '" -cmd upconfig' + ` | |
" -zkhost $zookeepers" + ` | |
" -confname '$configName'" + ` | |
" -confdir '$configDir'" | |
Write-Host $cmd -ForegroundColor Green | |
Invoke-Expression "& $cmd" | |
} | |
<# | |
.SYNOPSIS | |
Sets the direction the replication will be running based on the defined Octopus host role for the machine. | |
.PARAMETER BlueGreen | |
BlueGreenSolrSettings object containing the properties populated with values supplied by Octopus, | |
or by the user from .\LocalSettings.psm1. | |
#> | |
function Set-ReplicationDirection() | |
{ | |
param( | |
[Parameter(Mandatory = $true)] | |
[BlueGreenSolrSettings] $BlueGreen | |
) | |
Write-Host "Changing the replication direction for '$($BlueGreen.Solr.IndexConfigName)'." -ForegroundColor Green | |
if($BlueGreen.Machine.HasRole($BlueGreen.Solr.ActiveRoleName)) | |
{ | |
Invoke-SetReplicationSource -BlueGreen $BlueGreen | |
return | |
} | |
if($BlueGreen.Machine.HasRole($BlueGreen.Solr.NonActiveRoleName)) | |
{ | |
Invoke-SetReplicationTarget -BlueGreen $BlueGreen | |
return | |
} | |
Write-Host "Active/Non-Active role is not configured for this host. This process requires an active or non-active role assigned to the host in Octopus." -ForegroundColor Red | |
} | |
<# | |
.SYNOPSIS | |
Reverses the direction replication will be running based on the defined Role for the machine. | |
.DESCRIPTION | |
Reversing is only used in a failure deploy to revert any changes that might have been made in a | |
Blue/Green deployment that failed. | |
.PARAMETER BlueGreen | |
BlueGreenSolrSettings object containing the properties populated with values supplied by Octopus, | |
or by the user from .\LocalSettings.psm1. | |
#> | |
function Set-ReplicationDirectionReverse() | |
{ | |
param( | |
[Parameter(Mandatory = $true)] | |
[BlueGreenSolrSettings] $BlueGreen | |
) | |
Write-Host "Reversing the replication direction for '$($BlueGreen.Solr.IndexConfigName)'." -ForegroundColor Green | |
if($BlueGreen.Machine.HasRole($BlueGreen.Solr.ActiveRoleName)) | |
{ | |
Invoke-SetReplicationTarget -BlueGreen $BlueGreen | |
return | |
} | |
if($BlueGreen.Machine.HasRole($BlueGreen.Solr.NonActiveRoleName)) | |
{ | |
Invoke-SetReplicationSource -BlueGreen $BlueGreen | |
return | |
} | |
Write-Host "Active/Non-Active role is not configured for this host. This process requires an active or non-active role assigned to the host in Octopus." -ForegroundColor Red | |
} | |
<# | |
.SYNOPSIS | |
Invokes the process to push the configs to the Zookeepers for the 'Source' in prepartion for | |
starting or changing the direction of replication. | |
.PARAMETER BlueGreen | |
BlueGreenSolrSettings object containing the properties populated with values supplied by Octopus, | |
or by the user from .\LocalSettings.psm1. | |
#> | |
function Invoke-SetReplicationSource() | |
{ | |
param( | |
[Parameter(Mandatory = $true)] | |
[BlueGreenSolrSettings] $BlueGreen | |
) | |
Write-Host "Setting this instance as the Source of replication." | |
$sourceConfigDir = Join-Path $BlueGreen.Solr.InstallDir -ChildPath "server\solr\configsets\$($BlueGreen.Solr.IndexConfigName)_source\conf" | |
Push-ConfigToZookeepers -solrPath $BlueGreen.Solr.InstallDir ` | |
-zookeepers $BlueGreen.Solr.Zookeepers ` | |
-configName $BlueGreen.Solr.IndexConfigName ` | |
-configDir $sourceConfigDir | |
return | |
} | |
<# | |
.SYNOPSIS | |
Invokes the process to push the configs to the Zookeepers for the 'Target' in prepartion for | |
starting or changing the direction of replication. | |
.PARAMETER BlueGreen | |
BlueGreenSolrSettings object containing the properties populated with values supplied by Octopus, | |
or by the user from .\LocalSettings.psm1. | |
#> | |
function Invoke-SetReplicationTarget() | |
{ | |
param( | |
[Parameter(Mandatory = $true)] | |
[BlueGreenSolrSettings] $BlueGreen | |
) | |
Write-Host "Setting this instance as the Target of replication." | |
$targetConfigDir = Join-Path $BlueGreen.Solr.InstallDir -ChildPath "server\solr\configsets\$($BlueGreen.Solr.IndexConfigName)_target\conf" | |
Push-ConfigToZookeepers -solrPath $BlueGreen.Solr.InstallDir ` | |
-zookeepers $BlueGreen.Solr.Zookeepers ` | |
-configName $BlueGreen.Solr.IndexConfigName ` | |
-configDir $targetConfigDir | |
return | |
} | |
<# | |
.SYNOPSIS | |
Starting the replication process for 'Source' or 'Target' based on the defined Role for the Octopus host. | |
.DESCRIPTION | |
Called from .\BlueGreenSolr.ps1 or .\BlueGreenSolr-Local.ps1 scripts that are called after building the | |
BlueGreenSolrSettings object to start the replication. | |
.PARAMETER BlueGreen | |
BlueGreenSolrSettings object containing the properties populated with values supplied by Octopus, | |
or by the user from .\LocalSettings.psm1. | |
.EXAMPLE | |
Start-Replication -BlueGreen $blueGreenSettings | |
#> | |
function Start-Replication() | |
{ | |
param( | |
[Parameter(Mandatory = $true)] | |
[BlueGreenSolrSettings] $BlueGreen | |
) | |
Write-Host "Giving Solr a chance to initialize from previous steps. Sleeping for 5 seconds before initializing." | |
Start-Sleep -s 5 | |
Write-Host "Starting replication for $($BlueGreen.Solr.IndexConfigName)." -ForegroundColor Green | |
if($BlueGreen.Machine.HasRole($BlueGreen.Solr.ActiveRoleName)) | |
{ | |
#Instance needs to be started | |
Invoke-StartReplicationSource -BlueGreen $BlueGreen | |
return | |
} | |
if($BlueGreen.Machine.HasRole($BlueGreen.Solr.NonActiveRoleName)) | |
{ | |
#Instance needs the buffer disabled | |
Invoke-StartReplicationTarget -BlueGreen $BlueGreen | |
return | |
} | |
Write-Host "Active/Non-Active role is not configured for this host. This process requires an active or non-active role assigned to the host in Octopus." -ForegroundColor Red | |
} | |
<# | |
.SYNOPSIS | |
Starting the replication reversal process for 'Source' or 'Target' based on the defined Role for the Octopus host. | |
.DESCRIPTION | |
Called from .\BlueGreenSolr.ps1 or .\BlueGreenSolr-Local.ps1 scripts that are called after building the | |
BlueGreenSolrSettings object to start the reversal of replication. | |
.PARAMETER BlueGreen | |
BlueGreenSolrSettings object containing the properties populated with values supplied by Octopus, | |
or by the user from .\LocalSettings.psm1. | |
.EXAMPLE | |
Start-ReplicationReverse -BlueGreen $blueGreenSettings | |
#> | |
function Start-ReplicationReverse() | |
{ | |
param( | |
[Parameter(Mandatory = $true)] | |
[BlueGreenSolrSettings] $BlueGreen | |
) | |
Write-Host "Giving Solr a chance to initialize from previous steps. Sleeping for 5 seconds before initializing." | |
Start-Sleep -s 5 | |
Write-Host "Starting replication for SolrCloud" -ForegroundColor Green | |
if($BlueGreen.Machine.HasRole($BlueGreen.Solr.ActiveRoleName)) | |
{ | |
#Instance needs the buffer disabled | |
Invoke-StartReplicationTarget -BlueGreen $BlueGreen | |
return | |
} | |
if($BlueGreen.Machine.HasRole($BlueGreen.Solr.NonActiveRoleName)) | |
{ | |
#Instance needs to be started | |
Invoke-StartReplicationSource -BlueGreen $BlueGreen | |
return | |
} | |
Write-Host "Active/Non-Active role is not configured for this host. This process requires an active or non-active role assigned to the host in Octopus." -ForegroundColor Red | |
} | |
<# | |
.SYNOPSIS | |
Makes the Solr web request to start replication for the 'Source'. | |
.DESCRIPTION | |
Called from Start-Replication or Start-ReplicationReverse functions. | |
.PARAMETER BlueGreen | |
BlueGreenSolrSettings object containing the properties populated with values supplied by Octopus, | |
or by the user from .\LocalSettings.psm1. | |
.EXAMPLE | |
Invoke-StartReplicationSource -BlueGreen $BlueGreen | |
#> | |
function Invoke-StartReplicationSource() | |
{ | |
param( | |
[Parameter(Mandatory = $true)] | |
[BlueGreenSolrSettings] $BlueGreen | |
) | |
#Instance needs to be started | |
$startRequestUrl = $BlueGreen.Solr.InstanceUrl + "/$($BlueGreen.Solr.IndexConfigName)/cdcr?action=START" | |
Write-Host "Enabling Replication on Source: $($startRequestUrl)" | |
Invoke-WebRequest -Uri $startRequestUrl -UseBasicParsing | |
$bufferRequestUrl = $BlueGreen.Solr.InstanceUrl + "/$($BlueGreen.Solr.IndexConfigName)/cdcr?action=ENABLEBUFFER" | |
Write-Host "Enabling the buffer on Source: $($bufferRequestUrl)" | |
Invoke-WebRequest -Uri $bufferRequestUrl -UseBasicParsing | |
return | |
} | |
<# | |
.SYNOPSIS | |
Makes the Solr web request to start replication for the 'Target'. | |
.DESCRIPTION | |
Called from Start-Replication or Start-ReplicationReverse functions. | |
.PARAMETER BlueGreen | |
BlueGreenSolrSettings object containing the properties populated with values supplied by Octopus, | |
or by the user from .\LocalSettings.psm1. | |
.EXAMPLE | |
Invoke-StartReplicationTarget -BlueGreen $BlueGreen | |
#> | |
function Invoke-StartReplicationTarget() | |
{ | |
param( | |
[Parameter(Mandatory = $true)] | |
[BlueGreenSolrSettings] $BlueGreen | |
) | |
#Instance needs the buffer disabled | |
$stopRequestUrl = $BlueGreen.Solr.InstanceUrl + "/$($BlueGreen.Solr.IndexConfigName)/cdcr?action=STOP" | |
Write-Host "Disabling Replication on Target: $($stopRequestUrl)" | |
Invoke-WebRequest -Uri $stopRequestUrl -UseBasicParsing | |
$bufferRequestUrl = $BlueGreen.Solr.InstanceUrl + "/$($BlueGreen.Solr.IndexConfigName)/cdcr?action=DISABLEBUFFER" | |
Write-Host "Disabling the buffer on Target: $($bufferRequestUrl)" | |
Invoke-WebRequest -Uri $bufferRequestUrl -UseBasicParsing | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment