irods.sh 3.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

# LECA Bash library
#
# The LECA bash library provides a set of function used for helping
# development of bash script mainly to write job script on the luke
# cluster
#
# The files from the LECA Bash library must be sourced from your main script
#
#

include logging
logdebug "Load irods package"

LECABASHLIB_MAXTRIAL="5"

#
# Look for the iRods commands on the system
#
if ! which iget 2>&1 > /dev/null ; then
	if module 2> /dev/null ; then
22
	  loginfo "Loading the iRods module"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	  module load irods
	fi
fi

if  ! which iget 2>&1 > /dev/null ; then
	logerror "No iRods commands available"
fi

#
# Switch between the secure and the none secure iCommands
#
# --> if the bash function secure_iget is define we use it
#     otherwise we use the classical iget
#
if type secure_iget 1> /dev/null; then
	LECABASHLIB_IGET="secure_iget"
39
	loginfo "Activating secure icommand for iget"
40 41 42 43 44 45 46 47 48 49
else
	LECABASHLIB_IGET="iget"
fi

#
# Same thing for iput
#

if type secure_iput 1> /dev/null; then
	LECABASHLIB_IPUT="secure_iput"
50
	loginfo "Activating secure icommand for iput"
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
else
	LECABASHLIB_IPUT="iput"
fi


#
# Checks between md5sum and md5 is the command to run
# for computing a MD5 checksum
#
if ! which md5  2>&1 > /dev/null ; then
	if ! which md5sum  2>&1 > /dev/null ; then
		logerror "No md5 commands available"
	else
		LECABASHLIB_MD5SUM=md5sum
	fi
else
	LECABASHLIB_MD5SUM=md5
fi

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#
# isetsecure
#

function isetsecure() {
  LECABASHLIB_IGET=$( echo $LECABASHLIB_IGET | sed "s/^iget/secure_iget/" )
  LECABASHLIB_IPUT=$( echo $LECABASHLIB_IPUT | sed "s/^iput/secure_iput/" )
  loginfo "Activating secure icommand"
}

#
# isetsecure
#

function iunsetsecure() {
  LECABASHLIB_IGET=$( echo $LECABASHLIB_IGET | sed "s/^secure_iget/iget/" )
  LECABASHLIB_IPUT=$( echo $LECABASHLIB_IPUT | sed "s/^secure_iput/iput/" )
  loginfo "Desactivating secure icommand"
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#
# isetresource :
#
function isetresource() {
	if [[ ! -z "${1}" ]]; then
		local IRESOURCE="${1}"
		LECABASHLIB_IPUT="iput -R ${IRESOURCE}"
	else
		if type secure_iput 1> /dev/null; then
			LECABASHLIB_IPUT="secure_iput"
		else
			LECABASHLIB_IPUT="iput"
		fi
	fi
}

function isetmaxtrial() {
	if [[ ! -z "${1}" ]]; then
		LECABASHLIB_MAXTRIAL=$(( $1 + 0 ))
		if (( LECABASHLIB_MAXTRIAL )) ; then
			logwarning "Max trails set to a no-sens value reset to 5"
			LECABASHLIB_MAXTRIAL="5"
		fi
	else
		LECABASHLIB_MAXTRIAL="5"
	fi
}

function iresourceexists() {
  local rep=1
120
  ils "$1" 2> /dev/null >/dev/null
121 122

  if (( $? == 0 )) ; then
123
  	logdebug "iRods resource : $1 exists"
124
    rep=0
125 126
  else
   	logdebug "iRods resource : $1 absents"
127 128 129 130 131 132 133 134 135 136 137 138 139 140
  fi

  return $rep
}


function igetwithmd5() {

	local SOURCE=$1
	shift

	if [[ ! -z "${1}" ]]; then
		local TARGET=$1
	else
141
		local TARGET=$(basename $SOURCE)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	fi

	if ! iresourceexists "$SOURCE" ; then
		logerror "The requested iRods resource does not exit : $SOURCE"
		return 1
	fi

    local TRIAL=0
    local LMD5="XXXXXX"
    local IMD5="YYYYYY"
    local IROD_STATUS=1

    while [[ "${LMD5}" != "${IMD5}" ]] && (($TRIAL < $LECABASHLIB_MAXTRIAL)); do

	    TRIAL=$((TRIAL+1))

	    "${LECABASHLIB_IGET}" "${SOURCE}" "${TARGET}"
159
	    
160 161
	    HAS_MD5=0
	    "${LECABASHLIB_IGET}" "${SOURCE}.md5" "${TARGET}.md5" && HAS_MD5=1
162 163
	    
	    if (( HAS_MD5 == 1 ))
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	    then
            LMD5=$(${LECABASHLIB_MD5SUM} ${TARGET} | awk '{print $1}')
            IMD5=$(cat ${TARGET}.md5 | awk '{print $1}')
	    else
            logwarning "no MD5 available on iRods"
            IMD5="NO-MD5"
            LMD5="NO-MD5"
	    fi
	    if [[ "${LMD5}" != "${IMD5}" ]] && (($TRIAL < $LECABASHLIB_MAXTRIAL)); then
            logwarning "MD5 doesn't match; re-load file from iRods"
	    fi

    done

    rm -f "${TARGET}.md5"

    if [[ "${LMD5}" != "${IMD5}" ]]; then
        logerror "MD5 doesn't match; re-load file from iRods"
        return 1
    fi

    return 0

}

function iputwithmd5() {

echo Not yet implemented

}