Skip to content

Instantly share code, notes, and snippets.

@kimcuhoang
Last active November 22, 2018 19:47
Show Gist options
  • Save kimcuhoang/27c2563dc8b9dc98eed1db41de1603cd to your computer and use it in GitHub Desktop.
Save kimcuhoang/27c2563dc8b9dc98eed1db41de1603cd to your computer and use it in GitHub Desktop.
Param(
[string]$siteName = "DemoHome",
[string]$siteSuffix = $siteName.ToLower(),
[string]$siteHostName = "$($siteSuffix).dev.local",
[string]$engineSuffix = $siteName.ToLower(),
[string]$engineHostName = "localhost",
[string]$identityServerHost = "$($engineHostName):5050",
[switch]$Initialize,
[switch]$Bootstrap,
[switch]$SkipPublish,
[string]$webRoot = "E:\Inetpub\wwwroot",
[string[]] $engines = @("Authoring", "Minions", "Ops", "Shops"),
[string]$CommerceOpsPort = "5000",
[string]$adminUser = "admin",
[string]$adminPassword = "b",
[string]$publishFolder = (Join-Path $PWD "publishTemp"),
[string]$certificateName = "$($siteHostName).xConnect.Client"
)
$ErrorActionPreference = 'Stop'
Function Start-CommerceEngineCompile ( [string] $basePublishPath = $(Join-Path $publishFolder "engine") )
{
$engineSolutionName = "DemoHome.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") )
{
$certPrefix = "CN="
$fullCertificateName = $certPrefix + $certificateName
$thumbprint = Get-ChildItem -path cert:\LocalMachine\my | Where-Object {$_.Subject -like $fullCertificateName} | Select-Object Thumbprint
foreach ($engine in $engines) {
Write-Host ("Customizing configuration values for {0}" -f $engine) -ForegroundColor Green
$engineFullName = ("Commerce{0}" -f $engine)
$environmentName = "$($siteName)$($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
$appSettings.SitecoreIdentityServerUrl = ("https://{0}" -f $identityServerHost)
$certificateNode = $config.Certificates.Certificates[0]
$certificateNode.Thumbprint = $thumbprint.Thumbprint
$config | ConvertTo-Json -Depth 10 | 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 = @("$($siteName)Authoring")
$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
}
else {
Write-Host "Cleaning for $($env) failed" -ForegroundColor Red
Exit -1
}
}
}
Function BootStrapCommerceServices {
$UrlCommerceShopsServicesBootstrap = ("https://{0}:{1}/commerceops/Bootstrap()" -f $engineHostName, $CommerceOpsPort)
Write-Host "BootStrapping Commerce Services: $($urlCommerceShopsServicesBootstrap)" -ForegroundColor Green
$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(environment='envNameValue')"
$UrlInitializeEnvironment = ("https://{0}:{1}{2}" -f $engineHostName, $CommerceOpsPort, $initializeParam)
$UrlCheckCommandStatus = ("https://{0}:{1}{2}" -f $engineHostName, $CommerceOpsPort, "/commerceops/CheckCommandStatus(taskId=taskIdValue)")
$Environments = @("$($siteName)Authoring")
$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 -replace "envNameValue", $env
$result = Invoke-RestMethod $initializeUrl -TimeoutSec 1200 -Method Get -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 ($DeployOnly) {
}
if (!($SkipPublish)) {
Start-CommerceEngineCompile
Start-CommerceEnginePepare
Publish-CommerceEngine
}
if ($Bootstrap -or $Initialize) {
Get-IdServerToken
}
if ($Bootstrap) {
BootStrapCommerceServices
}
if ($Initialize) {
CleanEnvironment
InitializeCommerceServices
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment