From 4ede1ae605c6df25e27823fba68b37a612be070a Mon Sep 17 00:00:00 2001
From: Mark Goddard <mark@stackhpc.com>
Date: Wed, 15 Mar 2017 09:44:19 +0000
Subject: [PATCH] Install Bifrost into Kayobe virtualenv

---
 ansible/group_vars/all/bifrost                |  9 ++++
 ansible/group_vars/all/kolla                  |  4 +-
 ansible/kolla-bifrost.yml                     | 10 +++++
 ansible/roles/kolla-bifrost/defaults/main.yml | 12 +++++
 ansible/roles/kolla-bifrost/tasks/config.yml  | 18 ++++++++
 ansible/roles/kolla-bifrost/tasks/install.yml | 44 +++++++++++++++++++
 ansible/roles/kolla-bifrost/tasks/main.yml    | 19 +-------
 etc/kayobe/bifrost.yml                        |  9 ++++
 8 files changed, 106 insertions(+), 19 deletions(-)
 create mode 100644 ansible/roles/kolla-bifrost/tasks/config.yml
 create mode 100644 ansible/roles/kolla-bifrost/tasks/install.yml

diff --git a/ansible/group_vars/all/bifrost b/ansible/group_vars/all/bifrost
index dcd1e4de6..51eb1646c 100644
--- a/ansible/group_vars/all/bifrost
+++ b/ansible/group_vars/all/bifrost
@@ -1,6 +1,15 @@
 ---
 # Kayobe configuration for Bifrost.
 
+###############################################################################
+# Bifrost installation.
+
+# URL of Bifrost source code repository.
+kolla_bifrost_source_url: "https://github.com/stackhpc/bifrost"
+
+# Version (branch, tag, etc.) of Bifrost source code repository.
+kolla_bifrost_source_version: "stackhpc-3.0.0"
+
 ###############################################################################
 # Diskimage-builder configuration.
 
diff --git a/ansible/group_vars/all/kolla b/ansible/group_vars/all/kolla
index 94cbe1cad..4022017fb 100644
--- a/ansible/group_vars/all/kolla
+++ b/ansible/group_vars/all/kolla
@@ -51,8 +51,8 @@ kolla_openstack_release: "4.0.0.0rc1"
 kolla_sources:
   bifrost-base:
     type: "git"
-    location: "https://github.com/stackhpc/bifrost"
-    reference: "stackhpc-3.0.0"
+    location: "{{ kolla_bifrost_source_url }}"
+    reference: "{{ kolla_bifrost_source_version }}"
 
 ###############################################################################
 # Kolla-ansible configuration.
diff --git a/ansible/kolla-bifrost.yml b/ansible/kolla-bifrost.yml
index 17c5ff992..294cf6ea4 100644
--- a/ansible/kolla-bifrost.yml
+++ b/ansible/kolla-bifrost.yml
@@ -7,6 +7,14 @@
       - { name: agent_ipmitool, enabled: "{{ kolla_bifrost_enable_ipmitool_drivers | bool }}" }
 
   pre_tasks:
+    - name: Fail if not running in a virtualenv
+      fail:
+        msg: >
+          This playbook should be executed from a virtualenv in order to avoid
+          installing python packages to the system location. Typically this
+          will be named 'kayobe-venv'
+      when: "{{ not lookup('env', 'VIRTUAL_ENV') }}"
+
     - name: Check whether a Kolla Bifrost extra globals configuration file exists
       stat:
         path: "{{ kolla_bifrost_extra_globals_path }}"
@@ -20,6 +28,8 @@
   roles:
     - role: kolla-bifrost
 
+      kolla_bifrost_venv: "{{ lookup('env', 'VIRTUAL_ENV') }}"
+
       # Generate a list of enabled drivers from the map.
       kolla_bifrost_enabled_drivers: >
         {{ kolla_bifrost_driver_map | selectattr('enabled') | map(attribute='name') | list }}
diff --git a/ansible/roles/kolla-bifrost/defaults/main.yml b/ansible/roles/kolla-bifrost/defaults/main.yml
index 2ac0cc855..fc3b495e4 100644
--- a/ansible/roles/kolla-bifrost/defaults/main.yml
+++ b/ansible/roles/kolla-bifrost/defaults/main.yml
@@ -1,4 +1,16 @@
 ---
+# Path to directory for source code checkouts.
+source_checkout_path:
+
+# Virtualenv directory where Bifrost will be installed.
+kolla_bifrost_venv:
+
+# URL of Bifrost source code repository.
+kolla_bifrost_source_url:
+
+# Version (branch, tag, etc.) of Bifrost source code repository.
+kolla_bifrost_source_version:
+
 # Directory where Kolla custom configuration files will be installed.
 kolla_node_custom_config_path:
 
