-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgracefulRestart.sh
199 lines (161 loc) · 5.88 KB
/
gracefulRestart.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash
. /etc/init.d/functions
LOG="gracefulRestart.log"
STARTTIME=`date +"%y-%m-%d_%H-%M-%s"`
SHUTDOWNTIMER=360
echo '--------------------------------------------------------------------------------------' >> $LOG
echo $STARTTIME >> $LOG
echo `hostname` >> $LOG
echo '--------------------------------------------------------------------------------------' >> $LOG
# Use step(), try(), and next() to perform a series of commands and print
# [ OK ] or [FAILED] at the end. The step as a whole fails if any individual
# command fails.
#
step() {
echo -n -e "$@"
echo -e "\n\nSTEP - $@">> $LOG
STEP_OK=0
[[ -w /tmp ]] && echo $STEP_OK > /tmp/step.$$
}
try() {
# Check for `-b' argument to run command in the background.
local BG=
[[ $1 == -b ]] && { BG=1; shift; }
[[ $1 == -- ]] && { shift; }
# Run the command.
echo -e "$@" >> $LOG
if [[ -z $BG ]]; then
"$@" | tee -a $LOG
else
"$@" &
fi
# Check if command failed and update $STEP_OK if so.
local EXIT_CODE=$?
if [[ $EXIT_CODE -ne 0 ]]; then
STEP_OK=$EXIT_CODE
[[ -w /tmp ]] && echo $STEP_OK > /tmp/step.$$
if [[ -n $LOG_STEPS ]]; then
local FILE=$(readlink -m "${BASH_SOURCE[1]}")
local LINE=${BASH_LINENO[0]}
echo "$FILE: line $LINE: Command \'$*\' failed with exit code $EXIT_CODE." >> "$LOG_STEPS"
fi
fi
return $EXIT_CODE
}
next() {
[[ -f /tmp/step.$$ ]] && { STEP_OK=$(< /tmp/step.$$); rm -f /tmp/step.$$; }
[[ $STEP_OK -eq 0 ]] && echo_success || echo_failure
echo
return $STEP_OK
}
setpass() {
echo -n "$@"
STEP_OK=0
[[ -w /tmp ]] && echo $STEP_OK > /tmp/step.$$
}
setfail() {
echo -n "$@"
STEP_OK=1
[[ -w /tmp ]] && echo $STEP_OK > /tmp/step.$$
}
logger -t SCRIPT Executing $0, see $LOG for details
step "Saving any system infomration for debug"
#todo put information gathing inforamtion here such as xmsinfo.sh or gencores.sh
#./xmsinfo.sh
echo -e "No Debug Collection Enabled" >> $LOG
setpass;
next
step "Configuring graceful shutdown timer to $SHUTDOWNTIMER seconds"
RES="$(curl -s -i -H "Accept: application/json" -H "Content-Type: application/json" -H "UserName: superadmin" -X PUT -d "{\"graceful_shutdown_timeout\":\"240\"}" http://localhost:10080/services)"
echo -e $RES >> $LOG
setpass;
next
step "Initiating graceful shutdown"
RES="$(curl -s -i -H "Accept: application/json" -H "Content-Type: application/json" -H "UserName: superadmin" -X PUT -d "{\"state\":\"OUTOFSERVICE\"}" http://localhost:10080/services)"
echo -e $RES >> $LOG
setpass;
next
step "Waiting for graceful shutdown to complete"
LOOPTIMEOUT=$(($SHUTDOWNTIMER+120 ));
LOOPTIMEOUT=$(($LOOPTIMEOUT/10));
#echo -e "Looptimeout=${LOOPTIMEOUT}\n" ;
while [ $LOOPTIMEOUT -gt 0 ] ;
do
SIPCOUNT=$(cat /var/lib/xms/meters/currentValue.txt | grep xmsResources.xmsSignalingSessions | awk -F' ' '{print $3}')
if [[ -z "$SIPCOUNT" ]];
then
SIPCOUNT="N/A"
fi
MEDIACOUNT=$(cat /var/lib/xms/meters/currentValue.txt | grep xmsResources.xmsMediaTransactions | awk -F' ' '{print $3}')
if [[ -z "$MEDIACOUNT" ]];
then
MEDIACOUNT="N/A"
fi
STATE="$(curl -s http://127.0.0.1:10080/services | grep -P -o 'state=".*?"'|awk -F '"' '{print $2}')"
echo -e "State=$STATE, SIPCOUNT=$SIPCOUNT, MediaTransaction=$MEDIACOUNT" >> $LOG
if [[ $STATE == "STOPPED" ]];
then
LOOPTIMEOUT=0
elif [[ $STATE == "FAILED" ]];
then
LOOPTIMEOUT=0
else
(( LOOPTIMEOUT-- ))
echo -n "."
sleep 10
fi
done
setpass;
next
#Step 3-Stop the Nodecontroller (TODO- Update this to graceful shutdown)
#step "Stoping nodecontroller service"
echo "STEP - Stopping nodecontroller">>$LOG
service nodecontroller stop | tee -a $LOG
#Step 4-check the number of files in the cache
step "Checking the number of files in cache\n"
FILECOUNT="$(echo `ls -l /var/cache/xms/http/xmserver/ |wc -l`)"
#TODO check to make sure there are a large number of files
setpass;
next
echo -e " $FILECOUNT files found in the xms http cache \n" | tee -a $LOG
#Step 5- Note the permisions and owner of the xmserver
#TODO should ceck to see if we have ability to make write a new folder
step "Checking File permissions on current cache directory"
try ls -al /var/cache/xms/http | grep xmserver >> $LOG
next
#Step 6- Move the Current folder to a new name
step "Renaming current directory"
try mv /var/cache/xms/http/xmserver /var/cache/xms/http/xmserver_del
next
#Step 7- Make a new directory (TODO- Need to check the file permisions
step "Making new cache directory"
try mkdir /var/cache/xms/http/xmserver
next
#Step 7a - Resetting file permisions
step "Setting chown on new cache folder"
try chown --reference=/var/cache/xms/http/xmserver_del /var/cache/xms/http/xmserver
next
#Step 7b - Resetting file permisions
step "Setting chmod on new cache folder"
try chmod --reference=/var/cache/xms/http/xmserver_del /var/cache/xms/http/xmserver
next
#Step 7c - Cehcking permissions and owner of new dir
step "Checking File permissions on current cache directory"
try ls -al /var/cache/xms/http | grep xmserver >> $LOG
next
#TODO should check to make sure that it is set correctly
#NOTE: If this is taking too long, likely you can comment this line out and uncomment one after restart
#Step - Delete the old del dir
step "\nDeleting files (This may take some time)"
start=$(date +%s.%N);
try find /var/cache/xms/http/xmserver_del -type f -delete
try rm -rf /var/cache/xms/http/xmserver_del
dur=$(echo "$(date +%s.%N) - $start" | bc)
next
printf " Complete Execution time: %.3f seconds\n" $dur | tee -a $LOG
#Step 9- Restart the Nodecontroller
#step "Restarting XMS"
#TODO Should check to see if this should be done before or after the delet happens
echo "STEP - Starting nodecontroller">>$LOG
service nodecontroller start | tee -a $LOG
echo -e "Process complete, services have been restarted. Log saved to $LOG \n"