From e32f5c52a43380a7a391d09f9c0630e5770b7c53 Mon Sep 17 00:00:00 2001
From: Jeffrey Zhang <zhang.lei.fly@gmail.com>
Date: Thu, 31 Dec 2015 12:16:03 +0800
Subject: [PATCH] Fix the kolla to find the docker image folder in virtualenv

Closes-Bug:#1530256
Change-Id: I05f10d13e7ba1e2b985c2944aec71ce55630442b
---
 kolla/cmd/build.py | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)

diff --git a/kolla/cmd/build.py b/kolla/cmd/build.py
index fa2bbd7ab8..13d9cd69ef 100755
--- a/kolla/cmd/build.py
+++ b/kolla/cmd/build.py
@@ -19,7 +19,6 @@ import errno
 import json
 import logging
 import os
-import platform
 import re
 import requests
 import shutil
@@ -47,6 +46,9 @@ LOG.setLevel(logging.INFO)
 
 signal.signal(signal.SIGINT, signal.SIG_DFL)
 
+PROJECT_ROOT = os.path.abspath(os.path.join(
+    os.path.dirname(os.path.realpath(__file__)), '../..'))
+
 
 class KollaDirNotFoundException(Exception):
     pass
@@ -276,34 +278,11 @@ class WorkerThread(Thread):
             self.push_queue.put(image)
 
 
-def find_os_type():
-    return platform.linux_distribution()
-
-
-def find_base_dir():
-    script_path = os.path.dirname(os.path.realpath(sys.argv[0]))
-    if os.path.basename(script_path) == 'cmd':
-        return os.path.realpath(os.path.join(script_path, '..', '..'))
-    if os.path.basename(script_path) == 'bin':
-        if find_os_type()[0] in ['Ubuntu', 'debian']:
-            return '/usr/local/share/kolla'
-        else:
-            return '/usr/share/kolla'
-    if os.path.exists(os.path.join(script_path, 'tests')):
-        return script_path
-    raise KollaDirNotFoundException(
-        'I do not know where your Kolla directory is'
-    )
-
-
 class KollaWorker(object):
 
     def __init__(self, conf):
         self.conf = conf
-        self.base_dir = os.path.abspath(find_base_dir())
-        LOG.debug("Kolla base directory: " + self.base_dir)
-        self.images_dir = os.path.join(self.base_dir, 'docker')
-
+        self.images_dir = self._get_images_dir()
         self.registry = conf.registry
         if self.registry:
             self.namespace = self.registry + '/' + conf.namespace
@@ -342,6 +321,20 @@ class KollaWorker(object):
         self.image_statuses_unmatched = dict()
         self.maintainer = conf.maintainer
 
+    def _get_images_dir(self):
+        possible_paths = (
+            PROJECT_ROOT,
+            os.path.join(sys.prefix, 'share/kolla'),
+            os.path.join(sys.prefix, 'local/share/kolla'))
+
+        for path in possible_paths:
+            image_path = os.path.join(path, 'docker')
+            if os.path.exists(image_path):
+                LOG.info('Found the docker image folder at %s', image_path)
+                return image_path
+        else:
+            raise KollaDirNotFoundException('Image dir can not be found')
+
     def build_rpm_setup(self, rpm_setup_config):
         """Generates a list of docker commands based on provided configuration.