From c89cbff6a225de678d3ae7a3941169a9661fb49a Mon Sep 17 00:00:00 2001
From: Jeffrey Zhang <zhang.lei.fly@gmail.com>
Date: Sat, 5 Nov 2016 02:28:02 +0800
Subject: [PATCH] Fix neutron lbaas dashboard

Load neutron lbaas dynamic when neutron lbaas is enabled.

Change-Id: I47913824fea87b721f254633c16d1dc6903b4f5d
Closes-Bug: #1639325
---
 ansible/roles/horizon/tasks/start.yml |  2 ++
 docker/horizon/Dockerfile.j2          |  8 ++++----
 docker/horizon/extend_start.sh        | 27 ++++++++++++++++++++++++++-
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/ansible/roles/horizon/tasks/start.yml b/ansible/roles/horizon/tasks/start.yml
index e178b92a23..746cae5683 100644
--- a/ansible/roles/horizon/tasks/start.yml
+++ b/ansible/roles/horizon/tasks/start.yml
@@ -5,6 +5,8 @@
     common_options: "{{ docker_common_options }}"
     image: "{{ horizon_image_full }}"
     name: "horizon"
+    environment:
+      ENABLE_NEUTRON_LBAAS: "{{ 'yes' if  enable_neutron_lbaas | bool else 'no' }}"
     volumes:
       - "{{ node_config_directory }}/horizon/:{{ container_config_directory }}/:ro"
       - "/etc/localtime:/etc/localtime:ro"
diff --git a/docker/horizon/Dockerfile.j2 b/docker/horizon/Dockerfile.j2
index 5a589615f0..7e62af8e0e 100644
--- a/docker/horizon/Dockerfile.j2
+++ b/docker/horizon/Dockerfile.j2
@@ -12,7 +12,8 @@ MAINTAINER {{ maintainer }}
                 'openstack-dashboard',
                 'httpd',
                 'mod_wsgi',
-                'gettext'
+                'gettext',
+                'openstack-neutron-lbaas-ui'
         ] %}
 
 {{ macros.install_packages(horizon_packages | customizable("packages")) }}
@@ -28,7 +29,8 @@ RUN useradd --user-group horizon \
     && cp /usr/share/openstack-dashboard/manage.py /usr/bin/manage.py \
     && /usr/bin/python /usr/bin/manage.py collectstatic --noinput --clear \
     && (cd /usr/lib/python2.7/site-packages/horizon && /usr/bin/python /usr/bin/manage.py compilemessages) \
-    && (cd /usr/lib/python2.7/site-packages/openstack_dashboard && /usr/bin/python /usr/bin/manage.py compilemessages)
+    && (cd /usr/lib/python2.7/site-packages/openstack_dashboard && /usr/bin/python /usr/bin/manage.py compilemessages) \
+    && rm -f /usr/share/openstack-dashboard/openstack_dashboard/local/enabled/_148*_project*loadbalancersv2_panel.py*
 {% endblock %}
 
     {% elif base_distro in ['ubuntu'] %}
@@ -107,8 +109,6 @@ RUN ln -s horizon-source/* horizon \
     && cp /horizon/manage.py /var/lib/kolla/venv/bin/manage.py \
     && if [ "$(ls /plugins)" ]; then \
            pip --no-cache-dir install --upgrade -c requirements/upper-constraints.txt /plugins/*; \
-           ln -s `find /plugins -name 'neutron-lbaas-dashboard-*' -type d`/neutron_lbaas_dashboard/enabled/_1481_project_ng_loadbalancersv2_panel.py \
-                 /var/lib/kolla/venv/lib/python2.7/site-packages/openstack_dashboard/enabled/_1481_project_ng_loadbalancersv2_panel.py; \
        fi \
     && /var/lib/kolla/venv/bin/python /var/lib/kolla/venv/bin/manage.py collectstatic --noinput --clear \
     && (cd /var/lib/kolla/venv/lib/python2.7/site-packages/horizon && /var/lib/kolla/venv/bin/python /var/lib/kolla/venv/bin/manage.py compilemessages) \
diff --git a/docker/horizon/extend_start.sh b/docker/horizon/extend_start.sh
index 51be17405b..8ceb81bc79 100644
--- a/docker/horizon/extend_start.sh
+++ b/docker/horizon/extend_start.sh
@@ -1,5 +1,7 @@
 #!/bin/bash
 
+set -o errexit
+
 # Bootstrap and exit if KOLLA_BOOTSTRAP variable is set. This catches all cases
 # of the KOLLA_BOOTSTRAP variable being set, including empty.
 if [[ "${!KOLLA_BOOTSTRAP[@]}" ]]; then
@@ -11,6 +13,29 @@ if [[ "${!KOLLA_BOOTSTRAP[@]}" ]]; then
     exit 0
 fi
 
+FORCE_GENERATE="no"
+
+if [[ ${KOLLA_INSTALL_TYPE} == "binary" ]]; then
+    SITE_PACKAGES="/usr/lib/python2.7/site-packages"
+elif [[ ${KOLLA_INSTALL_TYPE} == "source" ]]; then
+    SITE_PACKAGES="/var/lib/kolla/venv/lib/python2.7/site-packages"
+fi
+
+function config_neutron_lbaas {
+    SRC="${SITE_PACKAGES}/neutron_lbaas_dashboard/enabled/_1481_project_ng_loadbalancersv2_panel.py"
+    DEST="${SITE_PACKAGES}/openstack_dashboard/local/enabled/_1481_project_ng_loadbalancersv2_panel.py"
+    if [[ "${ENABLE_NEUTRON_LBAAS}" == "yes" ]] && [[ ! -f ${DEST} ]]; then
+        cp -a $SRC $DEST
+        FORCE_GENERATE="yes"
+    elif [[ "${ENABLE_NEUTRON_LBAAS}" != "yes" ]] && [[ -f ${DEST} ]]; then
+        # remove pyc pyo files too
+        rm -f ${DEST} ${DEST}c ${DEST}o
+        FORCE_GENERATE="yes"
+    fi
+}
+
+config_neutron_lbaas
+
 # NOTE(pbourke): httpd will not clean up after itself in some cases which
 # results in the container not being able to restart. (bug #1489676, 1557036)
 if [[ "${KOLLA_BASE_DISTRO}" =~ debian|ubuntu ]]; then
@@ -25,7 +50,7 @@ fi
 # behavior, so re-generate the compressed javascript and css if it
 # is changed
 MD5SUM_TXT_PATH="/tmp/.local_settings.md5sum.txt"
-if [[ ! -f ${MD5SUM_TXT_PATH} || $(md5sum -c --status ${MD5SUM_TXT_PATH};echo $?) != 0 ]]; then
+if [[ ! -f ${MD5SUM_TXT_PATH} || $(md5sum -c --status ${MD5SUM_TXT_PATH};echo $?) != 0 || ${FORCE_GENERATE} == "yes" ]]; then
     md5sum /etc/openstack-dashboard/local_settings > ${MD5SUM_TXT_PATH}
     if [[ "${KOLLA_INSTALL_TYPE}" == "binary" ]]; then
         /usr/bin/manage.py compress --force