-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_vm.sh
executable file
·325 lines (290 loc) · 9.61 KB
/
create_vm.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
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
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/bash
set -eu -o pipefail
PROGNAME="$(basename "$0")"
# Parse arguments
ARGS=$(getopt \
--options s:l:g:v:n:u: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_SHARED_RG=""
AZ_VNET=""
AZ_VNET_SUBNET_NAME=""
AZ_VNET_SUBNET=""
AZ_VM_RG=""
AZ_VM=""
AZ_LB=""
AZ_LB_DNS=""
AZ_CONTAINER="backup-001"
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_SHARED_RG="$2"
shift 2
continue
;;
'-v'|'--vnet-name')
AZ_VNET="$2"
shift 2
continue
;;
'-n'|'--subnet-name')
AZ_VNET_SUBNET_NAME="$2"
shift 2
continue
;;
'-u'|'--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_SHARED_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 %s subscription...\\n" "$(az account show --subscription "${AZ_SUBSCRIPTION_ID}" --query name --output tsv)"
az account set --subscription "${AZ_SUBSCRIPTION_ID}" --output none
if ! az storage account show --subscription "${AZ_SUBSCRIPTION_ID}" --resource-group "${AZ_SHARED_RG}" --name "${AZ_LB_DNS}${AZ_LOCATION}" --output none; then
printf "Create %s resource group...\\n" "${AZ_SHARED_RG}"
az group create \
--location "${AZ_LOCATION}" \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--name "${AZ_SHARED_RG}" \
--output none
printf "Create %s%s Storage Account...\\n" "${AZ_LB_DNS}" "${AZ_LOCATION}"
az storage account create \
--location "${AZ_LOCATION}" \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--resource-group "${AZ_SHARED_RG}" \
--name "${AZ_LB_DNS}${AZ_LOCATION}" \
--https-only true \
--allow-blob-public-access false \
--kind StorageV2 \
--encryption-services blob \
--access-tier Hot \
--sku Standard_LRS \
--output none
printf "Awaiting Storage Account creation...\\n"
sleep 60
fi
if ! az storage container show --subscription "${AZ_SUBSCRIPTION_ID}" --account-name "${AZ_LB_DNS}${AZ_LOCATION}" --name "${AZ_CONTAINER}" --output none; then
printf "Create %s blob container...\\n" "${AZ_CONTAINER}"
az storage container create \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--name "${AZ_CONTAINER}" \
--account-name "${AZ_LB_DNS}${AZ_LOCATION}" \
--public-access "off" \
--output none
printf "Create a ReadWriteList policy for %s blob container...\\n" "${AZ_CONTAINER}"
az storage container policy create \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--container-name "${AZ_CONTAINER}" \
--account-name "${AZ_LB_DNS}${AZ_LOCATION}" \
--name "rwl" \
--permissions "rwl" \
--expiry "$(date -u -d "100 years" '+%Y-%m-%dT%H:%MZ')" \
--start "$(date -u -d "-1 days" '+%Y-%m-%dT%H:%MZ')" \
--output none
printf "Generate SAS Token to access the %s blob container...\\n" "${AZ_CONTAINER}"
sas=$(az storage container generate-sas \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--name "${AZ_CONTAINER}" \
--account-name "${AZ_LB_DNS}${AZ_LOCATION}" \
--policy-name "rwl" \
--https-only \
--output tsv)
printf "Your Storage access key is: \"%s\"\\n" "${sas}"
echo "${sas}" > ~/"${AZ_LB_DNS}""${AZ_LOCATION}"_"${AZ_CONTAINER}"_sas.txt
fi
if ! az network vnet subnet show --subscription "${AZ_SUBSCRIPTION_ID}" --resource-group "${AZ_SHARED_RG}" --vnet-name "${AZ_VNET}" --name "${AZ_VNET_SUBNET_NAME}" --output none; then
printf "Create a new 10.1.0.0/16 VNET named %s...\\n" "${AZ_VNET}"
az network vnet create \
--location "${AZ_LOCATION}" \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--resource-group "${AZ_SHARED_RG}" \
--name "${AZ_VNET}" \
--address-prefix "10.1.0.0/16" \
--output none
printf "Create a new %s subnet named %s...\\n" "${AZ_VNET_SUBNET}" "${AZ_VNET_SUBNET_NAME}"
az network vnet subnet create \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--resource-group "${AZ_SHARED_RG}" \
--vnet-name "${AZ_VNET}" \
--name "${AZ_VNET_SUBNET_NAME}" \
--address-prefix "${AZ_VNET_SUBNET}" \
--output none
fi
printf "Create %s resource group...\\n" "${AZ_VM_RG}"
az group create \
--location "${AZ_LOCATION}" \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--name "${AZ_VM_RG}" \
--output none
printf "Create NSG %s-nsg...\\n" "${AZ_VM}"
az network nsg create \
--location "${AZ_LOCATION}" \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--name "${AZ_VM}-nsg" \
--resource-group "${AZ_VM_RG}" \
--output none
printf "Create NSG rule to allow inbound connections...\\n"
az network nsg rule create \
--name "AllowSSH" \
--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 "4160" \
--access "Allow" \
--protocol "tcp" \
--description "Allow SSH traffic from Any" \
--output none
az network nsg rule create \
--name "AllowValheim" \
--nsg-name "${AZ_VM}-nsg" \
--resource-group "${AZ_VM_RG}" \
--priority "1010" \
--direction "Inbound" \
--source-address-prefixes "*" \
--source-port-ranges "*" \
--destination-address-prefixes "VirtualNetwork" \
--destination-port-ranges "2456-2457" \
--access "Allow" \
--protocol "udp" \
--description "Allow Valheim traffic from Any" \
--output none
printf "Create %s.%s.cloudapp.azure.com standard public IP address...\\n" "${AZ_LB_DNS}" "${AZ_LOCATION}"
az network public-ip create \
--location "${AZ_LOCATION}" \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--name "${AZ_LB}-public-ip" \
--resource-group "${AZ_VM_RG}" \
--allocation-method "Static" \
--sku "Standard" \
--version "IPv4" \
--ip-tags 'RoutingPreference=Internet' \
--zone 1 2 3 \
--dns-name "${AZ_LB_DNS}" \
--output none
printf "Create NIC...\\n"
az network nic create \
--location "${AZ_LOCATION}" \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--name "${AZ_VM}-nic" \
--resource-group "${AZ_VM_RG}" \
--subnet "/subscriptions/${AZ_SUBSCRIPTION_ID}/resourceGroups/${AZ_SHARED_RG}/providers/Microsoft.Network/virtualNetworks/${AZ_VNET}/subnets/${AZ_VNET_SUBNET_NAME}" \
--public-ip-address "${AZ_LB}-public-ip" \
--network-security-group "${AZ_VM}-nsg" \
--output none
printf "Create %s Azure Virtual Machine...\\n" "${AZ_VM}"
az vm create \
--location "${AZ_LOCATION}" \
--subscription "${AZ_SUBSCRIPTION_ID}" \
--name "${AZ_VM}" \
--resource-group "${AZ_VM_RG}" \
--image "Canonical:0001-com-ubuntu-server-focal:20_04-lts-gen2:latest" \
--size "Standard_F2s_v2" \
--nics "${AZ_VM}-nic" \
--storage-sku "StandardSSD_LRS" \
--admin-username "yandolfat" \
--ssh-key-value "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCe0lgCF/ZKiUJnl8gbSQSKvzIiWZM8ZouxUxjmXGJIXvacmZCC/Ou7UvX5JMQFqUcYe63BSGOz93X2r4e17M++JbOR+ShloGS+4+w+wu6MAYaiVIC6/PmhSfyzFXEWuE+dLadNwJMF8ePUXqwYZntRy5Gahu1wYSkqaif3TNsDRCDYcd0viCOEmGN+NYeoNJwGQ9HIWJ29sY/BUZJWEVB0ZweTvNqwtl3bMvY/JHmEmEIYwdRcdROPEPmxcuBH81Tt2fsD9V7DYhyvz2lQPVJD++3jIZX2i9sPQj8SVJbo23xOZZykVIKU7WaztBtPPz3RdytBiyQ8sgNwKLbJX7Vv0+qY1no4xUnKwJPc5zfikje4rYxTksjIRg7igMNrCFGWZA75hb+Nm+HhQsKqVHtOIaw3P6j6slysQQ5MOQYTqg7k60yxTRGTv8Y6V45jrYWQg+vhKO4gzVTKsqrqJTRhJXU3vv//1NPW7ucNlNPCF8n0RyjXue6Y1Xr8rZv5QheLZvcHumd23pA+Z6aRA/Hd2VINy00PQz9dscOpWHpUiiu4HMPHLcLdlhaVMFr2otwB2749xHciZFCsWnprMGX6V3lVGHQ3OFfIBFz1ZVFG+eAbXmZZepdtwVJDidXDfvzAtMol/+PwVVUJgpA1a1dryyZkg9k2FbO1bSVolvmkpQ== Yves ANDOLFATTO" \
--custom-data "./cloudinit.yml" \
--output none
printf "Done.\\n\\n"