-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnvironmentDeployment.ps1
139 lines (123 loc) · 6.54 KB
/
EnvironmentDeployment.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
<#
.SYNOPSIS
Script used to deploy all of infrastructure which is defined in EnvironmentDeployment.json.
.DESCRIPTION
This script allows you deploy or update all cloud infrastructure pieces which are necessary to host an environment. You can specify certain resources to be deployed with the resources array parameter.
.PARAMETER configFile
The json configuration file which contains the deployment info for each environment such as ip address spaces and infrastructure resources that makes up an environment
.PARAMETER environment
The environment to which you will deploy to, specified in EnvironmentDeployment.json
.PARAMETER baseDir
This parameter is your base directory to your Code Repo e.g. C:\code
.PARAMETER resources
Array of resources that can be specified in order to deploy only certain resources. If this parameter is left blank, all resources will be deployed or updated. The naming convention for the resource is
the ResourceGroupSuffix tag found in the EnvironmentDeployment.json file (i.e. VirtualMachines, VNet, etc).
.EXAMPLE
./EnvironmentDeployment.ps1 -configFile .\EnvironmentDeployment.json -environment pcac -baseDir C:\Repos -resources ServiceFabric,PostgreSQL
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$configFile,
[Parameter(Mandatory = $true)]
[string]$environment,
[Parameter(Mandatory = $false)]
[string]$baseDir = 'C:\Repos\',
[Parameter(Mandatory = $false)]
[array]$resources = @(),
[Parameter(Mandatory = $false)]
[string]$logFolder = "C:\Logs\",
[Parameter(Mandatory = $false)]
[string]$logFileName = "DeploymentLogs.log"
)
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Import-Module $ScriptDir\Shared\UploadArtifacts\UploadArtifacts.psm1 -Force
Import-Module $ScriptDir\Shared\Logging\Logging.psm1 -Force
Import-Module $ScriptDir\Shared\AzureHelpers\AzureHelpers.psm1 -Force
$logFilePath = PrepareToLog $logFolder $logFileName
$conf = (Get-Content $configFile) -join "`n" | ConvertFrom-Json
$environmentConfiguration = $conf.$environment
if (!$environmentConfiguration) {
LogErrorMessage "No Environment Configuration for $environment - please create your configuration in file $configFile" $logFilePath
exit 1
}
if($environment.Length -gt 14) {
LogErrorMessage "Environment name is too long. Maximum length is 12 characters" $logFilePath
exit 1
}
if($environment -cmatch '[A-Z]') {
LogErrorMessage "Environment name must be lowercase" $logFilePath
exit 1
}
Select-Subscription -SubscriptionName $environmentConfiguration.Subscription
$subscriptionId = (Get-AzureRmContext).Subscription.Id
$location = $environmentConfiguration.location
$ipbase = $environmentConfiguration.ipbase
$pdns = $environmentConfiguration.primarydns
$sdns = $environmentConfiguration.secondarydns
$monitoripbase = $environmentConfiguration.monitoripbase
$gatewayipbase = $environmentConfiguration.gatewayipbase
$puppetipbase = $environmentConfiguration.puppetipbase
$clientipbase = $environmentConfiguration.clientipbase
$baseDir = Join-Path $baseDir \
foreach ($deployment in $environmentConfiguration.deployments) {
$resourceGroup = $environment + "-" + $deployment.resourceGroupSuffix
# we want to skip anything not contained in the array
if ($resources.Length -gt 0) {
if (!$resources.contains($deployment.resourceGroupSuffix)) {
continue
}
}
if($deployment.overrideResourceGroup.length -gt 0){
$resourceGroup = $deployment.overrideResourceGroup
}
$tf = $deployment.templateFile
$pf = $deployment.paramsFile -replace '==ENVIRONMENTNAME==', $environment
$folder = Split-Path -Path $pf
$fileName = Split-Path -Path $pf -Leaf -Resolve
$outfile = $folder + "\tmp_deployment_" + $fileName
(Get-Content $pf) | Set-Content $outfile
LogInfoMessage "Deployment. Group: $resourceGroup TemplateFile: $tf Parameters File: $pf" $logFilePath
if ($deployment.UploadDirectory) {
$artifactsResources = GetArtifactsResourceNames -resourceName "Supporting" -environmentName $environment
$artifactResourceGroup = $artifactsResources[0]
$artifactStorageAccountName = $artifactsResources[1]
$artifactsLocationSasToken = "_artifactsLocationSasToken"
$artifactsLocation = "_artifactsLocation"
LogInfoMessage "Uploading artifacts to Resource Group [$artifactResourceGroup] and Storage Account [$artifactStorageAccountName]" $logFilePath
if($deployment.UploadDirectory -like "*longExpireTime*") {
$artifactsHash = UploadArtifacts -storageAccountName $artifactStorageAccountName `
-resourceGroupLocation $environmentConfiguration.location `
-resourceGroupName $artifactResourceGroup `
-artifactStagingDirectory $deployment.UploadDirectory.Replace('==BASEDIR==', $baseDir) `
-artifactsLocationName $artifactsLocation `
-artifactsLocationSasTokenName $artifactsLocationSasToken `
-expireTimeHours 87600
} else {
$artifactsHash = UploadArtifacts -storageAccountName $artifactStorageAccountName `
-resourceGroupLocation $environmentConfiguration.location `
-resourceGroupName $artifactResourceGroup `
-artifactStagingDirectory $deployment.UploadDirectory.Replace('==BASEDIR==', $baseDir) `
-artifactsLocationName $artifactsLocation `
-artifactsLocationSasTokenName $artifactsLocationSasToken
}
LogInfoMessage "Uploaded Artifacts" $logFilePath
(Get-Content -Path $outfile) | Foreach-Object {
$_ -Replace '==ARTIFACTSASTOKEN==', $artifactsHash[$artifactsLocationSasToken] `
-Replace '==ARTIFACTLOCATION==', $artifactsHash[$artifactsLocation]
} | Set-Content -Path $outfile
}
(Get-Content -Path $outfile) | Foreach-Object {
$_ -Replace '==SUBSCRIPTIONID==', $subscriptionId `
-Replace '==ENVIRONMENTNAME==', $environment `
-Replace '==LOCATION==', $location `
-Replace '==IPBASE==', $ipbase `
-Replace '==PDNS==', $pdns `
-Replace '==SDNS==', $sdns `
-replace '==MONITORIPBASE==', $monitoripbase `
-replace '==GATEWAYIPBASE==', $gatewayipbase `
-replace '==PUPPETIPBASE==', $puppetipbase `
-replace '==CLIENTIPBASE==', $clientipbase
} | Set-Content -Path $outfile
AzureResourceGroupDeployment -templateFile $tf -templateParametersFile $outfile -resourceGroupName $resourceGroup -resourceGroupLocation $environmentConfiguration.location
}