Switch to Python Virtual Environment 95/71195/5
authorVictor Morales <v.morales@samsung.com>
Thu, 1 Oct 2020 21:24:32 +0000 (17:24 -0400)
committerMichael Pedersen <michaelx.pedersen@intel.com>
Wed, 3 Mar 2021 08:55:39 +0000 (08:55 +0000)
Virtual Environments allow the python modules installation without
affecting the hosting node. This helps to control dependencies required
by this project.

Signed-off-by: Victor Morales <v.morales@samsung.com>
Change-Id: Ib53d9dd335a4707ff863a6fd732d23d323513430
Reviewed-on: https://gerrit.opnfv.org/gerrit/c/kuberef/+/71195
Tested-by: jenkins-ci <jenkins-opnfv-ci@opnfv.org>
Reviewed-by: Michael Pedersen <michaelx.pedersen@intel.com>
Reviewed-by: Rihab Banday <rihab.banday@ericsson.com>
bindep.txt
deploy.sh
functions.sh
tox.ini

index f989692..fc14d44 100644 (file)
@@ -1,5 +1,6 @@
 python3-all-dev [platform:dpkg]
 python3-all [platform:dpkg]
 python-all-dev [platform:dpkg]
+pkg-config [platform:dpkg]
 python-devel [platform:rpm !platform:centos-8]
 python3-devel [platform:rpm]
index 7b230f1..d817022 100755 (executable)
--- a/deploy.sh
+++ b/deploy.sh
@@ -8,9 +8,11 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
-set -o xtrace
 set -o errexit
 set -o nounset
+if [ "${DEBUG:-false}" == "true" ]; then
+    set -o xtrace
+fi
 
 # Script for end to end RI-2 deployment using Infra engine and BMRA.
 # Please refer to README for detailed information.
@@ -30,6 +32,11 @@ check_prerequisites
 # shellcheck source=./deploy.env
 source "$CURRENTPATH/deploy.env"
 
+# ---------------------------------------------------------------------
+# creates a virtual environment for installation of dependencies
+# ---------------------------------------------------------------------
+creates_virtualenv
+
 # ---------------------------------------------------------------------
 # bootstrap install prerequisites
 # ---------------------------------------------------------------------
index 03e2284..305dcb7 100755 (executable)
@@ -26,6 +26,9 @@ assert_non_empty() {
         error "$2"
     fi
 }
+if [ "${DEBUG:-false}" == "true" ]; then
+    set -o xtrace
+fi
 
 check_prerequisites() {
     info "Check prerequisites"
@@ -65,7 +68,21 @@ check_prerequisites() {
     sudo sed -i "s/^Defaults.*env_reset/#&/" /etc/sudoers
 
     #-------------------------------------------------------------------------------
-    # Check if some tools are installed
+    # Check if Python Virtual Environment is installed
+    #-------------------------------------------------------------------------------
+    if ! command -v virtualenv &> /dev/null; then
+        error "VirtualEnv not found. Please install."
+    fi
+
+    #-------------------------------------------------------------------------------
+    # Check if PIP Installs Packages is installed
+    #-------------------------------------------------------------------------------
+    if ! command -v pip &> /dev/null; then
+        error "PIP not found. Please install."
+    fi
+
+    #-------------------------------------------------------------------------------
+    # Check is libvirt is installed
     #-------------------------------------------------------------------------------
     for tool in ansible yq virsh jq; do
         if ! command -v "$tool" &> /dev/null; then
@@ -253,11 +270,19 @@ EOF
     fi
 }
 
+# Creates a python virtual environment
+creates_virtualenv() {
+    if [  ! -d "$CURRENTPATH/.venv" ]; then
+        virtualenv .venv
+    fi
+    # shellcheck disable=SC1090
+    source "$CURRENTPATH/.venv/bin/activate"
+    pip install -r "$CURRENTPATH/requirements.txt"
+}
+
 # Executes a specific Ansible playbook
 run_playbook() {
-    ansible_cmd="$(command -v ansible-playbook)"
-    ansible_cmd+=" -i $CURRENTPATH/inventory/localhost.ini"
-    ansible_cmd+=" -e ansible_python_interpreter=$(command -v python)"
+    ansible_cmd="$(command -v ansible-playbook) -i $CURRENTPATH/inventory/localhost.ini -e ansible_python_interpreter=$(command -v python)"
     if [ "${DEBUG:-false}" == "true" ]; then
         ansible_cmd+=" -vvv"
     fi
diff --git a/tox.ini b/tox.ini
index 0d309d7..a4d4dc7 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -12,6 +12,7 @@ deps =
 whitelist_externals = bash
 commands = bash -c "find {toxinidir} \
    -not -path {toxinidir}/.tox/\* \
+   -not -path {toxinidir}/.venv/\* \
 # E006 check for lines longer than 79 columns
    -name \*.sh | xargs bashate -v -iE006"
    bash -c "yamllint {toxinidir}"