This repository was archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDeployAzure.ps1
More file actions
244 lines (205 loc) · 9.82 KB
/
Copy pathDeployAzure.ps1
File metadata and controls
244 lines (205 loc) · 9.82 KB
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
<#
.SYNOPSIS
Builds and deploys BMA.
.DESCRIPTION
Builds BMA and deploys it to Azure using a free-tier app service plan and a locally-redundant storage account for logging.
.PARAMETER serviceName
Name to use as domain for front-end application (so, it will be deployed to <serviceName>.azurewebsites.net)
.PARAMETER serviceLocation
Location of azure datacenter to use. Default is "North Europe"
.PARAMETER resourceGroupName
The resource group where BMA will be deployed. Can be the name of an existing or a new resource group. If not specified, serviceName will be used instead.
.PARAMETER deploymentName
The deployment name. Default is <serviceName>Deployment
.PARAMETER storageAccountName
Name of the storage account to use in this deployment. Default is <serviceName>storage.
.PARAMETER apiServiceName
Name to use as domain for API sevice. Default is <serviceName>api.
.PARAMETER servicePlanName
Name of app service plan to use in this deployment. Default is <serviceName>Plan.
#>
# Copyright (c) Microsoft Research 2016
# License: MIT. See LICENSE
param(
[Parameter(Mandatory=$True, Position=1)]
[string]
$serviceName,
[string]
$serviceLocation = "North Europe",
[string]
$resourceGroupName,
[string]
$deploymentName,
[string]
$storageAccountName,
[string]
$apiServiceName,
[string]
$servicePlanName
)
$ErrorActionPreference = "stop"
$exitcode = 0
if ($serviceName.Length -ge 60) {
Write-Error -Message 'ERROR: service name is too long (more than 60 characters).'
exit 1
}
# default values for empty parameters
if ([System.String]::IsNullOrEmpty($resourceGroupName)) {
$resourceGroupName = $serviceName
if ($resourceGroupName.Length -gt 60) {
Write-Error -Message "ERROR: resource group name defaulted to an incorrect value ('$resourceGroupName') - it's longer than 60 characters. Either provide a different service name or specify a correct resource group name explicitly (via -resourceGroupName parameter)."
exit 1
}
}
if ([System.String]::IsNullOrEmpty($storageAccountName)) {
$storageAccountName = $serviceName + "storage"
if ($storageAccountName -notmatch "^[a-z1-9]{3,24}$") {
Write-Error -Message "ERROR: storage account name defaulted to an incorrect value ('$storageAccountName') - only lowercase letters and digits are allowed, must be between 3 and 24 characters long. Either provide a different service name or specify a correct storage account name explicitly (via -storageAccountName parameter)."
exit 1
}
}
if ([System.String]::IsNullOrEmpty($apiServiceName)) {
$apiServiceName = $serviceName + "api"
if ($apiServiceName.Length -gt 60) {
Write-Error -Message "ERROR: api service name defaulted to an incorrect value ('$apiServiceName') - it's longer than 60 characters. Either provide a different service name or specify a correct api service name explicitly (via -apiServiceName parameter)."
exit 1
}
}
if ([System.String]::IsNullOrEmpty($servicePlanName)) {
$servicePlanName = $serviceName + "Plan"
if ($servicePlanName.Length -gt 40) {
Write-Error -Message "ERROR: service plan name defaulted to an incorrect value ('$servicePlanName') - it's longer than 40 characters. Either provide a different service name or specify a correct service plan name explicitly (via -servicePlanName parameter)."
exit 1
}
}
if ([System.String]::IsNullOrEmpty($deploymentName)) {
$deploymentName = $serviceName + "Deployment"
}
if ($storageAccountName -notmatch "^[a-z1-9]{3,24}$") {
Write-Error -Message 'ERROR: storage account name is incorrect (only lowercase letters and digits are allowed, must be between 3 and 24 characters long).'
exit 1
}
if ($resourceGroupName.Length -gt 60) {
Write-Error -Message 'ERROR: resource group name is too long (more than 60 characters).'
exit 1
}
if ($apiServiceName.Length -gt 60) {
Write-Error -Message 'ERROR: api service name is too long (more than 60 characters).'
exit 1
}
if ($servicePlanName.Length -gt 40) {
Write-Error -Message 'ERROR: service plan name is too long (more than 40 characters).'
exit 1
}
$cpath = Get-Location
$cdir = $cpath.Path
$resourceGroupLocation = $serviceLocation
$templateFilePath = Join-Path $cdir "deployment\template.json"
$parametersTemplateFilePath = Join-Path $cdir "deployment\parameters.template.json"
$parametersFilePath = Join-Path $cdir "deployment\parameters.json"
Copy-Item $parametersTemplateFilePath $parametersFilePath
.\deployment\installAzurePowerShell.ps1
.\PrepareRepository.ps1
Import-Module .\packages\publish-module\tools\publish-module.psm1
Write-Host "Building web apps..."
.\deployment\buildWebApps.ps1
if (!$?) {
Write-Error -Message 'ERROR: Build script (deployment\buildWebApps.ps1) ended with an error'
exit 1
}
Try {
# sign in
Write-Host "Logging in...";
Login-AzureRmAccount;
$subscriptions = Get-AzureRmSubscription
if ($subscriptions.Length -eq 1) {
$subscriptionId = $subscriptions[0].SubscriptionId
$subscriptionName = $subscriptions[0].SubscriptionName
} elseif ($subscriptions.Length -eq 0) {
Write-Error "No Azure subscriptions found."
exit 1;
} else {
Write-Host "Multiple subscriptions are found:"
for ($i = 1; $i -le $subscriptions.Length; ++$i) {
Write-Host "$i) $($subscriptions[$i-1].SubscriptionName)"
}
$ans = Read-Host -Prompt "Type the number corresponding to the subscription you'd like to use"
$num = 0
$succ = [System.Int32]::TryParse($ans, [ref] $num)
while (-not $succ -or $num -lt 1 -or $num -gt $subscriptions.Length) {
$ans = Read-Host -Prompt "Please, type a number between 1 and $($subscriptions.Length)"
$succ = [System.Int32]::TryParse($ans, [ref] $num)
}
$subscriptionId = $subscriptions[$num - 1].SubscriptionId
$subscriptionName = $subscriptions[$num - 1].SubscriptionName
}
# select subscription
Write-Host "Selecting subscription '$subscriptionName' with id '$subscriptionId'";
Select-AzureRmSubscription -SubscriptionID $subscriptionId;
# preparing parameters.json
$parameters = Get-Content $parametersFilePath | ConvertFrom-Json
$parameters.parameters.storageAccounts_bmastorage_name.value = $storageAccountName
$parameters.parameters.serverfarms_bmaplan_name.value = $servicePlanName
$parameters.parameters.sites_bmafrontend_name.value = $serviceName
$parameters.parameters.sites_bmaapi_name.value = $apiServiceName
$parameters.parameters.location.value = $serviceLocation
$parameters | ConvertTo-Json | set-content $parametersFilePath
Write-Host "Deploying resource group..."
.\deployment\deployResourceGroup.ps1 $resourceGroupName $resourceGroupLocation $deploymentName $templateFilePath $parametersFilePath
if (!$?) {
Write-Error -Message 'ERROR: Failed to create a resource group for the deployment'
exit 1
}
Write-Host "Retrieving storage and publishing credentials..."
$frontendSiteConf = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $serviceName/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force
$apiSiteConf = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $apiServiceName/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force
$storageSiteConf = Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -AccountName $storageAccountName
$storageKey = $storageSiteConf[0].Value
$connectionString = "DefaultEndpointsProtocol=https;AccountName=$storageAccountName;AccountKey=$storageKey"
Write-Host "Configuring ApiServer..."
$unityConfPath = Join-Path $cdir "\deployment\ApiServer\unity.azure-appservice.config"
$unityConf = [xml] (Get-Content $unityConfPath)
$unityConf.unity.container.register[0].constructor.param.value = $connectionString
$unityConf.unity.container.register[1].constructor.param.value = $connectionString
$unityConf.Save($unityConfPath)
$apiConfPath = Join-Path $cdir "\deployment\ApiServer\Web.config"
$apiConf = [xml] (Get-Content $apiConfPath)
$apiConf.configuration.unity.SetAttribute("configSource", "unity.azure-appservice.config")
$apiConf.Save($apiConfPath)
Write-Host "Configuring bma.client..."
$clientConfPath = Join-Path $cdir "\deployment\bma.client\Web.config"
$clientConf = [xml] (Get-Content $clientConfPath)
($clientConf.configuration.appSettings.add | where {$_.key -eq 'BackEndUrl'}).SetAttribute("value", "https://$apiServiceName.azurewebsites.net")
$clientConf.Save($clientConfPath)
Write-Host "Publishing ApiServer..."
$publishProperties = @{'WebPublishMethod' = 'MSDeploy';
'MSDeployServiceUrl' = "$apiServiceName.scm.azurewebsites.net:443";
'DeployIisAppPath' = $apiServiceName;
'Username' = $apiSiteConf.properties.publishingUserName
'Password' = $apiSiteConf.properties.publishingPassword}
Publish-AspNet -packOutput (Join-Path $cdir "deployment\ApiServer") -publishProperties $publishProperties
Write-Host "Publishing bma.client..."
$publishProperties = @{'WebPublishMethod' = 'MSDeploy';
'MSDeployServiceUrl' = "$serviceName.scm.azurewebsites.net:443";
'DeployIisAppPath' = $serviceName;
'Username' = $frontendSiteConf.properties.publishingUserName;
'Password' = $frontendSiteConf.properties.publishingPassword}
Publish-AspNet -packOutput (Join-Path $cdir "deployment\bma.client") -publishProperties $publishProperties
}
Catch
{
Write-Host "An error occured during deployment. Error details:"
Write-Host $_
$exitcode = 1
}
Finally
{
Write-Host "Deleting Temporary Files"
Remove-Item (Join-Path $cdir "\deployment\ApiServer") -Recurse
Remove-Item (Join-Path $cdir "\deployment\bma.client") -Recurse
$objpath = Join-Path $cdir "\deployment\obj"
if (Test-Path $objpath) {
Remove-Item $objpath -Recurse
}
exit $exitcode
}