-
Notifications
You must be signed in to change notification settings - Fork 21
/
deploy-commerce-engine.ps1
237 lines (188 loc) · 9.04 KB
/
deploy-commerce-engine.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
Param(
[string]$siteName = "habitathome.dev.local",
[string]$engineHostName = "localhost",
[string]$identityServerHost = "habitathome-identityserver.dev.local",
[switch]$Initialize,
[switch]$Bootstrap,
[switch]$SkipPublish,
[string]$webRoot = "C:\inetpub\wwwroot",
[string[]] $engines = @("Authoring", "Minions", "Ops", "Shops"),
[string]$BizFxPathName = "habitathome-SitecoreBizFx.dev.local",
[string]$IdentityServerPathName = "SitecoreIdentityServer",
[string]$engineSuffix = "habitathome",
[string]$CommerceOpsPort = "5000",
[string]$adminUser = "admin",
[string]$adminPassword = "b",
[string]$certificateName = "habitathomestorefront.engine",
[string]$publishFolder = (Join-Path $PWD "publishTemp")
)
Function Start-CommerceEngineCompile ( [string] $basePublishPath = $(Join-Path $publishFolder "engine") ) {
$engineSolutionName = "HabitatHome.Commerce.Engine.sln"
if (Test-Path $publishFolder) {
Remove-Item $publishFolder -Recurse -Force
}
Write-Host ("Compiling and Publishing Engine to {0}" -f $basePublishPath) -ForegroundColor Green
dotnet publish $engineSolutionName -o $basePublishPath
}
Function Start-CommerceEnginePepare ( [string] $basePublishPath = $(Join-Path $publishFolder "engine") ) {
$thumbprint = Get-ChildItem -path cert:\LocalMachine\my | Where-Object {$_.FriendlyName -like $certificateName} | Select-Object Thumbprint
$pathToGlobalJson = $(Join-Path -Path $basePublishPath -ChildPath "wwwroot\bootstrap\Global.json")
$global = Get-Content $pathToGlobalJson -Raw | ConvertFrom-Json
$global.Policies.'$values'[6].Host = $siteName
$global.Policies.'$values'[6].UserName = $adminUser
$global.Policies.'$values'[6].Password = $adminPassword
$global | ConvertTo-Json -Depth 10 -Compress | set-Content $pathToGlobalJson
$pathToJson = $(Join-Path -Path $basePublishPath -ChildPath "wwwroot\config.json")
$config = Get-Content $pathToJson -Raw | ConvertFrom-Json
$certificateNode = $config.Certificates.Certificates[0]
$certificateNode.Thumbprint = $thumbprint.Thumbprint
$appSettings = $config.AppSettings
$appSettings.allowedOrigins = $appSettings.allowedOrigins.replace('localhost', $engineHostName)
$appSettings.allowedOrigins = $appSettings.allowedOrigins.replace('habitathome.dev.local', $siteName)
$appSettings.SitecoreIdentityServerUrl = ("https://{0}" -f $identityServerHost)
$config | ConvertTo-Json -Depth 10 -Compress | set-content $pathToJson
# Modify PlugIn.Content.PolicySet
$pathToContentPolicySet = $(Join-Path -Path $basePublishPath -ChildPath "wwwroot\data\environments\PlugIn.Content.PolicySet-1.0.0.json")
$contentPolicySet = Get-Content $pathToContentPolicySet -Raw | ConvertFrom-Json
$contentPolicySet.Policies.'$values'[0].Host = $siteName
$contentPolicySet.Policies.'$values'[0].username = $adminUser
$contentPolicySet.Policies.'$values'[0].password = $adminPassword
$contentPolicySet | ConvertTo-Json -Depth 10 -Compress | Set-Content $pathToContentPolicySet
foreach ($engine in $engines) {
Write-Host ("Customizing configuration values for {0}" -f $engine) -ForegroundColor Green
$engineFullName = ("Commerce{0}" -f $engine)
$environmentName = ("Habitat{0}" -f $engine)
$enginePath = Join-Path $publishFolder $engineFullName
Copy-Item $basePublishPath $enginePath -Recurse -Force
$pathToJson = $(Join-Path -Path $enginePath -ChildPath "wwwroot\config.json")
$config = Get-Content $pathToJson -Raw | ConvertFrom-Json
$appSettings = $config.AppSettings
$appSettings.EnvironmentName = $environmentName
$config | ConvertTo-Json -Depth 10 -Compress | set-content $pathToJson
}
}
Function Publish-CommerceEngine {
Write-Host ("Deploying Commerce Engine") -ForegroundColor Green
IISRESET /STOP
foreach ($engine in $engines) {
$engineFullName = ("Commerce{0}" -f $engine)
$enginePath = Join-Path $publishFolder $engineFullName
if ($engineSuffix.length -gt 0) {
$engineWebRoot = Join-Path $webRoot $($engineFullName + "_" + $engineSuffix)
}
else {
$engineWebRoot = [System.IO.Path]::Combine($webRoot, $engineFullName)
}
Write-Host ("Copying to {0}" -f $engineWebRoot) -ForegroundColor Green
$engineWebRootBackup = ("{0}_backup" -f $engineWebRoot)
if (Test-Path $engineWebRootBackup -PathType Container) {
Remove-Item $engineWebRootBackup -Recurse -Force
}
Rename-Item $engineWebRoot -NewName $engineWebRootBackup
Get-ChildItem $engineWebRoot -Recurse | ForEach-Object {Remove-Item $_.FullName -Recurse}
Copy-Item -Path "$enginePath" -Destination $engineWebRoot -Container -Recurse -Force
}
IISRESET /START
Start-Sleep 10
}
Function Get-IdServerToken {
$UrlIdentityServerGetToken = ("https://{0}/connect/token" -f $identityServerHost)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", 'application/x-www-form-urlencoded')
$headers.Add("Accept", 'application/json')
$body = @{
password = "$adminPassword"
grant_type = 'password'
username = ("sitecore\{0}" -f $adminUser)
client_id = 'postman-api'
scope = 'openid EngineAPI postman_api'
}
Write-Host "Getting Identity Token From Sitecore.IdentityServer" -ForegroundColor Green
$response = Invoke-RestMethod $UrlIdentityServerGetToken -Method Post -Body $body -Headers $headers
$sitecoreIdToken = "Bearer {0}" -f $response.access_token
$global:sitecoreIdToken = $sitecoreIdToken
Write-Host $global:sitecoreIdToken
}
Function CleanEnvironment {
Write-Host "Cleaning Environments" -ForegroundColor Green
$initializeParam = "/commerceops/CleanEnvironment()"
$initializeUrl = ("https://{0}:{1}{2}" -f $engineHostName, $CommerceOpsPort, $initializeParam)
$Environments = @("HabitatAuthoring", "HabitatMinions")
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $global:sitecoreIdToken);
foreach ($env in $Environments) {
Write-Host "Cleaning $($env) ..." -ForegroundColor Yellow
$body = @{
environment = $env
}
$result = Invoke-RestMethod $initializeUrl -TimeoutSec 1200 -Method Post -Headers $headers -Body ($body | ConvertTo-Json) -ContentType "application/json"
if ($result.ResponseCode -eq "Ok") {
Write-Host "Cleaning for $($env) completed successfully" -ForegroundColor Green
Write-Host "Flushing Redis caches..."
Invoke-Command -ScriptBlock { redis-cli flushall }
Write-Host "Flushing Redis caches completed."
}
else {
Write-Host "Cleaning for $($env) failed" -ForegroundColor Red
Exit -1
}
}
}
Function BootStrapCommerceServices {
Write-Host "BootStrapping Commerce Services: $($urlCommerceShopsServicesBootstrap)" -ForegroundColor Green
$UrlCommerceShopsServicesBootstrap = ("https://{0}:{1}/commerceops/Bootstrap()" -f $engineHostName, $CommerceOpsPort)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $global:sitecoreIdToken)
Invoke-RestMethod $UrlCommerceShopsServicesBootstrap -TimeoutSec 1200 -Method PUT -Headers $headers
Write-Host "Commerce Services BootStrapping completed" -ForegroundColor Green
}
Function InitializeCommerceServices {
Write-Host "Initializing Environments" -ForegroundColor Green
$initializeParam = "/commerceops/InitializeEnvironment()"
$UrlInitializeEnvironment = ("https://{0}:{1}{2}" -f $engineHostName, $CommerceOpsPort, $initializeParam)
$UrlCheckCommandStatus = ("https://{0}:{1}{2}" -f $engineHostName, $CommerceOpsPort, "/commerceops/CheckCommandStatus(taskId=taskIdValue)")
Write-Host $UrlInitializeEnvironment
$Environments = @("HabitatAuthoring", "HabitatMinions")
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $global:sitecoreIdToken);
foreach ($env in $Environments) {
Write-Host "Initializing $($env) ..." -ForegroundColor Yellow
$initializeUrl = $UrlInitializeEnvironment
$payload = @{
"environment" = $env;
}
$result = Invoke-RestMethod $initializeUrl -TimeoutSec 1200 -Method POST -Body ($payload|ConvertTo-Json) -Headers $headers -ContentType "application/json"
$checkUrl = $UrlCheckCommandStatus -replace "taskIdValue", $result.TaskId
$sw = [system.diagnostics.stopwatch]::StartNew()
$tp = New-TimeSpan -Minute 10
do {
Start-Sleep -s 30
Write-Host "Checking if $($checkUrl) has completed ..." -ForegroundColor White
$result = Invoke-RestMethod $checkUrl -TimeoutSec 1200 -Method Get -Headers $headers -ContentType "application/json"
if ($result.ResponseCode -ne "Ok") {
$(throw Write-Host "Initialize environment $($env) failed, please check Engine service logs for more info." -Foregroundcolor Red)
}
else {
write-Host $result.ResponseCode
Write-Host $result.Status
}
} while ($result.Status -ne "RanToCompletion" -and $sw.Elapsed -le $tp)
Write-Host "Initialization for $($env) completed ..." -ForegroundColor Green
}
Write-Host "Initialization completed ..." -ForegroundColor Green
}
if (!($SkipPublish)) {
Start-CommerceEngineCompile
Start-CommerceEnginePepare
Publish-CommerceEngine
}
if ($Bootstrap -or $Initialize) {
Get-IdServerToken
}
if ($Bootstrap) {
BootStrapCommerceServices
}
if ($Initialize) {
CleanEnvironment
InitializeCommerceServices
}