-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnginx_config_updater.sh
More file actions
executable file
·237 lines (210 loc) · 5.96 KB
/
nginx_config_updater.sh
File metadata and controls
executable file
·237 lines (210 loc) · 5.96 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash
#
# Author: Joshua Chen <iesugrace@gmail.com>
# Date: 2016-03-10
# Location: Shenzhen
# Desc: update cache server config
#
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/nginx ]; then
. /etc/sysconfig/nginx
fi
log() {
local tag="CDN-CONFIG-UPDATE"
local pri=local0.info
logger -t "$tag" -p "$pri" "$*"
echo "$(date '+%Y-%m-%d %H:%M:%S') $*"
}
report() {
local groupName apiUrl msg
#groupName="PLCDN-SUPPORT"
#groupName="PLCDN-STATUS"
apiUrl="http://push.plcdn.net:7890/20160128"
msg=$1
groupName=$2
wget -q --header="To: $groupName" \
--post-data="$msg" "$apiUrl" \
-O /dev/null
}
# Check if it's necessary to update
# It needs to update only when the md5 sum of
# the local profile and the remote profile differ.
needUpdate() {
local url="http://${mhost}/index.php/portal/profiles_sync?ip=$self"
localMd5=NULL remoteMd5=
remoteMd5=$(curl -s --connect-timeout 30 -m 30 "$url" 2>/dev/null)
test ${#remoteMd5} -ne 32 && log "bad md5 sum of the remote profile" && exit 1
test -f "$zprofiles" && localMd5=$(md5sum $zprofiles | awk '{print $1}')
test "$remoteMd5" != "$localMd5"
}
fetchConfig() {
local tmout=30
local url="http://${mhost}/profiles/$self/profiles.zip"
test -f "$zprofiles" && mv $zprofiles $backupZprofiles
wget -q -T $tmout -O "$zprofiles" "$url"
cd $tmpConfigDir
unzip -qq $zprofiles
}
# Backup all files but those
# in the list of specialFiles.
backupConfig() {
rm -rf $backupConfigDir && \
mkdir $backupConfigDir && \
cp -r $targetConfigDir/* $backupConfigDir/ && \
cd $backupConfigDir && rm $specialFiles
}
# Remove the names in specialFiles from
# stdin, write the result to stdout.
excludeSpecialFiles() {
local text
text=$(cat)
for name in $specialFiles
do
text=$(sed -r "/^${name}$/d" <<< "$text")
done
echo "$text"
}
# Remove all files except those listed in
# specialFiles, from the target config dir.
cleanTargetConfigDir() {
cd $targetConfigDir
ls | excludeSpecialFiles | xargs rm -rf
}
#checkError() {
# local logFile=$1
# if grep -q 'host not found in upstream' $logFile; then
# errorDom=$(awk -F'"|:' '/[emerg]/{print $3}' $logFile | head -1)
# if grep -qE '(\.)+' <<<$errorDom; then
# bash /root/scripts/source_site_check.sh $errorDom
# test $? -eq 0 && return 0 || return 1
# else
# return 1
# fi
# else
# return 1
# fi
#}
checkError() {
local logFile=$1
if grep -q 'host not found in upstream' $logFile; then
bash /root/scripts/source_site_check.sh &>/dev/null
$nginx -t &> $checkSyntaxLog
ret=$?
return $ret
else
return 1
fi
}
# Move all but those in the specialFiles
# to the target config dir, then check.
#checkSyntax() {
# cleanTargetConfigDir
# cd $tmpConfigDir
# ls | excludeSpecialFiles | xargs -I{} mv {} $targetConfigDir
# $nginx -t &> $checkSyntaxLog
# [ $? -ne 0 ] && checkError $checkSyntaxLog || return 0
# #ret=$?
# #return $ret
#}
checkSyntax() {
cleanTargetConfigDir
cd $tmpConfigDir
ls | excludeSpecialFiles | xargs -I{} mv {} $targetConfigDir
$nginx -t &> $checkSyntaxLog
if test $? -ne 0; then
checkError $checkSyntaxLog
else
return 0
fi
}
# Since all config files that used by the cache
# server are moved to the target location in
# the syntax checking step, here to move only
# the files that are listed in specialFiles.
# mv command is used intentionally for atomicity.
updateConfig() {
cd $tmpConfigDir
mv $specialFiles $targetConfigDir/
}
applyConfig() {
/sbin/service nginx reload &> /dev/null
}
# move all data from the backup dir back to the
# target config dir, remove the downloaded file.
rollback() {
cleanTargetConfigDir
mv $backupConfigDir/* $targetConfigDir/
test -s $backupZprofiles && mv $backupZprofiles $zprofiles
}
# Check if the cache server is running
serverAlive() {
status -p $pidfile -l $lockfile $nginx &> /dev/null
}
feedback() {
local url time
time=$(date +%Y%m%d%H%M%S)
url="http://${mhost}/index.php/portal/device_feedback?ip=$self&version=$pversion&sync_time=$time"
if [ "$1" = good ]; then
url+="&status=2&message="
else
url+="&status=4&message=="
fi
curl -s --connect-timeout 30 -m 30 "$url" &> /dev/null
}
cleanup() {
rm -rf $tmpConfigDir
rm -f $backupZprofiles
rm -f $checkSyntaxLog
}
nodeconf=/etc/nginx/node.conf
zprofiles=/var/log/nginx/profiles.zip
backupZprofiles=$(mktemp)
backupConfigDir="/var/log/nginx/webconf.d_bak"
targetConfigDir="/etc/nginx/webconf.d"
tmpConfigDir=$(mktemp -d)
checkSyntaxLog=$(mktemp)
nginx=${NGINX-/usr/sbin/nginx}
lockfile=${LOCKFILE-nginx}
pidfile=${PIDFILE-/var/run/nginx.pid}
specialFiles="devicelist.txt sitelist.txt"
pversion=$(rpm -q nginx)
self=
mhost=
trap cleanup exit
if ! serverAlive; then
log "cache server is dead"
exit 1
fi
# Get the local IP address, and the server address
if [ -f "$nodeconf" ]; then
self=$(awk -F '[ ;]+' '/^\s*bind/ {print $2}' $nodeconf)
mhost=$(awk -F '[ ;]+' '/^\s*mhost/ {print $2}' $nodeconf)
fi
test -z "$self" && log "missing value of 'bind' in $nodeconf" && exit 1
test -z "$mhost" && log "missing value of 'mhost' in $nodeconf" && exit 1
if ! needUpdate; then
log "no change, nothing to do"
exit 0
fi
if ! fetchConfig; then
rm -rf $zprofiles
log "failed to fetch config"
exit 1
fi
if ! backupConfig; then
log "failed to backup config"
exit 1
fi
if checkSyntax; then
updateConfig
applyConfig
log "config updated: $localMd5 => $remoteMd5"
feedback good
else
rollback
log "syntax check failed, config is rolled back"
msg=$(echo -e ">CheckSyntax failed:\nNode: $self\n-------------------------\n$(cat $checkSyntaxLog)\n-------------------------\nTime: $(date '+%Y-%m-%d %H:%M:%S')")
report "$msg" "PLCDN-SUPPORT"
feedback notgood
fi