Simplify argument parsing and add usage info 55/39355/2
authorMartin Kulhavy <martin.kulhavy@nokia.com>
Tue, 15 Aug 2017 15:18:48 +0000 (18:18 +0300)
committerMartin Kulhavy <martin.kulhavy@nokia.com>
Tue, 15 Aug 2017 16:41:12 +0000 (19:41 +0300)
The code for initial parsing of the script arguments was simplified and
added better checks of the arguments. For clearer information, usage
information can be printed out.

Pod special cases were removed (can be directly specified using custom).

Change-Id: I42ca0ed703d5062f6deae74aa931513c235e2898
Signed-off-by: Martin Kulhavy <martin.kulhavy@nokia.com>
ci/03-maasdeploy.sh
ci/tools.sh

index 5ed3934..2b99ff6 100755 (executable)
@@ -42,9 +42,21 @@ sudo apt-get install bridge-utils openssh-server bzr git virtinst qemu-kvm libvi
 
 sudo -H pip install --upgrade pip
 
-#first parameter should be custom and second should be either
-# absolute location of file (including file name) or url of the
-# file to download.
+
+usage() {
+  # no xtrace output
+  { set +x; } 2> /dev/null
+
+  echo "OPNFV JOID deployer of the MAAS (Metal as a Service) infrastructure."
+  echo "Usage: $0 custom <path_to_labconfig>"
+  echo "       $0 virtual"
+  exit ${1-0}
+}
+
+if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]
+then
+    usage;
+fi
 
 
 #
@@ -52,43 +64,51 @@ sudo -H pip install --upgrade pip
 #
 
 # Get labconfig and generate deployconfig.yaml
-echo_info "Parsing lab configuration file"
+
 case "$labname" in
-    intelpod[569]|orangepod[12]|cengnpod[12] )
-        array=(${labname//pod/ })
-        cp ../labconfig/${array[0]}/pod${array[1]}/labconfig.yaml .
-        python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
-        ;;
-    'attvirpod1' )
-        cp ../labconfig/att/virpod1/labconfig.yaml .
-        python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
-        ;;
     'custom')
+        # Deployment with a custom labconfig file
         labfile=$2
-        if [ -e $labfile ]; then
-            cp $labfile ./labconfig.yaml || true
-        else
-            wget $labconfigfile -t 3 -T 10 -O ./labconfig.yaml || true
+        if [ -z "$labfile" ]; then
+            if [ ! -e ./labconfig.yaml ]; then
+                # no labconfig file was specified and no ci/labconfig.yaml is present
+                echo_error "Labconfig file must be specified when using custom"
+                usage 1
+            else
+                # no labconfig file was specified and but a (backup) ci/labconfig.yaml found
+                echo_warning "Labconfig was not specified, using ./labconfig.yaml instead"
+                # no action needed, ./labconfig.yaml already present
+            fi
+        elif [ ! -e "$labfile" ]; then
+            # labconfig file was specified but does not exist on disk
+            echo_warning "Labconfig not found locally, trying download"
+
+            wget $labfile -t 3 -T 10 -O ./labconfig.yaml || true
             count=`wc -l labconfig.yaml  | cut -d " " -f 1`
             if [ $count -lt 10 ]; then
-                rm -rf labconfig.yaml
+                echo_error "Unable to download labconfig"
+                exit 1
             fi
-        fi
-        if [ ! -e ./labconfig.yaml ]; then
-            virtinstall=1
-            labname="default"
-            cp ../labconfig/default/labconfig.yaml ./
-            cp ../labconfig/default/deployconfig.yaml ./
         else
-            python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
-            labname=`grep "maas_name" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //'`
+            echo_info "Using $labfile to setup deployment"
+            cp $labfile ./labconfig.yaml
         fi
+
+        python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
+        labname=`grep "maas_name" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //'`
         ;;
-    )
-        virtinstall=1
-        labname="default"
+    'virtual'|'')
+        # Virtual deployment using a default labconfig file
+        echo_info "Using default labconfig for virtual install"
         cp ../labconfig/default/labconfig.yaml ./
         python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
+        labname="default"
+        virtinstall=1
+        ;;
+    * )
+        # Unknown argument
+        echo_error "Unknown script argument: $labname"
+        usage 1
         ;;
 esac
 
index ecb2e80..45db67e 100644 (file)
@@ -28,7 +28,7 @@ function echo_info { (
 }
 
 #######################################
-# Echo error
+# Echo error (to stderr)
 # Arguments:
 #   Same as for echo
 # Returns:
@@ -43,3 +43,20 @@ function echo_error { (
     >&2 echo "${@:1:($#-1)}" -e "$red_bold${@: -1}$color_off";
   )
 }
+
+#######################################
+# Echo warning (to stderr)
+# Arguments:
+#   Same as for echo
+# Returns:
+#   None
+#######################################
+function echo_warning { (
+    # don't clutter the script output with the xtrace of the echo command
+    { set +x; } 2> /dev/null
+
+    red_italic='\033[3;91m'
+    color_off='\033[0m'
+    >&2 echo "${@:1:($#-1)}" -e "$red_italic${@: -1}$color_off";
+  )
+}