Install/OnPrem/Solr/Solr-Module.psm1

Import-Module "$PSScriptRoot\..\..\..\Common\Run-Pipelines.psm1" -Force
Import-Module "$PSScriptRoot\..\..\..\Common\Utils-Module.psm1" -Force
Import-Module "$PSScriptRoot\..\..\..\Common\WebAdministration-Module.psm1" -Force
Import-Module "$PSScriptRoot\..\..\..\Common\SSL\SSL-Module.psm1" -Force
$ErrorActionPreference = "Stop"

function InstallSolr {
    [CmdletBinding()]
    Param
    (
        [string]$Version,
        [string]$InstallDir,
        [string]$HostName,
        [string]$SSLCert,
        [string]$ServiceName
    )
   
    Write-Output "Solr installation started..."

    $JREPath = Get-ChildItem -Directory "C:\Program Files\Java" |  Sort-Object CreationTime -Descending | Select-Object -ExpandProperty FullName -First 1
    $jreVal = [Environment]::GetEnvironmentVariable("JAVA_HOME", [EnvironmentVariableTarget]::Machine)
    if ($jreVal -ne $JREPath) {
        Write-Output "Setting JAVA_HOME environment variable..."
        [Environment]::SetEnvironmentVariable("JAVA_HOME", $JREPath, [EnvironmentVariableTarget]::Machine)
    }

    DownloadSolr -Version $Version -InstallDir $InstallDir -ServiceName $ServiceName

    if ($HostName -ne "localhost") {
        AddHostFileEntry -IP "127.0.0.1" -HostName $HostName
    }

    if (!(Test-Path -Path "$InstallDir\server\etc\solr-ssl.keystore.pfx")) {
        Write-Output "Exporting SSL Certificate for Solr to use..."

        if (!(IsCertInstalled -Cert $SSLCert)) {
            Write-Error "SSL Cert '$SSLCert' not found..."
        }

        $cert = Get-ChildItem Cert:\LocalMachine\My | Where-Object FriendlyName -eq $SSLCert
        $certStore = "$InstallDir\server\etc\solr-ssl.keystore.pfx"
        $certPwd = ConvertTo-SecureString -String "secret" -Force -AsPlainText
        $cert | Export-PfxCertificate -FilePath $certStore -Password $certpwd | Out-Null
    }

    # Update solr cfg to use keystore & right host name
    if (!(Test-Path -Path "$InstallDir\bin\solr.in.cmd.old")) {
        Write-Output "Rewriting solr config..."

        $cfg = Get-Content "$InstallDir\bin\solr.in.cmd"
        Rename-Item "$InstallDir\bin\solr.in.cmd" "$InstallDir\bin\solr.in.cmd.old"
        $newCfg = $cfg | ForEach-Object { $_ -replace "REM set SOLR_SSL_KEY_STORE=etc/solr-ssl.keystore.jks", "set SOLR_SSL_KEY_STORE=$certStore" }
        $newCfg = $newCfg | ForEach-Object { $_ -replace "REM set SOLR_SSL_KEY_STORE_PASSWORD=secret", "set SOLR_SSL_KEY_STORE_PASSWORD=secret" }
        $newCfg = $newCfg | ForEach-Object { $_ -replace "REM set SOLR_SSL_TRUST_STORE=etc/solr-ssl.keystore.jks", "set SOLR_SSL_TRUST_STORE=$certStore" }
        $newCfg = $newCfg | ForEach-Object { $_ -replace "REM set SOLR_SSL_TRUST_STORE_PASSWORD=secret", "set SOLR_SSL_TRUST_STORE_PASSWORD=secret" }
        $newCfg = $newCfg | ForEach-Object { $_ -replace "REM set SOLR_HOST=192.168.1.1", "set SOLR_HOST=$HostName" }
        $newCfg | Set-Content "$InstallDir\bin\solr.in.cmd"
    }
    
    Write-Output "Solr installation done."
}

function DownloadSolr {
    [CmdletBinding()]
    Param
    (
        [string]$Version,
        [string]$InstallDir,
        [string]$ServiceName
    )
   
    Write-Output "Solr download started..."

    $services = @($ServiceName)
    DeleteServices -Services $services

    if (Test-Path $InstallDir) {
        Remove-Item $InstallDir -Recurse -Force | Out-Null
    }
    New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null

    $downloadFolder = "~\Downloads"
    $solrPackage = "https://archive.apache.org/dist/lucene/solr/$Version/solr-$Version.zip"
    $solrZip = "$downloadFolder\SolrTemp.zip"
    DownloadAndUnzip -ToolName "Solr" -DownloadFullPath $solrZip -DownloadURL $solrPackage -TargetDir (Split-Path -Path $InstallDir)
    
    Write-Output "Solr download done."
}

function RunSolrAsService {
    [CmdletBinding()]
    Param
    (
        [string]$ServiceDir,
        [string]$ServiceName,
        [int]$Port,
        [string]$ServiceDisplayName,
        [string]$ServiceDescription
    )

    Write-Output "Solr service creation started..."

    $services = @($ServiceName)
    DeleteServices -Services $services

    Write-Output "Installing Solr service..."
    nssm install "$ServiceName" "$ServiceDir\bin\solr.cmd" "-f" "-p $Port"
    nssm set "$ServiceName" DisplayName "$ServiceDisplayName" 
    nssm set "$ServiceName" Description "$ServiceDescription"

    $svc = Get-Service "$ServiceName" -ErrorAction SilentlyContinue

    if ($svc.Status -ne "Running") {
        Write-Output "Starting Solr service..."
        Start-Service "$ServiceName"
    }

    Write-Output "Solr service created."
}

function StartInstall {
    [CmdletBinding()]
    Param
    (
        [switch]$Force
    )
    
    $pipeline = "install$($global:Configuration.system.hosting)Solr-$($global:Configuration.system.sitecoreMode)"
    RunSteps -Pipeline $pipeline -Force:$Force
}

function GenerateNewSolrConfigSets {
    [CmdletBinding()]
    Param
    (
        [switch]$Force
    )
    
    $pipeline = "generate$($global:Configuration.system.hosting)SolrConfigSets-$($global:Configuration.system.sitecoreMode)"
    RunSteps -Pipeline $pipeline -Force:$Force
}

function CreateNewSolrCoresViaHTTP {
    [CmdletBinding()]
    Param
    (
        [switch]$Force
    )
    
    $pipeline = "create$($global:Configuration.system.hosting)SolrCoresViaHTTP-$($global:Configuration.system.sitecoreMode)"
    RunSteps -Pipeline $pipeline -Force:$Force
}

Export-ModuleMember -Function "StartInstall"
Export-ModuleMember -Function "GenerateNewSolrConfigSets"
Export-ModuleMember -Function "CreateNewSolrCoresViaHTTP"
Export-ModuleMember -Function "InstallSolr"
Export-ModuleMember -Function "DownloadSolr"
Export-ModuleMember -Function "RunSolrAsService"