From d7dfae75b275acffe64d6dbd65388df6c397d42a Mon Sep 17 00:00:00 2001
From: Sean Mooney <sean.k.mooney@intel.com>
Date: Sat, 30 Jul 2016 01:11:35 +0000
Subject: [PATCH] generate bifrost yaml configs

- This change indroduces a merge_yaml action_plugin
- This change generates bifrost yaml configs.

Change-Id: I9814e6a5d55cbd46c4b60c06ed70ed54a575bd2f
Implements: blueprint bifrost-support
---
 ansible/action_plugins/merge_yaml.py          | 96 +++++++++++++++++++
 ansible/group_vars/all.yml                    |  2 +
 ansible/library/merge_yaml.py                 | 51 ++++++++++
 ansible/roles/bifrost/defaults/main.yml       |  7 ++
 ansible/roles/bifrost/tasks/config.yml        | 21 ++++
 ansible/roles/bifrost/tasks/deploy.yml        |  3 +
 ansible/roles/bifrost/tasks/main.yml          |  2 +
 ansible/roles/bifrost/tasks/reconfigure.yml   |  1 +
 ansible/roles/bifrost/tasks/upgrade.yml       |  1 +
 .../roles/bifrost/templates/bifrost.yml.j2    |  3 +
 ansible/roles/bifrost/templates/dib.yml.j2    |  2 +
 .../roles/bifrost/templates/servers.yml.j2    |  1 +
 12 files changed, 190 insertions(+)
 create mode 100755 ansible/action_plugins/merge_yaml.py
 create mode 100644 ansible/library/merge_yaml.py
 create mode 100644 ansible/roles/bifrost/defaults/main.yml
 create mode 100644 ansible/roles/bifrost/tasks/config.yml
 create mode 100644 ansible/roles/bifrost/tasks/deploy.yml
 create mode 100644 ansible/roles/bifrost/tasks/main.yml
 create mode 100644 ansible/roles/bifrost/tasks/reconfigure.yml
 create mode 100644 ansible/roles/bifrost/tasks/upgrade.yml
 create mode 100644 ansible/roles/bifrost/templates/bifrost.yml.j2
 create mode 100644 ansible/roles/bifrost/templates/dib.yml.j2
 create mode 100644 ansible/roles/bifrost/templates/servers.yml.j2

diff --git a/ansible/action_plugins/merge_yaml.py b/ansible/action_plugins/merge_yaml.py
new file mode 100755
index 0000000000..8eed1559ea
--- /dev/null
+++ b/ansible/action_plugins/merge_yaml.py
@@ -0,0 +1,96 @@
+#!/usr/bin/python
+
+# Copyright 2015 Sam Yaple
+# Copyright 2016 intel
+#
+# 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.
+
+import inspect
+import os
+
+from yaml import dump
+from yaml import load
+try:
+    from yaml import CDumper as Dumper  # noqa: F401
+    from yaml import CLoader as Loader  # noqa: F401
+except ImportError:
+    from yaml import Dumper  # noqa: F401
+    from yaml import Loader  # noqa: F401
+
+
+from ansible.plugins.action import ActionBase
+
+
+class ActionModule(ActionBase):
+
+    TRANSFERS_FILES = True
+
+    def read_config(self, source):
+        result = None
+        # Only use config if present
+        if os.access(source, os.R_OK):
+            with open(source, 'r') as f:
+                template_data = f.read()
+            template_data = self._templar.template(template_data)
+            result = load(template_data)
+        return result or {}
+
+    def run(self, tmp=None, task_vars=None):
+        if task_vars is None:
+            task_vars = dict()
+        result = super(ActionModule, self).run(tmp, task_vars)
+
+        # NOTE(jeffrey4l): Ansible 2.1 add a remote_user param to the
+        # _make_tmp_path function.  inspect the number of the args here. In
+        # this way, ansible 2.0 and ansible 2.1 are both supported
+        make_tmp_path_args = inspect.getargspec(self._make_tmp_path)[0]
+        if not tmp and len(make_tmp_path_args) == 1:
+            tmp = self._make_tmp_path()
+        if not tmp and len(make_tmp_path_args) == 2:
+            remote_user = (task_vars.get('ansible_ssh_user')
+                           or self._play_context.remote_user)
+            tmp = self._make_tmp_path(remote_user)
+        # save template args.
+        extra_vars = self._task.args.get('vars', list())
+        old_vars = self._templar._available_variables
+
+        temp_vars = task_vars.copy()
+        temp_vars.update(extra_vars)
+        self._templar.set_available_variables(temp_vars)
+
+        output = {}
+        sources = self._task.args.get('sources', None)
+        if not isinstance(sources, list):
+            sources = [sources]
+        for source in sources:
+            output.update(self.read_config(source))
+
+        # restore original vars
+        self._templar.set_available_variables(old_vars)
+
+        remote_path = self._connection._shell.join_path(tmp, 'src')
+        xfered = self._transfer_data(remote_path,
+                                     dump(output,
+                                          default_flow_style=False))
+        new_module_args = self._task.args.copy()
+        new_module_args.update(
+            dict(
+                src=xfered
+            )
+        )
+        del new_module_args['sources']
+        result.update(self._execute_module(module_name='copy',
+                                           module_args=new_module_args,
+                                           task_vars=task_vars,
+                                           tmp=tmp))
+        return result
diff --git a/ansible/group_vars/all.yml b/ansible/group_vars/all.yml
index eb588fac23..fc5bb021b0 100644
--- a/ansible/group_vars/all.yml
+++ b/ansible/group_vars/all.yml
@@ -107,6 +107,8 @@ api_interface: "{{ network_interface }}"
 storage_interface: "{{ network_interface }}"
 cluster_interface: "{{ network_interface }}"
 tunnel_interface: "{{ network_interface }}"
