-
Notifications
You must be signed in to change notification settings - Fork 6
/
configure.ps1
213 lines (183 loc) · 6 KB
/
configure.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
param
(
$configurationName
)
# unload modules if loaded
Get-Module AppVeyor | Remove-Module
Get-Module AppRolla | Remove-Module
# import modules
Import-Module AppRolla
Import-Module AppVeyor
# globals
$isAppveyorEnvironment = $true
$scriptsPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$settingsPath = "SOFTWARE\AppVeyor\Deployment"
$azureApps = @{}
function GetAzureApp($name)
{
$app = $azureApps[$name]
if($app -eq $null)
{
$app = @{}
$azureApps[$name] = $app
}
return $app
}
# is it AppVeyor CI environment?
if(-not $projectName)
{
Write-Host "Running deployment script interactively"
# no, the script is being run interactively from command line
$isAppveyorEnvironment = $false
# load script parameters from the Registry
$configkey = ([Microsoft.Win32.Registry]::CurrentUser).OpenSubKey($settingsPath)
if(-not $configkey)
{
throw "Cannot read script parameters from Registry key: $settingsPath"
}
$variables = @{}
foreach($name in $configkey.GetValueNames())
{
$variables[$name] = $configkey.GetValue($name)
}
# load named sub-configuration if specified and found
if($configurationName)
{
$subConfigkey = ([Microsoft.Win32.Registry]::CurrentUser).OpenSubKey("$settingsPath\$configurationName")
if($subConfigkey)
{
# override variables
foreach($name in $subConfigkey.GetValueNames())
{
$variables[$name] = $subConfigkey.GetValue($name)
}
}
}
}
# get script settings
$specificProject = $false
if($variables.Project)
{
$specificProject = $true
$projectName = $variables.Project
$projectVersion = $variables.Version
}
$apiAccessKey = $variables.ApiAccessKey
$apiSecretKey = $variables.ApiSecretKey
$serverUsername = $variables.ServerUsername
$serverPassword = $variables.ServerPassword
if(-not $apiAccessKey -or -not $apiSecretKey)
{
throw "Specify ApiAccessKey and ApiSecretKey variables"
}
# set API keys
Set-AppveyorConnection $apiAccessKey $apiSecretKey
# this is needed to download artifacts on remote servers
Set-DeploymentConfiguration AppveyorApiKey $apiAccessKey
Set-DeploymentConfiguration AppveyorApiSecret $apiSecretKey
# details of Azure subscription (required by Azure deployment cmdlets)
Set-DeploymentConfiguration AzureSubscriptionID $variables.AzureSubscriptionID
Set-DeploymentConfiguration AzureSubscriptionCertificate $variables.AzureSubscriptionCertificate
# details of Azure cloud storage (required by Azure deployment cmdlets to download CS configuration file)
Set-DeploymentConfiguration AzureStorageAccountName $variables.AzureStorageAccountName
Set-DeploymentConfiguration AzureStorageAccountKey $variables.AzureStorageAccountKey
# add new application
New-Application $projectName
# add applications and roles from artifacts
$projectArtifacts = $null
if($specificProject)
{
if($projectVersion)
{
Write-Host "Fetching version $projectVersion details for project $projectName using AppVeyor API"
# load specific project version
$version = Get-AppveyorProjectVersion $projectName $projectVersion
# get project artifacts
$projectArtifacts = $version.artifacts
}
else
{
Write-Host "Fetching last version of project $projectName using AppVeyor API"
# load last project version
$project = Get-AppveyorProject -Name $projectName
$projectVersion = $project.lastVersion.version
# get project artifacts
$projectArtifacts = $project.lastVersion.artifacts
}
}
else
{
Write-Host "Deploying from build artifacts"
$projectArtifacts = $artifacts.values
}
Write-Host "Configuring applications for project `"$projectName`" version $projectVersion"
# build AppRolla application from artifacts
Write-Host "Total project artifacts: $($projectArtifacts.Count)"
foreach($artifact in $projectArtifacts)
{
if($artifact.type -eq "WindowsApplication")
{
# Windows service or console application
Add-ServiceRole $projectName $artifact.name -PackageUrl $artifact.url -DeploymentGroup app
}
elseif($artifact.type -eq "WebApplication")
{
# Web application
Add-WebsiteRole $projectName $artifact.name -PackageUrl $artifact.url -DeploymentGroup web
}
elseif($artifact.type -eq "AzureCloudService")
{
# Azure Cloud Service package
$app = GetAzureApp $artifact.name
$app.Name = $artifact.name
$app.PackageUrl = $artifact.customUrl
}
elseif($artifact.type -eq "AzureCloudServiceConfig")
{
# Azure Cloud Service configuration
$appName = $artifact.name.substring(0, $artifact.name.lastIndexOf("-"))
$app = GetAzureApp $appName
$app.ConfigUrl = $artifact.customUrl
}
else
{
Write-Host "$($artifact.type) artifact $($artifact.name) skipped"
}
}
# add Azure applications if found
foreach($azureApp in $azureApps.Values)
{
New-AzureApplication -Name $azureApp.Name -PackageUrl $azureApp.PackageUrl -ConfigUrl $azureApp.ConfigUrl
}
# load project specific settings and customizations
Write-Host "Loading project custom settings"
. (Join-Path $scriptsPath "project.ps1")
# set environment credentials
if($serverUsername -and $serverPassword)
{
Write-Host "Setting environment credentials"
$securePassword = ConvertTo-SecureString $serverPassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $serverUsername, $securePassword
foreach($environment in (Get-Environment))
{
if($environment.Name -ne "local")
{
Set-Environment $environment.Name -Credential $credential
}
}
}
# output what apps and environments are available
# apps
Write-Host
Write-Host "Configured applications:"
foreach($app in Get-Application)
{
Write-Host " $($app.Name)"
}
# environments
Write-Host
Write-Host "Configured environments:"
foreach($environment in Get-Environment)
{
Write-Host " $($environment.Name)"
}