Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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