-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdeploy-xm1-initial.ps1
245 lines (195 loc) · 9.49 KB
/
deploy-xm1-initial.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
#
# Deploy Sitecore 8.2u5 xM1 infrastructure components on ASP - leverage original ARM as shared on
# https://github.com/Sitecore/Sitecore-Azure-Quickstart-Templates/tree/master/Sitecore%208.2.3
#
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$SubscriptionName,
[Parameter(Mandatory=$True)]
[string]$RGName,
[Parameter(Mandatory=$True)]
[string]$Location,
[Parameter(Mandatory=$True)]
[string]$ResourcePrefix,
[Parameter(Mandatory=$True)]
[string]$SitecorePwd,
[Parameter(Mandatory=$True)]
[string]$SqlServerLogin,
[Parameter(Mandatory=$True)]
[string]$SqlServerPwd,
[Parameter(Mandatory=$True)]
[string]$StorageAccountNameDeploy,
[Parameter(Mandatory=$False)]
[string]$KeyVaultAdminMailAddressOrObjectId, #if e-mail address supplied we assume get by ADUser, use ID supplied
[Parameter(Mandatory=$False)]
[string]$PathToSitecoreLicenseFile = "$(Split-Path $MyInvocation.MyCommand.Path)\license.xml",
[Parameter(Mandatory=$False)]
[string]$ArtifactsRootDir, #optional for running locally, required for used by VSTS to pass current artifacts root dir
[Parameter(Mandatory=$false)]
[switch]$LeaveTempFilesOnDisk
)
$ErrorActionPreference = "Stop"
# Check if given license file exists
if ( ! (test-path -pathtype leaf $PathToSitecoreLicenseFile)) {
throw "LICENSE FILE DOES NOT EXIST - PLEASE SPECIFY VALID LICENSE FILE"
}
# If the KeyVaultAdminMailAddressOrObjectId is an e-mailaddress
if($KeyVaultAdminMailAddressOrObjectId)
{
if ($KeyVaultAdminMailAddressOrObjectId -match ".+\@.+\..+"){
$KeyVaultUserId = (Get-AzureRmADUser -Mail "$KeyVaultAdminMailAddressOrObjectId" | Select -Expand Id).ToString()
Write-Host "Keyvault Admin User ID selected via e-mail: $KeyVaultUserId"
}else{
#we assume it's an ServicePrincipal ObjectId or a User ObjectId - Application ID will not work
Write-Host "KeyVaultAdminMailAddressOrObjectId passed in argument as ID"
$KeyVaultUserId = $KeyVaultAdminMailAddressOrObjectId
}
}
else{
$account = $(Get-AzureRmContext).Account
Write-Host "Account type: $account"
if($account.AccountType -eq "User"){
$KeyVaultUserId = $(Get-AzureRmADUser -UserPrincipalName $account.Id).Id
Write-Host "Using the Keyvault ObjectId gotten via current user context: $KeyVaultUserId"
}else{
throw "Current context is probably a Service Principal. Are you running this in VSTS? Please supply the VSTS Service principal ObjectId through argument -KeyVaultAdminMailAddressOrObjectId"
}
}
#when running local code, use relative path to script, if on VSTS, the artifacts dir is different
if ($ArtifactsRootDir){
$scriptDir = $ArtifactsRootDir
}else{
$scriptDir = Split-Path $MyInvocation.MyCommand.Path
}
Select-AzureRmSubscription -SubscriptionName $SubscriptionName
Write-Host "Selected subscription: $SubscriptionName"
# Find existing or deploy new Resource Group:
$rg = Get-AzureRmResourceGroup -Name $RGName -ErrorAction SilentlyContinue
if (-not $rg)
{
New-AzureRmResourceGroup -Name "$RGName" -Location "$Location"
Write-Host "New resource group deployed: $RGName"
}
else{ Write-Host "Resource group found: $RGName"}
#============================
# Create a new container, upload the Webdeploy Sitecore packages and save the new URLs to variables
# Create Storage Account if not exists yet
$storageAccount = Get-AzureRmStorageAccount -ResourceGroupName $RGName -Name $StorageAccountNameDeploy -ErrorAction SilentlyContinue
if(!$storageAccount)
{
$storageAccount = New-AzureRmStorageAccount -ResourceGroupName $RGName -Name $StorageAccountNameDeploy -Location $Location -SkuName "Standard_LRS"
Write-Host "New storage account created: $StorageAccountNameDeploy"
}
else{ Write-Host "Storage account found: $StorageAccountNameDeploy"}
$ctx = $storageAccount.Context
# Create container to upload packages towards
$packagesContainerName = "tempsitecore825packages"
$packagesContainer = New-AzureStorageContainer -Name $packagesContainerName -Permission Off -Context $ctx -ErrorAction SilentlyContinue
# Upload XM1 package to container, so it is available for ARM deployment
$cmBlobName = "Sitecore 8.2 rev. 170728_cm.scwdp.zip"
$localFile = "$scriptDir\packages\xM1\" + $cmBlobName
Set-AzureStorageBlobContent -Container $packagesContainerName -File $localFile -Blob $cmBlobName -Context $ctx -Force
$cdBlobName = "Sitecore 8.2 rev. 170728_cd.scwdp.zip"
$localCdFile = "$scriptDir\packages\xM1\" + $cdBlobName
Set-AzureStorageBlobContent -Container $packagesContainerName -File $localCdFile -Blob $cdBlobName -Context $ctx -Force
# Create SAS token for the packages container
$packagesContainerSas = New-AzureStorageContainerSASToken -Context $ctx -Name $packagesContainerName -Permission r -ExpiryTime (Get-Date).AddHours(4)
Write-Host "Sas for packages: $packagesContainerSas"
$cmWebdeployPackageUri = (Get-AzureStorageBlob -Blob $cmBlobName -Container $packagesContainerName -Context $ctx).ICloudBlob.Uri.AbsoluteUri + $packagesContainerSas
$cdWebdeployPackageUri = (Get-AzureStorageBlob -Blob $cdBlobName -Container $packagesContainerName -Context $ctx).ICloudBlob.Uri.AbsoluteUri + $packagesContainerSas
Write-Output "Blob URL and SAS - $cdWebdeployPackageUri"
$sqlCompatibilityLevelBlobName = "SetCompatibilityLevel.scwdp.zip"
$localSqlCompatibilityLevelFile = "$scriptDir\packages\" + $sqlCompatibilityLevelBlobName
Set-AzureStorageBlobContent -Container $packagesContainerName -File $localSqlCompatibilityLevelFile -Blob $sqlCompatibilityLevelBlobName -Context $ctx -Force
$sqlCompatibilityLeveldeployPackageUri = (Get-AzureStorageBlob -Blob $sqlCompatibilityLevelBlobName -Container $packagesContainerName -Context $ctx).ICloudBlob.Uri.AbsoluteUri + $packagesContainerSas
Write-Output "Blob URL and SAS for SQL compatibility level - $sqlCompatibilityLeveldeployPackageUri"
# Create container to upload templates towards
$templatesContainerName = "tempsitecore825templates"
$templatesContainerUri = "https://$StorageAccountNameDeploy.blob.core.windows.net/$templatesContainerName/"
# Upload main template:
& "$scriptDir\copyFilesToAzureStorageContainer.ps1" -LocalPath "$scriptDir\templates\xm\" `
-StorageContainer $templatesContainerName -StorageContext $ctx -CreateStorageContainer -Recurse -Force
# Create SAS token for the packages container
$templatesContainerSas = New-AzureStorageContainerSASToken -Context $ctx -Name $templatesContainerName -Permission r -ExpiryTime (Get-Date).AddHours(4)
Write-Host "Sas for templates: $templatesContainerSas"
$rootTemplateUri = (Get-AzureStorageBlob -Blob "azuredeploy.json" -Container $templatesContainerName -Context $ctx).ICloudBlob.Uri.AbsoluteUri
Write-Output "Root template SAS - $rootTemplateUri"
# Get content from license file:
$licenseFileContent = Get-Content -Raw -Encoding UTF8 -Path "$PathToSitecoreLicenseFile" | Out-String;
# Set locations to two custom Sitecore addons: keyvault and slots
$keyvaultModuleUriWithSas = (Get-AzureStorageBlob -Blob "addons/keyvault.json" -Container $templatesContainerName -Context $ctx).ICloudBlob.Uri.AbsoluteUri + $templatesContainerSas
$slotsModuleUriWithSas = (Get-AzureStorageBlob -Blob "addons/slots.json" -Container $templatesContainerName -Context $ctx).ICloudBlob.Uri.AbsoluteUri + $templatesContainerSas
# Generate the parameters Json file dynamically, on the fly
$paramsFile = @{
'$schema' = "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#"
contentVersion = "1.0.0.0"
parameters = @{
deploymentId = @{
value = "$ResourcePrefix"
}
'sitecoreAdminPassword' = @{
value = "$SitecorePwd"
}
'sqlServerLogin' = @{
value = "$SqlServerLogin"
}
'sqlServerPassword' = @{
value ="$SqlServerPwd"
}
'setCompatibilityLevelMsDeployPackageUrl' = @{
value = "$sqlCompatibilityLeveldeployPackageUri"
}
'cmMsDeployPackageUrl' = @{
value = "$cmWebdeployPackageUri"
}
'cdMsDeployPackageUrl' = @{
value = "$cdWebdeployPackageUri"
}
'applicationInsightsLocation' = @{
value = "West Europe"
}
'templateLinkBase' = @{
value = $templatesContainerUri
}
'templateLinkAccessToken' = @{
value = $templatesContainerSas
}
'licenseXml' = @{
value = "$licenseFileContent"
}
modules = @{
value = @{
items = @(
@{
name = "keyvault"
templateLink = $keyvaultModuleUriWithSas
parameters = @{
userIdforKeyvault = "$KeyVaultUserId"
keyvaultSku = "Standard"
}
}
@{
name = "slots"
templateLink = $slotsModuleUriWithSas
parameters = @{}
}
)
}
}
}
}
$paramsFilePath = "$scriptDir\xm-asp-sitecore.parameters.tmp.json"
Write-Host "Temp params file to be written to: $paramsFilePath"
$paramsFile | ConvertTo-Json -Depth 10 | Out-File $paramsFilePath
#============================
# Deploy ARM template
New-AzureRmResourceGroupDeployment -Verbose -Force -ErrorAction Stop `
-Name "sitecore" `
-ResourceGroupName $RGName `
-TemplateFile "$scriptDir/templates/xm/azuredeploy.json" `
-TemplateParameterFile $paramsFilePath
# Clean up temporary params file:
if($LeaveTempFilesOnDisk -eq $false) {
Remove-Item -Path $paramsFilePath
}