[VOL-5100] - periodic-voltha-test-bbsim

vars/installKind.groovy
vars/installKind.sh
-----------------------
  o Convert installKind.groovy from a closure based method to function with parameters.
  o Script failure mode is completely silent so trying something different.
  o Extract inlined download script and place in named script installKind.sh
    jenkins issued a strange error about quoting shell variable $* to display args.
  o With groovy and shell separated lint can now syntax check both sources.

Change-Id: I68ca3f968a41a479535a8f9b96f0275a0919d4bd
diff --git a/jjb/pipeline/voltha/master/bbsim-tests.groovy b/jjb/pipeline/voltha/master/bbsim-tests.groovy
index 6fecb98..47e6c10 100644
--- a/jjb/pipeline/voltha/master/bbsim-tests.groovy
+++ b/jjb/pipeline/voltha/master/bbsim-tests.groovy
@@ -70,7 +70,7 @@
     try
     {
 	println("** ${iam} Running: installKind() { debug:true }"
-	installKind() { debug:true }
+	installKind(branch_name)
 	println("** ${iam}: Ran to completion")
 	ans = True // iff
     }
diff --git a/vars/installKind.groovy b/vars/installKind.groovy
index f0ed70d..f3402aa 100644
--- a/vars/installKind.groovy
+++ b/vars/installKind.groovy
@@ -17,7 +17,7 @@
 
 // -----------------------------------------------------------------------
 // -----------------------------------------------------------------------
-def getIam(String func)
+String getIam(String func)
 {
     // Cannot rely on a stack trace due to jenkins manipulation
     String src = 'vars/installKind.groovy'
@@ -27,46 +27,22 @@
 
 // -----------------------------------------------------------------------
 // -----------------------------------------------------------------------
-def process(Map args) {
-
+def process(Map args)
+{
     String iam = getIam('process')
     println("** ${iam}: ENTER")
+    println("args = " + args)
 
     // go install sigs.k8s.io/kind@v0.18.0
-    sh(
-        returnStdout: true,
-        script: """#!/bin/bash
-
-set -eu -o pipefail
-umask 0
-
-function error()
-{
-    echo "** ERROR: $*"
-    exit 1
-}
-
-dir="$WORKSPACE/bin"
-cmd="$dir/kind"
-if [ ! -f "$cmd" ]; then
-    mkdir -p "$dir"
-    pushd "$dir" || error "pushd $dir failed"
-    curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.0/kind-linux-amd64
-    chmod +x ./kind
-    popd         || error "popd $dir failed"
-fi
-
-## Sanity check installed binary
-echo
-echo "Kind command verison: $("$cmd" --version)"
-
-return
-""")
+    sh('./installKind.sh')
     println("** ${iam}: LEAVE")
+    return
 }
 
 // -----------------------------------------------------------------------
+// TODO: Support native syntax:   installKind() { debug:true }
 // -----------------------------------------------------------------------
+/*
 Boolean call\
     (
     // def self,  // jenkins env object for access to primitives like echo()
@@ -77,7 +53,12 @@
     body.resolveStrategy = Closure.DELEGATE_FIRST
     body.delegate        = config // make parameters visible down below
     body()
+ */
 
+// -----------------------------------------------------------------------
+// -----------------------------------------------------------------------
+def call(String branch)
+{
     String iam = getIam('main')
     println("** ${iam}: ENTER")
     println("** ${iam}: Debug= is " + config.contains(debug))
diff --git a/vars/installKind.sh b/vars/installKind.sh
new file mode 100755
index 0000000..24eb6cc
--- /dev/null
+++ b/vars/installKind.sh
@@ -0,0 +1,88 @@
+#!/bin/bash
+## -----------------------------------------------------------------------
+## Intent: 
+## -----------------------------------------------------------------------
+
+##-------------------##
+##---]  GLOBALS  [---##
+##-------------------##
+set -eu -o pipefail
+umask 0
+
+## -----------------------------------------------------------------------
+## Intent: Display a message then exit with non-zero shell exit status.
+## -----------------------------------------------------------------------
+function error()
+{
+    echo "** ERROR: $*"
+    exit 1
+}
+
+## -----------------------------------------------------------------------
+## Intent: Display program usage
+## -----------------------------------------------------------------------
+function usage
+{
+    cat <<EOH
+Usage: $0
+  --cmd       Kind command name to download (w/arch type)
+  --dir       Target bin directory to download into
+  --ver       Kind command version to download (default=v0.11.0)
+
+[MODEs]
+  --clean     Clean download, remove kind binary if it exists.
+EOH
+    exit 1
+}
+
+##----------------##
+##---]  MAIN  [---##
+##----------------##
+
+WORKSPACE="${WORKSPACE:-$(/bin/pwd)}"
+dir="$WORKSPACE/bin"
+
+bin='kind-linux-amd64'
+ver='v0.11.0'
+
+while [ $# -gt 0 ]; do
+    arg="$1"; shift
+    case "$arg" in
+	-*clean) declare -i clean_mode=1 ;;
+	-*cmd) bin="$1";s shift ;; # cmd-by-arch
+	-*dir) dir="$1"; shift  ;; # target dir
+	-*help) usage; exit 0 ;;
+	-*ver) ver="$1"; shift  ;; # version
+	*) echo "[SKIP] Unknown argument [$arg]" ;;
+    esac
+done
+
+
+cmd="$dir/kind"
+[[ -v clean_mode ]] && /bin/rm -f "$cmd"
+
+if [ ! -f "$cmd" ]; then
+    mkdir -p "$dir"
+    pushd "$dir" || error "pushd $dir failed"
+    echo "** ${0##*/}: Download ${bin} ${ver}"
+
+    curl --silent -Lo ./kind "https://kind.sigs.k8s.io/dl/${ver}/${bin}"
+    readarray -t file_info < <(file "$cmd")
+
+    ## Validate binary
+    case "${file_info[@]}" in
+	*ASCII*)
+	    echo "ERROR: kind command is invalid"
+	    set -x; cat "$cmd"; set +x
+	    echo
+	    /bin/rm -f "$cmd"
+	    ;;
+	*) chmod +x ./kind ;;
+    esac
+
+    popd         || error "popd $dir failed"
+fi
+
+echo "Kind command version: $($cmd --version)"
+
+# [EOF]