Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2003 - (hide annotations) (download)
Mon Aug 13 09:32:23 2012 UTC (11 years, 9 months ago) by niro
Original Path: mcore-src/trunk/mcore-tools/daemon/include/daemon.global.class
File size: 6464 byte(s)
-fixed whitespaces
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 1252 # # import_resource $table $serial $resource $value
24     # import_resource()
25     # {
26     # local table="$1"
27     # local serial="$2"
28     # local resource="$3"
29     # local value="$4"
30     #
31     # if [[ ${DEBUG} = 1 ]]
32     # then
33     # echo "${table}->${resource}=${value}" >> /root/lala.log
34     # echo "mysqldo \"update ${table} set ${resource}='${value}' where serial=${serial};\"" >> /root/lala.log
35     # fi
36     #
37     # mysql_insert "${table}",serial="${serial}","${resource}"="${value}"
38     # }
39 niro 1248
40     # run_class $method $caller $argv1 $argv2 ... $argvN
41     run_class()
42     {
43     local method="$1"
44     local caller="$2"
45     local class
46     local cmd
47     local argv
48    
49 niro 1308 if valid_session
50 niro 1248 then
51     class="${caller%.*}"
52     cmd="${caller#*.}"
53     argv="${@/${caller}/}" # remove caller
54     argv="${argv/${method}/}" # remove method
55    
56     # echo "method=${method}"
57     # echo "caller=${caller}"
58     # echo "class=${class}"
59     # echo "cmd=${cmd}"
60     # echo "argv=${argv}"
61    
62     # check if class.cmd exist
63     if [[ ! -z $(typeset -f "${method}"_"${class}"_"${cmd}") ]]
64     then
65     "${method}"_"${class}"_"${cmd}" ${argv}
66     else
67 niro 1639 eecho "unkown method '${method}' . class '${class}' . cmd '${cmd}'"
68 niro 1248 fi
69     else
70     invalid_session
71     fi
72     }
73    
74     help_topics()
75     {
76     local i
77     local topics
78    
79     topics=$(typeset -f | grep '^help_' | sed 's:help_\(.*\)\ .*():\1:' | sed 's:_:\.:' | sort)
80     mecho "Global commands:"
81 niro 1350 mecho "\timport - import settings to database"
82     mecho "\tget - shows current value for a settings"
83     mecho "\tset - sets value for a setting"
84     mecho "\tauth - authenticate to the daemon"
85     mecho "\tprovide - shows provides of a system"
86     mecho "\trequire - verify plugin requirements"
87 niro 1925 mecho "\treload - reloads all client classes plugins"
88     mecho "\trestart - restarts the daemon"
89 niro 1330 mecho "\tnocolors - disable colors, useful for the webclient"
90 niro 1639 mecho "\tquiet - do not print any unecessary messages"
91 niro 1350 mecho "\thelp - shows help"
92     mecho "\tquit - quits the connection to the server"
93 niro 1248 mecho
94     mecho "Help topics:"
95     for i in ${topics}
96     do
97     # excludes
98     case ${i} in
99     help_topics|topics) continue ;;
100     esac
101    
102     mecho "\t${i}"
103     done
104 niro 1264 mecho
105     mecho "Type 'help [topic]' for more information about every topic."
106 niro 1248 }
107    
108     # on newer xorg-servers root is not allowed to run progs in a user session
109     x11runas()
110     {
111 niro 1666 if [[ -n $(pidof X) ]]
112 niro 1639 then
113     su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@"
114     fi
115 niro 1248 }
116    
117     addconfig()
118     {
119 niro 1308 local opts
120    
121 niro 1248 if [[ -z ${CONFIG} ]]
122     then
123 niro 1639 eecho "You must define \$CONFIG varibale first!"
124 niro 1248 return 1
125     fi
126    
127     if [[ ! -d $(dirname ${CONFIG}) ]]
128     then
129     install -d $(dirname ${CONFIG})
130     fi
131 niro 1308
132     # check for opts
133     case $1 in
134     -n) shift; opts=" -n" ;;
135     -e) shift; opts=" -e" ;;
136     esac
137    
138     echo ${opts} "$@" >> ${CONFIG}
139 niro 1248 }
140    
141     clearconfig()
142     {
143     if [[ -z ${CONFIG} ]]
144     then
145 niro 1639 eecho "You must define \$CONFIG varibale first!"
146 niro 1248 return 1
147     fi
148    
149 niro 1252 if [[ ! -d $(dirname ${CONFIG}) ]]
150     then
151     install -d $(dirname ${CONFIG})
152     fi
153 niro 1248 : > ${CONFIG}
154     }
155    
156 niro 1264 # no_duplicate $list $item
157     no_duplicate()
158     {
159     local i
160     local list="$1"
161     local item="$2"
162    
163     for i in ${list}
164     do
165     [[ ${i} = ${item} ]] && return 1
166     done
167    
168     return 0
169     }
170    
171 niro 1248 require()
172     {
173     local requires="$@"
174     local i
175    
176     for i in ${requires}
177     do
178 niro 1264 # check for duplicate provides
179     if no_duplicate "${PROVIDE}" "${i}"
180     then
181     export REQUIRE="${REQUIRE} ${i}"
182     else
183 niro 1639 decho "duplicate provide '${i}' detected!"
184 niro 1264 fi
185 niro 1248 done
186     }
187    
188 niro 1264 verify_requirements()
189 niro 1248 {
190 niro 1264 local req
191     local prov
192     local missing
193     local sorted
194 niro 1248
195 niro 1264 for req in ${REQUIRE}
196 niro 1248 do
197 niro 1264 # scan PROVIDE for dupes
198     # if a dupe is found, then requirement is fullfilled
199     # else add to missing
200     if no_duplicate "${PROVIDE}" "${req}"
201     then
202     missing="${missing} ${req}"
203     fi
204 niro 1248 done
205    
206 niro 1264 # sort them alpabetically
207     sorted=$(for i in ${REQUIRE}; do echo "${i}"; done | sort)
208    
209     # show missing and set the right retval
210     if [[ -z ${missing} ]]
211     then
212 niro 1639 rvecho "${sorted}"
213 niro 1264 return 0
214     else
215     for req in ${sorted}
216     do
217     if no_duplicate "${missing}" "$req"
218     then
219     # print normal
220 niro 1639 rvecho -n " ${req}"
221 niro 1264 else
222     # print missing
223     eecho -n " ${req}"
224     fi
225     done
226     return 1
227     fi
228 niro 1248 }
229    
230     provide()
231     {
232     local provides="$@"
233     local i
234    
235     for i in ${provides}
236     do
237     # check for duplicate provides
238 niro 1264 if no_duplicate "${PROVIDE}" "${i}"
239 niro 1248 then
240     export PROVIDE="${PROVIDE} ${i}"
241     else
242 niro 1639 decho "duplicate provide '${i}' detected!"
243 niro 1248 fi
244     done
245     }
246    
247     print_provide()
248     {
249     local sorted
250    
251     # sort them alpabetically
252     sorted=$(for i in ${PROVIDE}; do echo "${i}"; done | sort)
253     # do not escape, or CRLFS get printed to screen too
254 niro 2003 rvecho ${sorted}
255 niro 1248 }
256    
257 niro 1639 # message only echo | disabled in quiet mode
258 niro 1248 mecho()
259     {
260     local COLCYAN="\033[1;36m"
261     local COLDEFAULT="\033[0m"
262 niro 1264 local opts
263 niro 1308 local webcrlf
264 niro 1264
265 niro 1639 # print nothing if quiet mode was requested
266     [[ ${QUIET} = true ]] && return
267    
268 niro 1248 if [[ ${NOCOLORS} = true ]]
269     then
270     COLCYAN=""
271     COLDEFAULT=""
272     fi
273    
274 niro 1308 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
275    
276 niro 1264 # respect -n
277     case $1 in
278     -n) shift; opts="n" ;;
279     esac
280    
281 niro 1308 echo -e${opts} "${COLCYAN}$@${COLDEFAULT}${webcrlf}"
282 niro 1248 }
283 niro 1264
284 niro 1639 # prints error messages | enabled even in quiet mode
285 niro 1264 eecho()
286     {
287     local COLRED="\033[1;31m"
288     local COLDEFAULT="\033[0m"
289     local opts
290 niro 1308 local webcrlf
291 niro 1264
292     if [[ ${NOCOLORS} = true ]]
293     then
294     COLRED=""
295     COLDEFAULT=""
296     fi
297    
298 niro 1308 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
299    
300 niro 1264 # respect -n
301     case $1 in
302     -n) shift; opts="n" ;;
303     esac
304    
305 niro 1308 echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}"
306 niro 1264 }
307    
308 niro 1639 # prints return values of get | enabled even in quiet mode
309     rvecho()
310     {
311     local COLPURPLE="\033[1;35m"
312     local COLDEFAULT="\033[0m"
313     local opts
314     local webcrlf
315    
316     if [[ ${NOCOLORS} = true ]]
317     then
318     COLPURPLE=""
319     COLDEFAULT=""
320     fi
321    
322     [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
323    
324     # respect -n
325     case $1 in
326     -n) shift; opts="n" ;;
327     esac
328    
329     echo -e${opts} "${COLPURPLE}$@${COLDEFAULT}${webcrlf}"
330     }
331    
332     # prints debug messages if requested | enabled even in quiet mode
333     decho()
334     {
335     # print nothing if debug mode was *not* requested
336     [[ ${DEBUG} != 1 ]] && return
337    
338     eecho "DEBUG: ${@}"
339     }
340    
341 niro 1264 path_not_empty()
342     {
343     local path="$1"
344 niro 1639 [[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1
345 niro 1264
346     # return ERR if path does not exist
347     [[ ! -d ${path} ]] && return 1
348     # return ERR if path empty
349     [[ -z $(find "${path}" -mindepth 1 -maxdepth 1) ]] && return 1
350    
351     # every thing went ok, directory not empty
352     return 0
353     }