bash: Update jjb version in jjb-sandbox-env function
[vpp.git] / extras / bash / functions.bash
index c99cb55..467d9da 100644 (file)
@@ -15,7 +15,6 @@
 # This file is meant to be sourced in a .bashrc file to add useful
 # bash functions to an interactive shell
 
-
 # Bash function to run vpp 'make test' testcases
 # repeatedly, stopping on test failure or when
 # a test log contains the optionally specified text
@@ -26,21 +25,24 @@ vpp-make-test()
     local all
     local debug
     local grep_for
+    local show_grep
     local run_make_test
     local old_pwd
+    local test_desc
     local is_feature="false"
     local retry_count=100
     local tester=${GERRIT_USER:-$USER}
-    
+    local jobs="auto"
+
     if [ -z "$WS_ROOT" ] ; then
         echo "ERROR: WS_ROOT is not set!"
         return
-    elif [ -z "$(find $WS_ROOT -type d -name vppinfra)" ] ; then
+    elif [ ! -d "$WS_ROOT/src/vppinfra" ] ; then
         echo "ERROR: WS_ROOT is not set to a VPP workspace!"
         return
     fi
-    
-    options=$(getopt -o "adfg:r:" -- "$@")
+
+    options=$(getopt -o "adfg:j:r:" -- "$@")
     if [ $? -eq 1 ] ; then
         usage=true
     else
@@ -63,6 +65,14 @@ vpp-make-test()
                 show_grep=$1
                 grep_for="${1//-/\\-}"
                 ;;
+            -j)
+                shift
+                jobs=$1
+                if [ $((jobs)) != $jobs ] ; then
+                    echo "ERROR: Invalid option value for -j option ($jobs)!"
+                    usage=true;
+                fi
+                ;;
             -r)
                 shift
                 retry_count=$1
@@ -78,19 +88,20 @@ vpp-make-test()
         esac
         shift
     done
-    
+
     if [ -n "$usage" ] || [ -z "$1" ] ; then
         if [ -z "$1" ] ; then
             echo "ERROR: no testcase specified!"
         fi
-        echo "Usage: vpp-make-test [-a][-d][-f][-g <text>][-r <retry count>] <testcase> [<retry_count>]"
+        echo "Usage: vpp-make-test [-a][-d][-f][-g <text>][-j <jobs>][-r <retry count>] <testcase> [<retry_count>]"
         echo "         -a                Run extended tests"
         echo "         -d                Run vpp debug image (i.e. with ASSERTS)"
         echo "         -f                Testcase is a feature set (e.g. tcp)"
         echo "         -g <text>         Text to grep for in log, FAIL on match."
         echo "                           Enclose <text> in single quotes when it contains any dashes:"
         echo "                           e.g.  vpp-make-test -g 'goof-bad-' test_xyz"
-        echo "         -r <retry count>  Retry Count (default = 100 for individual | 1 for feature)"
+        echo "         -j <# jobs>       Set TEST_JOBS (default = auto) for feature set"
+        echo "         -r <retry count>  Retry Count (default = 100 for individual test | 1 for feature set)"
         return
     fi
 
@@ -98,7 +109,7 @@ vpp-make-test()
         retry_count=1
     fi
     if [ "$is_feature" == "true" ] ; then
-        run_make_test="make test$all$debug TEST=$1 SANITY=no TEST_JOBS=auto"
+        run_make_test="make test$all$debug TEST=$1 SANITY=no TEST_JOBS=$jobs"
     else
         run_make_test="make test$all$debug TEST=*.*.$1 SANITY=no"
     fi
@@ -106,9 +117,9 @@ vpp-make-test()
     old_pwd=$(pwd)
     cd $WS_ROOT
     line="------------------------------------------------------------------------------"
-    local test_desc="'$run_make_test'"
+    test_desc="'$run_make_test'"
     if [ -n "$grep_for" ] ; then
-        test_desc="$test_desc [grep $show_grep]"
+        test_desc="$test_desc [grep '$show_grep']"
     fi
     for ((i=1; i<=retry_count; i++)) ; do
         echo -e "\n$line"
@@ -132,9 +143,119 @@ vpp-make-test()
             return
         fi
     done
-    
+
     echo -e "\n$line\nPASS [$((i-1))/$retry_count]: $test_desc\n$line\n"
     echo -e "Hey $tester, Life is good!!! :D\n"
     cd $old_pwd
 }
