-
Notifications
You must be signed in to change notification settings - Fork 0
/
restart_cluster_services.sh
executable file
·60 lines (51 loc) · 1.58 KB
/
restart_cluster_services.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
#!/bin/bash
SERVICE_FILE=cluster_services.csv
F_PromptContinue () {
read -p "Are you sure [yn]? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborting!"
exit 1
fi
}
F_Log () {
echo "[$(date)] $1"
}
echo
F_Log "The following services will be restarted:"
echo
echo "----------------------------------------------------------------------------"
cat $SERVICE_FILE |column -t -s ,
echo "----------------------------------------------------------------------------"
F_PromptContinue
echo
F_Log "Starting Service Termination"
while IFS="," read -r NAMESPACE SERVICE_TYPE SERVICE_NAME SERVICE_RESTART
do
if [[ $SERVICE_RESTART != 'true' ]]; then
continue
fi
F_Log "Restarting $SERVICE_NAME"
PODS=$(kubectl get pods -n $NAMESPACE |grep -P "^$SERVICE_NAME*" |grep -v Terminating |awk '{print $1}')
for POD in $PODS
do
kubectl delete -n $NAMESPACE --wait=true pod/$POD
done
done < <(sed '1d' $SERVICE_FILE)
F_Log "Completed Service Termination"
echo
F_Log "Checking for Services to become ready"
while IFS="," read -r NAMESPACE SERVICE_TYPE SERVICE_NAME SERVICE_RESTART
do
if [[ $SERVICE_RESTART != 'true' ]]; then
continue
fi
F_Log "Waiting for $SERVICE_NAME to become Ready"
PODS=$(kubectl get pods -n $NAMESPACE |grep -P "^$SERVICE_NAME*" |grep -v Terminating |awk '{print $1}')
for POD in $PODS
do
kubectl wait --for=condition=Ready -n $NAMESPACE pod/$POD --timeout=120s || echo "Failed to restart $SERVICE_NAME"
done
done < <(sed '1d' $SERVICE_FILE)
echo
F_Log "All service have restarted successfully"