Contents of /mcore-src/trunk/mcore-tools/src/include/daemon.global.class.in
Parent Directory | Revision Log
Revision 1915 -
(show annotations)
(download)
Wed Nov 9 13:24:00 2011 UTC (12 years, 7 months ago) by niro
Original Path: mcore-src/trunk/mcore-tools/daemon/include/daemon.global.class
File size: 6366 byte(s)
Wed Nov 9 13:24:00 2011 UTC (12 years, 7 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 | # $Id$ |
2 | |
3 | # 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 | # # 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 | |
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 | if valid_session |
50 | 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 | eecho "unkown method '${method}' . class '${class}' . cmd '${cmd}'" |
68 | 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 | 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 | mecho "\tnocolors - disable colors, useful for the webclient" |
88 | mecho "\tquiet - do not print any unecessary messages" |
89 | mecho "\thelp - shows help" |
90 | mecho "\tquit - quits the connection to the server" |
91 | 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 | mecho |
103 | mecho "Type 'help [topic]' for more information about every topic." |
104 | } |
105 | |
106 | # on newer xorg-servers root is not allowed to run progs in a user session |
107 | x11runas() |
108 | { |
109 | if [[ -n $(pidof X) ]] |
110 | then |
111 | su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@" |
112 | fi |
113 | } |
114 | |
115 | addconfig() |
116 | { |
117 | local opts |
118 | |
119 | if [[ -z ${CONFIG} ]] |
120 | then |
121 | eecho "You must define \$CONFIG varibale first!" |
122 | return 1 |
123 | fi |
124 | |
125 | if [[ ! -d $(dirname ${CONFIG}) ]] |
126 | then |
127 | install -d $(dirname ${CONFIG}) |
128 | fi |
129 | |
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 | } |
138 | |
139 | clearconfig() |
140 | { |
141 | if [[ -z ${CONFIG} ]] |
142 | then |
143 | eecho "You must define \$CONFIG varibale first!" |
144 | return 1 |
145 | fi |
146 | |
147 | if [[ ! -d $(dirname ${CONFIG}) ]] |
148 | then |
149 | install -d $(dirname ${CONFIG}) |
150 | fi |
151 | : > ${CONFIG} |
152 | } |
153 | |
154 | # 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 | require() |
170 | { |
171 | local requires="$@" |
172 | local i |
173 | |
174 | for i in ${requires} |
175 | do |
176 | # check for duplicate provides |
177 | if no_duplicate "${PROVIDE}" "${i}" |
178 | then |
179 | export REQUIRE="${REQUIRE} ${i}" |
180 | else |
181 | decho "duplicate provide '${i}' detected!" |
182 | fi |
183 | done |
184 | } |
185 | |
186 | verify_requirements() |
187 | { |
188 | local req |
189 | local prov |
190 | local missing |
191 | local sorted |
192 | |
193 | for req in ${REQUIRE} |
194 | do |
195 | # 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 | done |
203 | |
204 | # 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 | rvecho "${sorted}" |
211 | return 0 |
212 | else |
213 | for req in ${sorted} |
214 | do |
215 | if no_duplicate "${missing}" "$req" |
216 | then |
217 | # print normal |
218 | rvecho -n " ${req}" |
219 | else |
220 | # print missing |
221 | eecho -n " ${req}" |
222 | fi |
223 | done |
224 | return 1 |
225 | fi |
226 | } |
227 | |
228 | provide() |
229 | { |
230 | local provides="$@" |
231 | local i |
232 | |
233 | for i in ${provides} |
234 | do |
235 | # check for duplicate provides |
236 | if no_duplicate "${PROVIDE}" "${i}" |
237 | then |
238 | export PROVIDE="${PROVIDE} ${i}" |
239 | else |
240 | decho "duplicate provide '${i}' detected!" |
241 | 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 | rvecho ${sorted} |
253 | } |
254 | |
255 | # message only echo | disabled in quiet mode |
256 | mecho() |
257 | { |
258 | local COLCYAN="\033[1;36m" |
259 | local COLDEFAULT="\033[0m" |
260 | local opts |
261 | local webcrlf |
262 | |
263 | # print nothing if quiet mode was requested |
264 | [[ ${QUIET} = true ]] && return |
265 | |
266 | if [[ ${NOCOLORS} = true ]] |
267 | then |
268 | COLCYAN="" |
269 | COLDEFAULT="" |
270 | fi |
271 | |
272 | [[ ${WEBCRLF} = true ]] && webcrlf="<br>" |
273 | |
274 | # respect -n |
275 | case $1 in |
276 | -n) shift; opts="n" ;; |
277 | esac |
278 | |
279 | echo -e${opts} "${COLCYAN}$@${COLDEFAULT}${webcrlf}" |
280 | } |
281 | |
282 | # prints error messages | enabled even in quiet mode |
283 | eecho() |
284 | { |
285 | local COLRED="\033[1;31m" |
286 | local COLDEFAULT="\033[0m" |
287 | local opts |
288 | local webcrlf |
289 | |
290 | if [[ ${NOCOLORS} = true ]] |
291 | then |
292 | COLRED="" |
293 | COLDEFAULT="" |
294 | fi |
295 | |
296 | [[ ${WEBCRLF} = true ]] && webcrlf="<br>" |
297 | |
298 | # respect -n |
299 | case $1 in |
300 | -n) shift; opts="n" ;; |
301 | esac |
302 | |
303 | echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}" |
304 | } |
305 | |
306 | # 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 | path_not_empty() |
340 | { |
341 | local path="$1" |
342 | [[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1 |
343 | |
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 | } |