Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1308 - (show annotations) (download)
Sun Feb 6 23:48:20 2011 UTC (13 years, 2 months ago) by niro
File size: 5224 byte(s)
-renamed validate_session() to valid_session()
-added nocolors and webcrlf options which are allowed to be called before login (needed by web-interfaces)
-let addconfig() honor -e and -n echo variables
1 # $Id$
2
3 # # 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
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 if valid_session
30 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 mecho "\trequire - verify plugin requirements"
67 mecho "\nocolors - disable colors, useful for the webclient"
68 mecho "\thelp - shows help"
69 mecho "\tquit - quits the connection to the server"
70 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 mecho
82 mecho "Type 'help [topic]' for more information about every topic."
83 }
84
85 # on newer xorg-servers root is not allowed to run progs in a user session
86 x11runas()
87 {
88 su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@"
89 }
90
91 addconfig()
92 {
93 local opts
94
95 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
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 }
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 if [[ ! -d $(dirname ${CONFIG}) ]]
124 then
125 install -d $(dirname ${CONFIG})
126 fi
127 : > ${CONFIG}
128 }
129
130 # 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 require()
146 {
147 local requires="$@"
148 local i
149
150 for i in ${requires}
151 do
152 # 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 done
160 }
161
162 verify_requirements()
163 {
164 local req
165 local prov
166 local missing
167 local sorted
168
169 for req in ${REQUIRE}
170 do
171 # 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 done
179
180 # 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 }
203
204 provide()
205 {
206 local provides="$@"
207 local i
208
209 for i in ${provides}
210 do
211 # check for duplicate provides
212 if no_duplicate "${PROVIDE}" "${i}"
213 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 local opts
236 local webcrlf
237
238 if [[ ${NOCOLORS} = true ]]
239 then
240 COLCYAN=""
241 COLDEFAULT=""
242 fi
243
244 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
245
246 # respect -n
247 case $1 in
248 -n) shift; opts="n" ;;
249 esac
250
251 echo -e${opts} "${COLCYAN}$@${COLDEFAULT}${webcrlf}"
252 }
253
254 eecho()
255 {
256 local COLRED="\033[1;31m"
257 local COLDEFAULT="\033[0m"
258 local opts
259 local webcrlf
260
261 if [[ ${NOCOLORS} = true ]]
262 then
263 COLRED=""
264 COLDEFAULT=""
265 fi
266
267 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
268
269 # respect -n
270 case $1 in
271 -n) shift; opts="n" ;;
272 esac
273
274 echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}"
275 }
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 }