Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1264 - (show annotations) (download)
Fri Feb 4 20:13:23 2011 UTC (13 years, 3 months ago) by niro
File size: 4866 byte(s)
- added missing quit command to global help topics
- added hint about help [topic]
- let x11runas determine the DISPLAY itself by using MCORE_XORG_DISPLAY
- added no_duplicate() function for internal use
- let require() and provide() check for duplicate items via no_duplicate()
- added verify_requirements to be able to resolve missing plugin requirements
- mecho() and eecho() should honor the '-n' echo option
- added path_not_empty() functions which is used by helper_graphic_rebuild_xorg_conf_d() of the graphic plugin


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