INFRA: Switch to tox for JJB verify and merge jobs
[infra/cicd.git] / jjb / infra / lint.sh
diff --git a/jjb/infra/lint.sh b/jjb/infra/lint.sh
new file mode 100644 (file)
index 0000000..6d4e060
--- /dev/null
@@ -0,0 +1,86 @@
+#!/bin/bash
+
+set -o nounset
+set -o errexit
+set -o pipefail
+
+#-------------------------------------------------------------------------------
+# Various tests are run using tox such as jjbtest, yamllint, and shellehck.
+#-------------------------------------------------------------------------------
+run_tox() {
+
+  echo "Info  : Preparing to run tox for the repo $GERRIT_PROJECT"
+
+  # set and export defaults
+  LINT_TYPE="${LINT_TYPE:-jjbtest}"
+  VERBOSITY=${VERBOSITY:-false}
+  export LINT_TYPE VERBOSITY
+
+  # ensure we are in job build WORKSPACE
+  cd "$WORKSPACE"
+
+  # set DEBIAN_FRONTEND to run apt non-interactively
+  DEBIAN_FRONTEND=noninteractive
+  export DEBIAN_FRONTEND
+
+  # Wait for other apt auto-update process to finish by checking the dpkg lock file.
+  DPKG_LOCK="/var/lib/dpkg/lock-frontend"
+  try=0
+  while sudo lsof "${DPKG_LOCK}"  > /dev/null 2>&1 ; do
+    echo "DPKG file locked: ${DPKG_LOCK}."
+    echo "   Waiting for another pkg instalaltion process to finish ..."
+    sleep 10
+    if [[ ${try} -gt 120 ]] ; then
+      echo "ERROR: Max number of re-tries reached, exiting..."
+      exit 1
+    fi
+    try=$((try + 1))
+  done
+
+  # install dependencies
+  echo "Info  : Install python3.7-minimal python3-distutils virtualenv using apt"
+  redirect_cmd sudo apt update
+  redirect_cmd sudo apt install -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confnew -y \
+     python3.7-minimal python3-distutils virtualenv
+
+  # create and activate virtualenv
+  echo "Info  : Create and activate python virtualenv"
+  redirect_cmd virtualenv -p python3 .venv
+  set +u
+  redirect_cmd source .venv/bin/activate
+  set -u
+
+  # install only tox since the rest of the requirements are installed by tox itself
+  TOX_PACKAGE=$(grep "^tox==" jjb/infra/test-requirements.txt)
+  echo "Info  : Install $TOX_PACKAGE"
+  redirect_cmd pip install --force-reinstall "$TOX_PACKAGE"
+
+  # run tox
+  echo "Info  : Run $LINT_TYPE using tox"
+  echo "----------------------------------------------------"
+  tox -e "$LINT_TYPE"
+  echo "----------------------------------------------------"
+  echo "Info  : Done!"
+
+}
+#-------------------------------------------------------------------------------
+# In some cases, it is useful to see all the output generated by commands so
+# this function makes it possible for users to achieve that by not redirecting
+# output to /dev/null when verbosity is enabled
+#-------------------------------------------------------------------------------
+redirect_cmd() {
+
+  if [[ "$VERBOSITY" == "false" ]]; then
+    "$@" > /dev/null 2>&1
+  else
+    "$@"
+  fi
+
+}
+#-------------------------------------------------------------------------------
+# run tox
+#-------------------------------------------------------------------------------
+
+run_tox "$@"
+
+# vim: set ts=2 sw=2 ft=bash  expandtab: