Commit a1e2dc82 by Eric Coissac

First version of the mutex package.

parent 32d11dcb
......@@ -13,27 +13,27 @@
#
# Provided function:
#
# - download <URL> [USERNAME] [PASSWORD]
# Downloads the file specified by URL.
# If specifed the second (USERNAME) and third (PASSWORD) parametters allow
# to indicate a login and password for a protected connection
# - download <URL> [USERNAME] [PASSWORD]
# Downloads the file specified by URL.
# If specifed the second (USERNAME) and third (PASSWORD) parametters allow
# to indicate a login and password for a protected connection
#
# - downloadandcheck <URL> <MD5URL> <DESTINATION> [USERNAME] [PASSWORD]
# Downloads the file specified by URL and check file integrity.
# The MD5 checksum file URL is indicated using the MD5URL parametters.
# You must specified a destination filename to store the downloaded data
# using the DESTINATION parametter. The MD5 file is downloaded under the
# name DESTINATION.md5
# If specifed the fourth (USERNAME) and fifth (PASSWORD) parametters allow
# to indicate a login and password for a protected connection
# - downloadandcheck <URL> <MD5URL> <DESTINATION> [USERNAME] [PASSWORD]
# Downloads the file specified by URL and check file integrity.
# The MD5 checksum file URL is indicated using the MD5URL parametters.
# You must specified a destination filename to store the downloaded data
# using the DESTINATION parametter. The MD5 file is downloaded under the
# name DESTINATION.md5
# If specifed the fourth (USERNAME) and fifth (PASSWORD) parametters allow
# to indicate a login and password for a protected connection
#
# On error the function has a return status set to 1, 0 otherwise.
# On error the function has a return status set to 1, 0 otherwise.
#
# - setmaxretry <MAXRETRY>
# If downloading failed the downloadandcheck function can retry to download
# the file several times before a complete failure. This function allows to
# define the maximum of time the downloadandcheck tries to redownload before
# exiting in error
# - setmaxretry <MAXRETRY>
# If downloading failed the downloadandcheck function can retry to download
# the file several times before a complete failure. This function allows to
# define the maximum of time the downloadandcheck tries to redownload before
# exiting in error
#
#
......@@ -44,93 +44,93 @@ include logging
logdebug "Load download package"
if onluke; then
module load curl/7.24.0_gcc-4.6.2
module load openssl/1.0.0f_gcc-4.6.2
module load curl/7.24.0_gcc-4.6.2
module load openssl/1.0.0f_gcc-4.6.2
fi
CURL="curl -L"
LECA_MAX_RETRY=3
function download() {
logdebug "Function called with $# arguments"
logdebug "$@"
local URL="${1}"
shift
local LINK="${CURL} ${URL}"
if [[ ! -z $1 && -z $2 ]]; then
logdebug "Download with username ${1}"
local LINK="${CURL} --user ${1} ${URL}"
shift
fi
if [[ ! -z $1 && ! -z $2 ]]; then
logdebug "Download with username ${1} and password ${2}"
local LINK="${CURL} --user ${1}:${2} ${URL}"
shift
shift
fi
loginfo "Downloading URL : ${URL}"
${LINK}
loginfo "URL : ${URL} -> Done."
logdebug "Function called with $# arguments"
logdebug "$@"
local URL="${1}"
shift
local LINK="${CURL} ${URL}"
if [[ ! -z $1 && -z $2 ]]; then
logdebug "Download with username ${1}"
local LINK="${CURL} --user ${1} ${URL}"
shift
fi
if [[ ! -z $1 && ! -z $2 ]]; then
logdebug "Download with username ${1} and password ${2}"
local LINK="${CURL} --user ${1}:${2} ${URL}"
shift
shift
fi
loginfo "Downloading URL : ${URL}"
${LINK}
loginfo "URL : ${URL} -> Done."
}
function setmaxretry() {
LECA_MAX_RETRY="$1"
LECA_MAX_RETRY="$1"
}
function downloadandcheck() {
local URL="${1}"
shift
local MD5="${1}"
shift
local TARGETFILE="${1}"
shift
if [[ ! -z "${1}" ]]; then
local DOWLOADUSER="${1}"
shift
fi
if [[ ! -z "${1}" ]]; then
local PASSWORD="${1}"
shift
fi
local RETRY=0
local MD5_BAD=1
while (( MD5_BAD > 0 )) && (( RETRY < LECA_MAX_RETRY )); do
download "${URL}" "${DOWLOADUSER}" "${PASSWORD}" > "${TARGETFILE}"
download "${MD5}" "${DOWLOADUSER}" "${PASSWORD}" > "${TARGETFILE}.md5"
MD5LOCAL=$(md5sum "${TARGETFILE}" | awk '{print $1}')
MD5ORIGI=$(awk '{print $1}' "${TARGETFILE}.md5")
if [[ "$MD5LOCAL" == "$MD5ORIGI" ]]; then
MD5_BAD=0
else
MD5_BAD=1
fi
RETRY=$((RETRY+1))
if (( MD5_BAD > 0 )); then
logwarning "The two MD5 are different : $MD5LOCAL and $MD5ORIGI"
fi
done
if (( MD5_BAD > 0 )); then
logerror "The two MD5 are different after $RETRY retries: "
logerror " $MD5LOCAL and $MD5ORIGI"
return 1
else
loginfo "${TARGETFILE} downloaded"
loginfo " and checked with MD5 $MD5ORIGI"
return 0
fi
local URL="${1}"
shift
local MD5="${1}"
shift
local TARGETFILE="${1}"
shift
if [[ ! -z "${1}" ]]; then
local DOWLOADUSER="${1}"
shift
fi
if [[ ! -z "${1}" ]]; then
local PASSWORD="${1}"
shift
fi
local RETRY=0
local MD5_BAD=1
while (( MD5_BAD > 0 )) && (( RETRY < LECA_MAX_RETRY )); do
download "${URL}" "${DOWLOADUSER}" "${PASSWORD}" > "${TARGETFILE}"
download "${MD5}" "${DOWLOADUSER}" "${PASSWORD}" > "${TARGETFILE}.md5"
MD5LOCAL=$(md5sum "${TARGETFILE}" | awk '{print $1}')
MD5ORIGI=$(awk '{print $1}' "${TARGETFILE}.md5")
if [[ "$MD5LOCAL" == "$MD5ORIGI" ]]; then
MD5_BAD=0
else
MD5_BAD=1
fi
RETRY=$((RETRY+1))
if (( MD5_BAD > 0 )); then
logwarning "The two MD5 are different : $MD5LOCAL and $MD5ORIGI"
fi
done
if (( MD5_BAD > 0 )); then
logerror "The two MD5 are different after $RETRY retries: "
logerror " $MD5LOCAL and $MD5ORIGI"
return 1
else
loginfo "${TARGETFILE} downloaded"
loginfo " and checked with MD5 $MD5ORIGI"
return 0
fi
}
......@@ -17,31 +17,108 @@ include logging
logdebug "Load mutex package"
if onluke; then
include lecaluke
include lecaluke
else
LOCK_DIR="/tmp"
LOCK_DIR="/tmp"
fi
include clobber
LECA_LOCK_DELAY=2
LECA_LOCK_MAXRETRY=0
function lockfile() {
echo "${LOCK_DIR}/__${1}__.lecalock"
echo "${LOCK_DIR}/__${1}__.lecalock"
}
function locktag() {
echo "$(hostname).$$"
}
function getlock() {
local name=$(lockfile "$1")
shift
notgetlock=1
while (( notgetlock )); do
pushnoclobber
notgetlock=$?
if (( notgetlock )); then
sleep "${LECA_LOCK_DELAY}"
fi
done
cat "${TAG}" >
local name="$1"
shift
if [[ ! -z "${1}" ]]; then
mode="${1}"
if [[ "$mode" != "global" ]]; then
mode="user"
fi
else
mode="user"
fi
if [[ "${mode}" == "global" ]]; then
name=$(lockfile "${name}")
else
name=$(lockfile "${name}.$(whoami)")
fi
local TAG=$(locktag)
local retry=0
notgetlock=1
while (( notgetlock > 0 && ( LECA_LOCK_MAXRETRY == 0 || retry > LECA_LOCK_MAXRETRY) )); do
pushnoclobber
echo "${TAG}" 2>/dev/null 1> "${name}"
notgetlock=$?
popnoclobber
if (( notgetlock > 0 )); then
if [[ $(cat "${name}" 2> /dev/null) == "${TAG}" ]]; then
notgetlock=0
fi
fi
if (( notgetlock > 0 )); then
sleep "${LECA_LOCK_DELAY}"
retry=$((retry + 1))
fi
done
if (( notgetlock > 0 )); then
return $notgetlock
else
return 0
fi
}
function releaselock() {
local name="$1"
shift
if [[ ! -z "${1}" ]]; then
mode="${1}"
if [[ "$mode" != "global" ]]; then
mode="user"
fi
else
mode="user"
fi
if [[ "${mode}" == "global" ]]; then
name=$(lockfile "${name}")
else
name=$(lockfile "${name}.$(whoami)")
fi
local TAG=$(locktag)
local LOCK=""
if [[ -f "${name}" ]]; then
LOCK=$(cat "${name}" 2> /dev/null)
else
return 0
fi
if [[ "${LOCK}" == "${TAG}" ]]; then
rm -f "${name}"
return 0
fi
return 1
}
......@@ -50,7 +50,7 @@ if [[ -z "${LECALIB_UTIL_SH}" ]]; then
include logging
if onluke; then
loginfo Script run on the LUKE cluster
logdebug Script run on the LUKE cluster
source ${LECALIB_DIR}/../etc/bashrc
fi
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment