Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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