forked from Sitecore/XM-Cloud-Introduction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.ps1
306 lines (254 loc) · 11.5 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#Requires -RunAsAdministrator
[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,
[Parameter(Mandatory = $true,
HelpMessage = "The Client ID used to authenticate with Auth0.",
ParameterSetName = "env-init")]
[string]$Auth0_ClientId,
[Parameter(Mandatory = $true,
HelpMessage = "The Client Secret used to authenticate with Auth0.",
ParameterSetName = "env-init")]
[string]$Auth0_ClientSecret,
[Parameter(Mandatory = $true,
HelpMessage = "The Domain used to authenticate with Auth0.",
ParameterSetName = "env-init")]
[string]$Auth0_Domain,
[Parameter(Mandatory = $true,
HelpMessage = "The Audience used to authenticate with Auth0.",
ParameterSetName = "env-init")]
[string]$Auth0_Audience,
[Parameter(Mandatory = $true,
HelpMessage = "The Default Authority used to connect to XM Cloud.",
ParameterSetName = "env-init")]
[string]$XMCloud_Default_Authority,
[Parameter(Mandatory = $true,
HelpMessage = "The ClientId used to connect to XM Cloud.",
ParameterSetName = "env-init")]
[string]$XMCloud_ClientId,
[Parameter(Mandatory = $true,
HelpMessage = "The Deploy Application used to connect to XM Cloud.",
ParameterSetName = "env-init")]
[string]$XMCloud_Deploy_App,
[Parameter(Mandatory = $true,
HelpMessage = "The Deploy EndPoint used to connect to XM Cloud.",
ParameterSetName = "env-init")]
[string]$XMCloud_Deploy_EndPoint,
[Parameter(Mandatory = $true,
HelpMessage = "The Audience used to connect to XM Cloud.",
ParameterSetName = "env-init")]
[string]$XMCloud_Audience,
[Parameter(Mandatory = $true,
HelpMessage = "The Audience Client Credentials used to connect to XM Cloud.",
ParameterSetName = "env-init")]
[String]$XMCloud_Audience_Client_Credentials,
[Parameter(HelpMessage = "The hostname used for the local CM instance.",
ParameterSetName = "env-init")]
[string]$Host_Suffix = "xmcloudcm.localhost",
[Parameter(HelpMessage = "The Edge url used when running in Edge Mode.",
ParameterSetName = "env-init")]
[string]$Edge_Url,
[Parameter(HelpMessage = "The Edge token used when running in Edge Mode.",
ParameterSetName = "env-init")]
[string]$Edge_Token
)
$ErrorActionPreference = "Stop";
###############
# License Check
###############
Write-Host "Checking for existence of License." -ForegroundColor Green
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
##################
# Create .env file
##################
Write-Host "Creating Environment File..." -ForegroundColor Green
Copy-Item ".\.env.template" ".\.env" -Force
################################################
# Retrieve and import SitecoreDockerTools module
################################################
# 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
Unregister-PSRepository -Name SitecoreGallery -ErrorAction SilentlyContinue
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.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
###########################
# Setup site host variables
###########################
Write-Host "Setting Hosts Values..." -ForegroundColor Green
$CM_Host = $Host_Suffix
$MVP_Host = "mvp.$Host_Suffix"
$SUGCON_EU_HOST = "sugconeu.$Host_Suffix"
$SUGCON_ANZ_HOST = "sugconanz.$Host_Suffix"
##################################
# 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 "*.$Host_Suffix"
& $mkcert $Host_Suffix
# 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
Add-HostsEntry $CM_Host
Add-HostsEntry $MVP_Host
Add-HostsEntry $SUGCON_EU_HOST
Add-HostsEntry $SUGCON_ANZ_HOST
###############################
# Generate scjssconfig
###############################
$xmCloudBuild = Get-Content "xmcloud.build.json" | ConvertFrom-Json
$scjssconfig = @{
sitecore= @{
deploySecret = $xmCloudBuild.renderingHosts.xmcloudpreview.jssDeploymentSecret
deployUrl = "https://xmcloudcm.localhost/sitecore/api/jss/im.port"
}
}
# ConvertTo-Json -InputObject $scjssconfig | Out-File -FilePath "src\rendering\scjssconfig.json"
# Set-EnvFileVariable "JSS_DEPLOYMENT_SECRET_xmcloudpreview" -Value $xmCloudBuild.renderingHosts.xmcloudpreview.jssDeploymentSecret
################################
# Generate Sitecore Api Key
################################
$sitecoreApiKey = (New-Guid).Guid
Set-EnvFileVariable "SITECORE_API_KEY_xmcloudpreview" -Value $sitecoreApiKey
################################
# Generate JSS_EDITING_SECRET
################################
$jssEditingSecret = Get-SitecoreRandomString 64 -DisallowSpecial
Set-EnvFileVariable "JSS_EDITING_SECRET" -Value $jssEditingSecret
###############################
# Populate the environment file
###############################
if ($InitEnv) {
Write-Host "Populating Environment File..." -ForegroundColor Green
# HOST_LICENSE_FOLDER
Set-EnvFileVariable "HOST_LICENSE_FOLDER" -Value $LicenseXmlPath
# CM_HOST
Set-EnvFileVariable "CM_HOST" -Value $CM_Host
# MVP_RENDERING_HOST
Set-EnvFileVariable "MVP_RENDERING_HOST" -Value $MVP_Host
# REPORTING_API_KEY
Set-EnvFileVariable "REPORTING_API_KEY" -Value (Get-SitecoreRandomString 128 -DisallowSpecial)
# TELERIK_ENCRYPTION_KEY
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)
# SQL_SA_PASSWORD
# Need to ensure it meets SQL complexity requirements
Set-EnvFileVariable "SQL_SA_PASSWORD" -Value (Get-SitecoreRandomString 19 -DisallowSpecial -EnforceComplexity)
# SQL_SERVER
Set-EnvFileVariable "SQL_SERVER" -Value "mssql"
# SQL_SA_LOGIN
Set-EnvFileVariable "SQL_SA_LOGIN" -Value "sa"
# SITECORE_ADMIN_PASSWORD
Set-EnvFileVariable "SITECORE_ADMIN_PASSWORD" -Value $AdminPassword
# SITECORE_FedAuth_dot_Auth0_dot_ClientId
Set-EnvFileVariable "SITECORE_FedAuth_dot_Auth0_dot_ClientId" -Value $Auth0_ClientId
# SITECORE_FedAuth_dot_Auth0_dot_ClientSecret
Set-EnvFileVariable "SITECORE_FedAuth_dot_Auth0_dot_ClientSecret" -Value $Auth0_ClientSecret
# SITECORE_FedAuth_dot_Auth0_dot_RedirectBaseUrl
Set-EnvFileVariable "SITECORE_FedAuth_dot_Auth0_dot_RedirectBaseUrl" -Value "https://$CM_Host/"
# SITECORE_FedAuth_dot_Auth0_dot_Domain
Set-EnvFileVariable "SITECORE_FedAuth_dot_Auth0_dot_Domain" -Value $Auth0_Domain
# SITECORE_FedAuth_dot_Auth0_dot_Audience
Set-EnvFileVariable "SITECORE_FedAuth_dot_Auth0_dot_Audience" -Value $Auth0_Audience
# EXPERIENCE_EDGE_URL
Set-EnvFileVariable "EXPERIENCE_EDGE_URL" -Value $Edge_Url
# EXPERIENCE_EDGE_TOKEN
Set-EnvFileVariable "EXPERIENCE_EDGE_TOKEN" -Value $Edge_Token
# JSS_EDITING_SECRET
Set-EnvFileVariable "JSS_EDITING_SECRET" -Value (Get-SitecoreRandomString 64 -DisallowSpecial)
}
Write-Host "Done!" -ForegroundColor Green
#####################################################
# TEMP Step: Populate xmcloud.plugin.pre-release.json
#####################################################
Write-Host "Populating xmcloud.plugin.pre-release.json..." -ForegroundColor Green
Copy-Item ".\xmcloud.plugin.pre-release.json.template" ".\xmcloud.plugin.pre-release.json" -Force
$envFile = Join-Path $PWD '.env'
$xmCloudDeployConfig = Get-EnvFileVariable -Path $envFile -Variable 'XMCLOUD_DEPLOY_CONFIG'
$json = Get-Content $xmCloudDeployConfig | ConvertFrom-Json
$json.defaultAuthority = $XMCloud_Default_Authority
$json.clientId = $XMCloud_ClientId
$json.xmCloudDeployApp = $XMCloud_Deploy_App
$json.xmCloudDeployEndpoint = $XMCloud_Deploy_EndPoint
$json.audience = $XMCloud_Audience
$json.audienceClientCredentials = $XMCloud_Audience_Client_Credentials
$json | ConvertTo-Json | Out-File $xmCloudDeployConfig
##########################
# Show Certificate Details
##########################
Push-Location docker\traefik\certs
try
{
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
}
catch {
Write-Error "An error occurred while attempting to generate TLS certificate: $_"
}
finally {
Pop-Location
}