diff --git a/ansible/roles/kolla-bifrost/tasks/config.yml b/ansible/roles/kolla-bifrost/tasks/config.yml
new file mode 100644
index 000000000..eae1faf6d
--- /dev/null
+++ b/ansible/roles/kolla-bifrost/tasks/config.yml
@@ -0,0 +1,18 @@
+---
+- name: Ensure the Kolla Bifrost configuration directores exist
+  file:
+    path: "{{ kolla_node_custom_config_path }}/bifrost"
+    state: directory
+    mode: 0755
+  become: True
+
+- name: Ensure the Kolla Bifrost configuration files exist
+  template:
+    src: "{{ item.src }}"
+    dest: "{{ kolla_node_custom_config_path }}/bifrost/{{ item.dest }}"
+    mode: 0644
+  become: True
+  with_items:
+    - { src: bifrost.yml.j2, dest: bifrost.yml }
+    - { src: dib.yml.j2, dest: dib.yml }
+    - { src: servers.yml.j2, dest: servers.yml }
diff --git a/ansible/roles/kolla-bifrost/tasks/install.yml b/ansible/roles/kolla-bifrost/tasks/install.yml
new file mode 100644
index 000000000..82a2e48a3
--- /dev/null
+++ b/ansible/roles/kolla-bifrost/tasks/install.yml
@@ -0,0 +1,44 @@
+---
+- name: Ensure required packages are installed
+  yum:
+    name: "{{ item }}"
+    state: installed
+  become: True
+  with_items:
+    - gcc
+    - libffi-devel
+    - openssl-devel
+    - python-devel
+    - python-pip
+    - python-virtualenv
+
+- name: Ensure the latest version of pip is installed
+  pip:
+    name: "{{ item.name }}"
+    state: latest
+    virtualenv: "{{ kolla_bifrost_venv }}"
+  with_items:
+    - { name: pip }
+
+- name: Ensure source code checkout path exists
+  file:
+    path: "{{ source_checkout_path }}"
+    state: directory
+    recurse: True
+
+- name: Ensure Bifrost source code checkout exists
+  git:
+    repo: "{{ kolla_bifrost_source_url }}"
+    dest: "{{ source_checkout_path }}/bifrost"
+    version: "{{ kolla_bifrost_source_version }}"
+
+- name: Ensure required Python packages are installed
+  pip:
+    name: "{{ item.name }}"
+    version: "{{ item.version | default(omit) }}"
+    state: present
+    virtualenv: "{{ kolla_bifrost_venv }}"
+  with_items:
+    # Intall Bifrost from source.
+    - name: "{{ source_checkout_path }}/bifrost"
+    - name: shade
diff --git a/ansible/roles/kolla-bifrost/tasks/main.yml b/ansible/roles/kolla-bifrost/tasks/main.yml
index eae1faf6d..8570db68c 100644
--- a/ansible/roles/kolla-bifrost/tasks/main.yml
+++ b/ansible/roles/kolla-bifrost/tasks/main.yml
@@ -1,18 +1,3 @@
 ---
-- name: Ensure the Kolla Bifrost configuration directores exist
-  file:
-    path: "{{ kolla_node_custom_config_path }}/bifrost"
-    state: directory
-    mode: 0755
-  become: True
-
-- name: Ensure the Kolla Bifrost configuration files exist
-  template:
-    src: "{{ item.src }}"
-    dest: "{{ kolla_node_custom_config_path }}/bifrost/{{ item.dest }}"
-    mode: 0644
-  become: True
-  with_items:
-    - { src: bifrost.yml.j2, dest: bifrost.yml }
-    - { src: dib.yml.j2, dest: dib.yml }
-    - { src: servers.yml.j2, dest: servers.yml }
+- include: install.yml
+- include: config.yml
diff --git a/etc/kayobe/bifrost.yml b/etc/kayobe/bifrost.yml
index 9e2dd34b6..c7ac96526 100644
--- a/etc/kayobe/bifrost.yml
+++ b/etc/kayobe/bifrost.yml
@@ -1,6 +1,15 @@
 ---
 # Kayobe configuration for Bifrost.
 
+###############################################################################
+# Bifrost installation.
+
+# URL of Bifrost source code repository.
+#kolla_bifrost_source_url:
+
+# Version (branch, tag, etc.) of Bifrost source code repository.
+#kolla_bifrost_source_version:
+
 ###############################################################################
 # Diskimage-builder configuration.