From 72d72623a92f8b1813309e9fe7a250a1e3ce16f6 Mon Sep 17 00:00:00 2001
From: Akash Gangil <gangila@vmware.com>
Date: Wed, 2 Dec 2015 04:02:20 -0800
Subject: [PATCH] Admin util should work from any dir

It wasn't working because of two reasons:
1. The packages filed in setup.cfg takes a directory name.
so specifying tools.python_nsxadmin.admin didn't put the py files
in /usr/local/lib/python2.7/dist-packages when we pip installed
vmware-nsx package. (sudo pip install .). So changing it to tools
installs all the code in tools including python_nsxadmin there. Read
2 to know why we need this.

2. To enable admin utility to dynamically pick up any hooks that users
might add we use the importlib module. The bug was here, as the path was
absolute tools/python_nsxadmin/.... So when we executed it from other
diretory those modules were not imported. So I changed that to relative
path. Though, over here there could be a case where the hooks location
directory path contains a '-' / dash, which will cause it to fail.

>>> import
>>> home.gangil.t1-review.e_nsx.tools.python_nsxadmin.admin.plugins.nsxv.resources.edges

  File "<stdin>", line 1

    import
home.gangil.t1-review.vmware_nsx.tools.python_nsxadmin.admin.plugins.nsxv.resources.edges

                         ^

SyntaxError: invalid syntax

So we install the tools code using setup.cfg and then just import
resources from there.

Now it works from any directory.

Change-Id: Idc414fabc769d0503974e8f77b84d32008b3c08a
---
 setup.cfg                            | 2 ++
 tools/python_nsxadmin/admin/shell.py | 5 +++--
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/setup.cfg b/setup.cfg
index 50b674fd36..abbddae4a4 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -21,12 +21,14 @@ classifier =
 [files]
 packages =
     vmware_nsx
+    tools/python_nsxadmin.admin
 data_files =
     etc/neutron/plugins/vmware = etc/nsx.ini
 
 [entry_points]
 console_scripts =
     neutron-check-nsx-config = vmware_nsx.check_nsx_config:main
+    nsxadmin = tools.python_nsxadmin.admin.shell:main
 neutron.db.alembic_migrations =
     vmware-nsx = vmware_nsx.db.migration:alembic_migrations
 neutron.core_plugins =
diff --git a/tools/python_nsxadmin/admin/shell.py b/tools/python_nsxadmin/admin/shell.py
index 4cafef3631..88726fba0e 100644
--- a/tools/python_nsxadmin/admin/shell.py
+++ b/tools/python_nsxadmin/admin/shell.py
@@ -104,7 +104,7 @@ def _get_plugin():
 
 
 def _get_plugin_dir():
-    plugin_dir = 'tools/python_nsxadmin/admin/plugins'
+    plugin_dir = os.path.dirname(os.path.realpath(__file__)) + "/plugins"
     return '{}/{}/resources'.format(plugin_dir, _get_plugin())
 
 
@@ -151,7 +151,8 @@ def _init_resource_plugin():
     for resource in resources:
         if resource != '__init__':
             importlib.import_module("." + resource,
-                                    _get_plugin_dir().replace("/", "."))
+                                    "tools.python_nsxadmin.admin.plugins."
+                                    "{}.resources".format(_get_plugin()))
 
 
 def _init_cfg():