-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
318 lines (263 loc) · 9.52 KB
/
Copy pathsetup.sh
File metadata and controls
318 lines (263 loc) · 9.52 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/bin/bash
rg=mf-hybrid-conn
loc=uksouth
vnet=mf-hybrid-conn-vnet
vmsubnet=vms
bastionsubnet=AzureBastionSubnet
bastion_public_ip_name=mf-hybrid-conn-bastion-public-ip
vnet_ip=10.0.0.0/16
vms_ip=10.0.0
bastion_ip=10.0.1
vm_ip_mask=24
bastion_ip_mask=24
resource_prefix="my-hy"
api_vm_prefix="$resource_prefix-api"
hcm_vm_prefix="$resource_prefix-hcm"
api_vm_count=2
hcm_vm_count=2
uid=AzureAdmin
read -p 'Administrator Password for VM: ' pwd
storagename=mfhybridscripts
container_name=scripts
app_plan_name="$resource_prefix-app-plan"
function_name="$resource_prefix-func"
servicebus_name="myhyconnsb"
hc_prefix="mfapivmhc"
dns_zone="$resource_prefix.lan"
dns_link_name="$resource_prefix-link"
hc_connection_strings=""
#create the resource group
az group create --location $loc --name $rg
#create the vnet
az network vnet create \
--name $vnet \
--resource-group $rg \
--location $loc \
--address-prefix $vnet_ip
#add the vm subnet
az network vnet subnet create \
--address-prefixes "$vms_ip.0/$vm_ip_mask" \
--name $vmsubnet \
--resource-group $rg \
--vnet-name $vnet
#add the bastion subnet
az network vnet subnet create \
--address-prefixes "$bastion_ip.0/$bastion_ip_mask" \
--name $bastionsubnet \
--resource-group $rg \
--vnet-name $vnet
#create a public ip for the Bastion
az network public-ip create \
-n $bastion_public_ip_name \
-g $rg \
--sku Standard
#Create the private dns zone
az network private-dns zone create -g $rg -n $dns_zone
az network private-dns link vnet create \
--name $dns_link_name \
--registration-enabled true \
--resource-group $rg \
--virtual-network $vnet \
--zone-name $dns_zone
#TODO: Create the Bastion
#create a storage account
az storage account create \
-g $rg -n $storagename
#get the keys
storagekey=$(az storage account keys list -g $rg -n $storagename --query "[?keyName=='key1'].value" --output tsv)
#create a blob container
az storage container create \
--name $container_name \
--public-access off \
--account-name $storagename \
--account-key $storagekey
#upload the files
az storage blob upload \
-f ./addjson.ps1 \
-c $container_name \
-n "addjson.ps1" \
--account-name $storagename \
--account-key $storagekey
az storage blob upload \
-f ./HybridConnectionManager.msi \
-c $container_name \
-n "HybridConnectionManager.msi" \
--account-name $storagename \
--account-key $storagekey
az storage blob upload \
-f ./updateHCM.ps1 \
-c $container_name \
-n "updateHCM.ps1" \
--account-name $storagename \
--account-key $storagekey
#get the sas token for a blob
scriptscontainersas=$(az storage container generate-sas \
-n $container_name \
--account-name $storagename \
--account-key $storagekey \
--expiry 2020-02-28T00:00:00Z \
--permissions r)
escapedsas=$(echo $scriptscontainersas | tr -d '"')
containeruri="https://$storagename.blob.core.windows.net/$container_name"
addjsonuri="$containeruri/addjson.ps1?$escapedsas"
echo $addjsonuri
hcminstalluri="$containeruri/HybridConnectionManager.msi?$escapedsas"
echo $hcminstalluri
updateHCMUri="$containeruri/updateHCM.ps1?$escapedsas"
echo $updateHCMUri
#Create a relay namespace
az relay namespace create --name $servicebus_name --resource-group $rg
#Create an app service plan
az appservice plan create -n $app_plan_name -g $rg --sku S1
#create the function
az functionapp create -g $rg -n $function_name -p $app_plan_name -s $storagename --runtime dotnet
#add the api vms
start_range=5
end_range=$(($api_vm_count + $start_range - 1))
for i in $( seq $start_range $end_range)
do
vmname="$api_vm_prefix-$i"
#create the vm
az vm create \
--name "$vmname" \
--resource-group $rg \
--private-ip-address "$vms_ip.$i" \
--vnet-name $vnet \
--subnet $vmsubnet \
--image Win2019Datacenter \
--admin-username $uid \
--admin-password $pwd \
--public-ip-address "" \
--os-disk-name "$api_vm_prefix-$i-os-disk"
# Open port 80 to allow web traffic to host.
az vm open-port --port 80 --resource-group $rg --name $vmname --priority 100
# Open port 443 to allow web traffic to host.
az vm open-port --port 443 --resource-group $rg --name $vmname --priority 200
#install IIS on the machine
# Use CustomScript extension to install IIS.
az vm extension set \
--publisher Microsoft.Compute \
--version 1.8 \
--name CustomScriptExtension \
--vm-name $vmname \
--resource-group $rg \
--settings '{"commandToExecute":"powershell.exe Install-WindowsFeature -Name Web-Server"}'
prefix='{"fileUris": ["'
suffix='"], "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File addjson.ps1"}'
addjsonsettings="$prefix$addjsonuri$suffix"
# Use CustomScript extension to create a file at the wwwroor.
az vm extension set \
--publisher Microsoft.Compute \
--version 1.8 \
--name CustomScriptExtension \
--vm-name $vmname \
--resource-group $rg \
--settings "$addjsonsettings"
# add a hybrid connection to the relay
hc_name="http$hc_prefix$i"
machine_name="$vmname.$dns_zone"
relay_metadata="[{\"key\":\"endpoint\",\"value\":\"$machine_name:80\"}]"
az relay hyco create \
--resource-group $rg \
--namespace-name $servicebus_name \
--name $hc_name \
--user-metadata $relay_metadata
#Create the defaultListener auth rule
az relay hyco authorization-rule create \
--name defaultListener \
--resource-group $rg \
--namespace-name $servicebus_name \
--hybrid-connection-name $hc_name \
--rights Listen
primaryConnKey=$(az relay hyco authorization-rule keys list \
--name defaultListener \
--resource-group $rg \
--namespace-name $servicebus_name \
--hybrid-connection-name $hc_name \
--query "primaryKey" --output tsv)
hc_conn_string="Endpoint=sb://$servicebus_name.servicebus.windows.net/$hc_name;SharedAccessKeyName=defaultListener;SharedAccessKey=$primaryConnKey"
hc_connection_strings="$hc_connection_strings|$hc_conn_string"
az functionapp hybrid-connection add \
--hybrid-connection $hc_name \
--name $function_name \
--namespace $servicebus_name \
--resource-group $rg
# add a hybrid connection to the relay (HTTPS)
hc_name="https$hc_prefix$i"
machine_name="$vmname.$dns_zone"
relay_metadata="[{\"key\":\"endpoint\",\"value\":\"$machine_name:443\"}]"
#Create the hybrid connection with the defaultSender auth rule
az relay hyco create \
--resource-group $rg \
--namespace-name $servicebus_name \
--name $hc_name \
--user-metadata $relay_metadata
#Create the defaultListener auth rule
az relay hyco authorization-rule create \
--name defaultListener \
--resource-group $rg \
--namespace-name $servicebus_name \
--hybrid-connection-name $hc_name \
--rights Listen
primaryConnKey=$(az relay hyco authorization-rule keys list \
--name defaultListener \
--resource-group $rg \
--namespace-name $servicebus_name \
--hybrid-connection-name $hc_name \
--query "primaryKey" --output tsv)
hc_conn_string="Endpoint=sb://$servicebus_name.servicebus.windows.net/$hc_name;SharedAccessKeyName=defaultListener;SharedAccessKey=$primaryConnKey"
hc_connection_strings="$hc_connection_strings|$hc_conn_string"
az functionapp hybrid-connection add \
--hybrid-connection $hc_name \
--name $function_name \
--namespace $servicebus_name \
--resource-group $rg
done
#add the hcm vms
start_range=$(($endrange + 10))
end_range=$(($hcm_vm_count + $start_range - 1))
for i in $( seq $start_range $end_range)
do
vmname="$hcm_vm_prefix-$i"
#create the vm
az vm create \
--name "$vmname" \
--resource-group $rg \
--private-ip-address "$vms_ip.$i" \
--vnet-name $vnet \
--subnet $vmsubnet \
--image Win2019Datacenter \
--admin-username $uid \
--admin-password $pwd \
--public-ip-address "" \
--os-disk-name "$hcm_vm_prefix-$i-os-disk"
# Open port 80 to allow web traffic to host.
az vm open-port --port 80 --resource-group $rg --name $vmname --priority 100
# Open port 443 to allow web traffic to host.
az vm open-port --port 443 --resource-group $rg --name $vmname --priority 200
prefix='{"fileUris": ["'
suffix='"], "commandToExecute": "start /wait msiexec /package HybridConnectionManager.msi /quiet"}'
hcminstallsettings="$prefix$hcminstalluri$suffix"
echo $hcminstallsettings
# Use CustomScript extension to install the hybrid connection manager
az vm extension set \
--publisher Microsoft.Compute \
--version 1.8 \
--name CustomScriptExtension \
--vm-name $vmname \
--resource-group $rg \
--settings "$hcminstallsettings"
prefix='{"fileUris": ["'
suffix='"], "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File updateHCM.ps1 \"'
suffix2='\" "}'
updatehcmsettings="$prefix$updateHCMUri$suffix$hc_connection_strings$suffix2"
echo $updatehcmsettings
#Update HCM Listener config to point to the configured endpoints
az vm extension set \
--publisher Microsoft.Compute \
--version 1.8 \
--name CustomScriptExtension \
--vm-name $vmname \
--resource-group $rg \
--settings "$updatehcmsettings"
done