Magellan Linux

Annotation of /mcore-src/trunk/mcore-tools/src/include/daemon.global.class.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2139 - (hide annotations) (download)
Fri Jan 10 10:23:50 2014 UTC (10 years, 4 months ago) by niro
Original Path: mcore-src/trunk/mcore-tools/daemon/include/daemon.global.class
File size: 8352 byte(s)
-renamed variable MCLIBDIR -> MCORE_LIBDIR
1 niro 1248 # $Id$
2    
3 niro 2139 # loads client classes from $MCORE_LIBDIR
4 niro 1915 load_client_classes()
5     {
6 niro 2030 local i
7    
8 niro 1915 # client specific
9 niro 2139 for i in $(find ${MCORE_LIBDIR}/include -type f -name \*.client.class)
10 niro 1915 do
11     source ${i} || eecho "error loading ${i}"
12     done
13     }
14    
15     # restarts the whole service via remote cmd
16     restart_service()
17     {
18     local pid
19     for pid in $(pidof sslsvd)
20     do
21     kill -SIGHUP ${pid}
22     done
23     }
24    
25 niro 2005 # stops the whole service via remote cmd
26     stop_service()
27     {
28     local pid
29     for pid in $(pidof sslsvd)
30     do
31     kill -15 ${pid}
32     sleep 1
33     kill -9 ${pid}
34     done
35     }
36    
37 niro 1252 # # import_resource $table $serial $resource $value
38     # import_resource()
39     # {
40     # local table="$1"
41     # local serial="$2"
42     # local resource="$3"
43     # local value="$4"
44     #
45     # if [[ ${DEBUG} = 1 ]]
46     # then
47     # echo "${table}->${resource}=${value}" >> /root/lala.log
48     # echo "mysqldo \"update ${table} set ${resource}='${value}' where serial=${serial};\"" >> /root/lala.log
49     # fi
50     #
51     # mysql_insert "${table}",serial="${serial}","${resource}"="${value}"
52     # }
53 niro 1248
54     # run_class $method $caller $argv1 $argv2 ... $argvN
55     run_class()
56     {
57     local method="$1"
58     local caller="$2"
59     local class
60     local cmd
61     local argv
62    
63 niro 1308 if valid_session
64 niro 1248 then
65     class="${caller%.*}"
66     cmd="${caller#*.}"
67     argv="${@/${caller}/}" # remove caller
68     argv="${argv/${method}/}" # remove method
69    
70     # echo "method=${method}"
71     # echo "caller=${caller}"
72     # echo "class=${class}"
73     # echo "cmd=${cmd}"
74     # echo "argv=${argv}"
75    
76     # check if class.cmd exist
77     if [[ ! -z $(typeset -f "${method}"_"${class}"_"${cmd}") ]]
78     then
79     "${method}"_"${class}"_"${cmd}" ${argv}
80     else
81 niro 1639 eecho "unkown method '${method}' . class '${class}' . cmd '${cmd}'"
82 niro 1248 fi
83     else
84     invalid_session
85     fi
86     }
87    
88     help_topics()
89     {
90     local i
91     local topics
92    
93     topics=$(typeset -f | grep '^help_' | sed 's:help_\(.*\)\ .*():\1:' | sed 's:_:\.:' | sort)
94     mecho "Global commands:"
95 niro 1350 mecho "\timport - import settings to database"
96     mecho "\tget - shows current value for a settings"
97     mecho "\tset - sets value for a setting"
98     mecho "\tauth - authenticate to the daemon"
99     mecho "\tprovide - shows provides of a system"
100     mecho "\trequire - verify plugin requirements"
101 niro 1925 mecho "\treload - reloads all client classes plugins"
102     mecho "\trestart - restarts the daemon"
103 niro 2005 mecho "\tstop - stops the daemon"
104 niro 1330 mecho "\tnocolors - disable colors, useful for the webclient"
105 niro 2006 mecho "\tcolors - enable colors"
106 niro 1639 mecho "\tquiet - do not print any unecessary messages"
107 niro 1350 mecho "\thelp - shows help"
108 niro 2044 mecho "\tversion - prints version of the daemon"
109 niro 1350 mecho "\tquit - quits the connection to the server"
110 niro 1248 mecho
111     mecho "Help topics:"
112     for i in ${topics}
113     do
114     # excludes
115     case ${i} in
116     help_topics|topics) continue ;;
117     esac
118    
119     mecho "\t${i}"
120     done
121 niro 1264 mecho
122     mecho "Type 'help [topic]' for more information about every topic."
123 niro 1248 }
124    
125     # on newer xorg-servers root is not allowed to run progs in a user session
126     x11runas()
127     {
128 niro 1666 if [[ -n $(pidof X) ]]
129 niro 1639 then
130     su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@"
131     fi
132 niro 1248 }
133    
134     addconfig()
135     {
136 niro 1308 local opts
137    
138 niro 1248 if [[ -z ${CONFIG} ]]
139     then
140 niro 1639 eecho "You must define \$CONFIG varibale first!"
141 niro 1248 return 1
142     fi
143    
144     if [[ ! -d $(dirname ${CONFIG}) ]]
145     then
146     install -d $(dirname ${CONFIG})
147     fi
148 niro 1308
149     # check for opts
150     case $1 in
151     -n) shift; opts=" -n" ;;
152     -e) shift; opts=" -e" ;;
153     esac
154    
155     echo ${opts} "$@" >> ${CONFIG}
156 niro 1248 }
157    
158     clearconfig()
159     {
160     if [[ -z ${CONFIG} ]]
161     then
162 niro 1639 eecho "You must define \$CONFIG varibale first!"
163 niro 1248 return 1
164     fi
165    
166 niro 1252 if [[ ! -d $(dirname ${CONFIG}) ]]
167     then
168     install -d $(dirname ${CONFIG})
169     fi
170 niro 1248 : > ${CONFIG}
171     }
172    
173 niro 1264 # no_duplicate $list $item
174     no_duplicate()
175     {
176     local i
177     local list="$1"
178     local item="$2"
179    
180     for i in ${list}
181     do
182     [[ ${i} = ${item} ]] && return 1
183     done
184    
185     return 0
186     }
187    
188 niro 1248 require()
189     {
190     local requires="$@"
191     local i
192    
193     for i in ${requires}
194     do
195 niro 1264 # check for duplicate provides
196     if no_duplicate "${PROVIDE}" "${i}"
197     then
198     export REQUIRE="${REQUIRE} ${i}"
199     else
200 niro 1639 decho "duplicate provide '${i}' detected!"
201 niro 1264 fi
202 niro 1248 done
203     }
204    
205 niro 1264 verify_requirements()
206 niro 1248 {
207 niro 1264 local req
208     local prov
209     local missing
210     local sorted
211 niro 1248
212 niro 1264 for req in ${REQUIRE}
213 niro 1248 do
214 niro 1264 # scan PROVIDE for dupes
215     # if a dupe is found, then requirement is fullfilled
216     # else add to missing
217     if no_duplicate "${PROVIDE}" "${req}"
218     then
219     missing="${missing} ${req}"
220     fi
221 niro 1248 done
222    
223 niro 1264 # sort them alpabetically
224     sorted=$(for i in ${REQUIRE}; do echo "${i}"; done | sort)
225    
226     # show missing and set the right retval
227     if [[ -z ${missing} ]]
228     then
229 niro 2115 # do not escape, or CRLFS get printed to screen too
230     rvecho ${sorted}
231 niro 1264 return 0
232     else
233     for req in ${sorted}
234     do
235     if no_duplicate "${missing}" "$req"
236     then
237     # print normal
238 niro 2115 rvecho -n "${req} "
239 niro 1264 else
240     # print missing
241 niro 2115 eecho -n "${req} "
242 niro 1264 fi
243     done
244 niro 2115 # print CRLF
245     echo
246 niro 1264 return 1
247     fi
248 niro 1248 }
249    
250     provide()
251     {
252     local provides="$@"
253     local i
254    
255     for i in ${provides}
256     do
257     # check for duplicate provides
258 niro 1264 if no_duplicate "${PROVIDE}" "${i}"
259 niro 1248 then
260     export PROVIDE="${PROVIDE} ${i}"
261     else
262 niro 1639 decho "duplicate provide '${i}' detected!"
263 niro 1248 fi
264     done
265     }
266    
267     print_provide()
268     {
269     local sorted
270    
271     # sort them alpabetically
272     sorted=$(for i in ${PROVIDE}; do echo "${i}"; done | sort)
273     # do not escape, or CRLFS get printed to screen too
274 niro 2003 rvecho ${sorted}
275 niro 1248 }
276    
277 niro 1639 # message only echo | disabled in quiet mode
278 niro 1248 mecho()
279     {
280     local COLCYAN="\033[1;36m"
281     local COLDEFAULT="\033[0m"
282 niro 1264 local opts
283 niro 1308 local webcrlf
284 niro 1264
285 niro 1639 # print nothing if quiet mode was requested
286     [[ ${QUIET} = true ]] && return
287    
288 niro 1248 if [[ ${NOCOLORS} = true ]]
289     then
290     COLCYAN=""
291     COLDEFAULT=""
292     fi
293    
294 niro 1308 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
295    
296 niro 1264 # respect -n
297     case $1 in
298     -n) shift; opts="n" ;;
299     esac
300    
301 niro 1308 echo -e${opts} "${COLCYAN}$@${COLDEFAULT}${webcrlf}"
302 niro 1248 }
303 niro 1264
304 niro 1639 # prints error messages | enabled even in quiet mode
305 niro 1264 eecho()
306     {
307     local COLRED="\033[1;31m"
308     local COLDEFAULT="\033[0m"
309     local opts
310 niro 1308 local webcrlf
311 niro 1264
312     if [[ ${NOCOLORS} = true ]]
313     then
314     COLRED=""
315     COLDEFAULT=""
316     fi
317    
318 niro 1308 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
319    
320 niro 1264 # respect -n
321     case $1 in
322     -n) shift; opts="n" ;;
323     esac
324    
325 niro 1308 echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}"
326 niro 1264 }
327    
328 niro 1639 # prints return values of get | enabled even in quiet mode
329     rvecho()
330     {
331     local COLPURPLE="\033[1;35m"
332     local COLDEFAULT="\033[0m"
333     local opts
334     local webcrlf
335    
336     if [[ ${NOCOLORS} = true ]]
337     then
338     COLPURPLE=""
339     COLDEFAULT=""
340     fi
341    
342     [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
343    
344     # respect -n
345     case $1 in
346     -n) shift; opts="n" ;;
347     esac
348    
349     echo -e${opts} "${COLPURPLE}$@${COLDEFAULT}${webcrlf}"
350     }
351    
352     # prints debug messages if requested | enabled even in quiet mode
353     decho()
354     {
355     # print nothing if debug mode was *not* requested
356     [[ ${DEBUG} != 1 ]] && return
357    
358     eecho "DEBUG: ${@}"
359     }
360    
361 niro 1264 path_not_empty()
362     {
363     local path="$1"
364 niro 1639 [[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1
365 niro 1264
366     # return ERR if path does not exist
367     [[ ! -d ${path} ]] && return 1
368     # return ERR if path empty
369     [[ -z $(find "${path}" -mindepth 1 -maxdepth 1) ]] && return 1
370    
371     # every thing went ok, directory not empty
372     return 0
373     }
374 niro 2007
375     help_daemon_mroot()
376     {
377     mecho "get daemon.mroot"
378     mecho " Prints current MROOT variable."
379     mecho
380     mecho "set daemon.mroot [path]"
381     mecho " set MROOT variable to given path."
382     }
383    
384     get_daemon_mroot()
385     {
386     rvecho "${MROOT}"
387     }
388    
389     set_daemon_mroot()
390     {
391     local path=$1
392    
393     if [[ -d ${path} ]]
394     then
395     export MROOT="${path}"
396     decho "MROOT='${MROOT}' is set."
397     else
398     eecho "Path '${path}' does not exist. MROOT not set."
399     fi
400     }
401 niro 2008
402     list_files_in_directory()
403     {
404     local i
405     local retval
406     local path
407     local opts
408     local type
409    
410     # basic getops
411     for i in $*
412     do
413     case $1 in
414     -mindepth) shift; opts+=" -mindepth $1" ;;
415     -maxdepth) shift; opts+=" -maxdepth $1" ;;
416     -type) shift; type="$1" ;;
417 niro 2091 -name) shift; opts+=" -name $1" ;;
418 niro 2008 '') continue ;;
419     *) path="$1" ;;
420     esac
421     shift
422     done
423    
424     if [[ -z ${path} ]]
425     then
426     eecho "No path given."
427     return 1
428     fi
429    
430     if [[ ! -d ${path} ]]
431     then
432     eecho "Directory '${path}' does not exist."
433     return 1
434     fi
435    
436     # default to files
437     [[ -z ${type} ]] && type=f
438    
439 niro 2091 for i in $(find ${path} ${opts} -type ${type} -printf '%f\n' | sort)
440 niro 2008 do
441     if [[ -z ${retval} ]]
442     then
443 niro 2077 retval="${i}"
444 niro 2008 else
445 niro 2077 retval+=" ${i}"
446 niro 2008 fi
447     done
448    
449     rvecho "${retval}"
450     }
451 niro 2052
452     print_version()
453     {
454 niro 2139 echo "mcored-$(<${MCORE_LIBDIR}/VERSION)"
455 niro 2052 }
456 niro 2083
457     system_chroot()
458     {
459     local cmd="$@"
460     if [[ -z ${MROOT} ]]
461     then
462     echo "system_chroot(): \$MROOT was not set, doing nothing!"
463     return 1
464     fi
465     if [ ! -d ${MROOT} ]
466     then
467     eecho "system_chroot(): MROOT='${MROOT}' does not exist."
468     return 1
469     fi
470    
471     chroot ${MROOT} ${cmd}
472     }
473