-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeployInfra.ps1
More file actions
147 lines (120 loc) · 4.83 KB
/
Copy pathDeployInfra.ps1
File metadata and controls
147 lines (120 loc) · 4.83 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
#variables
$userUPN = "myusername@myorg.com"
$provisioningPackagePath = 'C:\myProvisioningPackage\'
$subscriptionID = "XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
$location = "westeurope"
$containerName = 'provisioningpackages'
##Start Script
az login
az account set --subscription $subscriptionID
#Get OrgName
$dnsOrgName = ($userUPN -split "@")[1]
$orgname = $dnsOrgName.Split(".")[0]
# create Resource Group based on OrgName
az group create --location $location --name $orgname
#create storageaccount for provisioning package
az storage account create `
--name ($orgname + 'escience') `
--sku Standard_LRS `
--https-only $true `
--location $location `
--resource-group $orgname
#Get Storage Account name
$storageAccountName = az storage account list `
--resource-group $orgname `
--query [0].'name' `
--output tsv
#Get account key
$artifactsStorageKey = az storage account keys list `
--account-name $storageAccountName `
--query [0].value `
--output tsv
#create container in the Storage account to upload the provisioning package to
az storage container create `
--name $containerName `
--account-name $storageAccountName `
--account-key $artifactsStorageKey
#Create zip archive for Provisioning Package artifacts
Compress-Archive -Path $provisioningPackagePath\* -DestinationPath .\$orgname.zip -Force -Debug
# upload to storage
$SasToken = az storage container generate-sas `
--account-name $storageAccountName `
--name $containerName `
--account-key $artifactsStorageKey `
--permissions w `
--output tsv
$connectionString = az storage account show-connection-string `
--resource-group $orgname `
--name $storageAccountName `
--output tsv
## upload artifacts to blob storage
az storage blob upload `
--name .\$orgname.zip `
--container-name $containerName.ToLower() `
--file .\$orgname.zip `
--account-name $storageAccountName `
--connection-string $connectionString `
--sas-token $SasToken
az storage blob upload `
--name .\RunProvisioningPackage.ps1 `
--container-name $containerName.ToLower() `
--file .\RunProvisioningPackage.ps1 `
--account-name $storageAccountName `
--connection-string $connectionString `
--sas-token $SasToken
#Get data for sas key
$date = (Get-Date).AddMinutes(90).ToString("yyyy-MM-dTH:mZ")
$date = $date.Replace(".", ":")
#Get SAS for powershell script
$scriptURILocation = az storage blob generate-sas `
--account-name $storageAccountName `
--container-name $containerName.ToLower() `
--name RunProvisioningPackage.ps1 `
--account-key $artifactsStorageKey `
--permissions rw `
--expiry $date `
--full-uri `
--output tsv
#Get SAS for powershell script
$provisioningPackageLocation = az storage blob generate-sas `
--account-name $storageAccountName `
--container-name $containerName.ToLower() `
--name "$($orgname).zip" `
--account-key $artifactsStorageKey `
--permissions rw `
--expiry $date `
--full-uri `
--output tsv
#Fix up parameters file
$tempParameterFile = '.\temp-eScienceVM.deployment.paramaters.json'
Copy-Item .\eScienceVM.deployment.paramaters.json .\$tempParameterFile
((Get-Content -path $tempParameterFile -Raw) -replace '"replaceUserUpn"', $('"' + $userUPN + '"')) | Set-Content -Path $tempParameterFile
((Get-Content -path $tempParameterFile -Raw) -replace '"replaceScriptURI"', $('"' + $scriptURILocation + '"')) | Set-Content -Path $tempParameterFile
((Get-Content -path $tempParameterFile -Raw) -replace '"replaceprovisioningPackage"', $('"' + $provisioningPackageLocation + '"')) | Set-Content -Path $tempParameterFile
# Deploy VM with Public IP
az deployment group create `
--resource-group $orgname `
--name (New-Guid).Guid `
--template-file .\eScienceVM.deployment.json `
--parameters $tempParameterFile
az vm extension set `
--publisher Microsoft.Azure.ActiveDirectory `
--name AADLoginForWindows `
--resource-group $orgname `
--vm-name "escience"
# Deploy VM with isolated network
$tempParameterFile = '.\temp-Isolated.eScienceVM.deployment.paramaters.json'
Copy-Item .\isolated.eScienceVM.deployment.paramaters.json .\$tempParameterFile
((Get-Content -path $tempParameterFile -Raw) -replace '"replaceUserUpn"', $('"' + $userUPN + '"')) | Set-Content -Path $tempParameterFile
((Get-Content -path $tempParameterFile -Raw) -replace '"replaceScriptURI"', $('"' + $scriptURILocation + '"')) | Set-Content -Path $tempParameterFile
((Get-Content -path $tempParameterFile -Raw) -replace '"replaceprovisioningPackage"', $('"' + $provisioningPackageLocation + '"')) | Set-Content -Path $tempParameterFile
az deployment group create `
--resource-group $orgname `
--name (New-Guid).Guid `
--template-file .\isolated.eScienceVM.deployment.json `
--parameters $tempParameterFile
az vm extension set `
--publisher Microsoft.Azure.ActiveDirectory `
--name AADLoginForWindows `
--resource-group $orgname `
--vm-name "escience1"