source: trunk/MagicSoft/Mars/datacenter/scripts/sourcefile@ 9614

Last change on this file since 9614 was 9610, checked in by Daniela Dorner, 15 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 16.4 KB
Line 
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
34if ! dirname $0 | grep -E '^/' >/dev/null 2>&1
35then
36 echo "Please start your script with an absolute path."
37 exit
38fi
39
40if [ "$AUTOMATIONSETUP" = "" ]
41then
42 echo "Please set the environment variable \$AUTOMATIONSETUP."
43 exit
44fi
45
46# possible solution for $0 problem:
47# ${BASH_SOURCE[0]}
48# but has to be checked and tested more carefully
49# should solve at least problem with login-shell and with source
50source `dirname $0`/setup.$AUTOMATIONSETUP
51
52datetime=`date +%F-%H-%M-%S`
53
54
55# function to make sure that a directory is made
56function makedir()
57{
58 if [ ! -d $@ ]
59 then
60 mkdir -pv $@
61 if [ ! -d $@ ]
62 then
63 if ! [ "$processlog" = "" ]
64 then
65 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] ERROR could not make dir "$@ >> $processlog
66 else
67 echo "could not make dir "$@
68 fi
69 if ls $lockfile >/dev/null 2>&1
70 then
71 rm -v $lockfile
72 fi
73 exit
74 fi
75 fi
76}
77
78# logging paths for runlogs and processlog
79runlogpath=$logpath/run/`date +%Y/%m/%d`
80processlogpath=$logpath/processlog
81makedir $runlogpath
82makedir $processlogpath
83processlog=$processlogpath/process`date +%F`.log
84
85makedir $lockpath
86
87
88# function to provide proper logging in a single logfile ($processlog)
89function printprocesslog
90{
91 makedir $processlogpath
92 echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "$@ >> $processlog
93}
94
95# function to exit a script properly
96function finish()
97{
98 if ! [ "$lockfile" = "" ] && ls $lockfile >/dev/null 2>&1
99 then
100 printprocesslog "INFO " `rm -v $lockfile`
101 fi
102 printprocesslog "INFO finished $0"
103 exit
104}
105
106
107# set checkvalue to ok at the beginning of the scripts
108check="ok"
109
110#failed codes
111#sequence build status
112Fbuildsequ=1
113Fdoexcl=2
114#run process status
115Ftimecorr=3
116Ffillraw=4
117Fsinope=5
118Ffillsinope=6
119Fresetexcl=7
120#sequence process status
121Fwritesequfile=8
122Ffilesavail=9
123Fnoccfile=10
124Fnocacofile=11
125Fmerppcc=12
126Fmerppcaco=13
127Fcallisto=14
128Ffillcalib=15
129Ffillsignal=16
130Fstar=17
131Ffillstar=18
132#dataset process status
133Fwritedatasetfile=19
134Fstardone=20
135Fganymed=21
136Ffillganymed=22
137#again run process status
138FCompmux=26
139Fdowebplots=27
140#again mc run process status
141Fmccallisto=28
142Ffillmccalib=29
143Ffillmcsignal=30
144Fmcstar=31
145Ffillmcstar=32
146Fcorsikasimtel=33
147Fchimp=34
148Fchimpcp=35
149Fctastar=36
150Fctastarcp=37
151FctastereoA=38
152FctastereoB=39
153FctastereoC=40
154FctastereoD=41
155FctastereoE=42
156FctastereoF=43
157FctastereoG=44
158FctastereoH=45
159Fctastereocp=46
160
161# setup for jobmanager:
162# log files (can't be defined in script itself, as script can run longer
163# than one day
164jmerrorlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`-error.log
165jmscriptlog=$runlogpath/jobmanager-`whoami`-$HOSTNAME-$AUTOMATIONSETUP-`date +%F`.log
166
167# check if rc-files are available
168if ! ls $steps >/dev/null
169then
170 echo "Can't find steps.rc ($steps)"
171 finish
172fi
173if ! ls $sqlrc >/dev/null
174then
175 echo "Can't find sql.rc ($sqlrc)"
176 finish
177fi
178
179# resetting values
180pno=0
181totalpno=0
182running=0
183queued=0
184runningscript=0
185queuedscript=0
186stillinqueue=0
187
188
189# alias (we cannot check the beginning of the line due to
190# color codes in filldotraw.C)
191alias 'intgrep'='grep -E -o \\\(int\\\)[0-9]+$ | grep -E -o [0-9]+'
192
193
194# in the following the functions, which are needed by several scripts, are
195# defined
196
197# function to check if a process is already locked
198# command line option can be used to execute something, e.g. 'continue'
199function checklock()
200{
201 if ! echo `date +%F\ %T`" "`whoami`"@"$HOSTNAME" "`basename $0`"["$$"] "`uname -a` > $lockfile 2>/dev/null
202 then
203 printprocesslog "WARN lockfile $lockfile exists"
204 $@
205 exit
206 else
207 printprocesslog "INFO created lockfile $lockfile"
208 fi
209}
210
211# print the current status values
212function printstatusvalues()
213{
214 echo "the current values are:"
215 echo " starttime=$starttime"
216 echo " stoptime=$stoptime"
217 echo " returncode=$returncode"
218 echo "-- check: -$check-"
219 echo ""
220}
221
222# get the db-setup from the sql.rc
223function getdbsetup()
224{
225 db=`grep Database $sqlrc | grep -v '#' | sed -e 's/Database: //' -e 's/ //g'`
226 pw=`grep Password $sqlrc | grep -v '#' | sed -e 's/Password: //' -e 's/ //g'`
227 us=`grep User $sqlrc | grep -v '#' | sed -e 's/User: //' -e 's/ //g'`
228 ho=`grep URL $sqlrc | grep -v '#' | sed -e 's/ //g' -e 's/URL:mysql:\/\///'`
229# echo "setup: "
230# echo " db: "$db
231# echo " pw: "$pw
232# echo " us: "$us
233# echo " ho: "$ho
234}
235
236# function to get the needed information from the dependencies-file steps.rc
237function getstepinfo()
238{
239 getdbsetup
240 needs=`grep "$step[.]Needs:" $steps | grep -v '#' | sed -e "s/$step[.]Needs://"`
241 noderestricted=`grep "$step[.]NodeRestricted:" $steps | grep -v '#' | sed -e "s/$step[.]NodeRestricted://" -e 's/ //g'`
242 prims=( `grep "$step[.]Primaries:" $steps | grep -v '#' | sed -e "s/$step[.]Primaries://"` )
243# echo " needs: $needs"
244# echo " noderestricted: $noderestricted"
245# echo " prims: ${prims[@]}"
246}
247
248# function to get the primaries of a step from the dependencies-file steps.rc
249function getprimary()
250{
251 getdbsetup
252 grep $@"[.]Primaries:" $steps | grep -v '#' | sed -e "s/$@[.]Primaries://"
253}
254
255# function to get the join of a step
256# from the dependencies-file steps.rc
257# $1 gives the step
258# $2 gives the type of join
259function getjoin()
260{
261 getdbsetup
262 grep $1"[.]"$2"Join:" $steps | grep -v '#' | sed -e "s/$1[.]$2Join://"
263}
264
265# function to get the needs of a step from the dependencies-file steps.rc
266function getneeds()
267{
268 getdbsetup
269 grep $@"[.]Needs:" $steps | sed -e "s/$@[.]Needs://"
270}
271
272# subquery which is needed to check the
273# status of the steps with different primaries
274function subquerypart()
275{
276 query=$query" SELECT COUNT(*) FROM "$need"Status "`getjoin $need "SpecialInfluence"`
277 needinfluences=`grep $need $steps | grep "Needs" | grep -v "$need[.]Needs" | cut -d'.' -f1`
278 for needinfl in $needinfluences
279 do
280 needinfljoin=`getjoin $needinfl "Influence"`
281 if ! [ "$needinfljoin" = "" ]
282 then
283 query=$query" LEFT JOIN "$needinfl"Status USING ("$needinfljoin") "
284 fi
285 query=$query" "`getjoin $needinfl "SpecialInfluence"`" "
286 done
287 query=$query" WHERE "`echo ${prims[0]} | sed -e 's/,//g'`"="$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`
288 for (( j=1 ; j < ${#prims[@]} ; j++ ))
289 do
290 query=$query" AND "`echo ${prims[$j]} | sed -e 's/,//g'`"="$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`
291 done
292}
293
294# function to create the middle part of a query
295# which is identical for the functions getstatus() and gettodo()
296function middlepartofquery()
297{
298 # add from which table the information is queried
299 query=$query" FROM "$step"Status "
300 # add the joins to the tables in which the status of the preceding steps is stored
301 for need in $needs
302 do
303 needprims=( `getprimary $need` )
304 # make sure that the correct joins are used
305 specialneedjoin=`getjoin $need "SpecialNeed"`
306 if [ "$specialneedjoin" = "" ]
307 then
308 query=$query" LEFT JOIN "$need"Status USING (${needprims[@]}) "
309 else
310 query=$query" "$specialneedjoin" "
311 query=$query" LEFT JOIN "$need"Status USING (${needprims[@]}) "
312 break
313 fi
314 done
315 # add condition
316 query=$query" WHERE "
317 # add condition for the status of the peceding steps
318 counter=0
319 specialneedjoin=
320 specialinfljoin=
321 for need in $needs
322 do
323 if [ $counter -gt 0 ]
324 then
325 query=$query" AND "
326 fi
327 specialneedjoin=$specialneedjoin" "`getjoin $need "SpecialNeed"`
328 specialinfljoin=$specialinfljoin" "`getjoin $need "SpecialInfluence"`
329 if [ "$specialneedjoin" = " " ]
330 then
331 # condition for step with same primaries
332 query=$query" NOT ISNULL("$need"Status.fStartTime) AND "
333 query=$query" NOT ISNULL("$need"Status.fStopTime) AND "
334 query=$query" ISNULL("$need"Status.fReturnCode) "
335 specialneedjoin=
336 specialinfljoin=
337 else
338 # special query to check steps which
339 # do not have the same primaries
340 # comparison of
341 # number of rows
342 # with
343 # number of rows which are done
344 query=$query" ( "
345 subquerypart
346 query=$query" ) = ( "
347 subquerypart
348 query=$query" AND NOT ISNULL("$need"Status.fStartTime) "
349 query=$query" AND NOT ISNULL("$need"Status.fStopTime) "
350 query=$query" AND ISNULL("$need"Status.fReturnCode)) "
351 fi
352 counter=`echo $counter + 1 | bc -l`
353 done
354 # add condition for the status of the step itself
355 if [ $counter -gt 0 ]
356 then
357 query=$query" AND "
358 fi
359 query=$query" ISNULL("$step"Status.fStartTime) "
360 query=$query" AND ISNULL("$step"Status.fStopTime) "
361 query=$query" AND ISNULL("$step"Status.fReturnCode) "
362}
363
364# function to get todolist
365# returns the next or the list of next steps
366function gettodo()
367{
368 # reset the variable for the number of the next step
369 process=
370 printprocesslog "INFO getting todo..."
371 getstepinfo
372 # get query
373 query=" SELECT "`echo ${prims[0]} | sed -e 's/,//g'`
374 for (( j=1 ; j < ${#prims[@]} ; j++ ))
375 do
376 query=$query", "`echo ${prims[$j]} | sed -e 's/,//g'`
377 done
378 # get middle par of query
379 middlepartofquery
380 # add requirement for production host in case it is needed
381 if [ "$2 " != " " ]
382 then
383 query=$query" AND fProductionHostKEY=$2 "
384 fi
385 # order by priority to the the number of the next step to be done
386 query=$query" ORDER BY "$step"Status.fPriority desc "
387 # add limitation in case only one or a limited number of
388 # processes should be executed
389 if [ "$1 " != " " ]
390 then
391 query=$query" limit 0, $1 "
392 fi
393 # print query
394 #echo " gettodo QUERY: "$query
395 printprocesslog "INFO gettodo QUERY: "$query
396 # execute query
397 if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
398 then
399 printprocesslog "ERROR could not query processes from db (program: $program, function gettodo)"
400 finish
401 fi
402 # get numbers of next step from mysql result
403 if [ "$process" = "" ]
404 then
405 printprocesslog "INFO => nothing to do"
406 finish
407 else
408 primaries=( $process )
409 num=`expr ${#primaries[@]} / ${#prims[@]} `
410 fi
411}
412
413# function to get the number of processes which still have to be done
414function getstatus()
415{
416 # reset the variable for the number of steps to be done
417 numproc=
418 getstepinfo
419 # get query
420 query=" SELECT COUNT(*), 1 " # the 1 is just for grouping
421 # get middle part of query
422 middlepartofquery
423 # add requirement for production host in case it is needed
424 if [ "$1 " != " " ]
425 then
426 query=$query" AND fProductionHostKEY=$1 "
427 fi
428 # group by an 'artifical' column to get the number of lines
429 query=$query" GROUP BY 2 "
430 # printing query
431 #echo " getstatus QUERY: "$query
432 printprocesslog "INFO getstatus QUERY: "$query
433 # execute query
434 if ! numprocs=( `mysql -s -u $us --password=$pw --host=$ho $db -e " $query "` )
435 then
436 printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
437 echo `date +%F\ %T`" ERROR could not query number of processes from db (program: $program, function getstatus)"
438 continue
439 fi
440 # get number of processes from mysql result
441 if [ "${numprocs[1]}" = "" ]
442 then
443 numproc=0
444 else
445 numproc=${numprocs[0]}
446 fi
447}
448
449# function to set status of a process in the db
450function setstatus()
451{
452 # remark:
453 # this function does not include the 'Default' flag
454 # for resetting steps
455
456 # for dowebplots (there are steps which have no entry in the DB)
457 if [ "$step" = "no" ]
458 then
459 return
460 fi
461
462 # reset status values
463 starttime=NULL
464 stoptime=NULL
465 returncode=NULL
466 # evaluate the status values
467 case $@ in
468 start) printprocesslog "INFO setstatus start"
469 starttime="Now()"
470 ;;
471 stop) case $check in
472 ok) printprocesslog "INFO setstatus stop - ok"
473 starttime=noreset
474 stoptime="Now()"
475 ;;
476 no) printprocesslog "INFO setstatus stop - nothing new"
477 check="ok"
478 ;;
479 *) printprocesslog "INFO setstatus stop - failed"
480 starttime=noreset
481 stoptime="Now()"
482 if [ "$check" == "" ]
483 then
484 returncode=1
485 else
486 returncode=$check
487 fi
488 check="ok"
489 ;;
490 esac
491 ;;
492 *) printprocesslog "ERROR function setstatus got wrong variable"
493 finish
494 ;;
495 esac
496 # get
497 getstepinfo
498
499 # get the influences from the steps.rc by evaluating the needs of all steps
500 influences=`grep $step $steps | grep "Needs" | grep -v "$step[.]Needs" | cut -d'.' -f1`
501
502 # get query
503 query=" UPDATE "$step"Status "
504 # add joins to the influenced tables
505 specialinfljoin=`getjoin $step "SpecialInfluence"`
506 if ! [ "$specialinfljoin" = "" ]
507 then
508 query=$query" "$specialinfljoin
509 fi
510 for influence in $influences
511 do
512 infljoin=`getjoin $influence "Influence"`
513 if [ "$infljoin" = "" ]
514 then
515 inflprims=( `getprimary $influence` )
516 query=$query" LEFT JOIN "$influence"Status USING (${inflprims[@]}) "
517 else
518 query=$query" LEFT JOIN "$influence"Status USING ("$infljoin") "
519 fi
520 specialinfljoin2=`getjoin $influence "SpecialInfluence"`
521 if ! [ "$specialinfljoin2" = "" ]
522 then
523 query=$query" "$specialinfljoin2
524 fi
525 done
526 # set the status values according to the new status of the step
527 query=$query" SET "
528 if ! [ "$starttime" = "noreset" ]
529 then
530 query=$query" "$step"Status.fStartTime=$starttime, "
531 fi
532 query=$query" "$step"Status.fStopTime=$stoptime, "$step"Status.fReturnCode=$returncode "
533 # set also the status values of the influenced steps
534 for influence in $influences
535 do
536 query=$query", "$influence"Status.fStartTime=NULL "
537 query=$query", "$influence"Status.fStopTime=NULL "
538 query=$query", "$influence"Status.fReturnCode=NULL "
539 done
540 # give the condition for which step the status values have to be set
541 query=$query" WHERE "
542 if [ "$s" = "" ]
543 then
544 s=0
545 fi
546 query=$query" "$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}]}'"
547 for (( j=1 ; j < ${#prims[@]} ; j++ ))
548 do
549 query=$query" AND "$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}+$j]}' "
550 done
551 # print query
552 #echo " setstatus QUERY: "$query
553 printprocesslog "INFO setstatus QUERY: "$query
554 # execute query
555 if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
556 then
557 printprocesslog "ERROR could not set status in db (program: $program, function setstatus)"
558 finish
559 fi
560}
561
562# function to send a mysql query
563function sendquery()
564{
565 getdbsetup
566 printprocesslog "INFO sendquery QUERY: "$query
567 if ! val=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
568 then
569 printprocesslog "ERROR could not query db (program: $program, function sendquery)"
570 return 1
571 fi
572 if [ "$val" = "NULL" ]
573 then
574 val=
575 fi
576 echo $val
577 return 0
578}
579
Note: See TracBrowser for help on using the repository browser.