Move pgrep_proc and pkill_proc into vars/*.groovy for reuse

Change-Id: I78cc51448d8132207aa9c7db3cc3ee1381a62f02
diff --git a/jjb/pipeline/voltha/voltha-2.12/bbsim-tests.groovy b/jjb/pipeline/voltha/voltha-2.12/bbsim-tests.groovy
index f66b29d..1f82010 100644
--- a/jjb/pipeline/voltha/voltha-2.12/bbsim-tests.groovy
+++ b/jjb/pipeline/voltha/voltha-2.12/bbsim-tests.groovy
@@ -70,52 +70,6 @@
 // -----------------------------------------------------------------------
 // Intent:
 // -----------------------------------------------------------------------
-void pgrep_proc(String proc) {
-
-    println("** RAW PROCESS OUTPUT:")
-    def stream = sh(returnStdout: true, script: 'ps faaux')
-    println(stream)
-    
-    String cmd = [
-	'pgrep',
-	// '--older', 5, // switch not supported, nodes using older version
-	'--list-full',
-	"\"${proc}\"",
-	';',
-	'echo', 'DONE',
-	';',
-	'true',
-    ].join(' ')
-
-    println("** Running: ${cmd}")
-    sh(
-        returnStdout: true,
-	"set +euo pipefail && ${cmd}"
-    )
-    return
-}
-
-// -----------------------------------------------------------------------
-// -----------------------------------------------------------------------
-void pkill_proc(String proc) {
-    String cmd = [
-	'pkill',
-	// switch not supported, nodes using older version
-	// NOTE: pkill should not kill it-self
-	// good old kill (ps | grep -v -e grep -e '$$-me') }
-	// '--older', 5,
-	'--echo',
-	"\"${proc}\"",
-    ].join(' ')
-
-    println("** Running: ${cmd}")
-    sh(""" if [[ \$(pgrep --count "${proc}") -gt 0 ]]; then "$cmd"; fi" """)
-    return
-}
-
-// -----------------------------------------------------------------------
-// Intent:
-// -----------------------------------------------------------------------
 void execute_test(testTarget, workflow, testLogging, teardown, testSpecificHelmFlags='') {
     String infraNamespace  = 'default'
     String volthaNamespace = 'voltha'
diff --git a/jjb/pipeline/voltha/voltha-2.12/software-upgrades.groovy b/jjb/pipeline/voltha/voltha-2.12/software-upgrades.groovy
index 7cfa54b..d7427bb 100755
--- a/jjb/pipeline/voltha/voltha-2.12/software-upgrades.groovy
+++ b/jjb/pipeline/voltha/voltha-2.12/software-upgrades.groovy
@@ -144,7 +144,9 @@
       // Currently only testing with ATT workflow
       // TODO: Support for other workflows
       volthaDeploy([bbsimReplica: olts.toInteger(), workflow: 'att', extraHelmFlags: extraHelmFlags, localCharts: localCharts])
-      // stop logging
+
+        // [TODO] pkill_proc("_TAG=kail-${name}")
+        // stop logging
       sh """
         P_IDS="\$(ps e -ww -A | grep "_TAG=kail-${name}" | grep -v grep | awk '{print \$1}')"
         if [ -n "\$P_IDS" ]; then
@@ -231,11 +233,16 @@
         # Run the specified tests
         make -C $WORKSPACE/voltha-system-tests \$TARGET || true
       """
-      // remove port-forwarding
+
+        pkill_proc('port-forw')
+	    // remove port-forwarding
+	    /*
       sh """
         # remove orphaned port-forward from different namespaces
         ps aux | grep port-forw | grep -v grep | awk '{print \$2}' | xargs --no-run-if-empty kill -9 || true
       """
+	     */
+
       // collect pod details
       get_pods_info("$WORKSPACE/${name}")
       sh """
@@ -307,37 +314,21 @@
     // -----------------------------------------------------------------------
     // -----------------------------------------------------------------------
     stage('Cleanup') {
-        steps {
-
-	script {
-            println('''
-** -----------------------------------------------------------------------
-** Raw process listing
-** -----------------------------------------------------------------------
-''')
-        sh(''' ps faaux ''')
-
-            println('''
-** -----------------------------------------------------------------------
-** pgrep --list-full port
-** -----------------------------------------------------------------------
-''')
-        sh(''' set +euo pipefail && pgrep --list-full 'port' ''')
-		}
-
-        // remove port-forwarding
-        sh """
-          # remove orphaned port-forward from different namespaces
-          ps aux | grep port-forw | grep -v grep | awk '{print \$2}' | xargs --no-run-if-empty kill -9 || true
-        """
-        helmTeardown(['infra', 'voltha'])
-      }
-    }
+        steps        {
+            script   {
+                pgrep_proc('port-forw')
+                pkill_proc('port-forw')
+            } // script
+            helmTeardown(['infra', 'voltha'])
+        } // steps
+    } // stage
+	
     stage('Create K8s Cluster') {
-      steps {
-        createKubernetesCluster([nodes: 3])
-      }
+        steps {
+            createKubernetesCluster([nodes: 3])
+        }
     }
+	
     stage('Run Test') {
       steps {
         test_software_upgrade("onos-app-upgrade")
@@ -371,3 +362,5 @@
     }
   }
 }
+
+// [EOF]
diff --git a/vars/pgrep_proc.groovy b/vars/pgrep_proc.groovy
new file mode 100644
index 0000000..bf6fa9a
--- /dev/null
+++ b/vars/pgrep_proc.groovy
@@ -0,0 +1,73 @@
+#!/usr/bin/env groovy
+// -----------------------------------------------------------------------
+// Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// -----------------------------------------------------------------------
+// Install the voltctl command by branch name "voltha-xx"
+// -----------------------------------------------------------------------
+
+// -----------------------------------------------------------------------
+// -----------------------------------------------------------------------
+String getIam(String func) {
+    // Cannot rely on a stack trace due to jenkins manipulation
+    String src = 'vars/pgrep_proc.groovy'
+    String iam = [src, func].join('::')
+    return iam
+}
+
+// -----------------------------------------------------------------------
+// -----------------------------------------------------------------------
+Boolean process(String proc) {
+    String iam  = getIam('process')
+    Boolean ans = true
+
+    println("** ${iam}: ENTER")
+
+    String cmdFull = "pgrep --list-full '${proc}'"
+    String cmd = """if [[ \$(pgrep --count "${proc}") -gt 0 ]]; then ${cmdFull}; fi"""
+
+    println(" ** Running: ${cmd}")
+    sh(
+        label  : 'pgrep_proc', // jenkins usability: label log entry 'step'
+        script : "${cmd}",
+    )
+
+    println("** ${iam}: LEAVE")
+    return(ans)
+}
+
+// -----------------------------------------------------------------------
+// Install: Jenkins/groovy callback for installing the kind command.
+//    o Paramter branch is passed but not yet used.
+//    o Installer should be release friendly and checkout a frozen version
+// -----------------------------------------------------------------------
+def call(String proc) {
+    String iam = getIam('main')
+
+    println("** ${iam}: ENTER")
+
+    try {
+        process(proc)
+    }
+    catch (Exception err) {
+        println("** ${iam}: EXCEPTION ${err}")
+        throw err
+    }
+    finally {
+        println("** ${iam}: LEAVE")
+    }
+    return
+}
+
+// [EOF]
diff --git a/vars/pkill_proc.groovy b/vars/pkill_proc.groovy
new file mode 100644
index 0000000..54d58ad
--- /dev/null
+++ b/vars/pkill_proc.groovy
@@ -0,0 +1,72 @@
+#!/usr/bin/env groovy
+// -----------------------------------------------------------------------
+// Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// -----------------------------------------------------------------------
+// Install the voltctl command by branch name "voltha-xx"
+// -----------------------------------------------------------------------
+
+// -----------------------------------------------------------------------
+// -----------------------------------------------------------------------
+String getIam(String func) {
+    // Cannot rely on a stack trace due to jenkins manipulation
+    String src = 'vars/pkill_proc.groovy'
+    String iam = [src, func].join('::')
+    return iam
+}
+
+// -----------------------------------------------------------------------
+// -----------------------------------------------------------------------
+Boolean process(String proc) {
+    String iam  = getIam('process')
+    Boolean ans = true
+
+    println("** ${iam}: ENTER")
+
+    String cmdKill = "pkill --echo '${proc}'"
+    String cmd = """if [[ \$(pgrep --count "${proc}") -gt 0 ]]; then ${cmdKill}; fi"""
+    println(" ** Running: ${cmd}")
+    sh(
+        label  : 'pkill_proc', // jenkins usability: label log entry 'step'
+        script : "${cmd}",
+    )
+
+    println("** ${iam}: LEAVE")
+    return(ans)
+}
+
+// -----------------------------------------------------------------------
+// Install: Jenkins/groovy callback for installing the kind command.
+//    o Paramter branch is passed but not yet used.
+//    o Installer should be release friendly and checkout a frozen version
+// -----------------------------------------------------------------------
+def call(String proc) {
+    String iam = getIam('main')
+
+    println("** ${iam}: ENTER")
+
+    try {
+        process(proc)
+    }
+    catch (Exception err) {
+        println("** ${iam}: EXCEPTION ${err}")
+        throw err
+    }
+    finally {
+        println("** ${iam}: LEAVE")
+    }
+    return
+}
+
+// [EOF]