forked from microsoft/BioModelAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeployAzure.ps1
More file actions
206 lines (168 loc) · 7.64 KB
/
Copy pathDeployAzure.ps1
File metadata and controls
206 lines (168 loc) · 7.64 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
<#
.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
$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;
# default values for empty parameters
if ([System.String]::IsNullOrEmpty($resourceGroupName)) {
$resourceGroupName = $serviceName
}
if ([System.String]::IsNullOrEmpty($storageAccountName)) {
$storageAccountName = $serviceName + "storage"
}
if ([System.String]::IsNullOrEmpty($apiServiceName)) {
$apiServiceName = $serviceName + "api"
}
if ([System.String]::IsNullOrEmpty($servicePlanName)) {
$servicePlanName = $serviceName + "Plan"
}
if ([System.String]::IsNullOrEmpty($deploymentName)) {
$deploymentName = $serviceName + "Deployment"
}
# 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"
$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
}