Skip to content

Commit 2892ca1

Browse files
authored
Update datasync.sh
Add additional options for existing Azure vNet and Subnet
1 parent b426bb9 commit 2892ca1

File tree

1 file changed

+59
-13
lines changed

1 file changed

+59
-13
lines changed

src/bash/datasync.sh

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@
44

55
set -e
66

7-
while getopts ":l:r:v:" opt; do
7+
while getopts ":d:l:r:v:g:n:s:" opt; do
88
case $opt in
9+
d) deployment_type="$OPTARG"
10+
;;
911
l) location="$OPTARG"
1012
;;
1113
r) resource_group="$OPTARG"
1214
;;
1315
v) vm_name="$OPTARG"
1416
;;
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
1624
exit 1
1725
;;
1826
esac
@@ -24,11 +32,33 @@ while getopts ":l:r:v:" opt; do
2432
esac
2533
done
2634

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'."
2954
exit 1
3055
fi
3156

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+
3262

3363
# Exiting if not running on X86_64 architecture
3464
arch=$(uname -m)||(arch)
@@ -43,9 +73,13 @@ if [ ! -w "/tmp" ]; then
4373
exit 1
4474
fi
4575

76+
echo -e "\033[0;33mArgument deployment type is $deployment_type\033[0m"
4677
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"
4983

5084
AZCOPY_VERSION=v10
5185

@@ -57,11 +91,11 @@ enabled=1
5791
gpgcheck=1
5892
gpgkey=https://packages.microsoft.com/keys/microsoft.asc" | tee /etc/yum.repos.d/azure-cli.repo > /dev/null
5993
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"
6195
curl -Ls "https://aka.ms/downloadazcopy-$AZCOPY_VERSION-linux" -o /tmp/azcopy.tar.gz
6296
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
6599
}
66100

67101
function download_datasync(){
@@ -110,15 +144,20 @@ function upload_to_azure(){
110144
}
111145

112146
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"
114148
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; }
115149
}
116150

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+
117156
function cleanup(){
118157
rm -f /tmp/datasync.zip
119158
rm -rf /tmp/aws-datasync-*
120159
rm -rf /tmp/azcopy*
121-
az logout
160+
az logout || true
122161
echo -e "\033[0m"
123162
}
124163

@@ -128,6 +167,13 @@ download_and_install_dependencies
128167
download_datasync
129168
convert_datasync
130169
upload_to_azure
131-
create_azure_vm
132-
cleanup
133170
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

Comments
 (0)