-export -f vpp-make-test
+
+# bash function to set up csit python virtual environment
+csit-env()
+{
+    if [ -f "$WS_ROOT/VPP_REPO_URL" ] && [ -f "$WS_ROOT/requirements.txt" ]; then
+        if [ -n "$(declare -f deactivate)" ]; then
+            echo "Deactivating Python Virtualenv!"
+            deactivate
+        fi
+        local PIP=pip
+        local setup_framework=$WS_ROOT/resources/libraries/python/SetupFramework.py
+        if [ -n "$(grep pip3 $setup_framework)" ]; then
+            PIP=pip3
+            local VENV_OPTS="-p python3"
+        fi
+        export CSIT_DIR=$WS_ROOT
+        export PYTHONPATH=$CSIT_DIR
+        rm -rf $PYTHONPATH/env && virtualenv $VENV_OPTS $PYTHONPATH/env \
+            && source $PYTHONPATH/env/bin/activate \
+            && $PIP install --upgrade -r $PYTHONPATH/requirements.txt \
+            && $PIP install --upgrade -r $PYTHONPATH/tox-requirements.txt
+    else
+        echo "ERROR: WS_ROOT not set to a CSIT workspace!"
+    fi
+}
+
+# bash function to set up jenkins sandbox environment
+#
+# See LF Sandbox documentation:
+#   https://docs.releng.linuxfoundation.org/en/latest/jenkins-sandbox.html
+#
+# Prerequisites:
+#   1. Create jenkins sandbox token and add it to your local jenkins.ini file
+#      Either specify the location of the init file in $JENKINS_INI or
+#      JENKINS_INI will be initialized to either
+#         ~/.config/jenkins_jobs/jenkins.ini
+#         $WS_ROOT/jenkins.ini
+#   2. Clone ci-management workspace from gerrit.fd.io
+#   3. export WS_ROOT=<local ci-management workspace>
+jjb-sandbox-env()
+{
+    if [ -z "$WS_ROOT" ] ; then
+        echo "ERROR: WS_ROOT is not set!"
+        return
+    elif [ ! -d "$WS_ROOT/jjb" ] ; then
+        echo "ERROR: WS_ROOT is not set to a ci-management workspace!"
+        return
+    fi
+
+    if [ -n "$(declare -f deactivate)" ]; then
+        echo "Deactivating Python Virtualenv!"
+        deactivate
+    fi
+
+    if [ -z "$JENKINS_INI" ] ; then
+        local user_jenkins_ini="/home/$USER/.config/jenkins_jobs/jenkins.ini"
+        if [ -f "$user_jenkins_ini" ] ; then
+            export JENKINS_INI=$user_jenkins_ini
+        elif [ -f "$WS_ROOT/jenkins.ini" ] ; then
+            export JENKINS_INI="$WS_ROOT/jenkins.ini"
+        else
+            echo "ERROR: Unable to find 'jenkins.ini'!"
+            return
+        fi
+        echo "Exporting JENKINS_INI=$JENKINS_INI"
+    elif [ ! -f "$JENKINS_INI" ] ; then
+        echo "ERROR: file specified in JENKINS_INI ($JENKINS_INI) not found!"
+        return
+    fi
+
+    if [ -n "$(declare -f deactivate)" ]; then
+        echo "Deactivating Python Virtualenv!"
+        deactivate
+    fi
+    cd $WS_ROOT
+    git submodule update --init --recursive
+
+    local VENV_DIR=$WS_ROOT/venv
+    rm -rf $VENV_DIR \
+       && python3 -m venv $VENV_DIR \
+       && source $VENV_DIR/bin/activate \
+       && pip3 install wheel \
+       && pip3 install jenkins-job-builder==3.5.0
+
+    alias jjsb='jenkins-jobs --conf $JENKINS_INI'
+    function jjsb-test() {
+        if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
+            echo "jenkins-jobs not found!  Run jjb-sandbox-env to activate."
+            return
+        fi
+        if [ -z "$1" ] ; then
+            echo "Usage: $FUNCNAME <jenkins-job-name>"
+            return
+        fi
+        which jenkins-jobs \
+            && jenkins-jobs --conf $JENKINS_INI test $WS_ROOT/jjb $@
+    }
+    function jjsb-update() {
+        if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
+            echo "jenkins-jobs not found!  Run jjb-sandbox-env to activate."
+            return
+        fi
+        if [ -z "$1" ] ; then
+            echo "Usage: $FUNCNAME <jenkins-job-name>"
+            return
+        fi
+        which jenkins-jobs \
+            && jenkins-jobs --conf $JENKINS_INI update $WS_ROOT/jjb $@
+    }
+    jenkins-jobs --version
+}