From aebecae1c3e6ebb0ab30efd2b86697226ac16440 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Mon, 8 Apr 2013 17:17:22 +0200 Subject: [PATCH] bug #1822: delete .isofiles on error in context script (cherry picked from commit 2fbfbee94a847e107c3f5fe8d90c77001e5379d9) --- src/mad/sh/scripts_common.sh | 22 ++++++++++++++++++++++ src/tm_mad/common/context | 24 +++++++++++++++++++----- src/tm_mad/vmfs/context | 27 +++++++++++++++++++++------ 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/mad/sh/scripts_common.sh b/src/mad/sh/scripts_common.sh index 2e4d30489d..53303918e3 100644 --- a/src/mad/sh/scripts_common.sh +++ b/src/mad/sh/scripts_common.sh @@ -140,6 +140,28 @@ function exec_and_log fi } +# Like exec_and_log but does not exit on failure. Just sets the variable +# ERROR to the error message. +function exec_and_set_error +{ + message=$2 + + EXEC_LOG_ERR=`$1 2>&1 1>/dev/null` + EXEC_LOG_RC=$? + + export ERROR="" + + if [ $EXEC_LOG_RC -ne 0 ]; then + log_error "Command \"$1\" failed: $EXEC_LOG_ERR" + + if [ -n "$2" ]; then + export ERROR="$2" + else + export ERROR="Error executing $1: $EXEC_LOG_ERR" + fi + fi +} + # Like exec_and_log but the first argument is the number of seconds # before here is timeout and kills the command # diff --git a/src/tm_mad/common/context b/src/tm_mad/common/context index 92a73faaff..f17acc3047 100755 --- a/src/tm_mad/common/context +++ b/src/tm_mad/common/context @@ -38,6 +38,13 @@ fi . $TMCOMMON +function exit_error +{ + error_message "$ERROR" + rm -rf $ISO_DIR > /dev/null 2>&1 + exit -1 +} + #------------------------------------------------------------------------------- # Set dst path and dirs #------------------------------------------------------------------------------- @@ -56,22 +63,29 @@ VM_ID=`basename $DST_DIR` ISO_DIR="$DS_DIR/.isofiles/$VM_ID" ISO_FILE="$ISO_DIR/$VM_ID.iso" -exec_and_log "mkdir -p $ISO_DIR" "Could not create tmp dir to make context dev" +exec_and_set_error "mkdir -p $ISO_DIR" \ + "Could not create tmp dir to make context dev" +[ -n "$ERROR" ] && exit_error for f in $SRC; do case $f in http://*) - exec_and_log "$WGET -P $ISO_DIR $f" "Error downloading $f" + exec_and_set_error "$WGET -P $ISO_DIR $f" "Error downloading $f" ;; *) - exec_and_log "cp -R $f $ISO_DIR" "Error copying $f to $ISO_DIR" + exec_and_set_error "cp -R $f $ISO_DIR" "Error copying $f to $ISO_DIR" ;; esac + + [ -n "$ERROR" ] && exit_error done -exec_and_log "$MKISOFS -o $ISO_FILE -J -R $ISO_DIR" "Error creating iso fs" +exec_and_set_error "$MKISOFS -o $ISO_FILE -J -R $ISO_DIR" \ + "Error creating iso fs" +[ -n "$ERROR" ] && exit_error -exec_and_log "$SCP $ISO_FILE $DST" "Error copying context ISO to $DST" +exec_and_set_error "$SCP $ISO_FILE $DST" "Error copying context ISO to $DST" +[ -n "$ERROR" ] && exit_error # Creates symbolic link to add a .iso suffix, needed for VMware CDROMs ssh_exec_and_log $DST_HOST "$LN -s $DST_PATH $DST_PATH.iso" "Error creating ISO symbolic link" diff --git a/src/tm_mad/vmfs/context b/src/tm_mad/vmfs/context index e219237702..fd8b0927e1 100755 --- a/src/tm_mad/vmfs/context +++ b/src/tm_mad/vmfs/context @@ -40,6 +40,13 @@ fi . $TMCOMMON +function exit_error +{ + error_message "$ERROR" + rm -rf $ISO_DIR > /dev/null 2>&1 + exit -1 +} + #------------------------------------------------------------------------------- # Retrieve needed information, set dst path and dir #------------------------------------------------------------------------------- @@ -73,30 +80,38 @@ log "Generating context block device at $DST" ISO_DIR="$DS_DIR/.isofiles/$VM_ID" ISO_FILE="$ISO_DIR/$VM_ID.iso" -exec_and_log "mkdir -p $ISO_DIR" "Could not create tmp dir to make context dev" +exec_and_set_error "mkdir -p $ISO_DIR" \ + "Could not create tmp dir to make context dev" +[ -n "$ERROR" ] && exit_error for f in $SRC; do case $f in http://*) - exec_and_log "$WGET -P $ISO_DIR $f" "Error downloading $f" + exec_and_set_error "$WGET -P $ISO_DIR $f" "Error downloading $f" ;; *) - exec_and_log "cp -R $f $ISO_DIR" "Error copying $f to $ISO_DIR" + exec_and_set_error "cp -R $f $ISO_DIR" "Error copying $f to $ISO_DIR" ;; esac + + [ -n "$ERROR" ] && exit_error done -exec_and_log "$MKISOFS -o $ISO_FILE -J -R $ISO_DIR" "Error creating iso fs" +exec_and_set_error "$MKISOFS -o $ISO_FILE -J -R $ISO_DIR" \ + "Error creating iso fs" +[ -n "$ERROR" ] && exit_error if [ "$USE_SSH" == "yes" ]; then - exec_and_log "$SCP $ISO_FILE $DST_PATH.iso" "Error copying context ISO to $DST" + exec_and_set_error "$SCP $ISO_FILE $DST_PATH.iso" "Error copying context ISO to $DST" else # Copies the iso file with .iso suffix, needed for VMware CDROMs vifs $VI_PARAMS -rm [$DS_ID]$VM_ID/$DST_FOLDER.iso - exec_and_log "vifs $VI_PARAMS -p $ISO_FILE [$DS_ID]$VM_ID/$DST_FOLDER.iso" \ + exec_and_set_error "vifs $VI_PARAMS -p $ISO_FILE [$DS_ID]$VM_ID/$DST_FOLDER.iso" \ "Error copying context ISO to [$DS_ID]$VM_ID/$DST_FOLDER.iso in $DST_HOST" fi +[ -n "$ERROR" ] && exit_error + rm -rf $ISO_DIR > /dev/null 2>&1 exit 0