common.sh: output all errors to stderr
authorGuido Trotter <ultrotter@google.com>
Wed, 22 Oct 2008 14:09:16 +0000 (14:09 +0000)
committerGuido Trotter <ultrotter@google.com>
Wed, 22 Oct 2008 14:09:16 +0000 (14:09 +0000)
Create a log_error function that outputs to stderr and use it to send
error messages. This also clarifies error messages a bit and fixes one
wrong check for new_name rather than old_name.

Reviewed-by: iustinp

common.sh
rename

index 7759730..5595bb7 100644 (file)
--- a/common.sh
+++ b/common.sh
@@ -1,4 +1,8 @@
 
+log_error() {
+  echo "$@" >&2
+}
+
 SCRIPT_NAME=$(basename $0)
 
 for dir in /lib/udev /sbin; do
@@ -8,13 +12,13 @@ for dir in /lib/udev /sbin; do
 done
 
 if [ -z "$VOL_ID" ]; then
-    echo "vol_id not found, please install udev"
+    log_error "vol_id not found, please install udev"
     exit 1
 fi
 
 get_api5_arguments() {
   TEMP=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
-  if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
+  if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
   # Note the quotes around `$TEMP': they are essential!
   eval set -- "$TEMP"
   while true; do
@@ -29,36 +33,37 @@ get_api5_arguments() {
 
       --) shift; break;;
 
-      *)  echo "Internal error!"; exit 1;;
+      *)  log_error "Internal error!" >&2; exit 1;;
     esac
   done
   if [ -z "$instance" -o -z "$blockdev" ]; then
-    echo "Missing OS API Argument"
+    log_error "Missing OS API Argument (-i, -n, or -b)"
     exit 1
   fi
   if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
-    echo "Missing OS API Argument"
+    log_error "Missing OS API Argument -s (swapdev)"
     exit 1
   fi
-  if [ "$SCRIPT_NAME" = "rename" -a -z "$new_name"  ]; then
-    echo "Missing OS API Argument"
+  if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
+    log_error "Missing OS API Argument -o (old_name)"
     exit 1
   fi
 }
 
 get_api10_arguments() {
   if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
-    echo "Missing OS API Variable"
+    log_error "Missing OS API Variable:"
+    log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
     exit 1
   fi
   instance=$INSTANCE_NAME
   if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
-    echo "At least one disk is needed"
+    log_error "At least one disk is needed"
     exit 1
   fi
   blockdev=$DISK_0_PATH
   if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
-    echo "Missing OS API Variable"
+    log_error "Missing OS API Variable OLD_INSTANCE_NAME"
   fi
   old_name=$OLD_INSTANCE_NAME
 }
@@ -69,10 +74,10 @@ if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
 elif [ "$OS_API_VERSION" = "10" ]; then
   get_api10_arguments
   if [ $SCRIPT_NAME = "import" -o $SCRIPT_NAME = "export" ]; then
-    echo "import/export still not compatible with API version 10"
+    log_error "import/export still not compatible with API version 10"
     exit 1
   fi
 else
-  echo "Unknown OS API VERSION $OS_API_VERSION"
+  log_error "Unknown OS API VERSION $OS_API_VERSION"
   exit 1
 fi
diff --git a/rename b/rename
index 81fda06..96060f6 100755 (executable)
--- a/rename
+++ b/rename
@@ -41,8 +41,8 @@ OLD_HNAME="`cat $HNAME`"
 if [ "$OLD_HNAME" = "$old_name" ]; then
        echo $instance > $HNAME
 else
-       echo "Cannot rename from $old_name to $instance:" 1>&2
-       echo "Instance has a different hostname ($OLD_HNAME)" 1>&2
+       log_error "Cannot rename from $old_name to $instance:"
+       log_error "Instance has a different hostname ($OLD_HNAME)"
        exit 1
 fi