From 3f1ef07fdd62f9d6de12ee5c19a7dac532a2d893 Mon Sep 17 00:00:00 2001 From: Roger Luethi Date: Wed, 10 Sep 2014 12:19:15 +0200 Subject: [PATCH] labs: osbashauto exit on script error Make osbashout abort if a client-side scripts returns an error; use a file named error in the status directory to signal the problem and to store the name of the offending script for use by Windows batch scripts. Have osbash.sh and the Windows batch scripts exit when an error file appears. Change-Id: I6722e75c9c23b99e5ed25b00429026c2e0ff6a12 --- labs/lib/functions.guest | 10 ++++++++-- labs/lib/osbash/functions.host | 9 +++++++-- labs/lib/wbatch/template-end_file_bat | 12 ++++++++++++ labs/scripts/osbash/template-osbashauto | 8 ++++++-- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/labs/lib/functions.guest b/labs/lib/functions.guest index b7844735..c3bd25a3 100644 --- a/labs/lib/functions.guest +++ b/labs/lib/functions.guest @@ -127,8 +127,14 @@ function as_root_exec_script { local log_path=$LOG_DIR/${prefix}_$script_name.auto su - "$VM_SHELL_USER" -c "bash $script_path" >"$log_path" 2>&1 - - echo "$(date) done" + local rc=$? + if [ $rc -ne 0 ]; then + echo "$(date) ERROR: status $rc for $script_path" | + tee >&2 -a "$LOG_DIR/error.log" + else + echo "$(date) done" + fi + return $rc } #------------------------------------------------------------------------------- diff --git a/labs/lib/osbash/functions.host b/labs/lib/osbash/functions.host index 32ad4538..fb93ef77 100644 --- a/labs/lib/osbash/functions.host +++ b/labs/lib/osbash/functions.host @@ -187,7 +187,7 @@ function wait_for_autofiles { ${OSBASH:+:} autostart_reset ${OSBASH:+:} return 0 - until [ -f "$STATUS_DIR/done" ]; do + until [ -f "$STATUS_DIR/done" -o -f "$STATUS_DIR/error" ]; do # Note: begin files (created by indicate_current_auto) are only visible # if the STATUS_DIR directory is shared between host and VM ${WBATCH:-:} process_begin_files @@ -196,7 +196,12 @@ function wait_for_autofiles { done # Check for remaining *.sh.begin files ${WBATCH:-:} process_begin_files - rm "$STATUS_DIR/done" + if [ -f "$STATUS_DIR/done" ]; then + rm "$STATUS_DIR/done" + else + echo -e >&2 "\nERROR occured. Exiting." + kill -- -$$ + fi echo } diff --git a/labs/lib/wbatch/template-end_file_bat b/labs/lib/wbatch/template-end_file_bat index b81a84ee..ee732fe9 100644 --- a/labs/lib/wbatch/template-end_file_bat +++ b/labs/lib/wbatch/template-end_file_bat @@ -13,6 +13,7 @@ ECHO. :terminate ENDLOCAL PAUSE +EXIT GOTO :eof REM ============================================================================ @@ -25,6 +26,17 @@ IF EXIST %STATUSDIR%\done ( DEL %STATUSDIR%\done GOTO :eof ) +IF EXIST %STATUSDIR%\error ( + ECHO. + ECHO %time% ERROR Script returned error: + ECHO. + TYPE %STATUSDIR%\error + ECHO. + ECHO %time% Aborting. + ECHO. + DEL %STATUSDIR%\error + GOTO :terminate +) TIMEOUT /T 5 /NOBREAK GOTO :wait_auto REM - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/labs/scripts/osbash/template-osbashauto b/labs/scripts/osbash/template-osbashauto index 216aad22..10e8e8ac 100644 --- a/labs/scripts/osbash/template-osbashauto +++ b/labs/scripts/osbash/template-osbashauto @@ -36,8 +36,12 @@ for AUTODIR in "/$SHARE_NAME/autostart" "/$SHARE_NAME/autostart/$HOSTNAME"; do if [ -d "$AUTODIR" ]; then echo "$(date) autodir $AUTODIR" for SCRIPT in $AUTODIR/*.sh; do - as_root_exec_script "$SCRIPT" - + if ! as_root_exec_script "$SCRIPT"; then + echo >&2 "Script returned with error, giving up." + # Tell host about the error + echo "ERROR in $SCRIPT" >> "$STATUS_DIR/error" + exit 1 + fi # Remove script after execution rm "$SCRIPT" done