2 #------------------------------------------------------------------
4 # Purpose: A batch worker script for starting a sam project.
8 # condor_start_project.sh [options]
10 # --sam_user <arg> - Specify sam user (default $GRID_USER).
11 # --sam_group <arg> - Specify sam group (required).
12 # --sam_station <arg> - Specify sam station (required).
13 # --sam_defname <arg> - Sam dataset definition name (required).
14 # --sam_project <arg> - Sam project name (required).
15 # --logdir <arg> - Specify log directory (optional).
16 # -g, --grid - No effect (allowed for compatibility).
17 # --recur - Recursive input dataset (force snapshot).
18 # --init <path> - Absolute path of environment initialization script (optional).
19 # --max_files <n> - Specify the maximum number of files to include in this project.
20 # --prestage_fraction - Fraction of files that should be prestaged.
21 # Specify as floating point number between 0 and 1.
25 # Created: H. Greenlee, 29-Aug-2012
27 #------------------------------------------------------------------
43 while [ $# -gt 0 ]; do
48 awk '/^# Usage:/,/^# End options/{print $0}' $0 | cut -c3- | head -n -2
76 # Sam dataset definition name.
109 # Specify environment initialization script path.
111 if [ $# -gt 1 ]; then
117 # Specify the maximum number of files for this project.
119 if [ $# -gt 1 ]; then
125 # Specify fraction of files that should be prestaged.
126 --prestage_fraction )
127 if [ $# -gt 1 ]; then
135 echo "Unknown option $1"
141 # Done with arguments.
143 echo "Nodename: `hostname`"
145 # Check and print configuraiton options.
147 echo "Sam user: $SAM_USER"
148 echo "Sam group: $SAM_GROUP"
149 echo "Sam station: $SAM_STATION"
150 echo "Sam dataset definition: $SAM_DEFNAME"
151 echo "Sam project name: $SAM_PROJECT"
152 echo "Recursive flag: $RECUR"
153 echo "Prestage fraction: $PRESTAGE_FRACTION"
155 # Complain if SAM_STATION is not defined.
157 if [ x$SAM_STATION = x ]; then
158 echo "Sam station was not specified (use option --sam_station)."
162 # Complain if SAM_GROUP is not defined.
164 if [ x$SAM_GROUP = x ]; then
165 echo "Sam group was not specified (use option --sam_group)."
169 # Complain if SAM_DEFNAME is not defined.
171 if [ x$SAM_DEFNAME = x ]; then
172 echo "Sam dataset was not specified (use option --sam_defname)."
176 # Complain if SAM_PROJECT is not defined.
178 if [ x$SAM_PROJECT = x ]; then
179 echo "Sam project name was not specified (use option --sam_project)."
183 # Initialize ups products and mrb.
185 echo "Initializing ups and mrb."
187 if [ x$INIT != x ]; then
188 if [ ! -f $INIT ]; then
189 echo "Environment initialization script $INIT not found."
192 echo "Sourcing $INIT"
195 echo "Sourcing setup_experiment.sh"
196 source ${CONDOR_DIR_INPUT}/setup_experiment.sh
199 echo PRODUCTS=$PRODUCTS
201 # Ifdh may already be setup by jobsub wrapper.
202 # If not, set it up here.
204 echo "IFDHC_DIR=$IFDHC_DIR"
205 if [ x$IFDHC_DIR = x ]; then
206 echo "Setting up ifdhc, because jobsub did not set it up."
209 echo "IFDHC_DIR=$IFDHC_DIR"
211 # Setup sam_web_client, if not already done.
213 if [ x$SAM_WEB_CLIENT_DIR = x ]; then
214 echo "Setting up sam_web_client."
217 echo "SAM_WEB_CLIENT_DIR=$SAM_WEB_CLIENT_DIR"
219 # Set options for ifdh.
221 echo "IFDH_OPT=$IFDH_OPT"
223 # Create the scratch directory in the condor scratch diretory.
224 # Copied from condor_lBdetMC.sh.
225 # Scratch directory path is stored in $TMP.
226 # Scratch directory is automatically deleted when shell exits.
228 # Do not change this section.
229 # It creates a temporary working directory that automatically cleans up all
230 # leftover files at the end.
231 TMP=`mktemp -d ${_CONDOR_SCRATCH_DIR}/working_dir.XXXXXXXXXX`
232 TMP=${TMP:-${_CONDOR_SCRATCH_DIR}/working_dir.$$}
234 { [[ -n "$TMP" ]] && mkdir -p "$TMP"; } || \
235 { echo "ERROR: unable to create temporary directory!" 1>&2; exit 1; }
236 trap "[[ -n \"$TMP\" ]] && { rm -rf \"$TMP\"; }" 0
238 # End of the section you should not change.
240 echo "Scratch directory: $TMP"
242 # Save the project name in a file.
244 echo $SAM_PROJECT > sam_project.txt
246 # Test whether project is already started.
248 samweb project-summary $SAM_PROJECT > /dev/null 2> /dev/null
251 # Following section only if project is not already started.
253 if [ $started -ne 0 ]; then
255 # Do some preliminary tests on the input dataset definition.
256 # If dataset definition returns zero files at this point, abort the job.
257 # If dataset definition returns too many files compared to --max_files, create
258 # a new dataset definition by adding a "with limit" clause.
260 nf=`ifdh translateConstraints "defname: $SAM_DEFNAME" | wc -l`
261 if [ $nf -eq 0 ]; then
262 echo "Input dataset $SAM_DEFNAME is empty."
265 echo "Input dataset contains $nf files."
267 if [ $MAX_FILES -ne 0 -a $nf -gt $MAX_FILES ]; then
268 limitdef=${SAM_PROJECT}_limit_$MAX_FILES
270 # Check whether limit def already exists.
271 # Have to parse command output because ifdh returns wrong status.
273 existdef=`ifdh describeDefinition $limitdef 2>/dev/null | grep 'Definition Name:' | wc -l`
274 if [ $existdef -gt 0 ]; then
275 echo "Using already created limited dataset definition ${limitdef}."
277 ifdh createDefinition $limitdef "defname: $SAM_DEFNAME with limit $MAX_FILES" $SAM_USER $SAM_GROUP
279 # Assume command worked, because it returns the wrong status.
281 echo "Created limited dataset definition ${limitdef}."
284 # If we get to here, we know that we want to user $limitdef instead of $SAM_DEFNAME
285 # as the input sam dataset definition.
287 SAM_DEFNAME=$limitdef
291 # If recursive flag, force snapshot of input dataset.
293 forcedef=$SAM_DEFNAME
294 if [ $RECUR -ne 0 ]; then
295 echo "Forcing snapshot"
296 forcedef=${SAM_DEFNAME}:force
301 echo "Starting project ${SAM_PROJECT}."
302 ifdh startProject $SAM_PROJECT $SAM_STATION $forcedef $SAM_USER $SAM_GROUP
303 if [ $? -eq 0 ]; then
304 echo "Project successfully started."
307 echo "Start project error status $?"
311 # Create a dataset definition corresponding to the project snapshot.
313 snapshotdefname=snapshot_${SAM_PROJECT}
314 ifdh createDefinition $snapshotdefname "snapshot_for_project_name $SAM_PROJECT" $SAM_USER $SAM_GROUP
315 if [ $? -ne 0 ]; then
316 echo "Failed to create snapshot dataset definition ${snapshotdefname}."
319 echo "Created snapshot dataset definition ${snapshotdefname}."
321 # Check the project snapshot.
324 if [ $started -eq 0 ]; then
325 nf=`ifdh translateConstraints "defname: $snapshotdefname" | wc -l`
326 echo "Project snapshot contains $nf files."
329 # Abort if snapshot contains zero files. Stop project and eventually exit with error status.
331 if [ $started -eq 0 -a $nf -eq 0 ]; then
332 echo "Stopping project."
334 PURL=`ifdh findProject $SAM_PROJECT $SAM_STATION`
335 if [ x$PURL != x ]; then
336 echo "Project url: $PURL"
337 ifdh endProject $PURL
338 if [ $? -eq 0 ]; then
339 echo "Project successfully stopped."
344 # Calculate the number of files to prestage.
346 npre=`echo "$PRESTAGE_FRACTION * $nf / 1" | bc`
347 echo "Will attempt to prestage $npre files."
349 # If number of prestage files is greater than zero, do prestage here.
351 if [ $npre -gt 0 ]; then
353 # Generate name of prestage project.
354 # Here we use a safe name that won't drain recursive datasets (unlike "samweb prestage-dataset").
356 prjname=prestage_${SAM_PROJECT}
357 echo "Prestage project: $prjname"
359 # Start prestage project.
361 ifdh startProject $prjname $SAM_STATION ${snapshotdefname}:latest $SAM_USER $SAM_GROUP
362 if [ $? -ne 0 ]; then
363 echo "Failed to start prestage project."
366 echo "Prestage project started."
368 # Get prestage project url.
370 prjurl=`ifdh findProject $prjname $SAM_STATION`
371 if [ x$prjurl = x ]; then
372 echo "Unable to find url for project ${prjname}."
376 # Start consumer process.
382 # Make description, which is conventionally the jobsub job id.
383 # This can not be empty.
386 if [ x$DESC = x ]; then
390 echo "Starting consumer process."
391 cpid=`ifdh establishProcess $prjurl $appname 1 $node $SAM_USER $appfamily $DESC $npre`
392 if [ x$cpid = x ]; then
393 echo "Unable to start consumer process for project ${prjname}."
396 echo "Prestage consumer process ${cpid} started."
404 # When this command returns, the file is prestaged.
406 fileurl=`ifdh getNextFile $prjurl $cpid`
407 if [ $? -ne 0 -o x$fileurl = x ]; then
408 echo "No more files."
409 echo "$n files prestaged."
412 filename=`basename $fileurl`
413 echo "$filename is prestaged."
418 ifdh updateFileStatus $prjurl $cpid $filename consumed
422 # End consumer process.
424 ifdh endProcess $prjurl $cpid
425 echo "Prestage consumer process stopped."
427 # End prestage project.
429 ifdh endProject $prjurl
430 echo "Prestage project stopped."
434 # Stash all of the files we want to save in a local
435 # directory with a unique name. Then copy this directory
436 # and its contents recursively.
438 if [ x$LOGDIR != x ]; then
439 LOGDIR=`echo $LOGDIR | sed 's/@s/sam/'`
440 OUTPUT_SUBDIR=${CLUSTER}_start
443 if [ $outfile != $OUTPUT_SUBDIR ]; then
444 mv $outfile $OUTPUT_SUBDIR
447 echo "ifdh cp -r $IFDH_OPT $OUTPUT_SUBDIR ${LOGDIR}/$OUTPUT_SUBDIR"
448 ifdh cp -r $IFDH_OPT $OUTPUT_SUBDIR ${LOGDIR}/$OUTPUT_SUBDIR
449 if [ $? -ne 0 ]; then
450 echo "ifdh cp failed with status ${stat}."
455 # Done. Set exit status to reflect whether project was started (0=success).