From 18a25fdb0580f0d68c93b2c63de99554034e19a7 Mon Sep 17 00:00:00 2001
From: Pierre Riteau <pierre@stackhpc.com>
Date: Wed, 31 Mar 2021 16:44:14 +0200
Subject: [PATCH] Exit when environment clashes are detected

When running kolla-ansible using a Kayobe environment, write the
environment name to $KOLLA_CONFIG_PATH/.environment. In later
executions, check that the environment name is the same. Warn user to
clean up $KOLLA_CONFIG_PATH otherwise.

Change-Id: Ibbb1fdcad4034620ff8fbfe184d803bcc86f53f9
Story: 2002009
Task: 42185
---
 ansible/kolla-ansible.yml                    | 24 +++++++++++++++++++-
 ansible/roles/kolla-ansible/tasks/config.yml |  7 ++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/ansible/kolla-ansible.yml b/ansible/kolla-ansible.yml
index f445c4f6..7a5f9e49 100644
--- a/ansible/kolla-ansible.yml
+++ b/ansible/kolla-ansible.yml
@@ -28,8 +28,30 @@
     - kolla-ansible
   gather_facts: false
   pre_tasks:
-    # Configuration of extra user-provided Kolla globals.
     - block:
+        - name: Look for environment file in Kolla configuration path
+          stat:
+            path: "{{ kolla_config_path ~ '/.environment' }}"
+            get_checksum: False
+            get_md5: False
+            mime: False
+          register: kolla_environment_file
+
+        - name: Flag that the Kolla configuration path has been used by another environment
+          vars:
+            kolla_environment: "{{ lookup('file', kolla_config_path ~ '/.environment') }}"
+          fail:
+            msg: >
+              The Kolla configuration path at {{ kolla_config_path }} was
+              already used by environment {{ kolla_environment }}. You should
+              clean up this directory, including the environment file located
+              at {{ kolla_config_path ~ '/.environment' }}, before running
+              Kayobe again.
+          when:
+            - kolla_environment_file.stat.exists
+            - kolla_environment != kayobe_environment
+
+        # Configuration of extra user-provided Kolla globals.
         - name: Check whether a Kolla extra globals configuration file exists
           stat:
             path: "{{ kayobe_env_config_path ~ '/kolla/globals.yml' }}"
diff --git a/ansible/roles/kolla-ansible/tasks/config.yml b/ansible/roles/kolla-ansible/tasks/config.yml
index 8e9accd4..79f61c63 100644
--- a/ansible/roles/kolla-ansible/tasks/config.yml
+++ b/ansible/roles/kolla-ansible/tasks/config.yml
@@ -40,6 +40,13 @@
     - "{{ kolla_overcloud_inventory_path }}/group_vars"
     - "{{ kolla_node_custom_config_path }}"
 
+- name: Write environment file into Kolla configuration path
+  copy:
+    dest: "{{ kolla_config_path ~ '/.environment' }}"
+    content: |
+      {{ kayobe_environment }}
+  when: (kayobe_environment | default('')) | length > 0
+
 - name: Ensure the Kolla global configuration file exists
   template:
     src: "globals.yml.j2"
-- 
GitLab