Fix helper's error monitoring system
authorNikos Skalkotos <skalkoto@grnet.gr>
Sat, 15 Dec 2012 22:07:18 +0000 (00:07 +0200)
committerNikos Skalkotos <skalkoto@grnet.gr>
Sat, 15 Dec 2012 22:07:18 +0000 (00:07 +0200)
Fix a bug where errors that where not created using log_error where
not registered to the monitoring subsystem.

snf-image-helper/common.sh
snf-image-helper/snf-image-helper.in

index 9c45142..7bbd219 100644 (file)
@@ -70,8 +70,10 @@ report_error() {
 log_error() {
     ERRORS+=("$*")
     echo "ERROR: $@" | tee $RESULT >&2
-    report_error
-    exit 1
+
+    # Use return instead of exit. The set -x options will terminate the script
+    # but will also trigger ERR traps if defined.
+    return 1
 }
 
 warn() {
@@ -404,6 +406,8 @@ task_cleanup() {
 
     if [ $rc -eq 0 ]; then
        report_task_end
+    else
+       report_error
     fi
 
     cleanup
index b9bcecb..f9cac5a 100644 (file)
@@ -33,6 +33,11 @@ fi
 
 set -e
 
+# Enable errtrace to make functions inherit the ERR trap
+set -o errtrace
+
+trap report_error ERR
+
 if grep snf_image_activate_helper /proc/cmdline > /dev/null; then
     # terminate helper vm when the script exits
     add_cleanup system_poweroff
@@ -88,9 +93,6 @@ if [ -z "$RUN_PARTS" ]; then
     log_error "run-parts program is missing from the system"
 fi
 
-# If something goes wrong with the tasks, try to umount the target filesystem
-# in case it is left mounted...
-trap '{ umount "$target"; }' ERR
 
 if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
 
@@ -108,6 +110,10 @@ if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
         log_error "Supported values for OSFAMILY property are: linux|windows"
     fi
 
+    # If something goes wrong with the tasks, try to umount the target
+    # file system in case it is left mounted...
+    trap '{ umount "$target"; }' ERR
+
     # Redirect standard error to standard output,
     # prepend a timestamp before each line of output.
     echo "Execute all snf-image tasks...."
@@ -115,10 +121,10 @@ if [ -z "$SNF_IMAGE_PROPERTY_EXCLUDE_ALL_TASKS" ]; then
         while IFS= read -r line; do
             echo $($DATE +%Y:%m:%d-%H:%M:%S.%N) "$line"
         done
+
+    trap report_error ERR
 fi
 
-# Disable the trap. If code reaches here, the filesystem is unmounted.
-trap - ERR
 
 echo "SUCCESS" > "$RESULT"