]> Creatis software - clitk.git/blob - cluster_tools/gate_common.sh
Debug RTStruct conversion with empty struc
[clitk.git] / cluster_tools / gate_common.sh
1
2 set -u
3
4 # print error message and exit immediately
5 programname="$(basename ${0})"
6 function error {
7     echo "${programname} **ERROR** $1"
8     exit 1
9 }
10
11 # ensure a valid proxy is present for at least one hour
12 # if no proxy is found, try to create a new one
13 # return 0 if a valid proxy is found or created
14 # else return voms-proxy-init error code
15 function ensure_proxy {
16     voms-proxy-info --exists -valid 1:0 > /dev/null && return 0 
17     voms-proxy-init --voms biomed -valid 24:00 || exit 1
18 }
19
20 # print prompt to ensure that the user want to continue further
21 # the user has to answer with 'y' to continue
22 function check_user {
23     prompt="${1:-is that correct?}"
24     read -p "${prompt} [y/n] " answer
25     test "${answer}" == "y" && return 0
26     test "${answer}" == "n" && return 1
27     check_user "${prompt}"
28 }
29
30 # checks if the lfn file exists
31 function file_exists {
32     lfnfile="${1:?"provide lfn to file"}"
33     lfc-ls ${lfnfile} 2>&1 > /dev/null
34 }
35
36
37 # upload file to grid storage element
38 # source can be a relative or an absolute path to the source file 
39 # dest must be the lfn to the target **file** (not the directory) 
40 # if dest already exists, it prompts the user for overwritting
41 function upload_file {
42     sourcefile=${1:?"provide source file"}
43     destlfn=${2:?"provide destination file lfn"}
44     sourcefile="$(readlink -f "${sourcefile}")" # convert to absolute path
45     test -f "${sourcefile}" || error "can't find ${sourcefile}"
46     echo "uploading ${sourcefile} to ${destlfn}"
47     if file_exists "${destlfn}"; then
48         check_user "${destlfn} already exists. overwrite it?" || return 2
49         lcg-del -a "lfn:${destlfn}" || error "lcg-del error"
50     fi
51     echo "lets roll"
52
53     local pending_ses=${SES}
54     local registered=false
55     while [ "x${pending_ses}" != "x" ]
56     do 
57         #select a se from list
58         local S=`echo ${pending_ses} | awk '{print $1}'`
59         #update list of SEs
60         local new_list=""
61         for i in `echo ${pending_ses}`
62         do
63             if [ "$i" != "${S}" ]
64             then
65                 new_list="${new_list} ${i}"
66             fi
67         done
68         pending_ses=${new_list}
69         local TEMP=`mktemp lcg-XXXXX`
70         if [ "${registered}" = "false" ]
71         then
72             echo -n "Registering release to ${S}..."
73             lcg-cr -v -d ${S} -l "lfn:${destlfn}" "file:${sourcefile}" &>${TEMP}
74             if [ $? != 0 ]
75             then
76                 echo -e "\t [\033[31m  FAILED  \033[0m]"
77                 cat ${TEMP}
78                 \rm ${TEMP}
79             else
80                 echo -e "\t [\033[32m  OK  \033[0m]"
81                 registered=true
82                 \rm ${TEMP}
83             fi
84         else
85             echo -n "Replicating release to ${S}..."
86             lcg-rep -v -d ${S} "lfn:${destlfn}" &>${TEMP}
87             if [ $? != 0 ]
88             then
89                 echo -e "\t [\033[31m  FAILED  \033[0m]"
90                 cat ${TEMP}
91                 \rm ${TEMP}
92             else
93                 echo -e "\t [\033[32m  OK  \033[0m]"
94                 \rm ${TEMP}
95             fi
96         fi
97     done
98 }
99
100
101 # common path used
102 lfnbase="/grid/biomed/creatis/fgate/"
103 lfnrelease="/grid/biomed/creatis/vip/data/groups/GateLab/releases/"
104 lfnworkflow="${lfnbase}workflow/"
105 lfngasw="${lfnbase}gasw/"
106 lfnscript="${lfnbase}bin/"
107
108 #list of SEs used for storage. Don't modify this list unless you know
109 # what you're doing. Replicating the release in a bad place (e.g. on a
110 # remote continent) can dramatically slow down the transfers
111 SES="ccsrm02.in2p3.fr sbgse1.in2p3.fr marsedpm.in2p3.fr"
112
113 # define the prefix for uploaded file
114 # default to local machine username
115 prefix="${USER:?"USER must be set"}_"