Abstract cleanups in a CLEANUP array
[ext/instance-debootstrap.git] / common.sh
index 7759730..941e425 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,49 @@ 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" = "export" ]; then
+    if [ -z "$EXPORT_DEVICE" ]; then
+      log_error "Missing OS API Variable EXPORT_DEVICE"
+    fi
+    blockdev=$EXPORT_DEVICE
+  elif [ "$SCRIPT_NAME" = "import" ]; then
+    if [ -z "$IMPORT_DEVICE" ]; then
+       log_error "Missing OS API Variable IMPORT_DEVICE"
+    fi
+    blockdev=$IMPORT_DEVICE
+  else
+    blockdev=$DISK_0_PATH
+  fi
   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
 }
@@ -68,11 +85,7 @@ if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
   get_api5_arguments
 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"
-    exit 1
-  fi
 else
-  echo "Unknown OS API VERSION $OS_API_VERSION"
+  log_error "Unknown OS API VERSION $OS_API_VERSION"
   exit 1
 fi