-
Notifications
You must be signed in to change notification settings - Fork 5
/
create_vmss.sh
executable file
·258 lines (231 loc) · 6.18 KB
/
create_vmss.sh
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
#!/bin/bash
set -eu -o pipefail
PROGNAME="$(basename "$0")"
# Parse arguments
ARGS=$(getopt \
--options s:l:g:v:s:n:r:m:b:d \
--longoptions subscription:,location:,rg-vnet:,vnet-name:,subnet-name:,subnet:,rg-vm:,vm-name:,lb-name:,dns-name: \
-n "${PROGNAME}" -- "$@")
eval set -- "${ARGS}"
unset ARGS
AZ_SUBSCRIPTION_ID=""
AZ_LOCATION=""
AZ_VNET_RG=""
AZ_VNET=""
AZ_VNET_SUBNET_NAME=""
AZ_VNET_SUBNET=""
AZ_VM_RG=""
AZ_VM=""
AZ_LB=""
AZ_LB_DNS=""
while true; do
case "$1" in
'-s'|'--subscription')
AZ_SUBSCRIPTION_ID="$2"
shift 2
continue
;;
'-l'|'--location')
AZ_LOCATION="$2"
shift 2
continue
;;
'-g'|'--rg-vnet')
AZ_VNET_RG="$2"
shift 2
continue
;;
'-v'|'--vnet-name')
AZ_VNET="$2"
shift 2
continue
;;
'-s'|'--subnet-name')
AZ_VNET_SUBNET_NAME="$2"
shift 2
continue
;;
'-n'|'--subnet')
AZ_VNET_SUBNET="$2"
shift 2
continue
;;
'-r'|'--rg-vm')
AZ_VM_RG="$2"
shift 2
continue
;;
'-m'|'--vm-name')
AZ_VM="$2"
shift 2
continue
;;
'-b'|'--lb-name')
AZ_LB="$2"
shift 2
continue
;;
'-d'|'--dns-name')
AZ_LB_DNS="$2"
shift 2
continue
;;
'--')
shift
break
;;
*)
usage
exit 1
;;
esac
done
# Show usage
usage() {
printf "usage: %s --subscription=<name> --location=<name> --rg-vnet=<name> --vnet-name=<name> --subnet-name=<name> --subnet=<name> --rg-vm=<name> --vm-name=<name> --lb-name=<name> --dns-name=<name>\\n" "${PROGNAME}"
}
# Pre-checks
if [[ -z $AZ_SUBSCRIPTION_ID ]]; then
echo "Error: --subscription is required !"
usage
exit 1
fi
if [[ -z $AZ_LOCATION ]]; then
echo "Error: --location is required !"
usage
exit 1
fi
if [[ -z $AZ_VNET_RG ]]; then
echo "Error: --rg-vnet is required !"
usage
exit 1
fi
if [[ -z $AZ_VNET ]]; then
echo "Error: --vnet-name is required !"
usage
exit 1
fi
if [[ -z $AZ_VNET_SUBNET_NAME ]]; then
echo "Error: --subnet-name is required !"
usage
exit 1
fi
if [[ -z $AZ_VNET_SUBNET ]]; then
echo "Error: --subnet is required !"
usage
exit 1
fi
if [[ -z $AZ_VM_RG ]]; then
echo "Error: --rg-vm is required !"
usage
exit 1
fi
if [[ -z $AZ_VM ]]; then
echo "Error: --vm-name is required !"
usage
exit 1
fi
if [[ -z $AZ_LB ]]; then
echo "Error: --lb-name is required !"
usage
exit 1
fi
if [[ -z $AZ_LB_DNS ]]; then
echo "Error: --dns-name is required !"
usage
exit 1
fi
printf "Switch to ${AZ_SUBSCRIPTION_ID} subscription...\\n"
az account set --subscription "${AZ_SUBSCRIPTION_ID}" --output none
if ! az network vnet show --resource-group ${AZ_VNET_RG} --name ${AZ_VNET} --output none; then
printf "Create ${AZ_VNET_RG} resource group...\\n"
az group create \
--location "${AZ_LOCATION}" \
--name "${AZ_VNET_RG}"
printf "Create a new 10.1.0.0/16 VNET named ${AZ_VNET}...\\n"
az network vnet create \
--resource-group "${AZ_VNET_RG}" \
--name "${AZ_VNET}" \
--address-prefix "10.1.0.0/16"
fi
printf "Create a new ${AZ_VNET_SUBNET} subnet named ${AZ_VNET_SUBNET_NAME}...\\n"
az network vnet subnet create \
--resource-group "${AZ_VNET_RG}" \
--vnet-name "${AZ_VNET}" \
--name "${AZ_VNET_SUBNET_NAME}" \
--address-prefix "${AZ_VNET_SUBNET}" \
--output none
printf "Create ${AZ_VM_RG} resource group...\\n"
az group create \
--location "${AZ_LOCATION}" \
--name "${AZ_VM_RG}" \
--output none
printf "Create ${AZ_LB_DNS}.${AZ_LOCATION}.cloudapp.azure.com basic public IP address...\\n"
az network public-ip create \
--name "${AZ_LB}-public-ip" \
--resource-group "${AZ_VM_RG}" \
--allocation-method "dynamic" \
--sku "Basic" \
--version "IPv4" \
--dns-name "${AZ_LB_DNS}" \
--output none
printf "Create ${AZ_LB} basic load balancer...\\n"
az network lb create \
--name "${AZ_LB}" \
--resource-group "${AZ_VM_RG}" \
--public-ip-address "${AZ_LB}-public-ip" \
--frontend-ip-name "${AZ_LB}-public-ip" \
--backend-pool-name "${AZ_VM}-backendpool" \
--sku "Basic" \
--output none
printf "Create NAT pool rules for RDP connection...\\n"
az network lb inbound-nat-pool create \
--name "${AZ_VM}-natpool" \
--resource-group "${AZ_VM_RG}" \
--lb-name "${AZ_LB}" \
--frontend-port-range-start "50000" \
--frontend-port-range-end "50001" \
--backend-port "3389" \
--frontend-ip-name "${AZ_LB}-public-ip" \
--protocol "tcp" \
--output none
printf "Create NSG ${AZ_VM}-nsg...\\n"
az network nsg create \
--name "${AZ_VM}-nsg" \
--resource-group "${AZ_VM_RG}" \
--output none
printf "Create NSG rule to allow RDP...\\n"
az network nsg rule create \
--name "Allow_RDP" \
--nsg-name "${AZ_VM}-nsg" \
--resource-group "${AZ_VM_RG}" \
--priority "1000" \
--direction "Inbound" \
--source-address-prefixes "*" \
--source-port-ranges "*" \
--destination-address-prefixes "VirtualNetwork" \
--destination-port-ranges "3389" \
--access "Allow" \
--protocol "tcp" \
--description "Allow RDP traffic from Any" \
--output none
printf "Create ${AZ_VM} Azure Virtual Machine Scale Set...\\n"
# az vm image list --publisher MicrosoftWindowsDesktop --all -otable
az vmss create \
--name "${AZ_VM}" \
--resource-group "${AZ_VM_RG}" \
--image "MicrosoftWindowsDesktop:Windows-10:rs4-pron:latest" \
--vm-sku "Standard_NV6" \
--storage-sku "StandardSSD_LRS" \
--instance-count "1" \
--eviction-policy "delete" \
--priority "Low" \
--upgrade-policy-mode "Automatic" \
--subnet "/subscriptions/${AZ_SUBSCRIPTION_ID}/resourceGroups/${AZ_VNET_RG}/providers/Microsoft.Network/virtualNetworks/${AZ_VNET}/subnets/${AZ_VNET_SUBNET_NAME}" \
--nsg "${AZ_VM}-nsg" \
--public-ip-address "" \
--load-balancer "${AZ_LB}" \
--lb-nat-pool-name "${AZ_VM}-natpool" \
--output none
printf "Done.\\n\\n"
printf "Please use Microsoft Remote Desktop app connect to ${AZ_LB_DNS}.${AZ_LOCATION}.cloudapp.azure.com:5000 or :5001.\\n"