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