Core/SitecoreInstall-Module.psm1

$ErrorActionPreference = "Stop"

function GetSitecoreInstallPipeline {
    if ($null -ne $SAFConfiguration.system.sitecoreMode) {
        return "install$($SAFConfiguration.system.hosting)$($SAFConfiguration.system.serverRole)-$($SAFConfiguration.system.sitecoreMode)"
    }
    else {
        return "install$($SAFConfiguration.system.hosting)$($SAFConfiguration.system.serverRole)"
    }
}

function GetSitecoreInstallPipelinesFile {
    $version = $SAFConfiguration.sitecore.version
    $majorMinorOnly = $version.Substring(0, ($version.LastIndexOf('.')))
    return "$SAFInstallSitecoreOnPremPipelines\$majorMinorOnly\Pipelines.json"
}

function ShowPostInstallSteps {
    Write-Output ""
    Write-Output ""
    Write-Output ""

    Write-Warning "POST-INSTALLATION STEPS (please, go through the following list manually)"

    
    Write-Warning "1. Rebuild the Search Indexes"
    Write-Warning "2. Rebuild the Link Database"

    if ($SAFConfiguration.system.sitecoreMode -eq "XP") {
        Write-Warning "3. Deploy Marketing Definitions"
    }
}

function InstallSitecore {
    [CmdletBinding()]
    Param
    (
        [switch]$Force
    )
    
    $pipelinesFile = GetSitecoreInstallPipelinesFile
    $pipelineName = GetSitecoreInstallPipeline
    RunPipeline -DefinitionFile $pipelinesFile -Name $pipelineName -Force:$Force -RunExtensions

    $role = $SAFConfiguration.system.serverRole
    if(($role -eq "AllInOneQuickly") -or ($role -eq "AllInOne") -or ($role -eq "AllInOneSitecore") -or ($role -eq "CM")) {
        ShowPostInstallSteps
    }
}

function InstallSitecorePackage {
    [CmdletBinding()]
    Param
    (
        [string]$WebsiteRoot,
        [string]$WebsiteUrl,
        [string]$PackagePath
    )
 
    Write-Output "Installing Package '$PackagePath'"
 
    #Generate a random 10 digit folder name. For security
    $folderKey = -join ((97..122) | Get-Random -Count 10 | ForEach-Object {[char]$_})
    
    #Generate a Access Key
    $accessKey = New-Guid
    
    Write-Output "Folder Key = $folderKey"
    Write-Output "Access Guid = $accessKey"
 
    #The path to the source Agent. Should be in the same folder as I'm running
    $sourceAgentPath = "$PSScriptRoot\Templates\PackageInstaller.asmx"
    
    #The folder where the actuall install happens
    $destPath = [IO.Path]::Combine($WebsiteRoot, 'sitecore', 'SAFPackageInstaller', $folderKey)
 
    #Full path including the installer name
    $fullFileDestPath = Join-Path $destPath "PackageInstaller.asmx"
    
    #Forcibly cread the folder
    New-Item -ItemType Directory -Force -Path $destPath | Out-Null
 
    #Read contents of the file, and embed the security token
    (Get-Content $sourceAgentPath).replace('[TOKEN]', $accessKey) | Set-Content $fullFileDestPath
 
    #How do we get to Sitecore? This URL!
    $webURI= "$WebsiteUrl/sitecore/SAFPackageInstaller/$folderKey/packageinstaller.asmx?WSDL"
     
    Write-Output "Installing package, please wait: $webURI"
    
    #Do the install here
    $proxy = New-WebServiceProxy -uri $webURI
    $proxy.Timeout = 1800000
 
    Copy-Item -Path $PackagePath -Destination "$WebsiteRoot\App_Data\packages" -Force | Out-Null
    $packageName = Split-Path $PackagePath -Leaf
    $packageAppDataPath = "$WebsiteRoot\App_Data\packages\$packageName" 
    #Invoke our proxy
    if($packageAppDataPath.EndsWith("zip")){
        $proxy.InstallZipPackage($packageAppDataPath, $accessKey)
    }
    elseif($packageAppDataPath.EndsWith("update")){
        $proxy.InstallUpdatePackage($packageAppDataPath, $accessKey)
    }
 
    #Remove the folderKey
    Remove-Item "$destPath" -Recurse -Force | Out-Null
}

Export-ModuleMember -Function "InstallSitecore"
Export-ModuleMember -Function "InstallSitecorePackage"