condor_stop_project.sh
Go to the documentation of this file.
1 #! /bin/bash
2 #------------------------------------------------------------------
3 #
4 # Purpose: A batch worker script for stoping a sam project.
5 # This script should run after all workers finish.
6 #
7 # Usage:
8 #
9 # condor_stop_project.sh [options]
10 #
11 # --sam_station <arg> - Specify sam station (required).
12 # --sam_project <arg> - Sam project name (required).
13 # --logdir <arg> - Specify log directory (optional).
14 # -g, --grid - Be grid-friendly.
15 # --init <path> - Absolute path of environment initialization script (optional).
16 #
17 # End options.
18 #
19 # Created: H. Greenlee, 29-Aug-2012
20 #
21 #------------------------------------------------------------------
22 
23 # Parse arguments.
24 
25 SAM_STATION=""
26 SAM_PROJECT=""
27 LOGDIR=""
28 GRID=0
29 INIT=""
30 IFDH_OPT=""
31 
32 while [ $# -gt 0 ]; do
33  case "$1" in
34 
35  # Help.
36  -h|--help )
37  awk '/^# Usage:/,/^# End options/{print $0}' $0 | cut -c3- | head -n -2
38  exit
39  ;;
40 
41  # Sam station.
42  --sam_station )
43  if [ $# -gt 1 ]; then
44  SAM_STATION=$2
45  shift
46  fi
47  ;;
48 
49  # Sam project name.
50  --sam_project )
51  if [ $# -gt 1 ]; then
52  SAM_PROJECT=$2
53  shift
54  fi
55  ;;
56 
57  # Log directory.
58  --logdir )
59  if [ $# -gt 1 ]; then
60  LOGDIR=$2
61  shift
62  fi
63  ;;
64 
65  # Grid flag.
66  -g|--grid )
67  GRID=1
68  ;;
69 
70  # Specify environment initialization script path.
71  --init )
72  if [ $# -gt 1 ]; then
73  INIT=$2
74  shift
75  fi
76  ;;
77 
78  # Other.
79  * )
80  echo "Unknown option $1"
81  exit 1
82  esac
83  shift
84 done
85 
86 # Done with arguments.
87 
88 echo "Nodename: `hostname`"
89 
90 # Check and print configuraiton options.
91 
92 echo "Sam station: $SAM_STATION"
93 echo "Sam project name: $SAM_PROJECT"
94 
95 # Complain if SAM_STATION is not defined.
96 
97 if [ x$SAM_STATION = x ]; then
98  echo "Sam station was not specified (use option --sam_station)."
99  exit 1
100 fi
101 
102 # Complain if SAM_PROJECT is not defined.
103 
104 if [ x$SAM_PROJECT = x ]; then
105  echo "Sam project name was not specified (use option --sam_project)."
106  exit 1
107 fi
108 
109 # Initialize ups products and mrb.
110 
111 echo "Initializing ups and mrb."
112 
113 if [ x$INIT != x ]; then
114  if [ ! -f $INIT ]; then
115  echo "Environment initialization script $INIT not found."
116  exit 1
117  fi
118  echo "Sourcing $INIT"
119  source $INIT
120 else
121  echo "Sourcing setup_experiment.sh"
122  source ${CONDOR_DIR_INPUT}/setup_experiment.sh
123 fi
124 
125 echo PRODUCTS=$PRODUCTS
126 
127 # Ifdh may already be setup by jobsub wrapper.
128 # If not, set it up here.
129 
130 echo "IFDHC_DIR=$IFDHC_DIR"
131 if [ x$IFDHC_DIR = x ]; then
132  echo "Setting up ifdhc, because jobsub did not set it up."
133  setup ifdhc
134 fi
135 echo "IFDHC_DIR=$IFDHC_DIR"
136 
137 # Set options for ifdh.
138 
139 #if [ $GRID -ne 0 ]; then
140 # echo "X509_USER_PROXY = $X509_USER_PROXY"
141 # if ! echo $X509_USER_PROXY | grep -q Production; then
142 # IFDH_OPT="--force=expgridftp"
143 # fi
144 #fi
145 echo "IFDH_OPT=$IFDH_OPT"
146 
147 # Create the scratch directory in the condor scratch diretory.
148 # Copied from condor_lBdetMC.sh.
149 # Scratch directory path is stored in $TMP.
150 # Scratch directory is automatically deleted when shell exits.
151 
152 # Do not change this section.
153 # It creates a temporary working directory that automatically cleans up all
154 # leftover files at the end.
155 TMP=`mktemp -d ${_CONDOR_SCRATCH_DIR}/working_dir.XXXXXXXXXX`
156 TMP=${TMP:-${_CONDOR_SCRATCH_DIR}/working_dir.$$}
157 
158 { [[ -n "$TMP" ]] && mkdir -p "$TMP"; } || \
159  { echo "ERROR: unable to create temporary directory!" 1>&2; exit 1; }
160 trap "[[ -n \"$TMP\" ]] && { rm -rf \"$TMP\"; }" 0
161 cd $TMP
162 # End of the section you should not change.
163 
164 echo "Scratch directory: $TMP"
165 
166 # See if we need to set umask for group write.
167 
168 LOGDIR=`echo $LOGDIR | sed 's/@s/sam/'`
169 if [ $GRID -eq 0 -a x$LOGDIR != x ]; then
170  LOGUSER=`stat -c %U $LOGDIR`
171  CURUSER=`whoami`
172  if [ $LOGUSER != $CURUSER ]; then
173  echo "Setting umask for group write."
174  umask 002
175  fi
176 fi
177 
178 # Save the project name in a file.
179 
180 echo $SAM_PROJECT > sam_project.txt
181 
182 # Find project url.
183 
184 PURL=`ifdh findProject $SAM_PROJECT $SAM_STATION`
185 if [ x$PURL = x ]; then
186  echo "Unable to find url for project ${SAM_PROJECT}."
187  exit 1
188 else
189  echo "Project url: $PURL"
190 fi
191 
192 # Stop the project.
193 
194 echo "Stopping project."
195 ifdh endProject $PURL
196 if [ $? -eq 0 ]; then
197  echo "Project successfully stopped."
198 else
199  echo "Stop project error status $?"
200 fi
201 
202 # Stash all of the files we want to save in a local
203 # directory with a unique name. Then copy this directory
204 # and its contents recursively.
205 
206 if [ x$LOGDIR != x ]; then
207  OUTPUT_SUBDIR=${CLUSTER}_stop
208  mkdir $OUTPUT_SUBDIR
209  for outfile in *; do
210  if [ $outfile != $OUTPUT_SUBDIR ]; then
211  mv $outfile $OUTPUT_SUBDIR
212  fi
213  done
214  echo "ifdh cp -r $IFDH_OPT $OUTPUT_SUBDIR ${LOGDIR}/$OUTPUT_SUBDIR"
215  ifdh cp -r $IFDH_OPT $OUTPUT_SUBDIR ${LOGDIR}/$OUTPUT_SUBDIR
216  stat=$?
217  if [ $stat -ne 0 ]; then
218  echo "ifdh cp failed with status ${stat}."
219  exit $stat
220  fi
221 fi