Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2030 - (hide annotations) (download)
Mon Aug 13 11:42:52 2012 UTC (11 years, 9 months ago) by niro
File size: 7863 byte(s)
-use find to fix some annoying error messages and fixed variable declaration
1 niro 1248 # $Id$
2    
3 niro 1915 # loads client classes from $MCLIBDIR
4     load_client_classes()
5     {
6 niro 2030 local i
7    
8 niro 1915 # client specific
9 niro 2030 for i in $(find ${MCLIBDIR}/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     mecho "\tquit - quits the connection to the server"
109 niro 1248 mecho
110     mecho "Help topics:"
111     for i in ${topics}
112     do
113     # excludes
114     case ${i} in
115     help_topics|topics) continue ;;
116     esac
117    
118     mecho "\t${i}"
119     done
120 niro 1264 mecho
121     mecho "Type 'help [topic]' for more information about every topic."
122 niro 1248 }
123    
124     # on newer xorg-servers root is not allowed to run progs in a user session
125     x11runas()
126     {
127 niro 1666 if [[ -n $(pidof X) ]]
128 niro 1639 then
129     su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@"
130     fi
131 niro 1248 }
132    
133     addconfig()
134     {
135 niro 1308 local opts
136    
137 niro 1248 if [[ -z ${CONFIG} ]]
138     then
139 niro 1639 eecho "You must define \$CONFIG varibale first!"
140 niro 1248 return 1
141     fi
142    
143     if [[ ! -d $(dirname ${CONFIG}) ]]
144     then
145     install -d $(dirname ${CONFIG})
146     fi
147 niro 1308
148     # check for opts
149     case $1 in
150     -n) shift; opts=" -n" ;;
151     -e) shift; opts=" -e" ;;
152     esac
153    
154     echo ${opts} "$@" >> ${CONFIG}
155 niro 1248 }
156    
157     clearconfig()
158     {
159     if [[ -z ${CONFIG} ]]
160     then
161 niro 1639 eecho "You must define \$CONFIG varibale first!"
162 niro 1248 return 1
163     fi
164    
165 niro 1252 if [[ ! -d $(dirname ${CONFIG}) ]]
166     then
167     install -d $(dirname ${CONFIG})
168     fi
169 niro 1248 : > ${CONFIG}
170     }
171    
172 niro 1264 # no_duplicate $list $item
173     no_duplicate()
174     {
175     local i
176     local list="$1"
177     local item="$2"
178    
179     for i in ${list}
180     do
181     [[ ${i} = ${item} ]] && return 1
182     done
183    
184     return 0
185     }
186    
187 niro 1248 require()
188     {
189     local requires="$@"
190     local i
191    
192     for i in ${requires}
193     do
194 niro 1264 # check for duplicate provides
195     if no_duplicate "${PROVIDE}" "${i}"
196     then
197     export REQUIRE="${REQUIRE} ${i}"
198     else
199 niro 1639 decho "duplicate provide '${i}' detected!"
200 niro 1264 fi
201 niro 1248 done
202     }
203    
204 niro 1264 verify_requirements()
205 niro 1248 {
206 niro 1264 local req
207     local prov
208     local missing
209     local sorted
210 niro 1248
211 niro 1264 for req in ${REQUIRE}
212 niro 1248 do
213 niro 1264 # scan PROVIDE for dupes
214     # if a dupe is found, then requirement is fullfilled
215     # else add to missing
216     if no_duplicate "${PROVIDE}" "${req}"
217     then
218     missing="${missing} ${req}"
219     fi
220 niro 1248 done
221    
222 niro 1264 # sort them alpabetically
223     sorted=$(for i in ${REQUIRE}; do echo "${i}"; done | sort)
224    
225     # show missing and set the right retval
226     if [[ -z ${missing} ]]
227     then
228 niro 1639 rvecho "${sorted}"
229 niro 1264 return 0
230     else
231     for req in ${sorted}
232     do
233     if no_duplicate "${missing}" "$req"
234     then
235     # print normal
236 niro 1639 rvecho -n " ${req}"
237 niro 1264 else
238     # print missing
239     eecho -n " ${req}"
240     fi
241     done
242     return 1
243     fi
244 niro 1248 }
245    
246     provide()
247     {
248     local provides="$@"
249     local i
250    
251     for i in ${provides}
252     do
253     # check for duplicate provides
254 niro 1264 if no_duplicate "${PROVIDE}" "${i}"
255 niro 1248 then
256     export PROVIDE="${PROVIDE} ${i}"
257     else
258 niro 1639 decho "duplicate provide '${i}' detected!"
259 niro 1248 fi
260     done
261     }
262    
263     print_provide()
264     {
265     local sorted
266    
267     # sort them alpabetically
268     sorted=$(for i in ${PROVIDE}; do echo "${i}"; done | sort)
269     # do not escape, or CRLFS get printed to screen too
270 niro 2003 rvecho ${sorted}
271 niro 1248 }
272    
273 niro 1639 # message only echo | disabled in quiet mode
274 niro 1248 mecho()
275     {
276     local COLCYAN="\033[1;36m"
277     local COLDEFAULT="\033[0m"
278 niro 1264 local opts
279 niro 1308 local webcrlf
280 niro 1264
281 niro 1639 # print nothing if quiet mode was requested
282     [[ ${QUIET} = true ]] && return
283    
284 niro 1248 if [[ ${NOCOLORS} = true ]]
285     then
286     COLCYAN=""
287     COLDEFAULT=""
288     fi
289    
290 niro 1308 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
291    
292 niro 1264 # respect -n
293     case $1 in
294     -n) shift; opts="n" ;;
295     esac
296    
297 niro 1308 echo -e${opts} "${COLCYAN}$@${COLDEFAULT}${webcrlf}"
298 niro 1248 }
299 niro 1264
300 niro 1639 # prints error messages | enabled even in quiet mode
301 niro 1264 eecho()
302     {
303     local COLRED="\033[1;31m"
304     local COLDEFAULT="\033[0m"
305     local opts
306 niro 1308 local webcrlf
307 niro 1264
308     if [[ ${NOCOLORS} = true ]]
309     then
310     COLRED=""
311     COLDEFAULT=""
312     fi
313    
314 niro 1308 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
315    
316 niro 1264 # respect -n
317     case $1 in
318     -n) shift; opts="n" ;;
319     esac
320    
321 niro 1308 echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}"
322 niro 1264 }
323    
324 niro 1639 # prints return values of get | enabled even in quiet mode
325     rvecho()
326     {
327     local COLPURPLE="\033[1;35m"
328     local COLDEFAULT="\033[0m"
329     local opts
330     local webcrlf
331    
332     if [[ ${NOCOLORS} = true ]]
333     then
334     COLPURPLE=""
335     COLDEFAULT=""
336     fi
337    
338     [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
339    
340     # respect -n
341     case $1 in
342     -n) shift; opts="n" ;;
343     esac
344    
345     echo -e${opts} "${COLPURPLE}$@${COLDEFAULT}${webcrlf}"
346     }
347    
348     # prints debug messages if requested | enabled even in quiet mode
349     decho()
350     {
351     # print nothing if debug mode was *not* requested
352     [[ ${DEBUG} != 1 ]] && return
353    
354     eecho "DEBUG: ${@}"
355     }
356    
357 niro 1264 path_not_empty()
358     {
359     local path="$1"
360 niro 1639 [[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1
361 niro 1264
362     # return ERR if path does not exist
363     [[ ! -d ${path} ]] && return 1
364     # return ERR if path empty
365     [[ -z $(find "${path}" -mindepth 1 -maxdepth 1) ]] && return 1
366    
367     # every thing went ok, directory not empty
368     return 0
369     }
370 niro 2007
371     help_daemon_mroot()
372     {
373     mecho "get daemon.mroot"
374     mecho " Prints current MROOT variable."
375     mecho
376     mecho "set daemon.mroot [path]"
377     mecho " set MROOT variable to given path."
378     }
379    
380     get_daemon_mroot()
381     {
382     rvecho "${MROOT}"
383     }
384    
385     set_daemon_mroot()
386     {
387     local path=$1
388    
389     if [[ -d ${path} ]]
390     then
391     export MROOT="${path}"
392     decho "MROOT='${MROOT}' is set."
393     else
394     eecho "Path '${path}' does not exist. MROOT not set."
395     fi
396     }
397 niro 2008
398     list_files_in_directory()
399     {
400     local i
401     local retval
402     local path
403     local opts
404     local type
405    
406     # basic getops
407     for i in $*
408     do
409     case $1 in
410     -mindepth) shift; opts+=" -mindepth $1" ;;
411     -maxdepth) shift; opts+=" -maxdepth $1" ;;
412     -type) shift; type="$1" ;;
413     '') continue ;;
414     *) path="$1" ;;
415     esac
416     shift
417     done
418    
419     if [[ -z ${path} ]]
420     then
421     eecho "No path given."
422     return 1
423     fi
424    
425     if [[ ! -d ${path} ]]
426     then
427     eecho "Directory '${path}' does not exist."
428     return 1
429     fi
430    
431     # default to files
432     [[ -z ${type} ]] && type=f
433    
434     for i in $(find ${path} ${opts} -type ${type} | sort)
435     do
436     if [[ -z ${retval} ]]
437     then
438     retval="$(basename ${i})"
439     else
440     retval="${retval} $(basename ${i})"
441     fi
442     done
443    
444     rvecho "${retval}"
445     }