-
Notifications
You must be signed in to change notification settings - Fork 73
/
init.ps1
186 lines (149 loc) · 7.6 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
[CmdletBinding(DefaultParameterSetName = "no-arguments")]
Param (
[Parameter(HelpMessage = "Enables initialization of values in the .env file, which may be placed in source control.",
ParameterSetName = "env-init")]
[switch]$InitEnv,
[Parameter(Mandatory = $true,
HelpMessage = "The path to a valid Sitecore license.xml file.",
ParameterSetName = "env-init")]
[string]$LicenseXmlPath,
# We do not need to use [SecureString] here since the value will be stored unencrypted in .env,
# and used only for transient local development environments.
[Parameter(Mandatory = $true,
HelpMessage = "Sets the sitecore\\admin password for this environment via environment variable.",
ParameterSetName = "env-init")]
[string]$AdminPassword
)
$ErrorActionPreference = "Stop";
if ($InitEnv) {
if (-not $LicenseXmlPath.EndsWith("license.xml")) {
Write-Error "Sitecore license file must be named 'license.xml'."
}
if (-not (Test-Path $LicenseXmlPath)) {
Write-Error "Could not find Sitecore license file at path '$LicenseXmlPath'."
}
# We actually want the folder that it's in for mounting
$LicenseXmlPath = (Get-Item $LicenseXmlPath).Directory.FullName
}
Write-Host "Preparing your Sitecore Containers environment!" -ForegroundColor Green
################################################
# Retrieve and import SitecoreDockerTools module
################################################
# Check for Sitecore Gallery
Import-Module PowerShellGet
$SitecoreGallery = Get-PSRepository | Where-Object { $_.Name -eq "SitecoreGallery"}
if (-not $SitecoreGallery) {
Write-Host "Adding Sitecore PowerShell Gallery..." -ForegroundColor Green
# DEMO TEAM CUSTOMIZATION - Sync with XM Cloud. Unregister the repository
Unregister-PSRepository -Name SitecoreGallery -ErrorAction SilentlyContinue
Register-PSRepository -Name SitecoreGallery -SourceLocation https://nuget.sitecore.com/resources/v2 -InstallationPolicy Trusted
$SitecoreGallery = Get-PSRepository -Name SitecoreGallery
}
# Install and Import SitecoreDockerTools
$dockerToolsVersion = "10.2.7"
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
##################################
# 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
# DEMO TEAM CUSTOMIZATION - Custom host name
& $mkcert "*.edge.localhost"
# stash CAROOT path for messaging at the end of the script
$caRoot = "$(& $mkcert -CAROOT)\rootCA.pem"
}
catch {
Write-Error "An error occurred while attempting to generate TLS certificate: $_"
}
finally {
Pop-Location
}
################################
# Add Windows hosts file entries
################################
Write-Host "Adding Windows hosts file entries..." -ForegroundColor Green
# DEMO TEAM CUSTOMIZATION - Custom host names.
Add-HostsEntry "cm.edge.localhost"
Add-HostsEntry "cd.edge.localhost"
Add-HostsEntry "id.edge.localhost"
Add-HostsEntry "sh.edge.localhost"
Add-HostsEntry "www.edge.localhost"
###############################
# Populate the environment file
###############################
if ($InitEnv) {
Write-Host "Populating required .env file values..." -ForegroundColor Green
# HOST_LICENSE_FOLDER
Set-EnvFileVariable "HOST_LICENSE_FOLDER" -Value $LicenseXmlPath
# CM_HOST
# DEMO TEAM CUSTOMIZATION - Custom host name
Set-EnvFileVariable "CM_HOST" -Value "cm.edge.localhost"
# ID_HOST
# DEMO TEAM CUSTOMIZATION - Custom host name
Set-EnvFileVariable "ID_HOST" -Value "id.edge.localhost"
# RENDERING_HOST
# DEMO TEAM CUSTOMIZATION - Custom host name
Set-EnvFileVariable "RENDERING_HOST" -Value "www.edge.localhost"
# REPORTING_API_KEY = random 64-128 chars
Set-EnvFileVariable "REPORTING_API_KEY" -Value (Get-SitecoreRandomString 128 -DisallowSpecial)
# TELERIK_ENCRYPTION_KEY = random 64-128 chars
# DEMO TEAM CUSTOMIZATION - Add -DisallowSpecial
Set-EnvFileVariable "TELERIK_ENCRYPTION_KEY" -Value (Get-SitecoreRandomString 128 -DisallowSpecial)
# MEDIA_REQUEST_PROTECTION_SHARED_SECRET
# DEMO TEAM CUSTOMIZATION - Add -DisallowSpecial
Set-EnvFileVariable "MEDIA_REQUEST_PROTECTION_SHARED_SECRET" -Value (Get-SitecoreRandomString 64 -DisallowSpecial)
# SITECORE_IDSECRET = random 64 chars
Set-EnvFileVariable "SITECORE_IDSECRET" -Value (Get-SitecoreRandomString 64 -DisallowSpecial)
# SITECORE_ID_CERTIFICATE
$idCertPassword = Get-SitecoreRandomString 8 -DisallowSpecial
Set-EnvFileVariable "SITECORE_ID_CERTIFICATE" -Value (Get-SitecoreCertificateAsBase64String -KeyLength 2048 -DnsName "localhost" -Password (ConvertTo-SecureString -String $idCertPassword -Force -AsPlainText))
# SITECORE_ID_CERTIFICATE_PASSWORD
Set-EnvFileVariable "SITECORE_ID_CERTIFICATE_PASSWORD" -Value $idCertPassword
# SQL_SA_PASSWORD
# Need to ensure it meets SQL complexity requirements
Set-EnvFileVariable "SQL_SA_PASSWORD" -Value (Get-SitecoreRandomString 19 -DisallowSpecial -EnforceComplexity)
# SITECORE_ADMIN_PASSWORD
Set-EnvFileVariable "SITECORE_ADMIN_PASSWORD" -Value $AdminPassword
# DEMO TEAM CUSTOMIZATION - Add a Sitecore user password
# SITECORE_USER_PASSWORD (same as admin in local env.)
Set-EnvFileVariable "SITECORE_USER_PASSWORD" -Value $AdminPassword
# JSS_EDITING_SECRET
# Populate it for the Next.js local environment as well
$jssEditingSecret = Get-SitecoreRandomString 64 -DisallowSpecial
Set-EnvFileVariable "JSS_EDITING_SECRET" -Value $jssEditingSecret
# DEMO TEAM CUSTOMIZATION - Moved the Docker files up one level.
Set-EnvFileVariable "JSS_EDITING_SECRET" -Value $jssEditingSecret -Path .\Website\src\rendering\.env
# DEMO TEAM CUSTOMIZATION - Non-interactive CLI login
$clientSecret = Get-SitecoreRandomString 64 -DisallowSpecial
Set-EnvFileVariable "ID_SERVER_DEMO_CLIENT_SECRET" -Value $clientSecret
}
Write-Host "Done!" -ForegroundColor Green
Write-Host
Write-Host ("#"*75) -ForegroundColor Cyan
Write-Host "To avoid HTTPS errors, set the NODE_EXTRA_CA_CERTS environment variable" -ForegroundColor Cyan
Write-Host "using the following commmand:" -ForegroundColor Cyan
Write-Host "setx NODE_EXTRA_CA_CERTS $caRoot"
Write-Host
Write-Host "You will need to restart your terminal or VS Code for it to take effect." -ForegroundColor Cyan
Write-Host ("#"*75) -ForegroundColor Cyan