1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | # ========================================================================
|
---|
4 | #
|
---|
5 | # *
|
---|
6 | # * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
7 | # * Software. It is distributed to you in the hope that it can be a useful
|
---|
8 | # * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
9 | # * It is distributed WITHOUT ANY WARRANTY.
|
---|
10 | # *
|
---|
11 | # * Permission to use, copy, modify and distribute this software and its
|
---|
12 | # * documentation for any purpose is hereby granted without fee,
|
---|
13 | # * provided that the above copyright notice appear in all copies and
|
---|
14 | # * that both that copyright notice and this permission notice appear
|
---|
15 | # * in supporting documentation. It is provided "as is" without express
|
---|
16 | # * or implied warranty.
|
---|
17 | # *
|
---|
18 | #
|
---|
19 | #
|
---|
20 | # Author(s): Daniela Dorner 05/2005 <mailto:dorner@astro.uni-wuerzburg.de>
|
---|
21 | #
|
---|
22 | # Copyright: MAGIC Software Development, 2000-2010
|
---|
23 | #
|
---|
24 | #
|
---|
25 | # ========================================================================
|
---|
26 | #
|
---|
27 | # This a resource file for the scripts, in which the standard paths and
|
---|
28 | # functions, which are needed more often are stored.
|
---|
29 | # Only constant variables are stored here, changing variables are stored
|
---|
30 | # in datacenter/scripts/setup
|
---|
31 | #
|
---|
32 |
|
---|
33 | # check if script has been started with absolute path
|
---|
34 | if ! dirname $0 | grep -E '^/' >/dev/null 2>&1
|
---|
35 | then
|
---|
36 | echo "Please start your script with an absolute path."
|
---|
37 | exit
|
---|
38 | fi
|
---|
39 |
|
---|
40 | if [ "$AUTOMATIONSETUP" = "" ]
|
---|
41 | then
|
---|
42 | echo "Please set the environment variable \$AUTOMATIONSETUP."
|
---|
43 | exit
|
---|
44 | fi
|
---|
45 |
|
---|
46 | if [ "$SOURCEFILEPATH" = "" ]
|
---|
47 | then
|
---|
48 | export SOURCEFILEPATH=`dirname $0`
|
---|
49 | fi
|
---|
50 |
|
---|
51 | # possible solution for $0 problem:
|
---|
52 | # ${BASH_SOURCE[0]}
|
---|
53 | # but has to be checked and tested more carefully
|
---|
54 | # should solve at least problem with login-shell and with source
|
---|
55 | source $SOURCEFILEPATH/setup.$AUTOMATIONSETUP
|
---|
56 |
|
---|
57 | datetime=`date +%F-%H-%M-%S`
|
---|
58 |
|
---|
59 |
|
---|
60 | # function to make sure that a directory is made
|
---|
61 | function makedir()
|
---|
62 | {
|
---|
63 | if [ ! -d $@ ]
|
---|
64 | then
|
---|
65 | mkdir -pv $@
|
---|
66 | if [ ! -d $@ ]
|
---|
67 | then
|
---|
68 | if ! [ "$processlog" = "" ]
|
---|
69 | then
|
---|
70 | echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] ERROR could not make dir "$@ >> $processlog
|
---|
71 | else
|
---|
72 | echo "could not make dir "$@
|
---|
73 | fi
|
---|
74 | if ls $lockfile >/dev/null 2>&1
|
---|
75 | then
|
---|
76 | rm -v $lockfile
|
---|
77 | fi
|
---|
78 | exit
|
---|
79 | fi
|
---|
80 | fi
|
---|
81 | }
|
---|
82 |
|
---|
83 | # logging paths for runlogs and processlog
|
---|
84 | runlogpath=$logpath/run/`date +%Y/%m/%d`
|
---|
85 | processlogpath=$logpath/processlog
|
---|
86 | makedir $runlogpath
|
---|
87 | makedir $processlogpath
|
---|
88 | processlog=$processlogpath/process`date +%F`.log
|
---|
89 |
|
---|
90 | makedir $lockpath
|
---|
91 |
|
---|
92 |
|
---|
93 | # function to provide proper logging in a single logfile ($processlog)
|
---|
94 | function printprocesslog
|
---|
95 | {
|
---|
96 | makedir $processlogpath
|
---|
97 | echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "$@ >> $processlog
|
---|
98 | }
|
---|
99 |
|
---|
100 | # function to exit a script properly
|
---|
101 | function finish()
|
---|
102 | {
|
---|
103 | if ! [ "$lockfile" = "" ] && ls $lockfile >/dev/null 2>&1
|
---|
104 | then
|
---|
105 | printprocesslog "DEBUG " `rm -v $lockfile`
|
---|
106 | fi
|
---|
107 | printprocesslog "DEBUG finished $0"
|
---|
108 | exit
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | # set checkvalue to ok at the beginning of the scripts
|
---|
113 | check="ok"
|
---|
114 |
|
---|
115 | # setup for jobmanager:
|
---|
116 | # log files (can't be defined in script itself, as script can run longer
|
---|
117 | # than one day
|
---|
118 | jmerrorlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`-error.log
|
---|
119 | jmscriptlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`.log
|
---|
120 |
|
---|
121 | # check if rc-files are available
|
---|
122 | if ! ls $steps >/dev/null
|
---|
123 | then
|
---|
124 | echo "Can't find steps.rc ($steps)"
|
---|
125 | finish
|
---|
126 | fi
|
---|
127 | if ! ls $sqlrc >/dev/null
|
---|
128 | then
|
---|
129 | echo "Can't find sql.rc ($sqlrc)"
|
---|
130 | finish
|
---|
131 | fi
|
---|
132 |
|
---|
133 | # resetting values for jobmanager
|
---|
134 | pno=0
|
---|
135 | totalpno=0
|
---|
136 | running=0
|
---|
137 | queued=0
|
---|
138 | runningscript=0
|
---|
139 | queuedscript=0
|
---|
140 | stillinqueue=0
|
---|
141 |
|
---|
142 |
|
---|
143 | # alias (we cannot check the beginning of the line due to
|
---|
144 | # color codes in filldotraw.C)
|
---|
145 | alias 'intgrep'='grep -E -o \\\(int\\\)[0-9]+$ | grep -E -o [0-9]+'
|
---|
146 |
|
---|
147 |
|
---|
148 | # in the following the functions, which are needed by several scripts, are
|
---|
149 | # defined
|
---|
150 |
|
---|
151 | # function to check if a process is already locked
|
---|
152 | # command line option can be used to execute something, e.g. 'continue'
|
---|
153 | function checklock()
|
---|
154 | {
|
---|
155 | if ! echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "`uname -a` > $lockfile 2>/dev/null
|
---|
156 | then
|
---|
157 | printprocesslog "WARN lockfile $lockfile exists"
|
---|
158 | $@
|
---|
159 | exit
|
---|
160 | else
|
---|
161 | printprocesslog "DEBUG created lockfile $lockfile"
|
---|
162 | fi
|
---|
163 | }
|
---|
164 |
|
---|
165 | # print the current status values
|
---|
166 | function printstatusvalues()
|
---|
167 | {
|
---|
168 | echo "the current values are:"
|
---|
169 | echo " starttime=$starttime"
|
---|
170 | echo " stoptime=$stoptime"
|
---|
171 | echo " returncode=$returncode"
|
---|
172 | echo "-- check: -$check-"
|
---|
173 | echo ""
|
---|
174 | }
|
---|
175 |
|
---|
176 | # get the db-setup from the sql.rc
|
---|
177 | function getdbsetup()
|
---|
178 | {
|
---|
179 | db=`grep Database $sqlrc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
|
---|
180 | pw=`grep Password $sqlrc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
|
---|
181 | us=`grep User $sqlrc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
|
---|
182 | ho=`grep URL $sqlrc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
|
---|
183 | # echo "setup: "
|
---|
184 | # echo " db: "$db
|
---|
185 | # echo " pw: "$pw
|
---|
186 | # echo " us: "$us
|
---|
187 | # echo " ho: "$ho
|
---|
188 | }
|
---|
189 |
|
---|
190 | # function to get information from the setupfile $steps
|
---|
191 | function getfromsetup()
|
---|
192 | {
|
---|
193 | grep $1"[.]"$2":" $steps | grep -v '#' | sed -e "s/$1[.]$2://"
|
---|
194 | }
|
---|
195 |
|
---|
196 | # function to get the needed information from the dependencies-file steps.rc
|
---|
197 | function getstepinfo()
|
---|
198 | {
|
---|
199 | getdbsetup
|
---|
200 | needs=( `getfromsetup $step "Needs"` )
|
---|
201 | noderestricted=`getfromsetup $step "NodeRestricted"`
|
---|
202 | prims=( `getfromsetup $step "Primaries"` )
|
---|
203 | maintable=`getfromsetup $step "MainTable" | sed -e "s/\ //g"`
|
---|
204 | # echo " maintable: "$maintable
|
---|
205 | # echo " needs: "${needs[@]}
|
---|
206 | # echo " noderestricted: "$noderestricted
|
---|
207 | # echo " prims: "${prims[@]}
|
---|
208 | }
|
---|
209 |
|
---|
210 | # function to get the joins needed for the get/set status queries
|
---|
211 | function getalljoins()
|
---|
212 | {
|
---|
213 | # add table
|
---|
214 | query=$query" "$maintable"Status"
|
---|
215 | # add special join
|
---|
216 | query=$query" "`getfromsetup $maintable "SpecialJoin"`
|
---|
217 | # add join for step unless step is the same as maintable
|
---|
218 | if ! [ "$step" = "$maintable" ]
|
---|
219 | then
|
---|
220 | query=$query" LEFT JOIN "$step"Status USING("${prims[@]}") "
|
---|
221 | fi
|
---|
222 | # add joins for influences or needs
|
---|
223 | for otherstep in ${othersteps[@]}
|
---|
224 | do
|
---|
225 | if ! [ "$otherstep" = "$maintable" ]
|
---|
226 | then
|
---|
227 | query=$query" LEFT JOIN "$otherstep"Status USING("`getfromsetup $otherstep "Primaries"`") "
|
---|
228 | fi
|
---|
229 | done
|
---|
230 | }
|
---|
231 |
|
---|
232 | # function to create the middle part of a query
|
---|
233 | # which is identical for the functions getstatus() and gettodo()
|
---|
234 | function getstatusquery()
|
---|
235 | {
|
---|
236 | # add from which table the information is queried
|
---|
237 | query=$query" FROM "
|
---|
238 | othersteps=${needs[@]}
|
---|
239 | getalljoins
|
---|
240 | # add condition
|
---|
241 | query=$query" WHERE "
|
---|
242 | # add condition for step, i.e. step is not yet done
|
---|
243 | query=$query" ISNULL("$step"Status.fStartTime) "
|
---|
244 | query=$query" AND ISNULL("$step"Status.fStopTime) "
|
---|
245 | query=$query" AND ISNULL("$step"Status.fReturnCode) "
|
---|
246 | # add requirement for production host in case it is needed
|
---|
247 | if [ "$1 " != " " ]
|
---|
248 | then
|
---|
249 | query=$query" AND fProductionHostKEY=$2 "
|
---|
250 | fi
|
---|
251 | if ! echo $query | grep UPDATE >/dev/null 2>&1
|
---|
252 | then
|
---|
253 | query=$query" GROUP BY "${prims[@]}
|
---|
254 | fi
|
---|
255 | # add condition for needs, i.e. that step is done
|
---|
256 | for (( k=0 ; k < ${#needs[@]} ; k++ ))
|
---|
257 | do
|
---|
258 | if [ $k -eq 0 ]
|
---|
259 | then
|
---|
260 | query=$query" HAVING "
|
---|
261 | else
|
---|
262 | query=$query" AND "
|
---|
263 | fi
|
---|
264 | query=$query" COUNT(*)=COUNT(IF("
|
---|
265 | query=$query" NOT ISNULL("${needs[$k]}"Status.fStartTime) "
|
---|
266 | query=$query" AND NOT ISNULL("${needs[$k]}"Status.fStopTime) "
|
---|
267 | query=$query" AND ISNULL("${needs[$k]}"Status.fReturnCode) "
|
---|
268 | query=$query" , 1, NULL)) "
|
---|
269 | done
|
---|
270 | }
|
---|
271 |
|
---|
272 | # function to get todolist
|
---|
273 | # returns the next or the list of next steps
|
---|
274 | function gettodo()
|
---|
275 | {
|
---|
276 | # reset the variable for the number of the next step
|
---|
277 | process=
|
---|
278 | printprocesslog "DEBUG getting todo for step $step..."
|
---|
279 | getstepinfo
|
---|
280 | # get query
|
---|
281 | query=" SELECT "${prims[@]}
|
---|
282 | getstatusquery $2
|
---|
283 | # order by priority to the the number of the next step to be done
|
---|
284 | query=$query" ORDER BY "$step"Status.fPriority desc "
|
---|
285 | # add limitation in case only one or a limited number of
|
---|
286 | # processes should be executed
|
---|
287 | if [ "$1 " != " " ]
|
---|
288 | then
|
---|
289 | query=$query" limit 0, $1 "
|
---|
290 | fi
|
---|
291 | # print query
|
---|
292 | printprocesslog "DEBUG gettodo for step $step QUERY: "$query
|
---|
293 | # execute query
|
---|
294 | if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
---|
295 | then
|
---|
296 | printprocesslog "ERROR could not query processes from db (program: $program, function gettodo)"
|
---|
297 | finish
|
---|
298 | fi
|
---|
299 | # get numbers of next step from mysql result
|
---|
300 | if [ "$process" = "" ]
|
---|
301 | then
|
---|
302 | printprocesslog "DEBUG => nothing to do"
|
---|
303 | finish
|
---|
304 | else
|
---|
305 | primaries=( $process )
|
---|
306 | num=`expr ${#primaries[@]} / ${#prims[@]} `
|
---|
307 | fi
|
---|
308 | }
|
---|
309 |
|
---|
310 | # function to get the number of processes which still have to be done
|
---|
311 | function getstatus()
|
---|
312 | {
|
---|
313 | # reset the variable for the number of steps to be done
|
---|
314 | numproc=0
|
---|
315 | getstepinfo
|
---|
316 | # get query
|
---|
317 | query=" SELECT "${prims[@]}
|
---|
318 | getstatusquery $1
|
---|
319 | # print query
|
---|
320 | printprocesslog "DEBUG getstatus for step $step QUERY: "$query
|
---|
321 | # execute query
|
---|
322 | if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query " | wc -l`
|
---|
323 | then
|
---|
324 | printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
|
---|
325 | echo `date +%F\ %T`" ERROR could not query number of processes from db (program: $program, function getstatus)"
|
---|
326 | continue
|
---|
327 | fi
|
---|
328 | }
|
---|
329 |
|
---|
330 | # function to set status of a process in the db
|
---|
331 | function setstatus()
|
---|
332 | {
|
---|
333 | # remark:
|
---|
334 | # this function does not include the 'Default' flag
|
---|
335 | # for resetting steps
|
---|
336 |
|
---|
337 | # for dowebplots (there are steps which have no entry in the DB)
|
---|
338 | if [ "$step" = "no" ]
|
---|
339 | then
|
---|
340 | return
|
---|
341 | fi
|
---|
342 |
|
---|
343 | # reset status values
|
---|
344 | starttime=NULL
|
---|
345 | stoptime=NULL
|
---|
346 | returncode=NULL
|
---|
347 | # evaluate the status values
|
---|
348 | case $@ in
|
---|
349 | start) printprocesslog "DEBUG setstatus start"
|
---|
350 | starttime="Now()"
|
---|
351 | ;;
|
---|
352 | stop) case $check in
|
---|
353 | ok) printprocesslog "DEBUB setstatus stop - ok"
|
---|
354 | starttime=noreset
|
---|
355 | stoptime="Now()"
|
---|
356 | ;;
|
---|
357 | no) printprocesslog "DEBUG setstatus stop - nothing new"
|
---|
358 | check="ok"
|
---|
359 | ;;
|
---|
360 | *) printprocesslog "DEBUG setstatus stop - failed"
|
---|
361 | starttime=noreset
|
---|
362 | stoptime="Now()"
|
---|
363 | if [ "$check" == "" ]
|
---|
364 | then
|
---|
365 | returncode=1
|
---|
366 | else
|
---|
367 | returncode=$check
|
---|
368 | fi
|
---|
369 | check="ok"
|
---|
370 | ;;
|
---|
371 | esac
|
---|
372 | ;;
|
---|
373 | *) printprocesslog "ERROR function setstatus got wrong variable"
|
---|
374 | finish
|
---|
375 | ;;
|
---|
376 | esac
|
---|
377 |
|
---|
378 | # get
|
---|
379 | getstepinfo
|
---|
380 |
|
---|
381 | # get the influences from the steps.rc by evaluating the needs of all steps
|
---|
382 | othersteps=`grep $step $steps | grep "Needs" | grep -v "$step[.]Needs" | cut -d'.' -f1`
|
---|
383 |
|
---|
384 | # get query
|
---|
385 | query=" UPDATE "
|
---|
386 | getalljoins
|
---|
387 | # set the status values according to the new status of the step
|
---|
388 | query=$query" SET "
|
---|
389 | if ! [ "$starttime" = "noreset" ]
|
---|
390 | then
|
---|
391 | query=$query" "$step"Status.fStartTime=$starttime, "
|
---|
392 | fi
|
---|
393 | query=$query" "$step"Status.fStopTime=$stoptime, "$step"Status.fReturnCode=$returncode "
|
---|
394 | # set also the status values of the influenced steps
|
---|
395 | for otherstep in $othersteps
|
---|
396 | do
|
---|
397 | query=$query", "$otherstep"Status.fStartTime=NULL "
|
---|
398 | query=$query", "$otherstep"Status.fStopTime=NULL "
|
---|
399 | query=$query", "$otherstep"Status.fReturnCode=NULL "
|
---|
400 | done
|
---|
401 | # give the condition for which step the status values have to be set
|
---|
402 | query=$query" WHERE "
|
---|
403 | if [ "$s" = "" ]
|
---|
404 | then
|
---|
405 | s=0
|
---|
406 | fi
|
---|
407 | query=$query" "$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}]}'"
|
---|
408 | for (( j=1 ; j < ${#prims[@]} ; j++ ))
|
---|
409 | do
|
---|
410 | query=$query" AND "$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}+$j]}' "
|
---|
411 | done
|
---|
412 | # print query
|
---|
413 | printprocesslog "DEBUG setstatus for step $step QUERY: "$query
|
---|
414 | # execute query
|
---|
415 | if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
|
---|
416 | then
|
---|
417 | printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
|
---|
418 | finish
|
---|
419 | fi
|
---|
420 | }
|
---|
421 |
|
---|
422 | # function to send a mysql query
|
---|
423 | function sendquery()
|
---|
424 | {
|
---|
425 | getdbsetup
|
---|
426 | printprocesslog "DEBUG sendquery QUERY: "$query
|
---|
427 | if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
|
---|
428 | then
|
---|
429 | printprocesslog "ERROR could not query db (program: $program, function sendquery)"
|
---|
430 | return 1
|
---|
431 | fi
|
---|
432 | if [ "$val" = "NULL" ]
|
---|
433 | then
|
---|
434 | val=
|
---|
435 | fi
|
---|
436 | echo $val
|
---|
437 | return 0
|
---|
438 | }
|
---|
439 |
|
---|