From aaab1d1b68db0c737b63ae6bce3179df0487f23f Mon Sep 17 00:00:00 2001
From: zengchen228 <zengchen228@gmail.com>
Date: Thu, 12 Nov 2020 01:11:39 +0800
Subject: [PATCH] Fix kolla-ansible to work with pyenv-virtualenv
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

One of the pyenv-virtualenv-set-up aliases depends on a symlink.
It seems pyenv runs the bash script from such a path and it fails
because of a failing comparison (VIRTUAL_ENV not detected).

The VIRTUAL_ENV is ensured to be fully resolved as well for safety.

This requires readlink from GNU coreutils but all supported platforms
have it by default.

Extra comments included, as well as simplification of directory
detection - readlink handles this (not that `bin` itself was
ever a symlink...).

Closes-Bug: #1903887
Co-Authored-By: Radosław Piliszek <radoslaw.piliszek@gmail.com>
Change-Id: I2fe6eb13ce7be68d346b1b3b7036859f34c896c4
---
 tools/kolla-ansible | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tools/kolla-ansible b/tools/kolla-ansible
index 87cc7a4404..dc30fa7c76 100755
--- a/tools/kolla-ansible
+++ b/tools/kolla-ansible
@@ -66,13 +66,16 @@ function check_environment_coherence {
 
 function find_base_dir {
     local dir_name
-    dir_name=$(cd "$(dirname "$0")" &>/dev/null && pwd)
+    dir_name=$(dirname "$0")
+    # NOTE(yoctozepto): Fix the case where dir_name is a symlink and VIRTUAL_ENV might not be. This
+    # happens with pyenv-virtualenv, see https://bugs.launchpad.net/kolla-ansible/+bug/1903887
+    dir_name=$(readlink -e "$dir_name")
     if [ -z "$SNAP" ]; then
         if [[ ${dir_name} == "/usr/bin" ]]; then
             BASEDIR=/usr/share/kolla-ansible
         elif [[ ${dir_name} == "/usr/local/bin" ]]; then
             BASEDIR=/usr/local/share/kolla-ansible
-        elif [[ -n ${VIRTUAL_ENV} ]] && [[ ${dir_name} == "${VIRTUAL_ENV}/bin" ]]; then
+        elif [[ -n ${VIRTUAL_ENV} ]] && [[ ${dir_name} == "$(readlink -e "${VIRTUAL_ENV}/bin")" ]]; then
             if test -f ${VIRTUAL_ENV}/lib/python*/site-packages/kolla-ansible.egg-link; then
                 # Editable install.
                 BASEDIR="$(head -n1 ${VIRTUAL_ENV}/lib/python*/site-packages/kolla-ansible.egg-link)"
@@ -80,6 +83,7 @@ function find_base_dir {
                 BASEDIR="${VIRTUAL_ENV}/share/kolla-ansible"
             fi
         else
+            # Running from sources (repo).
             BASEDIR="$(dirname ${dir_name})"
         fi
     else