-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.ps1
135 lines (112 loc) · 5.38 KB
/
init.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<#
.SYNOPSIS
Performs the initial setup procedures required for the containers.
.PARAMETER LicenseXmlPath
Specifies the fully-qualified path to the Sitecore license xml file.
#>
[CmdletBinding()]
Param (
[ValidateScript({return ![string]::IsNullOrEmpty($_)})]
[string]
$LicenseXmlPath = "C:\License\license.xml",
[string]
$HostName = "dev.local",
# 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 = "Password12345",
# 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";
if (-not (Test-Path $LicenseXmlPath)) {
throw "Did not find $LicenseXmlPath"
}
if (-not (Test-Path $LicenseXmlPath -PathType Leaf)) {
throw "$LicenseXmlPath is not a file"
}
if (-not (Test-Path ".env")) {
Write-Host "Copying new .env" -ForegroundColor Green
Copy-Item ".\docker\.env" ".env"
}
if (-not (Test-Path "docker-compose.override.yml")) {
Write-Host "Copying new docker-compose.override.yml" -ForegroundColor Green
Copy-Item ".\docker\docker-compose.override.yml" "docker-compose.override.yml"
}
##################################
# Configure TLS/HTTPS certificates
##################################
Push-Location docker\traefik\certs
try {
$certz = Join-Path -Path (Get-Location) -ChildPath "certz.exe"
if ($null -ne (Get-Command certz.exe -ErrorAction SilentlyContinue)) {
# certz installed in PATH
$certz = "certz"
} elseif (-not (Test-Path $certz)) {
Write-Host "Downloading and installing certz certificate tool..." -ForegroundColor Green
$url = "https://github.com/michaellwest/certz/releases/download/0.2/certz-0.2-win64.exe"
$webClient = New-Object System.Net.WebClient
$webClient.Downloadfile($url, $certz)
$currentHash = Get-FileHash -Path $certz -Algorithm SHA256 | Select-Object -Expand Hash
if ($currentHash -ne "D4625A4B55709DB1854DA8E1A2B93A3DF25C6F4E8FB5C0424A905029BB1FA2B6") {
Remove-Item $certz -Force
throw "Invalid certz.exe file"
}
}
Write-Host "Generating Traefik TLS certificate..." -ForegroundColor Green
& $certz create --f devcert.pfx --san "*.$($HostName)" "localhost" --p changeit --c devcert.cer --k devcert.key --days 1825
& $certz install --f devcert.pfx --p changeit --sl localmachine --sn root
}
catch {
Write-Host "An error occurred while attempting to generate TLS certificate: $_" -ForegroundColor Red
}
finally {
Pop-Location
}
Write-Host "Importing DockerToolsLite..." -ForegroundColor Green
Import-Module .\tools\DockerToolsLite
###############################
# Populate the environment file
###############################
$envPath = Join-Path -Path $PSScriptRoot -ChildPath ".env"
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
# CM_HOST
$cmHost = Get-EnvFileVariable -Variable "CM_HOST" -Path $envPath
if([string]::IsNullOrEmpty($cmHost)) {
$cmHost = "cm.$($HostName)"
Set-EnvFileVariable "CM_HOST" -Value $cmHost
}
# ID_HOST
$idHost = Get-EnvFileVariable -Variable "ID_HOST" -Path $envPath
if([string]::IsNullOrEmpty($idHost)) {
$idHost = "id.$($HostName)"
Set-EnvFileVariable "ID_HOST" -Value $idHost
}
# TELERIK_ENCRYPTION_KEY = random 64-128 chars
Set-EnvFileVariable "TELERIK_ENCRYPTION_KEY" -Value (Get-RandomString 128 -DisallowSpecial)
# MEDIA_REQUEST_PROTECTION_SHARED_SECRET
Set-EnvFileVariable "MEDIA_REQUEST_PROTECTION_SHARED_SECRET" -Value (Get-RandomString 64 -DisallowSpecial)
# SITECORE_IDSECRET = random 64 chars
Set-EnvFileVariable "SITECORE_IDSECRET" -Value (Get-RandomString 64 -DisallowSpecial)
# SITECORE_ID_CERTIFICATE
$certificatePath = Resolve-Path -Path ".\docker\traefik\certs\devcert.pfx"
$certificatePassword = Get-Content -Path (Resolve-Path -Path ".\docker\traefik\certs\devcert.password.txt") -Raw
$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList $certificatePath, $certificatePassword, ([System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable -bor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet)
$certificateBytes = $certificate.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx, $Password)
$certificateBase64String = [System.Convert]::ToBase64String($certificateBytes)
Set-EnvFileVariable "SITECORE_ID_CERTIFICATE" -Value $certificateBase64String
# SITECORE_ID_CERTIFICATE_PASSWORD
Set-EnvFileVariable "SITECORE_ID_CERTIFICATE_PASSWORD" -Value $idCertPassword
# SITECORE_LICENSE_LOCATION and SITECORE_LICENSE_PATH
$licensePath = Get-EnvFileVariable -Variable "SITECORE_LICENSE_LOCATION" -Path $envPath
if([string]::IsNullOrEmpty($licensePath)) {
Set-EnvFileVariable "SITECORE_LICENSE_LOCATION" -Value $LicenseXmlPath
Set-EnvFileVariable "SITECORE_LICENSE_PATH" -Value ([System.IO.Path]::GetDirectoryName($LicenseXmlPath))
}
Write-Host "Next try running up.ps1" -ForegroundColor Green