Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit 9cc1805f authored by Jonathan Schaeffer's avatar Jonathan Schaeffer
Browse files

Factorisation du code

parent 5559f2ad
......@@ -17,12 +17,39 @@ zabbix(){
zabbix_sender -k resifdatadump.failed -s $(hostname -a) -o $1 -z monitoring.osug.fr
}
# Push data to irods as a staging file
# Argument is the distant directory to push to.
# If something goes wrong, roll back
# If everything goes fine, then validate staging data
irods_push(){
KEY=$1
SIZE=$(stat -c %s $RESIFDD_WORKDIR/$KEY.tar)
SIZEMB=$(($SIZE/1024/1024))
echo "[$KEY] Sending data to iRODS ($SIZE MB)"
imkdir -p $KEY
if [[ $? -ne 0 ]]; then
echo "[$KEY] Error 002 creating remote directory. Manual action has to be taken."
zabbix "${KEY}:Error 002"
exit 1
fi
COMMAND="iput --retries 5 -T -K -f -X $RESIFDD_WORKDIR/${KEY}.restart $RESIFDD_WORKDIR/${KEY}.tar $KEY/staging.tar"
eval $COMMAND
if [[ $? -ne 0 ]]; then
echo "[$KEY] Error 011 sending file to irods. The command was: $COMMAND"
# Roll back
irods_rollback $KEY
# Alert to zabbix
zabbix "${KEY}:Error 011"
else
irods_commit $KEY
fi
}
# In case of any problem, this function rolls every distant file operation bask
# 1. tries to revover latest.tar from previous.tar
# 2. tries to recover previous.tar from previous_to_delete.tar
# Argument is the distant directory to work with
irods_rollback(){
KEY=$1
echo "[$KEY] Rollback : try to recover latest.tar from previous"
......@@ -46,6 +73,41 @@ irods_rollback(){
return 0
}
# When the transfer to irods is successfull, we can move file arounds remotely :
# 1. $KEY/previous.tar move to trash
# 2. $KEY/latest.tar move to $KEY/previous.tar
# 3. $KEY/staging.tar move to $KEY/latest.tar
irods_commit(){
KEY=$1
echo "[$KEY] If previous.tar exists, move it around but keep it safe"
ils $KEY/previous.tar 2>/dev/null && (
imv $KEY/previous.tar $KEY/previous_to_delete.tar
if [[ $? -ne 0 ]]; then
echo "[$KEY] Error 003 moving previous.tar around. Corrective action has to be taken manualy"
zabbix "${KEY}:Error 003"
return 1
fi
)
echo "[$KEY] If latest.tar exists, move it to previous.tar"
ils $KEY/latest.tar 2>/dev/null && (
imv $KEY/latest.tar $KEY/previous.tar
if [[ $? -ne 0 ]]; then
echo "[$KEY] Error 004 moving latest.tar to previous.tar. Corrective action has to be taken manualy"
zabbix "${KEY}:Error 004"
return 1
fi
)
echp "[$KEY] Validate staging.tar by moving it to staging.tar"
imv $KEY/staging.tar $KEY/latest.tar
if [[ $? -ne 0 ]]; then
echo "[$KEY] Error 005 moving statging.tar to latest.tar. Corrective action has to be taken manualy"
zabbix "${KEY}:Error 005"
return 1
fi
ils $KEY/previous_to_delete.tar 2>/dev/null && irm -f $KEY/previous_to_delete.tar
}
####################
#
# Preliminary tests
......@@ -81,54 +143,27 @@ if [[ ! -d $SNAPSHOT_DIR ]]; then
exit 1
fi
echo "[METADATA] Starting dump from ${SNAPSHOT_DIR}"
tar cf $RESIFDD_WORKDIR/validated_seismic_metadata.tar --exclude portalproducts -C $SNAPSHOT_DIR $SNAPSHOT_DIR
if [[ $? -ne 0 ]]; then
echo "[METADATA] Error 001 while creating tar archive. Exit 1"
exit 1
fi
SIZE=$(stat -c %s $RESIFDD_WORKDIR/$KEY.tar)
SIZEGB=$(( $SIZE / 1073741824 ))
KEY="validated-seismic-metadata"
# Creating remote directory if not exist
imkdir -p validated_seismic_metadata
echo "[$KEY] Starting dump from ${SNAPSHOT_DIR}"
tar cf $RESIFDD_WORKDIR/$KEY.tar --exclude portalproducts -C $SNAPSHOT_DIR $SNAPSHOT_DIR
if [[ $? -ne 0 ]]; then
echo "[METADATA] Error 002 creating remote directory validated_seismic_metadata. Exit 1"
echo "[$KEY] Error 001 while creating tar archive."
zabbix "${KEY}:Error 001"
exit 1
fi
echo "[METADATA] If previous.tar exists, move it around but keep it safe"
ils validated_seismic_metadata/previous.tar 2>/dev/null && (
imv validated_seismic_metadata/previous.tar validated_seismic_metadata/previous_to_delete.tar
if [[ $? -ne 0 ]]; then
echo "[METADATA] Error 003 moving previous.tar around Exit 1"
exit 1
fi
)
echo "[METADATA] If latest.tar exist, move it to previous.tar"
ils validated_seismic_metadata/latest.tar 2>/dev/null && (
imv validated_seismic_metadata/latest.tar validated_seismic_metadata/previous.tar
if [[ $? -ne 0 ]]; then
echo "[METADATA] Error 004 moving latest.tar to previous.tar Exit 1"
exit 1
fi
)
echo "Push metadata archive ($SIZEGB GB)"
iput -X $RESIFDD_WORKDIR/irods_restart --retries 5 -K -T -f $RESIFDD_WORKDIR/validated_seismic_metadata.tar validated_seismic_metadata/latest.tar
if [[ $? -ne 0 ]]; then
echo "[METADATA] Error 005 Something went wrong pushing validated metadata, Roll back"
irods_rollback validated_seismic_metadata
fi
# Cleaning
irm validated_seismic_metadata/previous_to_delete.tar
rm $RESIFDD_WORKDIR/validated_seismic_metadata.tar
echo "[METADATA] Dump terminated :"
ils -l validated_seismic_metadata
irods_push $KEY
echo "[$KEY] Cleaning workdir $RESIFDD_WORKDIR"
rm -f $RESIFDD_WORKDIR/$KEY.tar $RESIFDD_WORKDIR/${KEY}.restart
echo "[$KEY] Dump terminated :"
ils -l $KEY
####################
#
# Start dumping validated data
#
####################
SNAPSHOT_DIR="${RESIFDD_DATADIR}/validated_seismic_data/.snapshot/monthly.${MONTH}-01_0000"
if [[ ! -d $SNAPSHOT_DIR ]]; then
......@@ -136,17 +171,11 @@ if [[ ! -d $SNAPSHOT_DIR ]]; then
exit 1
fi
####################
#
# Start dumping validated data
#
####################
echo "Starting dump of validated data"
cd $SNAPSHOT_DIR
for dir in $(find . -maxdepth 3 -type d | sort); do
# First clean workspace
rm -f $RESIFDD_WORKDIR/*.tar
rm -rf $RESIFDD_WORKDIR/*
# Parse path to get year, station and network
IFS='/' read -r -a YNS <<< $dir
[ ! ${#YNS[@]} -eq 4 ] && continue
......@@ -160,69 +189,26 @@ for dir in $(find . -maxdepth 3 -type d | sort); do
# Something went wrong creating archive. Exit
echo "[$KEY] Error 007 creating tar"
# Send key to zabbix
zabbix $KEY
zabbix "$KEY:Error 007"
continue
fi
SIZE=$(stat -c %s $RESIFDD_WORKDIR/$KEY.tar)
SIZEGB=$(( $SIZE /1073741824 ))
# Check if file exists on irods server
ils -L $KEY/latest.tar > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
# Latest archive exist, lets move it to previous.
echo "[$KEY] latest.tar already exists on iRODS server. Let's compare hashes"
local_sha=$(sha256sum $RESIFDD_WORKDIR/$KEY.tar | awk '{print $1}' | xxd -r -p | base64)
irods_sha=$(ichksum $KEY/latest.tar | awk -F':' '/sha2:/ {print $2; exit;}')
echo "[$KEY] local checksum: $local_sha"
echo "[$KEY] irods checksum: $irods_sha"
# If the hashes differs, then move distant file and push this one
if [[ "$local_sha" = "$irods_sha" ]]; then
echo "[$KEY] The archive on irods is the same as our version. Skipping."
continue
fi
echo "[$KEY] local checksum: $local_sha"
echo "[$KEY] idos checksum: $irods_sha"
echo "[$KEY] Local checksum differs from last dump. Let's dump it"
# Put previous.tar aside in case we need to revover it.
ils $KEY/previous.tar 2>/dev/null && (
irm -f $KEY/previous_to_delete.tar
imv $KEY/previous.tar $KEY/previous_to_delete.tar
if [[ $? -ne 0 ]]; then
echo "[$KEY] Error 008 putting previous.tar aside. $KEY archive will not be sent"
zabbix $KEY
continue
fi
)
imv $KEY/latest.tar $KEY/previous.tar # Rename latest distant dump tp "previous"
if [[ $? -ne 0 ]]; then
# Something wrong moving remote file arround
echo "[$KEY] Error 009 something wrong moving $KEY/previous.tar, cancel iput."
ils $KEY/previous_to_delete.tar 2>/dev/null && (
imv $KEY/previous_to_delete.tar $KEY/previous.tar
if [[ $? -ne 0 ]]; then
echo "[KEY] Error 010 restoring previous.tar"
fi
)
zabbix $KEY
continue
fi
fi
# Send latest archive file to IRODS
echo "[$KEY] Sending latest.tar to iRODS ($SIZEGB GB)"
imkdir -p $KEY
# Décommenter le bloc et les redirections pour traiter stdout et stderr différemment
iput \
--retries 5 -T -K -f -X $RESIFDD_WORKDIR/irods_restart \
$RESIFDD_WORKDIR/$KEY.tar $KEY/latest.tar
if [[ $? -ne 0 ]]; then
echo "[$KEY] Error 011 sending file to irods. The command was: iput --lfrestart $RESIFDD_WORKDIR/$KEY.lfrestart --retries 5 -T -K -f $RESIFDD_WORKDIR/$KEY.tar $KEY/latest.tar"
# Roll back
irods_rollback $KEY
# Alert to zabbix
zabbix $KEY
fi
# Cleaning up the safety previous file
irm -f $KEY/previous_to_delete.tar 2>/dev/null
echo "[$KEY] Sent $SIZE bytes successfully"
# TODO Checksums check
echo "[$KEY] Cleaning local generated tar"
rm -f $RESIFDD_WORKDIR/$KEY.tar $RESIFDD_WORKDIR/irods_restart
irods_push $KEY
echo "[$KEY] Cleaning workdir $RESIFDD_WORKDIR"
rm -f $RESIFDD_WORKDIR/$KEY.tar $RESIFDD_WORKDIR/${KEY}.restart
done
echo "Dump of validated data done"
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