4
4
5
5
set -e
6
6
7
- while getopts " :l:r:v:" opt; do
7
+ while getopts " :d: l:r:v:g:n:s :" opt; do
8
8
case $opt in
9
+ d) deployment_type=" $OPTARG "
10
+ ;;
9
11
l) location=" $OPTARG "
10
12
;;
11
13
r) resource_group=" $OPTARG "
12
14
;;
13
15
v) vm_name=" $OPTARG "
14
16
;;
15
- \? ) echo " Invalid option -$OPTARG " >&3
17
+ g) vnet_rg=" $OPTARG "
18
+ ;;
19
+ n) vnet_name=" $OPTARG "
20
+ ;;
21
+ s) subnet_name=" $OPTARG "
22
+ ;;
23
+ \? ) echo " Invalid option -$OPTARG " >&2
16
24
exit 1
17
25
;;
18
26
esac
@@ -24,11 +32,33 @@ while getopts ":l:r:v:" opt; do
24
32
esac
25
33
done
26
34
27
- if [ $# -ne 6 ]; then
28
- echo " Missing -l or -r or -v"
35
+ shift $(( OPTIND - 1 ))
36
+
37
+ # Check if deployment type is "new_vnet" or "existing_vnet"
38
+ if [ " $deployment_type " != " new_vnet" ] && [ " $deployment_type " != " existing_vnet" ]; then
39
+ echo " Invalid value for deployment type (-d): $deployment_type . Deployment type must be 'new_vnet' or 'existing_vnet'."
40
+ exit 1
41
+ fi
42
+
43
+ # Mandatory parameters deployment_type, location, resource_group and vm_name
44
+ if [ -z " $deployment_type " ] || [ -z " $location " ] || [ -z " $resource_group " ] || [ -z " $vm_name " ]; then
45
+ echo " Required parameters are missing. Usage: -d [new_vnet|existing_vnet] -l [location] -r [resource_group] -v [vm_name] [-g [vnet_rg]] [-n [vnet_name]] [-s [subnet_name]]"
46
+ exit 1
47
+ fi
48
+
49
+
50
+
51
+ # Check if deployment_type is existing_vnet, then vnet_rg, vnet_name, and subnet_name are mandatory
52
+ if [ " $deployment_type " == " existing_vnet" ] && { [ -z " $vnet_rg " ] || [ -z " $vnet_name " ] || [ -z " $subnet_name " ]; }; then
53
+ echo " -g [vnet_rg] -n [vnet_name] -s [subnet_name] are mandatory when deployment_type [-d] is 'existing_vnet'."
29
54
exit 1
30
55
fi
31
56
57
+ # if [ $# -ne 12 ]; then
58
+ # echo "Missing -l or -r or -v or -g or -n or -s"
59
+ # exit 1
60
+ # fi
61
+
32
62
33
63
# Exiting if not running on X86_64 architecture
34
64
arch=$( uname -m) || (arch)
@@ -43,9 +73,13 @@ if [ ! -w "/tmp" ]; then
43
73
exit 1
44
74
fi
45
75
76
+ echo -e " \033[0;33mArgument deployment type is $deployment_type \033[0m"
46
77
echo -e " \033[0;33mArgument location is $location \033[0m"
47
- echo -e " \033[0;33mArgument resource_group is $resource_group \033[0m"
48
- echo -e " \033[0;33mArgument vm_name is $vm_name \033[0m"
78
+ echo -e " \033[0;33mArgument vm resource group is $resource_group \033[0m"
79
+ echo -e " \033[0;33mArgument vm name is $vm_name \033[0m"
80
+ echo -e " \033[0;33mArgument vnet resource group is $vnet_rg \033[0m"
81
+ echo -e " \033[0;33mArgument vnet name is $vnet_name \033[0m"
82
+ echo -e " \033[0;33mArgument subnet name is $subnet_name \033[0m"
49
83
50
84
AZCOPY_VERSION=v10
51
85
@@ -57,11 +91,11 @@ enabled=1
57
91
gpgcheck=1
58
92
gpgkey=https://packages.microsoft.com/keys/microsoft.asc" | tee /etc/yum.repos.d/azure-cli.repo > /dev/null
59
93
yum -y -q install qemu-img jq unzip azure-cli && yum -y clean all && rm -rf /var/cache
60
- echo " \033[0;33mWarning azcopy will be downloaded from Internet but there is no integrity hash available.\033[0;33m"
94
+ echo " \033[0;33mWarning azcopy will be downloaded from Internet but there is no integrity hash available.\033[0;33m"
61
95
curl -Ls " https://aka.ms/downloadazcopy-$AZCOPY_VERSION -linux" -o /tmp/azcopy.tar.gz
62
96
tar xzf /tmp/azcopy.tar.gz --directory /tmp || { echo " AzCopy download or extraction failed" ; exit 1; }
63
- cp /tmp/azcopy_linux_amd64* /azcopy /usr/local/sbin /azcopy
64
- chmod +x /usr/local/sbin /azcopy
97
+ cp /tmp/azcopy_linux_amd64* /azcopy /usr/bin /azcopy
98
+ chmod +x /usr/bin /azcopy
65
99
}
66
100
67
101
function download_datasync(){
@@ -110,15 +144,20 @@ function upload_to_azure(){
110
144
}
111
145
112
146
function create_azure_vm(){
113
- echo -e " \033[0;33mCreating Azure Virtual Machine for DataSync\033[0;33m"
147
+ echo -e " \033[0;33mCreating Azure Virtual Machine for DataSync with a new vnet \033[0;33m"
114
148
az vm create -g " $resource_group " -l " $location " --name " $vm_name " --size Standard_E4as_v4 --os-type linux --attach-os-disk " $disk_name " --public-ip-address " " --only-show-errors || { echo " An error occured while creating the Azure VM" ; exit 1; }
115
149
}
116
150
151
+ function create_azure_vm_existing_vnet(){
152
+ echo -e " \033[0;33mCreating Azure Virtual Machine for DataSync with an existing vnet\033[0;33m"
153
+ az vm create -g " $resource_group " -l " $location " --name " $vm_name " --size Standard_E4as_v4 --os-type linux --attach-os-disk " $disk_name " --subnet " $( az network vnet subnet show --resource-group $vnet_rg --vnet-name $vnet_name --name $subnet_name -o tsv --query id) " --public-ip-address " " --only-show-errors || { echo " An error occured while creating the Azure VM" ; exit 1; }
154
+ }
155
+
117
156
function cleanup(){
118
157
rm -f /tmp/datasync.zip
119
158
rm -rf /tmp/aws-datasync-*
120
159
rm -rf /tmp/azcopy*
121
- az logout
160
+ az logout || true
122
161
echo -e " \033[0m"
123
162
}
124
163
@@ -128,6 +167,13 @@ download_and_install_dependencies
128
167
download_datasync
129
168
convert_datasync
130
169
upload_to_azure
131
- create_azure_vm
132
- cleanup
133
170
popd
171
+
172
+ if [ " $deployment_type " == " new_vnet" ]; then
173
+ echo " Deployment type is new_vnet"
174
+ create_azure_vm
175
+ elif [ " $deployment_type " == " existing_vnet" ]; then
176
+ echo " Deployment type is existing_vnet"
177
+ create_azure_vm_existing_vnet
178
+ fi
179
+ cleanup
0 commit comments