Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1330 - (hide annotations) (download)
Wed Feb 16 18:24:35 2011 UTC (13 years, 2 months ago) by niro
File size: 5225 byte(s)
-fixed a typo in global help description
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     echo "unkown method '${method}' . class '${class}' . cmd '${cmd}'"
48     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     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 niro 1308 mecho "\trequire - verify plugin requirements"
67 niro 1330 mecho "\tnocolors - disable colors, useful for the webclient"
68 niro 1248 mecho "\thelp - shows help"
69 niro 1264 mecho "\tquit - quits the connection to the server"
70 niro 1248 mecho
71     mecho "Help topics:"
72     for i in ${topics}
73     do
74     # excludes
75     case ${i} in
76     help_topics|topics) continue ;;
77     esac
78    
79     mecho "\t${i}"
80     done
81 niro 1264 mecho
82     mecho "Type 'help [topic]' for more information about every topic."
83 niro 1248 }
84    
85     # on newer xorg-servers root is not allowed to run progs in a user session
86     x11runas()
87     {
88 niro 1264 su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@"
89 niro 1248 }
90    
91     addconfig()
92     {
93 niro 1308 local opts
94    
95 niro 1248 if [[ -z ${CONFIG} ]]
96     then
97     echo "You must define \$CONFIG varibale first!"
98     return 1
99     fi
100    
101     if [[ ! -d $(dirname ${CONFIG}) ]]
102     then
103     install -d $(dirname ${CONFIG})
104     fi
105 niro 1308
106     # check for opts
107     case $1 in
108     -n) shift; opts=" -n" ;;
109     -e) shift; opts=" -e" ;;
110     esac
111    
112     echo ${opts} "$@" >> ${CONFIG}
113 niro 1248 }
114    
115     clearconfig()
116     {
117     if [[ -z ${CONFIG} ]]
118     then
119     echo "You must define \$CONFIG varibale first!"
120     return 1
121     fi
122    
123 niro 1252 if [[ ! -d $(dirname ${CONFIG}) ]]
124     then
125     install -d $(dirname ${CONFIG})
126     fi
127 niro 1248 : > ${CONFIG}
128     }
129    
130 niro 1264 # no_duplicate $list $item
131     no_duplicate()
132     {
133     local i
134     local list="$1"
135     local item="$2"
136    
137     for i in ${list}
138     do
139     [[ ${i} = ${item} ]] && return 1
140     done
141    
142     return 0
143     }
144    
145 niro 1248 require()
146     {
147     local requires="$@"
148     local i
149    
150     for i in ${requires}
151     do
152 niro 1264 # check for duplicate provides
153     if no_duplicate "${PROVIDE}" "${i}"
154     then
155     export REQUIRE="${REQUIRE} ${i}"
156     else
157     [[ ${DEBUG} = 1 ]] && echo "duplicate provide '${i}' detected!"
158     fi
159 niro 1248 done
160     }
161    
162 niro 1264 verify_requirements()
163 niro 1248 {
164 niro 1264 local req
165     local prov
166     local missing
167     local sorted
168 niro 1248
169 niro 1264 for req in ${REQUIRE}
170 niro 1248 do
171 niro 1264 # scan PROVIDE for dupes
172     # if a dupe is found, then requirement is fullfilled
173     # else add to missing
174     if no_duplicate "${PROVIDE}" "${req}"
175     then
176     missing="${missing} ${req}"
177     fi
178 niro 1248 done
179    
180 niro 1264 # sort them alpabetically
181     sorted=$(for i in ${REQUIRE}; do echo "${i}"; done | sort)
182    
183     # show missing and set the right retval
184     if [[ -z ${missing} ]]
185     then
186     mecho "${sorted}"
187     return 0
188     else
189     for req in ${sorted}
190     do
191     if no_duplicate "${missing}" "$req"
192     then
193     # print normal
194     mecho -n " ${req}"
195     else
196     # print missing
197     eecho -n " ${req}"
198     fi
199     done
200     return 1
201     fi
202 niro 1248 }
203    
204     provide()
205     {
206     local provides="$@"
207     local i
208    
209     for i in ${provides}
210     do
211     # check for duplicate provides
212 niro 1264 if no_duplicate "${PROVIDE}" "${i}"
213 niro 1248 then
214     export PROVIDE="${PROVIDE} ${i}"
215     else
216     [[ ${DEBUG} = 1 ]] && echo "duplicate provide '${i}' detected!"
217     fi
218     done
219     }
220    
221     print_provide()
222     {
223     local sorted
224    
225     # sort them alpabetically
226     sorted=$(for i in ${PROVIDE}; do echo "${i}"; done | sort)
227     # do not escape, or CRLFS get printed to screen too
228     mecho ${sorted}
229     }
230    
231     mecho()
232     {
233     local COLCYAN="\033[1;36m"
234     local COLDEFAULT="\033[0m"
235 niro 1264 local opts
236 niro 1308 local webcrlf
237 niro 1264
238 niro 1248 if [[ ${NOCOLORS} = true ]]
239     then
240     COLCYAN=""
241     COLDEFAULT=""
242     fi
243    
244 niro 1308 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
245    
246 niro 1264 # respect -n
247     case $1 in
248     -n) shift; opts="n" ;;
249     esac
250    
251 niro 1308 echo -e${opts} "${COLCYAN}$@${COLDEFAULT}${webcrlf}"
252 niro 1248 }
253 niro 1264
254     eecho()
255     {
256     local COLRED="\033[1;31m"
257     local COLDEFAULT="\033[0m"
258     local opts
259 niro 1308 local webcrlf
260 niro 1264
261     if [[ ${NOCOLORS} = true ]]
262     then
263     COLRED=""
264     COLDEFAULT=""
265     fi
266    
267 niro 1308 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
268    
269 niro 1264 # respect -n
270     case $1 in
271     -n) shift; opts="n" ;;
272     esac
273    
274 niro 1308 echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}"
275 niro 1264 }
276    
277     path_not_empty()
278     {
279     local path="$1"
280     [[ -z ${path} ]] && "path_not_empty(): no path given!" && return 1
281    
282     # return ERR if path does not exist
283     [[ ! -d ${path} ]] && return 1
284     # return ERR if path empty
285     [[ -z $(find "${path}" -mindepth 1 -maxdepth 1) ]] && return 1
286    
287     # every thing went ok, directory not empty
288     return 0
289     }