From dfb0e55740d52d093772b235c759278ce790c8e3 Mon Sep 17 00:00:00 2001
From: "Michal (inc0) Jastrzebski" <inc007@gmail.com>
Date: Wed, 8 Jun 2016 18:12:53 +0000
Subject: [PATCH] Customizations continued

To correctly customize lists, we need to specify *_append, *_remove or
*_override variable in customization file.

Change-Id: I18d67ab89089e2696399ff1b99c1047a2f554442
Partially-implements: blueprint third-party-plugin-support
---
 kolla/image/build.py       | 12 ++++++++++++
 kolla/template/__init__.py |  0
 kolla/template/filters.py  | 29 +++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+)
 create mode 100644 kolla/template/__init__.py
 create mode 100644 kolla/template/filters.py

diff --git a/kolla/image/build.py b/kolla/image/build.py
index 9d3b388615..f34ab53aaf 100755
--- a/kolla/image/build.py
+++ b/kolla/image/build.py
@@ -38,6 +38,7 @@ from oslo_config import cfg
 from requests import exceptions as requests_exc
 import six
 
+
 PROJECT_ROOT = os.path.abspath(os.path.join(
     os.path.dirname(os.path.realpath(__file__)), '../..'))
 
@@ -49,6 +50,7 @@ if PROJECT_ROOT not in sys.path:
 
 from kolla.common import config as common_config
 from kolla.common import task
+from kolla.template import filters as jinja_filters
 from kolla import version
 
 logging.basicConfig()
@@ -607,12 +609,19 @@ class KollaWorker(object):
                 os.utime(os.path.join(root, dir_), (0, 0))
         LOG.debug('Set atime and mtime to 0 for all content in working dir')
 
+    def _get_filters(self):
+        filters = {
+            'customizable': jinja_filters.customizable,
+        }
+        return filters
+
     def create_dockerfiles(self):
         kolla_version = version.version_info.cached_version_string()
         supported_distro_release = common_config.DISTRO_RELEASE.get(
             self.base)
         for path in self.docker_build_paths:
             template_name = "Dockerfile.j2"
+            image_name = path.split("/")[-1]
             values = {'base_distro': self.base,
                       'base_image': self.conf.base_image,
                       'base_distro_tag': self.base_tag,
@@ -624,9 +633,11 @@ class KollaWorker(object):
                       'tag': self.tag,
                       'maintainer': self.maintainer,
                       'kolla_version': kolla_version,
+                      'image_name': image_name,
                       'rpm_setup': self.rpm_setup}
             env = jinja2.Environment(  # nosec: not used to render HTML
                 loader=jinja2.FileSystemLoader(self.working_dir))
+            env.filters.update(self._get_filters())
             tpl_path = os.path.join(
                 os.path.relpath(path, self.working_dir),
                 template_name)
@@ -638,6 +649,7 @@ class KollaWorker(object):
                 values['parent_template'] = template
                 env = jinja2.Environment(  # nosec: not used to render HTML
                     loader=jinja2.FileSystemLoader(template_path))
+                env.filters.update(self._get_filters())
                 template = env.get_template(template_name)
             if self.include_header:
                 with open(self.include_header, 'r') as f:
diff --git a/kolla/template/__init__.py b/kolla/template/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/kolla/template/filters.py b/kolla/template/filters.py
new file mode 100644
index 0000000000..1322ef4d1c
--- /dev/null
+++ b/kolla/template/filters.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from jinja2 import contextfilter
+
+
+@contextfilter
+def customizable(context, val_list, call_type):
+    name = context['image_name'].replace("-", "_") + "_" + call_type + "_"
+    if name + "override" in context:
+        return context[name + "override"]
+    if name + "append" in context:
+        val_list.extend(context[name + "append"])
+    if name + "remove" in context:
+        for removal in context[name + "remove"]:
+            if removal in val_list:
+                val_list.remove(removal)
+    return val_list