Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1915 - (hide annotations) (download)
Wed Nov 9 13:24:00 2011 UTC (12 years, 6 months ago) by niro
Original Path: mcore-src/trunk/mcore-tools/daemon/include/daemon.global.class
File size: 6366 byte(s)
-added functions to load client classes and to restart the service
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 1330 mecho "\tnocolors - disable colors, useful for the webclient"
88 niro 1639 mecho "\tquiet - do not print any unecessary messages"
89 niro 1350 mecho "\thelp - shows help"
90     mecho "\tquit - quits the connection to the server"
91 niro 1248 mecho
92     mecho "Help topics:"
93     for i in ${topics}
94     do
95     # excludes
96     case ${i} in
97     help_topics|topics) continue ;;
98     esac
99    
100     mecho "\t${i}"
101     done
102 niro 1264 mecho
103     mecho "Type 'help [topic]' for more information about every topic."
104 niro 1248 }
105    
106     # on newer xorg-servers root is not allowed to run progs in a user session
107     x11runas()
108     {
109 niro 1666 if [[ -n $(pidof X) ]]
110 niro 1639 then
111     su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@"
112     fi
113 niro 1248 }
114    
115     addconfig()
116     {
117 niro 1308 local opts
118    
119 niro 1248 if [[ -z ${CONFIG} ]]
120     then
121 niro 1639 eecho "You must define \$CONFIG varibale first!"
122 niro 1248 return 1
123     fi
124    
125     if [[ ! -d $(dirname ${CONFIG}) ]]
126     then
127     install -d $(dirname ${CONFIG})
128     fi
129 niro 1308
130     # check for opts
131     case $1 in
132     -n) shift; opts=" -n" ;;
133     -e) shift; opts=" -e" ;;
134     esac
135    
136     echo ${opts} "$@" >> ${CONFIG}
137 niro 1248 }
138    
139     clearconfig()
140     {
141     if [[ -z ${CONFIG} ]]
142     then
143 niro 1639 eecho "You must define \$CONFIG varibale first!"
144 niro 1248 return 1
145     fi
146    
147 niro 1252 if [[ ! -d $(dirname ${CONFIG}) ]]
148     then
149     install -d $(dirname ${CONFIG})
150     fi
151 niro 1248 : > ${CONFIG}
152     }
153    
154 niro 1264 # no_duplicate $list $item
155     no_duplicate()
156     {
157     local i
158     local list="$1"
159     local item="$2"
160    
161     for i in ${list}
162     do
163     [[ ${i} = ${item} ]] && return 1
164     done
165    
166     return 0
167     }
168    
169 niro 1248 require()
170     {
171     local requires="$@"
172     local i
173    
174     for i in ${requires}
175     do
176 niro 1264 # check for duplicate provides
177     if no_duplicate "${PROVIDE}" "${i}"
178     then
179     export REQUIRE="${REQUIRE} ${i}"
180     else
181 niro 1639 decho "duplicate provide '${i}' detected!"
182 niro 1264 fi
183 niro 1248 done
184     }
185    
186 niro 1264 verify_requirements()
187 niro 1248 {
188 niro 1264 local req
189     local prov
190     local missing
191     local sorted
192 niro 1248
193 niro 1264 for req in ${REQUIRE}
194 niro 1248 do
195 niro 1264 # scan PROVIDE for dupes
196     # if a dupe is found, then requirement is fullfilled
197     # else add to missing
198     if no_duplicate "${PROVIDE}" "${req}"
199     then
200     missing="${missing} ${req}"
201     fi
202 niro 1248 done
203    
204 niro 1264 # sort them alpabetically
205     sorted=$(for i in ${REQUIRE}; do echo "${i}"; done | sort)
206    
207     # show missing and set the right retval
208     if [[ -z ${missing} ]]
209     then
210 niro 1639 rvecho "${sorted}"
211 niro 1264 return 0
212     else
213     for req in ${sorted}
214     do
215     if no_duplicate "${missing}" "$req"
216     then
217     # print normal
218 niro 1639 rvecho -n " ${req}"
219 niro 1264 else
220     # print missing
221     eecho -n " ${req}"
222     fi
223     done
224     return 1
225     fi
226 niro 1248 }
227    
228     provide()
229     {
230     local provides="$@"
231     local i
232    
233     for i in ${provides}
234     do
235     # check for duplicate provides
236 niro 1264 if no_duplicate "${PROVIDE}" "${i}"
237 niro 1248 then
238     export PROVIDE="${PROVIDE} ${i}"
239     else
240 niro 1639 decho "duplicate provide '${i}' detected!"
241 niro 1248 fi
242     done
243     }
244    
245     print_provide()
246     {
247     local sorted
248    
249     # sort them alpabetically
250     sorted=$(for i in ${PROVIDE}; do echo "${i}"; done | sort)
251     # do not escape, or CRLFS get printed to screen too
252 niro 1639 rvecho ${sorted}
253 niro 1248 }
254    
255 niro 1639 # message only echo | disabled in quiet mode
256 niro 1248 mecho()
257     {
258     local COLCYAN="\033[1;36m"
259     local COLDEFAULT="\033[0m"
260 niro 1264 local opts
261 niro 1308 local webcrlf
262 niro 1264
263 niro 1639 # print nothing if quiet mode was requested
264     [[ ${QUIET} = true ]] && return
265    
266 niro 1248 if [[ ${NOCOLORS} = true ]]
267     then
268     COLCYAN=""
269     COLDEFAULT=""
270     fi
271    
272 niro 1308 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
273    
274 niro 1264 # respect -n
275     case $1 in
276     -n) shift; opts="n" ;;
277     esac
278    
279 niro 1308 echo -e${opts} "${COLCYAN}$@${COLDEFAULT}${webcrlf}"
280 niro 1248 }
281 niro 1264
282 niro 1639 # prints error messages | enabled even in quiet mode
283 niro 1264 eecho()
284     {
285     local COLRED="\033[1;31m"
286     local COLDEFAULT="\033[0m"
287     local opts
288 niro 1308 local webcrlf
289 niro 1264
290     if [[ ${NOCOLORS} = true ]]
291     then
292     COLRED=""
293     COLDEFAULT=""
294     fi
295    
296 niro 1308 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
297    
298 niro 1264 # respect -n
299     case $1 in
300     -n) shift; opts="n" ;;
301     esac
302    
303 niro 1308 echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}"
304 niro 1264 }
305    
306 niro 1639 # prints return values of get | enabled even in quiet mode
307     rvecho()
308     {
309     local COLPURPLE="\033[1;35m"
310     local COLDEFAULT="\033[0m"
311     local opts
312     local webcrlf
313    
314     if [[ ${NOCOLORS} = true ]]
315     then
316     COLPURPLE=""
317     COLDEFAULT=""
318     fi
319    
320     [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
321    
322     # respect -n
323     case $1 in
324     -n) shift; opts="n" ;;
325     esac
326    
327     echo -e${opts} "${COLPURPLE}$@${COLDEFAULT}${webcrlf}"
328     }
329    
330     # prints debug messages if requested | enabled even in quiet mode
331     decho()
332     {
333     # print nothing if debug mode was *not* requested
334     [[ ${DEBUG} != 1 ]] && return
335    
336     eecho "DEBUG: ${@}"
337     }
338    
339 niro 1264 path_not_empty()
340     {
341     local path="$1"
342 niro 1639 [[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1
343 niro 1264
344     # return ERR if path does not exist
345     [[ ! -d ${path} ]] && return 1
346     # return ERR if path empty
347     [[ -z $(find "${path}" -mindepth 1 -maxdepth 1) ]] && return 1
348    
349     # every thing went ok, directory not empty
350     return 0
351     }