Skip to content

Instantly share code, notes, and snippets.

@retohugi
Created November 16, 2021 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retohugi/1f520b04c34da0e8214c7b69fe0b6d81 to your computer and use it in GitHub Desktop.
Save retohugi/1f520b04c34da0e8214c7b69fe0b6d81 to your computer and use it in GitHub Desktop.
Enhanced init.ps1 to be used as a direct replacement for the original init.ps1 in the Sitecore Docker Examples Repo (https://github.com/Sitecore/docker-examples/blob/develop/getting-started/init.ps1)
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[string]
[ValidateNotNullOrEmpty()]
$LicenseXmlPath,
[string]
$HostName = "YourProjectDefault",
# We do not need to use [SecureString] here since the value will be stored unencrypted in .env,
# and used only for transient local example environment.
[string]
$SitecoreAdminPassword = "b",
# We do not need to use [SecureString] here since the value will be stored unencrypted in .env,
# and used only for transient local example environment.
[string]
$SqlSaPassword = "Password12345"
)
$ErrorActionPreference = "Stop";
function Initialize-EnvFile() {
$envTplFile = ".env.tpl"
$envBackupFile = ".env.bak"
$envFile = ".env"
if(Test-Path $envFile) {
Copy-Item -Force .\$envFile .\$envBackupFile
}
Copy-Item -Force .\$envTplFile .\$envFile
}
# Try resolving absolute or relative paths
$LicenseXmlPath = Resolve-Path -Path $LicenseXmlPath
if (-not (Test-Path $LicenseXmlPath)) {
throw "Did not find $LicenseXmlPath"
}
if (-not (Test-Path $LicenseXmlPath -PathType Leaf)) {
throw "$LicenseXmlPath is not a file"
}
Write-Host "Using License from: $LicenseXmlPath" -ForegroundColor Green
# Check for Sitecore Gallery
Import-Module PowerShellGet
$SitecoreGallery = Get-PSRepository | Where-Object { $_.SourceLocation -eq "https://sitecore.myget.org/F/sc-powershell/api/v2" }
if (-not $SitecoreGallery) {
Write-Host "Adding Sitecore PowerShell Gallery..." -ForegroundColor Green
Register-PSRepository -Name SitecoreGallery -SourceLocation https://sitecore.myget.org/F/sc-powershell/api/v2 -InstallationPolicy Trusted
$SitecoreGallery = Get-PSRepository -Name SitecoreGallery
}
# Install and Import SitecoreDockerTools
$dockerToolsVersion = "10.1.4"
Remove-Module SitecoreDockerTools -ErrorAction SilentlyContinue
if (-not (Get-InstalledModule -Name SitecoreDockerTools -RequiredVersion $dockerToolsVersion -ErrorAction SilentlyContinue)) {
Write-Host "Installing SitecoreDockerTools..." -ForegroundColor Green
Install-Module SitecoreDockerTools -RequiredVersion $dockerToolsVersion -Scope CurrentUser -Repository $SitecoreGallery.Name
}
Write-Host "Importing SitecoreDockerTools..." -ForegroundColor Green
Import-Module SitecoreDockerTools -RequiredVersion $dockerToolsVersion
Write-SitecoreDockerWelcome
###############################
# Populate the environment file
###############################
Initialize-EnvFile;
Write-Host "Populating required .env file variables..." -ForegroundColor Green
# SITECORE_ADMIN_PASSWORD
Set-EnvFileVariable "SITECORE_ADMIN_PASSWORD" -Value $SitecoreAdminPassword
# SQL_SA_PASSWORD
Set-EnvFileVariable "SQL_SA_PASSWORD" -Value $SqlSaPassword
# CD_HOST
Set-EnvFileVariable "CD_HOST" -Value "cd.$($HostName).localhost"
# CM_HOST
Set-EnvFileVariable "CM_HOST" -Value "cm.$($HostName).localhost"
# ID_HOST
Set-EnvFileVariable "ID_HOST" -Value "id.$($HostName).localhost"
# HRZ_HOST
Set-EnvFileVariable "HRZ_HOST" -Value "hrz.$($HostName).localhost"
# REPORTING_API_KEY = random 64-128 chars
Set-EnvFileVariable "REPORTING_API_KEY" -Value (Get-SitecoreRandomString 64 -DisallowSpecial)
# TELERIK_ENCRYPTION_KEY = random 64-128 chars
Set-EnvFileVariable "TELERIK_ENCRYPTION_KEY" -Value (Get-SitecoreRandomString 128)
# MEDIA_REQUEST_PROTECTION_SHARED_SECRET
Set-EnvFileVariable "MEDIA_REQUEST_PROTECTION_SHARED_SECRET" -Value (Get-SitecoreRandomString 64)
# SITECORE_IDSECRET = random 64 chars
Set-EnvFileVariable "SITECORE_IDSECRET" -Value (Get-SitecoreRandomString 64 -DisallowSpecial)
# SITECORE_ID_CERTIFICATE
$idCertPassword = Get-SitecoreRandomString 12 -DisallowSpecial
Set-EnvFileVariable "SITECORE_ID_CERTIFICATE" -Value (Get-SitecoreCertificateAsBase64String -DnsName "localhost" -Password (ConvertTo-SecureString -String $idCertPassword -Force -AsPlainText))
# SITECORE_ID_CERTIFICATE_PASSWORD
Set-EnvFileVariable "SITECORE_ID_CERTIFICATE_PASSWORD" -Value $idCertPassword
# SITECORE_LICENSE
Set-EnvFileVariable "SITECORE_LICENSE" -Value (ConvertTo-CompressedBase64String -Path $LicenseXmlPath)
##################################
# Configure TLS/HTTPS certificates
##################################
Push-Location docker\traefik\certs
try {
$mkcert = ".\mkcert.exe"
if ($null -ne (Get-Command mkcert.exe -ErrorAction SilentlyContinue)) {
# mkcert installed in PATH
$mkcert = "mkcert"
} elseif (-not (Test-Path $mkcert)) {
Write-Host "Downloading and installing mkcert certificate tool..." -ForegroundColor Green
Invoke-WebRequest "https://github.com/FiloSottile/mkcert/releases/download/v1.4.1/mkcert-v1.4.1-windows-amd64.exe" -UseBasicParsing -OutFile mkcert.exe
if ((Get-FileHash mkcert.exe).Hash -ne "1BE92F598145F61CA67DD9F5C687DFEC17953548D013715FF54067B34D7C3246") {
Remove-Item mkcert.exe -Force
throw "Invalid mkcert.exe file"
}
}
Write-Host "Generating Traefik TLS certificate..." -ForegroundColor Green
& $mkcert -install
& $mkcert -key-file key.pem -cert-file cert.pem "*.$($HostName).localhost"
}
catch {
Write-Host "An error occurred while attempting to generate TLS certificate: $_" -ForegroundColor Red
}
finally {
Pop-Location
}
################################
# Add Windows hosts file entries
################################
Write-Host "Adding Windows hosts file entries..." -ForegroundColor Green
Add-HostsEntry "cm.$($HostName).localhost"
Add-HostsEntry "id.$($HostName).localhost"
Add-HostsEntry "cd.$($HostName).localhost" # remove if you do not run CD containers locally
Add-HostsEntry "horizon.$($HostName).localhost" # remove if you do not use Horizon
Write-Host "Done!" -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment