1 |
############################################################################### |
---|
2 |
## |
---|
3 |
## Module library |
---|
4 |
## |
---|
5 |
## 2007-01-16 Andreas Vogel (omega) |
---|
6 |
## |
---|
7 |
############################################################################### |
---|
8 |
|
---|
9 |
modInstDir=/raid/data/tmp/module # temporary module directory during install |
---|
10 |
modBaseDir=/raid/data/module # root of the module tree |
---|
11 |
modCfgDir=$modBaseDir/cfg # config directory for all modules |
---|
12 |
modBinDir=$modBaseDir/bin # directory for all module executables |
---|
13 |
modShlibDir=$modBaseDir/shlib # directory for all module shared libraries |
---|
14 |
modBackupDir=$modBaseDir/backup/$modName # directory holding backup data for each module |
---|
15 |
modDir=$modBaseDir/$modName # home directory for this module |
---|
16 |
|
---|
17 |
modRC=$modCfgDir/module.rc/$modName.rc # system start/stop script for this module |
---|
18 |
modDB=$modCfgDir/module.db # system module database |
---|
19 |
|
---|
20 |
modLogDir=$modBaseDir/log # directory for log files |
---|
21 |
modLog=$modLogDir/$modName.log # install/uninstall/update log file for module |
---|
22 |
|
---|
23 |
############################################################################### |
---|
24 |
## |
---|
25 |
## Include E-Mail library |
---|
26 |
## |
---|
27 |
############################################################################### |
---|
28 |
shellDir="/raid/data/module/$modName/Shell" |
---|
29 |
[ ! -d "$shellDir" -o ! -r "$shellDir/email.lib" ] && shellDir=/raid/data/tmp/module/Shell |
---|
30 |
{ [ -r "$shellDir/email.lib" ] && source "$shellDir/email.lib"; } || { echo fail; exit 1; } |
---|
31 |
|
---|
32 |
############################################################################### |
---|
33 |
## |
---|
34 |
## Now |
---|
35 |
## |
---|
36 |
############################################################################### |
---|
37 |
Now () { |
---|
38 |
echo $(date '+%Y-%m-%d %H:%M:%S') |
---|
39 |
} |
---|
40 |
|
---|
41 |
############################################################################### |
---|
42 |
## |
---|
43 |
## ModVersion |
---|
44 |
## |
---|
45 |
############################################################################### |
---|
46 |
ModVersion () { |
---|
47 |
local version=$(awk '{print $2}' < "$modDir/VERSION") |
---|
48 |
|
---|
49 |
if [ -z $version ] ; then |
---|
50 |
version="0.00.00" |
---|
51 |
fi |
---|
52 |
echo $version |
---|
53 |
} |
---|
54 |
|
---|
55 |
############################################################################### |
---|
56 |
## |
---|
57 |
## ModuleInit |
---|
58 |
## |
---|
59 |
############################################################################### |
---|
60 |
ModuleInit () { |
---|
61 |
[ -d $modBaseDir -a ! -d $modLogDir ] && mkdir $modLogDir &>/dev/null |
---|
62 |
[ ! -d $modLogDir ] && modLog=/raid/data/$modName.log |
---|
63 |
> $modLog |
---|
64 |
} |
---|
65 |
|
---|
66 |
############################################################################### |
---|
67 |
## |
---|
68 |
## ModuleStart |
---|
69 |
## |
---|
70 |
############################################################################### |
---|
71 |
ModuleStart () { |
---|
72 |
mode=$1 |
---|
73 |
|
---|
74 |
ModuleInit |
---|
75 |
|
---|
76 |
echo "START module $mode: $modName ($(Now))" >>$modLog |
---|
77 |
echo "------------------------------------------------------------------------------" >>$modLog |
---|
78 |
} |
---|
79 |
|
---|
80 |
############################################################################### |
---|
81 |
## |
---|
82 |
## ModuleInstallStart |
---|
83 |
## |
---|
84 |
############################################################################### |
---|
85 |
ModuleInstallStart () { |
---|
86 |
ModuleStart install |
---|
87 |
|
---|
88 |
allDirs="$modCfgDir $modCfgDir/module.rc \ |
---|
89 |
$modBaseDir/backup $modBackupDir \ |
---|
90 |
$modShlibDir $modBinDir \ |
---|
91 |
$modDir $modDir/Shell $modDir/system $modDir/system/etc $modDir/www" |
---|
92 |
|
---|
93 |
for dir in $allDirs ; do |
---|
94 |
echo "Create standard directory: $dir" >>$modLog |
---|
95 |
[ ! -d $dir ] && mkdir $dir >>$modLog 2>&1 |
---|
96 |
FileAttr root root 755 $dir >>$modLog 2>&1 |
---|
97 |
done |
---|
98 |
ln -sf Shell $modDir/shell |
---|
99 |
echo >>$modLog |
---|
100 |
|
---|
101 |
for dir in Shell system www ; do |
---|
102 |
echo "Copy standard directory: $modInstDir/$dir -> $modDir/$dir" >>$modLog |
---|
103 |
for file in $modInstDir/$dir/* ; do |
---|
104 |
[ -e "$file" ] && cp -r $file $modDir/$dir/ >>$modLog 2>&1 |
---|
105 |
done |
---|
106 |
done |
---|
107 |
echo >>$modLog |
---|
108 |
|
---|
109 |
echo "Copy license file: $modInstDir/Configure/license.txt -> $modDir/COPY" >>$modLog |
---|
110 |
rm -f $modDir/COPY >>$modLog 2>&1 |
---|
111 |
cp $modInstDir/Configure/license.txt $modDir/COPY >>$modLog 2>&1 |
---|
112 |
FileAttr root root 644 $modDir/COPY >>$modLog 2>&1 |
---|
113 |
|
---|
114 |
echo "Create module.rc link: $modRC -> $modDir/Shell/module.rc" >>$modLog |
---|
115 |
rm -f $modRC >>$modLog 2>&1 |
---|
116 |
ln -s $modDir/Shell/module.rc $modRC >>$modLog 2>&1 |
---|
117 |
|
---|
118 |
modVersion="$(grep '<md:Version>.*</md:Version>' $modInstDir/Configure/install.rdf | tr '<' ' ' | tr '>' ' ' | awk '{print $2;exit}')" |
---|
119 |
echo "$modName $modVersion $(Now)" > $modDir/VERSION |
---|
120 |
FileAttr root root 644 $modDir/VERSION >>$modLog 2>&1 |
---|
121 |
|
---|
122 |
## |
---|
123 |
## TODO: Patch the apache config file in order to allow .htaccess files in the module directory |
---|
124 |
## |
---|
125 |
apacheCfg=/etc/httpd/conf/httpd.conf |
---|
126 |
echo "Apache config: Saving apache config file $apacheCfg -> $apacheCfg.ORIG" >>$modLog |
---|
127 |
[ ! -e "$apacheCfg.ORIG" ] && cp -p "$apacheCfg" "$apacheCfg.ORIG" >>$modLog 2>&1 |
---|
128 |
|
---|
129 |
grepLine='LoadModule access_module modules/mod_access.so' |
---|
130 |
if grep "$grepLine" "$apacheCfg" &>/dev/null ; then |
---|
131 |
echo "Apache config: add module_access to apache config file: $apacheCfg" >>$modLog |
---|
132 |
echo "$grepLine" > $tmpfile |
---|
133 |
cat "$apacheCfg" >> $tmpfile |
---|
134 |
mv $tmpfile "$apacheCfg" |
---|
135 |
fi |
---|
136 |
|
---|
137 |
grepLine='AllowOverride AuthConfig' |
---|
138 |
if grep "$grepLine" "$apacheCfg" &>/dev/null ; then |
---|
139 |
echo "Apache config: add 'AllowOverride Limit' apache config file: $apacheCfg" >>$modLog |
---|
140 |
sed -i -e "s/$grepLine/ AllowOverride Limit AuthConfig/g" "$apacheCfg" >>$modLog 2>&1 |
---|
141 |
fi |
---|
142 |
|
---|
143 |
## |
---|
144 |
## TODO: Augment PATH variable and add LD_LIBRARY_PATH variable to /etc/profile |
---|
145 |
## |
---|
146 |
profile=/etc/profile |
---|
147 |
tmpfile=/tmp/profile.tmp |
---|
148 |
|
---|
149 |
echo "Backup SHELL profile settings: $profile -> $profile.ORIG" >>$modLog |
---|
150 |
[ ! -e "$profile.ORIG" ] && cp -p "$profile" "$profile.ORIG" >>$modLog 2>&1 |
---|
151 |
|
---|
152 |
grepLine1='true && export PATH=' |
---|
153 |
grepLine2='true && export LD_LIBRARY_PATH=' |
---|
154 |
|
---|
155 |
cat $profile | grep -v "$grepLine1" | grep -v "$grepLine2" > $tmpfile |
---|
156 |
echo 'true && export PATH="/raid/data/module/bin:$PATH"' >> $tmpfile |
---|
157 |
echo 'true && export LD_LIBRARY_PATH="/raid/data/module/lib:/opt/apache/lib:$LD_LIBRARY_PATH"' >> $tmpfile |
---|
158 |
mv $tmpfile $profile |
---|
159 |
|
---|
160 |
## |
---|
161 |
## TODO: This patching code can be removed after Thecus fixed their software |
---|
162 |
## |
---|
163 |
rclocal=/app/cfg/rc.local |
---|
164 |
if grep '/img/bin/rc/rc.module >/dev/null 2>&1 &' $rclocal &>/dev/null ; then |
---|
165 |
echo "Patch $rclocal..." |
---|
166 |
if [ ! -e $rclocal.ORIG ] ; then |
---|
167 |
echo "Backup rc.local: $rclocal -> $rclocal.ORIG" >>$modLog |
---|
168 |
cp $rclocal $rclocal.ORIG >>$modLog 2>&1 |
---|
169 |
fi |
---|
170 |
sed -i -e 's#/img/bin/rc/rc.module >/dev/null 2>&1 &#/img/bin/rc/rc.module start >/dev/null 2>\&1 \&#' \ |
---|
171 |
$rclocal >>$modLog 2>&1 |
---|
172 |
fi |
---|
173 |
|
---|
174 |
echo >>$modLog |
---|
175 |
echo "---- Start module specific log -----------------------------------------------" >>$modLog |
---|
176 |
echo >>$modLog |
---|
177 |
} |
---|
178 |
|
---|
179 |
############################################################################### |
---|
180 |
## |
---|
181 |
## ModuleUpdateStart |
---|
182 |
## |
---|
183 |
############################################################################### |
---|
184 |
ModuleUpdateStart () { |
---|
185 |
ModuleStart update |
---|
186 |
|
---|
187 |
if [ -r "$modDir/VERSION" ] ; then |
---|
188 |
modVersion=$(awk '{print $2}' < "$modDir/VERSION") |
---|
189 |
else |
---|
190 |
modVersion="0.00.00" |
---|
191 |
fi |
---|
192 |
|
---|
193 |
echo >>$modLog |
---|
194 |
echo "---- Start module specific log -----------------------------------------------" >>$modLog |
---|
195 |
echo >>$modLog |
---|
196 |
} |
---|
197 |
|
---|
198 |
############################################################################### |
---|
199 |
## |
---|
200 |
## ModuleUninstallStart |
---|
201 |
## |
---|
202 |
############################################################################### |
---|
203 |
ModuleUninstallStart () { |
---|
204 |
ModuleStart uninstall |
---|
205 |
|
---|
206 |
echo "Stop module: $modRC stop" >>$modLog 2>&1 |
---|
207 |
$modRC stop >>$modLog 2>&1 |
---|
208 |
|
---|
209 |
if [ -r "$modDir/VERSION" ] ; then |
---|
210 |
modVersion=$(awk '{print $2}' < "$modDir/VERSION") |
---|
211 |
else |
---|
212 |
modVersion="0.00.00" |
---|
213 |
fi |
---|
214 |
|
---|
215 |
echo >>$modLog |
---|
216 |
echo "---- Start module specific log -----------------------------------------------" >>$modLog |
---|
217 |
echo >>$modLog |
---|
218 |
} |
---|
219 |
|
---|
220 |
############################################################################### |
---|
221 |
## |
---|
222 |
## ModuleEnd |
---|
223 |
## |
---|
224 |
############################################################################### |
---|
225 |
ModuleEnd () { |
---|
226 |
exitcode=$1 ; mode=$2 |
---|
227 |
|
---|
228 |
if [ "$exitcode" == "0" ] ; then |
---|
229 |
echo "FINISH module $mode: $modName ($(Now))" >>$modLog |
---|
230 |
exitMsg=pass |
---|
231 |
else |
---|
232 |
echo "ERROR($exitcode) module $mode: $modName ($(Now))" >>$modLog |
---|
233 |
exitMsg=fail |
---|
234 |
fi |
---|
235 |
|
---|
236 |
## |
---|
237 |
## Append this log to the log file containing all logs. |
---|
238 |
## |
---|
239 |
modLogAll=${modLog}-all |
---|
240 |
echo >>$modLogAll |
---|
241 |
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" >>$modLogAll |
---|
242 |
echo >>$modLogAll |
---|
243 |
cat $modLog >>$modLogAll |
---|
244 |
|
---|
245 |
## |
---|
246 |
## Mail the result |
---|
247 |
## |
---|
248 |
if [ -f $modLog -a -r $modLog ] ; then |
---|
249 |
case $mode in |
---|
250 |
install) modeNoun=Installation ; modeVerb=installed ;; |
---|
251 |
update) modeNoun=Update ; modeVerb=updated ;; |
---|
252 |
uninstall) modeNoun=Uninstallation ; modeVerb=uninstalled ;; |
---|
253 |
*) modeNoun=Unknown ; modeVerb=unknown ;; |
---|
254 |
esac |
---|
255 |
MailResult "$modName" "$modVersion" "$modLog" "$modeNoun" "$modeVerb" |
---|
256 |
fi |
---|
257 |
|
---|
258 |
echo $exitMsg |
---|
259 |
} |
---|
260 |
|
---|
261 |
############################################################################### |
---|
262 |
## |
---|
263 |
## ModuleInstallEnd |
---|
264 |
## |
---|
265 |
############################################################################### |
---|
266 |
ModuleInstallEnd () { |
---|
267 |
exitcode=$1 |
---|
268 |
|
---|
269 |
echo >>$modLog |
---|
270 |
echo "---- End module specific log -------------------------------------------------" >>$modLog |
---|
271 |
echo >>$modLog |
---|
272 |
|
---|
273 |
## |
---|
274 |
## Set file attributes for standard scripts |
---|
275 |
## |
---|
276 |
for script in configtool ; do |
---|
277 |
echo "Setting file attribs for: $modDir/system/$script" >>$modLog |
---|
278 |
FileAttr root root 755 $modDir/system/$script >>$modLog 2>&1 |
---|
279 |
done |
---|
280 |
|
---|
281 |
## |
---|
282 |
## Restore saved config files if there are any |
---|
283 |
## |
---|
284 |
echo "Restoring saved $modName configuration files (if any)" >>$modLog |
---|
285 |
$modDir/system/configtool restore $modName nowarn >>$modLog 2>&1 |
---|
286 |
|
---|
287 |
ModuleEnd $exitcode install |
---|
288 |
} |
---|
289 |
|
---|
290 |
############################################################################### |
---|
291 |
## |
---|
292 |
## ModuleUpdateEnd |
---|
293 |
## |
---|
294 |
############################################################################### |
---|
295 |
ModuleUpdateEnd () { |
---|
296 |
exitcode=$1 |
---|
297 |
|
---|
298 |
echo >>$modLog |
---|
299 |
echo "---- End module specific log -------------------------------------------------" >>$modLog |
---|
300 |
echo >>$modLog |
---|
301 |
|
---|
302 |
ModuleEnd $exitcode update |
---|
303 |
} |
---|
304 |
|
---|
305 |
############################################################################### |
---|
306 |
## |
---|
307 |
## ModuleUninstallEnd |
---|
308 |
## |
---|
309 |
############################################################################### |
---|
310 |
ModuleUninstallEnd () { |
---|
311 |
exitcode=$1 |
---|
312 |
|
---|
313 |
echo >>$modLog |
---|
314 |
echo "---- End module specific log -------------------------------------------------" >>$modLog |
---|
315 |
echo >>$modLog |
---|
316 |
|
---|
317 |
## |
---|
318 |
## Restore saved config files if there are any |
---|
319 |
## |
---|
320 |
echo "Save $modName configuration files" >>$modLog |
---|
321 |
$modDir/system/configtool backup $modName >>$modLog 2>&1 |
---|
322 |
|
---|
323 |
## |
---|
324 |
## Delete module from config database |
---|
325 |
## |
---|
326 |
echo "Delete module '$modName' from table 'module' in $modDB..." >>$modLog |
---|
327 |
/opt/bin/sqlite $modDB "delete from module where name = '$modName'" >>$modLog 2>&1 |
---|
328 |
|
---|
329 |
echo "Delete module '$modName' from table 'mod' in $modDB..." >>$modLog |
---|
330 |
/opt/bin/sqlite $modDB "delete from mod where module = '$modName'" >>$modLog 2>&1 |
---|
331 |
|
---|
332 |
echo "Remove start/stop script $modRC..." >>$modLog |
---|
333 |
rm -f $modRC >>$modLog 2>&1 |
---|
334 |
|
---|
335 |
echo "Remove module directory $modDir..." >>$modLog |
---|
336 |
rm -rf $modDir >>$modLog 2>&1 |
---|
337 |
|
---|
338 |
ModuleEnd $exitcode uninstall |
---|
339 |
} |
---|
340 |
|
---|
341 |
############################################################################### |
---|
342 |
## |
---|
343 |
## ProcsRunning |
---|
344 |
## |
---|
345 |
############################################################################### |
---|
346 |
ProcsRunning () { |
---|
347 |
searchPattern="$1" |
---|
348 |
|
---|
349 |
procs=$(ps w | grep -v grep | grep -E "$searchPattern" 2>/dev/null | wc -l 2>/dev/null) |
---|
350 |
[ -z $procs ] && procs=0 |
---|
351 |
|
---|
352 |
echo $procs |
---|
353 |
|
---|
354 |
return $(expr $procs == 0) |
---|
355 |
} |
---|
356 |
|
---|
357 |
############################################################################### |
---|
358 |
## |
---|
359 |
## ProcKillall |
---|
360 |
## |
---|
361 |
############################################################################### |
---|
362 |
ProcKillall () { |
---|
363 |
procPattern="$1" ; signal=${2:-9} |
---|
364 |
|
---|
365 |
if [ -z "$procPattern" ] ; then |
---|
366 |
return 0 |
---|
367 |
fi |
---|
368 |
|
---|
369 |
loop=0 |
---|
370 |
while true ; do |
---|
371 |
pidlist=$(ps -w | grep -v grep | grep -E "$procPattern" 2>/dev/null | awk '{print $1}') |
---|
372 |
for pid in $pidlist ; do |
---|
373 |
kill -$signal $pid &>/dev/null |
---|
374 |
done |
---|
375 |
|
---|
376 |
if ! ProcsRunning "$procPattern" &>/dev/null || test $loop -gt 10 ; then |
---|
377 |
break |
---|
378 |
fi |
---|
379 |
sleep 1 |
---|
380 |
let loop++ |
---|
381 |
done |
---|
382 |
|
---|
383 |
if ProcsRunning "$procPattern" &>/dev/null ; then |
---|
384 |
return 1 |
---|
385 |
fi |
---|
386 |
|
---|
387 |
return 0 |
---|
388 |
} |
---|
389 |
|
---|
390 |
############################################################################### |
---|
391 |
## |
---|
392 |
## MailResult |
---|
393 |
## |
---|
394 |
############################################################################### |
---|
395 |
MailResult() { |
---|
396 |
modName=$1 ; modVers=$2 ; instLogFile=$3 ; modeNoun=${4:-Installation} ; modeVerb=${5:-installed} |
---|
397 |
|
---|
398 |
tmpmsgfile=/tmp/tmp.TestMail.$$ |
---|
399 |
|
---|
400 |
cat > $tmpmsgfile <<-EOF |
---|
401 |
From: @mailEnvFrom@ |
---|
402 |
To: @mailEnvTo@ |
---|
403 |
Subject: $modeNoun finished for module $modName-$modVers |
---|
404 |
|
---|
405 |
Hello @mailEnvTo@, |
---|
406 |
|
---|
407 |
The module $modName-$modVers was $modeVerb on your Thecus N5200 device @hostname@. |
---|
408 |
|
---|
409 |
Please check the log output below for errors ($instLogFile): |
---|
410 |
======================================================================= |
---|
411 |
|
---|
412 |
EOF |
---|
413 |
|
---|
414 |
cat $instLogFile >> $tmpmsgfile |
---|
415 |
|
---|
416 |
cat >> $tmpmsgfile <<-EOF |
---|
417 |
|
---|
418 |
======================================================================= |
---|
419 |
|
---|
420 |
|
---|
421 |
----------------------------------------------------------------------- |
---|
422 |
This is a software generated mail message on @hostname@ at @now@. |
---|
423 |
EOF |
---|
424 |
|
---|
425 |
MailSend "" "" $tmpmsgfile |
---|
426 |
rm -f $tmpmsgfile |
---|
427 |
} |
---|
428 |
|
---|
429 |
|
---|
430 |
############################################################################### |
---|
431 |
## |
---|
432 |
## FileAttr |
---|
433 |
## |
---|
434 |
############################################################################### |
---|
435 |
FileAttr () { |
---|
436 |
sOwner=$1 ; sGroup=$2 ; sMode=$3 ; shift ; shift ; shift |
---|
437 |
|
---|
438 |
if [ $# -le 0 ] ; then |
---|
439 |
echo "FileAttr: WARNING: no files given" |
---|
440 |
return 1 |
---|
441 |
fi |
---|
442 |
|
---|
443 |
ret=0 |
---|
444 |
|
---|
445 |
for file in $* ; do |
---|
446 |
chown $sOwner.$sGroup "$file" || ret=1 |
---|
447 |
chmod $sMode "$file" || ret=1 |
---|
448 |
done |
---|
449 |
|
---|
450 |
return $ret |
---|
451 |
} |
---|
452 |
|
---|