+bifrost_network_interface: "{{ network_interface }}"
+
 
 # Valid options are [ openvswitch, linuxbridge ]
 neutron_plugin_agent: "openvswitch"
diff --git a/ansible/library/merge_yaml.py b/ansible/library/merge_yaml.py
new file mode 100644
index 0000000000..66d316fa51
--- /dev/null
+++ b/ansible/library/merge_yaml.py
@@ -0,0 +1,51 @@
+#!/usr/bin/python
+
+# Copyright 2015 Sam Yaple
+# Copyright 2016 intel
+#
+# 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.
+
+DOCUMENTATION = '''
+---
+module: merge_yaml
+short_description: Merge yaml-style configs
+description:
+     - PyYAML is used to merge several yaml files into one
+options:
+  dest:
+    description:
+      - The destination file name
+    required: True
+    type: str
+  sources:
+    description:
+      - A list of files on the destination node to merge together
+    default: None
+    required: True
+    type: str
+author: Sean Mooney
+'''
+
+EXAMPLES = '''
+Merge multiple yaml files:
+
+- hosts: localhost
+  tasks:
+    - name: Merge yaml files
+      merge_yaml:
+        sources:
+          - "/tmp/default.yml"
+          - "/tmp/override.yml"
+        dest:
+          - "/tmp/out.yml"
+'''
diff --git a/ansible/roles/bifrost/defaults/main.yml b/ansible/roles/bifrost/defaults/main.yml
new file mode 100644
index 0000000000..53c51361a3
--- /dev/null
+++ b/ansible/roles/bifrost/defaults/main.yml
@@ -0,0 +1,7 @@
+---
+####################
+# Docker
+####################
+bifrost_deploy_image: "{{ docker_registry ~ '/' if docker_registry else '' }}{{ docker_namespace }}/{{ kolla_base_distro }}-{{ kolla_install_type }}-bifrost-deploy"
+bifrost_deploy_tag: "{{ openstack_release }}"
+bifrost_deploy_image_full: "{{ bifrost_deploy_image }}:{{ bifrost_deploy_tag }}"
diff --git a/ansible/roles/bifrost/tasks/config.yml b/ansible/roles/bifrost/tasks/config.yml
new file mode 100644
index 0000000000..8a66523bc2
--- /dev/null
+++ b/ansible/roles/bifrost/tasks/config.yml
@@ -0,0 +1,21 @@
+---
+- name: Ensuring config directories exist
+  file:
+    path: "{{ node_config_directory }}/{{ item }}"
+    state: "directory"
+    recurse: yes
+  with_items:
+    - "bifrost"
+
+- name: Generate bifrost configs
+  merge_yaml:
+    sources:
+      - "{{ role_path }}/templates/{{ item }}.yml.j2"
+      - "{{ node_custom_config }}/{{ item }}.yml"
+      - "{{ node_custom_config }}/bifrost/{{ item }}.yml"
+    dest: "{{ node_config_directory }}/bifrost/{{ item }}.yml"
+  with_items:
+    - "bifrost"
+    - "dib"
+    - "servers"
+
diff --git a/ansible/roles/bifrost/tasks/deploy.yml b/ansible/roles/bifrost/tasks/deploy.yml
new file mode 100644
index 0000000000..d8717127f9
--- /dev/null
+++ b/ansible/roles/bifrost/tasks/deploy.yml
@@ -0,0 +1,3 @@
+---
+- include: config.yml
+
diff --git a/ansible/roles/bifrost/tasks/main.yml b/ansible/roles/bifrost/tasks/main.yml
new file mode 100644
index 0000000000..b017e8b4ad
--- /dev/null
+++ b/ansible/roles/bifrost/tasks/main.yml
@@ -0,0 +1,2 @@
+---
+- include: "{{ action }}.yml"
diff --git a/ansible/roles/bifrost/tasks/reconfigure.yml b/ansible/roles/bifrost/tasks/reconfigure.yml
new file mode 100644
index 0000000000..ed97d539c0
--- /dev/null
+++ b/ansible/roles/bifrost/tasks/reconfigure.yml
@@ -0,0 +1 @@
+---
diff --git a/ansible/roles/bifrost/tasks/upgrade.yml b/ansible/roles/bifrost/tasks/upgrade.yml
new file mode 100644
index 0000000000..ed97d539c0
--- /dev/null
+++ b/ansible/roles/bifrost/tasks/upgrade.yml
@@ -0,0 +1 @@
+---
diff --git a/ansible/roles/bifrost/templates/bifrost.yml.j2 b/ansible/roles/bifrost/templates/bifrost.yml.j2
new file mode 100644
index 0000000000..224d11a08a
--- /dev/null
+++ b/ansible/roles/bifrost/templates/bifrost.yml.j2
@@ -0,0 +1,3 @@
+mysql_service_name: mysql
+ansible_python_interpreter: /var/lib/kolla/venv/bin/python
+network_interface: "{{ bifrost_network_interface }}"
diff --git a/ansible/roles/bifrost/templates/dib.yml.j2 b/ansible/roles/bifrost/templates/dib.yml.j2
new file mode 100644
index 0000000000..9e54fba23b
--- /dev/null
+++ b/ansible/roles/bifrost/templates/dib.yml.j2
@@ -0,0 +1,2 @@
+create_image_via_dib: "true"
+dib_os_element: "debian"
diff --git a/ansible/roles/bifrost/templates/servers.yml.j2 b/ansible/roles/bifrost/templates/servers.yml.j2
new file mode 100644
index 0000000000..ed97d539c0
--- /dev/null
+++ b/ansible/roles/bifrost/templates/servers.yml.j2
@@ -0,0 +1 @@
+---