Contents of /mcore-src/trunk/mcore-tools/src/include/daemon.global.class.in
Parent Directory | Revision Log
Revision 2006 -
(show annotations)
(download)
Mon Aug 13 09:40:07 2012 UTC (12 years, 1 month ago) by niro
Original Path: mcore-src/trunk/mcore-tools/daemon/include/daemon.global.class
File size: 6694 byte(s)
Mon Aug 13 09:40:07 2012 UTC (12 years, 1 month ago) by niro
Original Path: mcore-src/trunk/mcore-tools/daemon/include/daemon.global.class
File size: 6694 byte(s)
-added colors command, to re-enable colors after calling 'nocolors' cmd
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 | # 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 | # # 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 | |
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 | if valid_session |
62 | 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 | eecho "unkown method '${method}' . class '${class}' . cmd '${cmd}'" |
80 | 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 | 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 | mecho "\treload - reloads all client classes plugins" |
100 | mecho "\trestart - restarts the daemon" |
101 | mecho "\tstop - stops the daemon" |
102 | mecho "\tnocolors - disable colors, useful for the webclient" |
103 | mecho "\tcolors - enable colors" |
104 | mecho "\tquiet - do not print any unecessary messages" |
105 | mecho "\thelp - shows help" |
106 | mecho "\tquit - quits the connection to the server" |
107 | 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 | mecho |
119 | mecho "Type 'help [topic]' for more information about every topic." |
120 | } |
121 | |
122 | # on newer xorg-servers root is not allowed to run progs in a user session |
123 | x11runas() |
124 | { |
125 | if [[ -n $(pidof X) ]] |
126 | then |
127 | su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@" |
128 | fi |
129 | } |
130 | |
131 | addconfig() |
132 | { |
133 | local opts |
134 | |
135 | if [[ -z ${CONFIG} ]] |
136 | then |
137 | eecho "You must define \$CONFIG varibale first!" |
138 | return 1 |
139 | fi |
140 | |
141 | if [[ ! -d $(dirname ${CONFIG}) ]] |
142 | then |
143 | install -d $(dirname ${CONFIG}) |
144 | fi |
145 | |
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 | } |
154 | |
155 | clearconfig() |
156 | { |
157 | if [[ -z ${CONFIG} ]] |
158 | then |
159 | eecho "You must define \$CONFIG varibale first!" |
160 | return 1 |
161 | fi |
162 | |
163 | if [[ ! -d $(dirname ${CONFIG}) ]] |
164 | then |
165 | install -d $(dirname ${CONFIG}) |
166 | fi |
167 | : > ${CONFIG} |
168 | } |
169 | |
170 | # 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 | require() |
186 | { |
187 | local requires="$@" |
188 | local i |
189 | |
190 | for i in ${requires} |
191 | do |
192 | # check for duplicate provides |
193 | if no_duplicate "${PROVIDE}" "${i}" |
194 | then |
195 | export REQUIRE="${REQUIRE} ${i}" |
196 | else |
197 | decho "duplicate provide '${i}' detected!" |
198 | fi |
199 | done |
200 | } |
201 | |
202 | verify_requirements() |
203 | { |
204 | local req |
205 | local prov |
206 | local missing |
207 | local sorted |
208 | |
209 | for req in ${REQUIRE} |
210 | do |
211 | # 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 | done |
219 | |
220 | # 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 | rvecho "${sorted}" |
227 | return 0 |
228 | else |
229 | for req in ${sorted} |
230 | do |
231 | if no_duplicate "${missing}" "$req" |
232 | then |
233 | # print normal |
234 | rvecho -n " ${req}" |
235 | else |
236 | # print missing |
237 | eecho -n " ${req}" |
238 | fi |
239 | done |
240 | return 1 |
241 | fi |
242 | } |
243 | |
244 | provide() |
245 | { |
246 | local provides="$@" |
247 | local i |
248 | |
249 | for i in ${provides} |
250 | do |
251 | # check for duplicate provides |
252 | if no_duplicate "${PROVIDE}" "${i}" |
253 | then |
254 | export PROVIDE="${PROVIDE} ${i}" |
255 | else |
256 | decho "duplicate provide '${i}' detected!" |
257 | 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 | rvecho ${sorted} |
269 | } |
270 | |
271 | # message only echo | disabled in quiet mode |
272 | mecho() |
273 | { |
274 | local COLCYAN="\033[1;36m" |
275 | local COLDEFAULT="\033[0m" |
276 | local opts |
277 | local webcrlf |
278 | |
279 | # print nothing if quiet mode was requested |
280 | [[ ${QUIET} = true ]] && return |
281 | |
282 | if [[ ${NOCOLORS} = true ]] |
283 | then |
284 | COLCYAN="" |
285 | COLDEFAULT="" |
286 | fi |
287 | |
288 | [[ ${WEBCRLF} = true ]] && webcrlf="<br>" |
289 | |
290 | # respect -n |
291 | case $1 in |
292 | -n) shift; opts="n" ;; |
293 | esac |
294 | |
295 | echo -e${opts} "${COLCYAN}$@${COLDEFAULT}${webcrlf}" |
296 | } |
297 | |
298 | # prints error messages | enabled even in quiet mode |
299 | eecho() |
300 | { |
301 | local COLRED="\033[1;31m" |
302 | local COLDEFAULT="\033[0m" |
303 | local opts |
304 | local webcrlf |
305 | |
306 | if [[ ${NOCOLORS} = true ]] |
307 | then |
308 | COLRED="" |
309 | COLDEFAULT="" |
310 | fi |
311 | |
312 | [[ ${WEBCRLF} = true ]] && webcrlf="<br>" |
313 | |
314 | # respect -n |
315 | case $1 in |
316 | -n) shift; opts="n" ;; |
317 | esac |
318 | |
319 | echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}" |
320 | } |
321 | |
322 | # 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 | path_not_empty() |
356 | { |
357 | local path="$1" |
358 | [[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1 |
359 | |
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 | } |