]> Creatis software - clitk.git/blob - grid/common.sh
wrote upload function
[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 file_exists ${destlfn} && ( \
46         check_user "${destlfn} already exists. overwrite it?" || return 2 && \
47         lcg-del -a "lfn:${destlfn}" || error "failed to delete ${destlfn}" \
48 ) && \
49 lcg-cr -v -d ccsrm02.in2p3.fr -l "lfn:${destlfn}" "file:${sourcefile}" 
50 }
51
52
53 # common path used
54 lfnbase="/grid/biomed/creatis/fgate/"
55 lfnrelease="${lfnbase}releases/"
56 lfnworkflow="${lfnbase}"
57 lfngasw="${lfnbase}gasw/"
58 lfnscript="${lfnbase}bin/"
59
60 # define the prefix for uploaded file
61 # default to local machine username
62 prefix="${USER:?"USER must be set"}_"