-
Notifications
You must be signed in to change notification settings - Fork 0
/
az-xdm-instance-restart.sh
executable file
·87 lines (75 loc) · 2.34 KB
/
az-xdm-instance-restart.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
#!/bin/bash -e
# Help menu
print_help() {
cat <<-HELP
Usage: $0 <--resource-group=resource-group-name> [--admin-password=admin-password]
HELP
exit 1
}
resourceGroupName=$XDM_RESOURCE_GROUP
serverPassword=$XDM_ADMIN_PASSWORD
# Parse Command Line Arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--resource-group=*)
resourceGroupName="${1#*=}"
;;
--admin-password=*)
serverPassword="${1#*=}"
;;
--help) print_help;;
*)
printf "***********************************************************\n"
printf "* Error: Invalid argument, run --help for argument list .*\n"
printf "***********************************************************\n"
exit 1
esac
shift
done
if [[ -z $resourceGroupName ]]
then
print_help
fi
if [[ -z $serverPassword ]]
then
# Read Password
echo -n Admin Password:
read -s serverPassword
echo
fi
if ! $(az group exists --name $resourceGroupName)
then
echo " !! resource group $resourceGroupName not found."
exit 1;
fi
scaleSetName=$(az resource list --tag xdm-resource-type=ss-passive --query "[?resourceGroup=='$resourceGroupName'].name" -o tsv)
if [[ -z $scaleSetName ]]
then
echo " !! Scale set not found in $resourceGroupName."
exit 1;
fi
echo " -- Scale set found ($scaleSetName)."
vmActiveId=$(az resource list --tag xdm-resource-type=vm-active --query "[?resourceGroup=='$resourceGroupName'].id" -o tsv)
if [[ -z $vmActiveId ]]
then
echo " !! Active VM not found in $resourceGroupName."
exit 1;
fi
echo " -- Active VM found ($vmActiveId)."
# retrieve current vm properties
currentVmProps=$(az vm show --ids $vmActiveId)
serverUser=$(echo $currentVmProps | jq -r '.osProfile.adminUsername')
vmName=$(echo $currentVmProps | jq -r '.name')
echo " --> Restarting active VM..."
az vm extension set \
--resource-group $resourceGroupName \
--vm-name $vmName \
--name customScript \
--publisher Microsoft.Azure.Extensions \
--force-update \
--protected-settings '{"commandToExecute": "/usr/local/xdm/bin/deploy-webapp-ubuntu.sh --xdm-package=active --server-user=\"'$serverUser'\" --server-password=\"'$serverPassword'\""}'
echo " -- Active VM restarted."
echo " --> Restarting scale set..."
az vmss reimage --name $scaleSetName --resource-group $resourceGroupName
echo " -- Scale set restarted."
echo " -- Instance restarted successfully."