Skip to content

Commit 33dbcff

Browse files
author
zhuangxy
committed
upload wmla mongodb recreate script
1 parent 5e7346f commit 33dbcff

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/bin/bash
2+
3+
R='\033[0;31m'
4+
G='\033[0;32m'
5+
NC='\033[0m'
6+
SUCCESS="${G}OK${NC}"
7+
FAIL="${R}FAIL${NC}"
8+
9+
oc project
10+
echo WARNING: This script will recreate WML-A MongoDB. Please stop all WML-A workloads and back up your data if necessary before proceeding.
11+
while true; do
12+
read -p "Continue? (yes/no): " choice
13+
case $choice in
14+
[Yy]es)
15+
break
16+
;;
17+
[Nn]o)
18+
echo "Exiting."
19+
exit
20+
;;
21+
*)
22+
echo "Please enter yes or no."
23+
;;
24+
esac
25+
done
26+
27+
shutdown_mongodb() {
28+
local timeout=300
29+
local start_time=$(date +%s)
30+
31+
echo -e "\nShutting down MongoDB..."
32+
oc scale --replicas=0 sts wmla-mongodb
33+
34+
echo Waiting for all MongoDB replicas to exit...
35+
local replicas=$(oc get sts wmla-mongodb -o=jsonpath='{.status.readyReplicas}')
36+
37+
while true; do
38+
replicas=$(oc get sts wmla-mongodb -o=jsonpath='{.status.readyReplicas}')
39+
if [ "_$replicas" == "_" ]; then
40+
echo "Replicas is 0 for Mongodb."
41+
break
42+
fi
43+
44+
local current_time=$(date +%s)
45+
local elapsed_time=$((current_time - start_time))
46+
if [ $elapsed_time -gt $timeout ]; then
47+
echo -e "Shut down MongoDB - ${FAIL}"
48+
echo Please ensure that all Mongodb pods have exited properly before rerunning this script.
49+
exit 1
50+
fi
51+
52+
sleep 5
53+
done
54+
echo -e "Shut down MongoDB - ${SUCCESS}"
55+
}
56+
57+
restart_mongodb() {
58+
local timeout=300
59+
local start_time=$(date +%s)
60+
61+
echo -e "\nRestarting MongoDB..."
62+
oc scale --replicas=3 sts wmla-mongodb
63+
64+
echo Waiting for all MongoDB replicas to start...
65+
local replicas=$(oc get sts wmla-mongodb -o=jsonpath='{.status.readyReplicas}')
66+
67+
while true; do
68+
replicas=$(oc get sts wmla-mongodb -o=jsonpath='{.status.readyReplicas}')
69+
if [ "_$replicas" == "_3" ]; then
70+
echo "Replicas is 3 for Mongodb."
71+
break
72+
fi
73+
74+
local current_time=$(date +%s)
75+
local elapsed_time=$((current_time - start_time))
76+
if [ $elapsed_time -gt $timeout ]; then
77+
echo -e "Restart MongoDB - ${FAIL}"
78+
echo Please ensure that all Mongodb pods have exited properly before rerunning this script.
79+
exit 1
80+
fi
81+
82+
sleep 5
83+
done
84+
echo -e "Restart MongoDB - ${SUCCESS}"
85+
}
86+
87+
reconnect_mongodb() {
88+
local timeout=300
89+
local start_time=$(date +%s)
90+
91+
echo -e "\nRestarting WML-A dlpd, msd and watchdog pods..."
92+
oc get po |grep -E 'wmla-dlpd|wmla-msd|wmla-watchdog' |awk '{print $1}' |xargs oc delete po
93+
94+
echo Waiting for WML-A dlpd, msd and watchdog pods to start...
95+
local replicas_dlpd=$(oc get deploy wmla-dlpd -o=jsonpath='{.status.readyReplicas}')
96+
local replicas_msd=$(oc get deploy wmla-msd -o=jsonpath='{.status.readyReplicas}')
97+
local replicas_watchdog=$(oc get deploy wmla-watchdog -o=jsonpath='{.status.readyReplicas}')
98+
99+
while true; do
100+
replicas_dlpd=$(oc get deploy wmla-dlpd -o=jsonpath='{.status.readyReplicas}')
101+
replicas_msd=$(oc get deploy wmla-msd -o=jsonpath='{.status.readyReplicas}')
102+
replicas_watchdog=$(oc get deploy wmla-watchdog -o=jsonpath='{.status.readyReplicas}')
103+
if [ "_$replicas_dlpd" == "_1" ] && [ "_$replicas_msd" == "_1" ] && [ "_$replicas_watchdog" == "_1" ]; then
104+
break
105+
fi
106+
107+
local current_time=$(date +%s)
108+
local elapsed_time=$((current_time - start_time))
109+
if [ $elapsed_time -gt $timeout ]; then
110+
echo -e "Restart WML-A dlpd, msd and watchdog pods timeout - ${FAIL}"
111+
echo Please ensure that the pods for services dlpd, msd, and watchdog are in the correct status.
112+
exit 1
113+
fi
114+
115+
sleep 5
116+
done
117+
echo -e "Restart WML-A dlpd, msd and watchdog pods - ${SUCCESS}"
118+
}
119+
120+
shutdown_mongodb
121+
122+
echo -e "\nCleaning up mongodb data..."
123+
oc delete pvc data-wmla-mongodb-0 data-wmla-mongodb-1 data-wmla-mongodb-2
124+
echo -e "Clean up mongodb data - ${SUCCESS}"
125+
126+
restart_mongodb
127+
reconnect_mongodb
128+
129+
echo All done.

0 commit comments

Comments
 (0)