]> Creatis software - clitk.git/blob - grid/common.sh
66203b6d3a68d55a8b067a2c2ae9a84e86bddc95
[clitk.git] / grid / 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 # upload file to grid storage element
37 # source can be a relative or an absolute path to the source file 
38 # dest must be the lfn to the target **file** (not the directory) 
39 # if dest already exists, it prompts the user for overwritting
40 function upload_file {
41 sourcefile=${1:?"provide source file"}
42 destlfn=${2:?"provide destination file lfn"}
43 sourcefile="$(readlink -f "${sourcefile}")" # convert to absolute path
44 test -f "${sourcefile}" || error "can't find ${sourcefile}"
45 echo "uploading ${sourcefile} to ${destlfn}"
46 file_exists "${destlfn}" \
47         && check_user "${destlfn} already exists. overwrite it?" \
48         && lcg-del -a "lfn:${destlfn}" \
49         || return 2
50 lcg-cr -v -d ccsrm02.in2p3.fr -l "lfn:${destlfn}" "file:${sourcefile}" 
51 }
52
53
54 # common path used
55 lfnbase="/grid/biomed/creatis/fgate/"
56 lfnrelease="${lfnbase}releases/"
57 lfnworkflow="${lfnbase}workflow/"
58 lfngasw="${lfnbase}gasw/"
59 lfnscript="${lfnbase}bin/"
60
61 # define the prefix for uploaded file
62 # default to local machine username
63 prefix="${USER:?"USER must be set"}_"