Last active
May 24, 2019 12:40
-
-
Save varunvns/d9143e03d44503ae2806c827e3ab5012 to your computer and use it in GitHub Desktop.
This file contains 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
Param( | |
$zooKeeperInstanceName = "zookeeperNode1", | |
$zooKeeperVersion = "3.4.14", | |
$installFolder = "c:\ZooKeeper\FirstNode", | |
$zooKeeperNode = "1", | |
$zooKeeperPort = "2141", | |
$zooKeeperHost = "zookeeper1", | |
$zooKeeperSSL = $true, | |
$nssmVersion = "2.24", | |
$JREVersion = "1.8.0_111", | |
$zooKeeperList = "zookeeper1,zookeeper2,zookeeper3" | |
#$zooKeeper1 = "server.2=zookeeper1:2888:3888", | |
#$zooKeeper2 = "server.2=zookeeper2:2888:3888", | |
#$zooKeeper3 = "server.3=zookeeper3:2888:3888" | |
) | |
$JREPath = "C:\Program Files\AdoptOpenJDK\jdk-8.0.202.08" ## Note that if you're running 32bit java, you will need to change this path | |
$zooKeeperName = "ZooKeeper-$zooKeeperVersion" | |
$zooKeeperRoot = $installFolder | |
$nssmRoot = "$installFolder\nssm-$nssmVersion" | |
#$zooKeeperPackage = "https://archive.apache.org/dist/zookeeper/zookeeper-$zooKeeperVersion/zookeeper-$zooKeeperVersion.tar.gz" | |
#$zooKeeperPackage = "https://archive.apache.org/dist/zookeeper/zookeeper-$zooKeeperVersion/zookeeper-$zooKeeperVersion.zip" | |
$nssmPackage = "https://nssm.cc/release/nssm-$nssmVersion.zip" | |
$downloadFolder = "C:\Varun\SolrCloud" | |
## Verify elevated | |
## https://superuser.com/questions/749243/detect-if-powershell-is-running-as-administrator | |
$elevated = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544") | |
if($elevated -eq $false) | |
{ | |
throw "In order to install services, please run this script elevated." | |
} | |
function unZipArchive | |
{ | |
Param( | |
[string]$toolName, | |
[string]$toolFolder, | |
[string]$toolZip, | |
[string]$installRoot | |
) | |
Write-Host "Extracting $toolName to $toolFolder..." | |
Expand-Archive $toolZip -DestinationPath $installRoot | |
} | |
function downloadAndUnzipIfRequired | |
{ | |
Param( | |
[string]$toolName, | |
[string]$toolFolder, | |
[string]$toolZip, | |
[string]$toolSourceFile, | |
[string]$installRoot | |
) | |
if(!(Test-Path -Path $toolFolder)) | |
{ | |
if(!(Test-Path -Path $toolZip)) | |
{ | |
Write-Host "Downloading $toolName..." | |
Start-BitsTransfer -Source $toolSourceFile -Destination $toolZip | |
} | |
Write-Host "Extracting $toolName to $toolFolder..." | |
Expand-Archive $toolZip -DestinationPath $installRoot | |
} | |
} | |
function downloadandUnZipGZFileIfRequired | |
{ | |
Param( | |
[string]$toolName, | |
[string]$toolFolder, | |
[string]$toolZip, | |
[string]$toolSourceFile, | |
[string]$installRoot | |
) | |
if(!(Test-Path -Path $toolFolder)) | |
{ | |
if(!(Test-Path -Path $toolZip)) | |
{ | |
Write-Host "Downloading $toolName..." | |
Start-BitsTransfer -Source $toolSourceFile -Destination $toolZip | |
} | |
Write-Host "Extracting $toolName to $toolFolder..." | |
#TODO - Install 7-Zip if not found and then Extract it using 7-Zip | |
#Expand-Archive $toolZip -DestinationPath $installRoot | |
} | |
} | |
# download & extract the ZooKeeper archive to the right folder | |
$zooKeeperZip = "$downloadFolder\$zooKeeperName.zip" | |
#Commented the Zookeeper Download and Extract as it requires 7-Zip | |
unZipArchive "ZooKeeper" $zooKeeperRoot $zooKeeperZip $installFolder | |
# download & extract the nssm archive to the right folder | |
$nssmZip = "$downloadFolder\nssm-$nssmVersion.zip" | |
downloadAndUnzipIfRequired "NSSM" $nssmRoot $nssmZip $nssmPackage $installFolder | |
# Ensure Java environment variable | |
$jreVal = [Environment]::GetEnvironmentVariable("JAVA_HOME", [EnvironmentVariableTarget]::Machine) | |
if($jreVal -ne $JREPath) | |
{ | |
Write-Host "Setting JAVA_HOME environment variable" | |
[Environment]::SetEnvironmentVariable("JAVA_HOME", $JREPath, [EnvironmentVariableTarget]::Machine) | |
} | |
#Create a Data Folder | |
New-Item -ItemType directory -Path $zooKeeperRoot"\\data" | |
#Create a Logs Folder | |
New-Item -ItemType directory -Path $zooKeeperRoot"\\log" | |
#Make changes for ZooKeeper Config File | |
$zooKeeperConfigFileName = $zooKeeperRoot + "\\conf\\zoo_sample.cfg" | |
$zooKeeperConfigFile = [System.Io.File]::ReadAllText($zooKeeperConfigFileName) | |
#$zooKeeperConfigFile = Get-Content $zooKeeperConfigFileName -Raw | |
#File before the change | |
Write-Host $zooKeeperConfigFile | |
if($zooKeeperConfigFile -match "dataDir=/tmp/zookeeper") | |
{ | |
#For replacing backslash - we need to put it in Square Brackets | |
#Helpful: https://www.systemcenterautomation.com/2018/04/replace-backslash-with-powershell/ | |
((Get-Content -path $zooKeeperConfigFileName -Raw) -replace "dataDir=/tmp/zookeeper", "dataDir=$zooKeeperRoot\data" -replace "[\\]","/") | Set-Content -Path $zooKeeperConfigFileName | |
} | |
#Change the port number to the one that is mentioned in the variables | |
if($zooKeeperConfigFile -match "clientPort=2181") | |
{ | |
((Get-Content -path $zooKeeperConfigFileName -Raw) -replace "clientPort=2181", "clientPort=$zooKeeperPort") |Set-Content -Path $zooKeeperConfigFileName | |
} | |
#Logging of the ZooKeeper | |
$logfolderEntry = "dataLogDir=$zooKeeperRoot\log" -replace "[\\]","/" | |
Add-Content -Path $zooKeeperConfigFileName -Value $logfolderEntry | |
#Add ZooKeeper Server Details to the ZooKeeper Config File | |
$ZKlistArray = $zooKeeperList.Split(",") | |
$index=1 | |
foreach($zkNode in $ZKlistArray) | |
{ | |
Write-Output "Hostname is $zkNode, index is $index" | |
switch ($index) { | |
"1" {Add-Content -Path $zooKeeperConfigFileName -Value "server.1=$($zkNode):2888:3888"; break} | |
"2" {Add-Content -Path $zooKeeperConfigFileName -Value "server.2=$($zkNode):2888:3888"; break} | |
"3" {Add-Content -Path $zooKeeperConfigFileName -Value "server.3=$($zkNode):2888:3888"; break} | |
default {"Unexpected Index"; break} | |
} | |
$index = $index + 1 | |
} | |
#Rename the Config File | |
Rename-Item -Path $zooKeeperConfigFileName -NewName "zoo.cfg" | |
#Get the renamed file | |
$zooKeeperConfigFileName = $zooKeeperRoot + "\\conf\\zoo.cfg" | |
#look at the Config File after all the changes | |
$zooKeeperConfigFile = [System.Io.File]::ReadAllText($zooKeeperConfigFileName) | |
Write-Host $zooKeeperConfigFile | |
#Create a new file for MYID and adding its ID | |
Write-Output "creating myid file" | |
New-Item -ItemType file -Path "$zooKeeperRoot\\data\\myid" | Add-Content -Value $zooKeeperNode | |
#Read the MYID File and Write it to the host | |
Write-Host "Data of myid file" | |
$myidFile = [System.Io.File]::ReadAllText("$zooKeeperRoot\\data\\myid") | |
Write-Host $myidFile | |
# Ensure the ZooKeeper host name is in your hosts file | |
if($zooKeeperHost -ne "localhost") | |
{ | |
$hostFileName = "c:\\windows\system32\drivers\etc\hosts" | |
$hostFile = [System.Io.File]::ReadAllText($hostFileName) | |
if(!($hostFile -like "*$zooKeeperHost*")) | |
{ | |
Write-Host "Updating host file" | |
"`r`n127.0.0.1`t$zooKeeperHost" | Add-Content $hostFileName | |
} | |
} | |
# install the service & runs | |
$svc = Get-Service "$zooKeeperInstanceName" -ErrorAction SilentlyContinue | |
if(!($svc)) | |
{ | |
Write-Host "Installing ZooKeeper service" | |
&"$installFolder\nssm-$nssmVersion\win64\nssm.exe" install "$zooKeeperInstanceName" "$zooKeeperRoot\bin\zkserver.cmd" | |
&"$installFolder\nssm-$nssmVersion\win64\nssm.exe" set "$zooKeeperInstanceName" "Description" "ZooKeeper Node $zooKeeperNode Running on Port $zooKeeperPort" | |
&"$installFolder\nssm-$nssmVersion\win64\nssm.exe" set "$zooKeeperInstanceName" AppStdout "$installFolder\logs\stdout.log" | |
$svc = Get-Service "$zooKeeperInstanceName" -ErrorAction SilentlyContinue | |
} | |
if($svc.Status -ne "Running") | |
{ | |
Write-Host "Starting ZooKeeper service" | |
Start-Service "$zooKeeperInstanceName" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment