-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptu_vm.sh
130 lines (114 loc) · 4.73 KB
/
ptu_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
#!/bin/bash
# 使用方式: bash ptu_vm.sh [create|delete]
if [ "$#" -ne 1 ]; then
echo "Usage: $0 create|delete"
echo " 'create' - create a new VM, or"
echo " 'delete' - delete an existing VM"
exit 1
fi
ACTION=$1
# The script is to create a new VM for PTU testing
#create the resource group
#export RANDOM_ID="$(openssl rand -hex 3)"
export RANDOM_ID="123"
export MY_RESOURCE_GROUP_NAME="ptu-rg$RANDOM_ID" # "swedencentral-vm"
#let the user input REGION name
echo "Please input region name - in lowercase (eg: australiaeast, eastus, swedencentral):"
read REGION
#export REGION="australiaeast" #"swedencentral"
export MY_VM_NAME="ptu-$REGION""_vm" #"swedencentral-vm" #"PTU_VM_$REGION$RANDOM_ID"
export MY_USERNAME=azureuser
export MY_VM_IMAGE="Ubuntu2204"
export SIZE="Standard_D4s_v3" #"Standard_D2s_v3"
# 删除虚拟机和资源组的函数
delete_vm() {
#ask for confirmation if the user really want to delete the resource group, add a line break after the prompt
read -p "Are you sure you want to delete the resource group $MY_RESOURCE_GROUP_NAME? (y/n) " -r
# if not delete, then ask for confirmation if the user really want to delete the VM
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "delete the resource group $MY_RESOURCE_GROUP_NAME"
az group delete --name $MY_RESOURCE_GROUP_NAME --yes --no-wait
else
echo "skip deleting the resource group $MY_RESOURCE_GROUP_NAME"
fi
read -p "Are you sure you want to delete the VM $MY_VM_NAME? (y/n) " -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "delete the VM $MY_VM_NAME"
az vm delete --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_VM_NAME --yes --no-wait
else
echo "skip deleting the VM $MY_VM_NAME"
fi
#echo "delete the VM $MY_VM_NAME"
#az vm delete --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_VM_NAME --yes --no-wait
#echo "delete the resource group $MY_RESOURCE_GROUP_NAME"
#az group delete --name $MY_RESOURCE_GROUP_NAME --yes --no-wait
}
# 创建虚拟机的函数
create_vm() {
echo "Resource group name: $MY_RESOURCE_GROUP_NAME"
# if not the first time creation, then don't create
# 检查资源组是否存在
EXISTS=$(az group exists -n $MY_RESOURCE_GROUP_NAME)
if [ "$EXISTS" = "true" ]; then
echo "resource group $MY_RESOURCE_GROUP_NAME exists"
else
echo "Create resource group $MY_RESOURCE_GROUP_NAME";
az group create --name $MY_RESOURCE_GROUP_NAME --location $REGION;
fi
# 检查VM是否存在
echo "VM name: $MY_VM_NAME, check if the VM exists..."
if [ "$(az vm list -d -o table --query "[?name=='$MY_VM_NAME']" )" = "" ]; then
echo "VM $MY_VM_NAME does not exist, Create a VM $MY_VM_NAME ...";
#create the VM
az vm create \
--resource-group $MY_RESOURCE_GROUP_NAME \
--name $MY_VM_NAME \
--image $MY_VM_IMAGE \
--admin-username $MY_USERNAME \
--assign-identity \
--generate-ssh-keys \
--size Standard_D2s_v3 \
--public-ip-sku Standard;
else
echo "VM $MY_VM_NAME was found, skip creating";
fi
}
# 根据ACTION执行相应的函数,if delete, then skip the following "enable Azure AD login and scp script", just exit after delete_vm()
case "$ACTION" in
create)
create_vm
;;
delete)
delete_vm
exit 0
;;
*)
echo "Usage: $0 [create|delete]"
exit 1
;;
esac
#enable Azure AD login for a linux VM in Azure,Configure prerequisites for Native SSH
echo "enable Azure AD login"
az vm extension set \
--publisher Microsoft.Azure.ActiveDirectory \
--name AADSSHLoginForLinux \
--resource-group $MY_RESOURCE_GROUP_NAME \
--vm-name $MY_VM_NAME
# Store IP address of VM in order to SSH
export IP_ADDRESS=$(az vm show --show-details --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_VM_NAME --query publicIps --output tsv)
#先将脚本传输到远程VM
echo 'Transit test_ptuscript.sh to VM, cmd:';
echo "scp -o StrictHostKeyChecking=no test_ptuscript.sh azureuser@$IP_ADDRESS:/home/azureuser/"
scp -o StrictHostKeyChecking=no test_ptuscript.sh azureuser@$IP_ADDRESS:/home/azureuser/
#execute command on the remote VM
COMMANDS="
if [ ! -d "azure-openai-benchmark" ]; then
echo 'git clone the benchmarking tool';
git clone https://github.com/michaeltremeer/azure-openai-benchmark.git;
fi
echo 'Please running test script test_ptuscript.sh on the VM directly...';
"
##execute downloading benchmarking repo command on the remote VM, not login to the VM
echo "ssh to the $IP_ADDRESS, download the benchmarking tool..."
ssh -o StrictHostKeyChecking=no $MY_USERNAME@$IP_ADDRESS "${COMMANDS}"
echo "ssh command: ssh -o StrictHostKeyChecking=no $MY_USERNAME@$IP_ADDRESS"