Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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