-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartClusterVmss.sh
102 lines (81 loc) · 2.61 KB
/
startClusterVmss.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
#!/bin/bash
# Usage: ./startClusterVMSS.sh -n <clusterName> -g <resourceGroup> [-r (to restart cluster)]
# Requires: Azure CLI, jq
# Azure CLI: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
# jq: https://stedolan.github.io/jq/
clusterName=""
resourceGroup=""
restart=0
while [ $# -gt 0 ]; do
case "$1" in
-n|-name|--name)
clusterName="$2"
;;
-g|-resource-group|--resource-group)
resourceGroup="$2"
;;
-restart|--restart)
restart=1
;;
*)
printf "***************************\n"
printf "* Error: Invalid argument.*\n"
printf "***************************\n"
exit 1
esac
shift
shift
done
# check whether Azure CLI & jq installed
which az 2>&1 > /dev/null
if [ $? -gt 0 ]; then
echo "Azure CLI not found. Please ensure you have it installed and in \$PATH"
exit 1
fi
which jq 2>&1 > /dev/null
if [ $? -gt 0 ]; then
echo "jq not found. Please ensure you have it installed and in \$PATH"
exit 1
fi
# check user is logged-in to Azure CLI
signedInUser=$(az ad signed-in-user show)
if [[ -z "$signedInUser" ]]; then
echo "Please login to Azure CLI before running this script"
exit 1
fi
#clusterName="$1"
#resourceGroup="$2"
clusterObjectJson=$(az aks show -n $clusterName -g $resourceGroup -o json)
# check if cluster exists
if [[ ! -z "$clusterObjectJson" ]]; then
clusterId=$(echo "$clusterObjectJson" | jq -r ".id")
echo "Found cluster $clusterId"
else
echo "Cluster not found. Nothing to do"
exit 1
fi
nodePoolNames=$(echo $clusterObjectJson | jq ".agentPoolProfiles[].name" -r)
nodeResourceGroup=$(echo $clusterObjectJson | jq ".nodeResourceGroup" -r)
powerState=$(echo $clusterObjectJson | jq ".powerState.code" -r)
vmssPrefix="aks-"
allVmssInRg=$(az vmss list -o tsv -g $nodeResourceGroup --query "[].name")
# check if cluster already started. Attempt to start if it's in a stopped state.
if [ "$powerState" == "Stopped" ]; then
echo "Cluster is stopped. Starting cluster..."
az aks start -n $clusterName -g $resourceGroup
else
if [ "$restart" == 1 ]; then
# restart the cluster if the restart flag was specified.
echo "Restarting the cluster..."
az aks stop -n $clusterName -g $resourceGroup && az aks start -n $clusterName -g $resourceGroup
else
echo "Cluster already started."
fi
fi
echo -e "Found the following VMSS for this cluster: \n\n$allVmssInRg"
echo "$allVmssInRg" | while IFS=$'\n' read -r line
do
echo starting VMSS $line in $nodeResourceGroup
az vmss start --name $line --resource-group $nodeResourceGroup
done
az aks get-credentials -n $clusterName -g $resourceGroup --admin --overwrite