-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshut.sh
executable file
·59 lines (55 loc) · 2.45 KB
/
shut.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
#!/usr/bin/env bash
#set -x
to_date=$(date +'%d-%m-%Y')
SUBSCRIPTIONS=$(az account list -o json)
jq -c '.[]' <<< $SUBSCRIPTIONS | while read subscription; do
SUBSCRIPTION_ID=$(jq -r '.id' <<< $subscription)
az account set -s $SUBSCRIPTION_ID
#CLUSTERS=$(az resource list \
#--resource-type Microsoft.ContainerService/managedClusters \
#--query "[?tags.autoShutdown == 'true']" -o json)
CLUSTERS=$(az resource list \
--resource-type Microsoft.ContainerService/managedClusters -o json)
jq -c '.[]' <<< $CLUSTERS | while read cluster; do
RESOURCE_GROUP=$(jq -r '.resourceGroup' <<< $cluster)
SKIP="false"
NAME=$(jq -r '.name' <<< $cluster)
echo "----------------"
ENV=$(echo $NAME|cut -d'-' -f2)
BU=$(echo $NAME|cut -d'-' -f1)
ENV=${ENV/#sbox/Sandbox}
ENV=${ENV/stg/Staging}
echo $NAME $BU $ENV
while read id
do
BA=$(jq -r '."Business area"' <<< $id)
ENVT=$(jq -r '."Environment"' <<< $id)
SD=$(jq -r '."Skip shutdown start date"' <<< $id)
ED=$(jq -r '."Skip shutdown end date"' <<< $id)
SDS=$(date -j -f "%d-%m-%Y" "${SD}" +"%s")
EDS=$(date -j -f "%d-%m-%Y" "${ED}" +"%s")
TOSEC=$(date -j -f "%d-%m-%Y" "${to_date}" +"%s")
DIFF=$(( $EDS - $TOSEC ))
if [[ ${ENVT} =~ ${ENV} ]] && [[ $BU == $BA ]] && [[ $SDS -eq $TOSEC ]] ; then
#echo $NAME $BU $ENV $BA $ENVT $SD $ED $SDS $EDS $TOSEC $to_date $DIFF
echo "Match: $id"
SKIP="true"
continue
elif [[ ${ENVT} =~ ${ENV} ]] && [[ $BU == $BA ]] && [[ $DIFF -lt 129600 ]]; then
echo "Match : $id"
#echo $NAME $BU $ENV $BA $ENVT $SD $ED $SDS $EDS $TOSEC $to_date $DIFF
SKIP="true"
continue
fi
echo $SKIP
done < <(jq -c '.[]' issues_list.json)
echo $SKIP
if [[ $SKIP == "false" ]]; then
echo "About to shutdown cluster $NAME (rg:$RESOURCE_GROUP)"
else
echo "cluster $NAME (rg:$RESOURCE_GROUP) has been skipped from todays shutdown schedule"
fi
#echo az aks stop --resource-group $RESOURCE_GROUP --name $NAME || echo Ignoring any errors stopping cluster
#-az aks stop --resource-group $RESOURCE_GROUP --name $NAME || echo Ignoring any errors stopping cluster
done # end_of_cluster_loop
done