From 82d56e94cd15c75109ef75d81a24590a0036957d Mon Sep 17 00:00:00 2001
From: caoyuan <cao.yuan@99cloud.net>
Date: Mon, 23 Jan 2017 23:22:06 +0800
Subject: [PATCH] Optimize reconfiguration for tempest

Change-Id: I75e3a3b32a5ad0bc613a90130fa5bcc65605ca68
Partially-implements: blueprint better-reconfigure
---
 ansible/roles/tempest/defaults/main.yml     | 11 ++++
 ansible/roles/tempest/handlers/main.yml     | 23 ++++++++
 ansible/roles/tempest/tasks/config.yml      | 58 +++++++++++++++----
 ansible/roles/tempest/tasks/deploy.yml      |  3 +-
 ansible/roles/tempest/tasks/pull.yml        |  6 +-
 ansible/roles/tempest/tasks/reconfigure.yml | 64 +--------------------
 ansible/roles/tempest/tasks/start.yml       | 11 ----
 ansible/roles/tempest/tasks/upgrade.yml     |  3 +-
 8 files changed, 90 insertions(+), 89 deletions(-)
 create mode 100644 ansible/roles/tempest/handlers/main.yml
 delete mode 100644 ansible/roles/tempest/tasks/start.yml

diff --git a/ansible/roles/tempest/defaults/main.yml b/ansible/roles/tempest/defaults/main.yml
index cebab6c55..8e2d1e1ff 100644
--- a/ansible/roles/tempest/defaults/main.yml
+++ b/ansible/roles/tempest/defaults/main.yml
@@ -1,6 +1,17 @@
 ---
 project_name: "tempest"
 
+tempest_services:
+  tempest:
+    container_name: "tempest"
+    image: "{{ tempest_image_full }}"
+    enabled: true
+    group: "tempest"
+    volumes:
+      - "{{ node_config_directory }}/tempest/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "kolla_logs:/var/log/kolla/"
+
 
 ########
 # Docker
diff --git a/ansible/roles/tempest/handlers/main.yml b/ansible/roles/tempest/handlers/main.yml
new file mode 100644
index 000000000..64c0f48e1
--- /dev/null
+++ b/ansible/roles/tempest/handlers/main.yml
@@ -0,0 +1,23 @@
+- name: Restart tempest container
+  vars:
+    service_name: "tempest"
+    service: "{{ tempest_services[service_name] }}"
+    config_json: "{{ tempest_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    tempest_conf: "{{ tempest_confs.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    policy_json: "{{ tempest_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    tempest_container: "{{ check_tempest_containers.results|selectattr('item.key', 'equalto', service_name)|first }}"
+  kolla_docker:
+    action: "recreate_or_restart_container"
+    common_options: "{{ docker_common_options }}"
+    name: "{{ service.container_name }}"
+    image: "{{ service.image }}"
+    privileged: "{{ service.privileged | default(False) }}"
+    volumes: "{{ service.volumes|reject('equalto', '')|list }}"
+  when:
+    - action != "config"
+    - inventory_hostname in groups[service.group]
+    - service.enabled | bool
+    - config_json.changed | bool
+      or tempest_conf.changed | bool
+      or policy_json.changed | bool
+      or tempest_container.changed | bool
diff --git a/ansible/roles/tempest/tasks/config.yml b/ansible/roles/tempest/tasks/config.yml
index 6422270d3..374b78168 100644
--- a/ansible/roles/tempest/tasks/config.yml
+++ b/ansible/roles/tempest/tasks/config.yml
@@ -1,18 +1,25 @@
 ---
 - name: Ensuring config directories exist
   file:
-    path: "{{ node_config_directory }}/{{ item }}"
+    path: "{{ node_config_directory }}/{{ item.key }}"
     state: "directory"
     recurse: yes
-  with_items:
-    - "tempest"
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ tempest_services }}"
 
 - name: Copying over config.json files for services
   template:
-    src: "{{ item }}.json.j2"
-    dest: "{{ node_config_directory }}/{{ item }}/config.json"
-  with_items:
-    - "tempest"
+    src: "{{ item.key }}.json.j2"
+    dest: "{{ node_config_directory }}/{{ item.key }}/config.json"
+  register: tempest_config_jsons
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ tempest_services }}"
+  notify:
+    - Restart tempest container
 
 - name: Copying over tempest.conf
   merge_configs:
@@ -21,9 +28,14 @@
     sources:
       - "{{ role_path }}/templates/tempest.conf.j2"
       - "{{ node_custom_config }}/tempest.conf"
-    dest: "{{ node_config_directory }}/{{ item }}/tempest.conf"
-  with_items:
-    - "tempest"
+    dest: "{{ node_config_directory }}/{{ item.key }}/tempest.conf"
+  register: tempest_confs
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ tempest_services }}"
+  notify:
+    - Restart tempest container
 
 - name: Check if policies shall be overwritten
   local_action: stat path="{{ node_custom_config }}/tempest/policy.json"
@@ -32,6 +44,28 @@
 - name: Copying over existing policy.json
   template:
     src: "{{ node_custom_config }}/tempest/policy.json"
-    dest: "{{ node_config_directory }}/tempest/policy.json"
+    dest: "{{ node_config_directory }}/{{ item.key }}/policy.json"
+  register: tempest_policy_jsons
+  when:
+    - tempest_policy.stat.exists
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ tempest_services }}"
+  notify:
+    - Restart tempest container
+
+- name: Check tempest containers
+  kolla_docker:
+    action: "compare_container"
+    common_options: "{{ docker_common_options }}"
+    name: "{{ item.value.container_name }}"
+    image: "{{ item.value.image }}"
+    volumes: "{{ item.value.volumes }}"
+  register: check_tempest_containers
   when:
-    tempest_policy.stat.exists
+    - action != "config"
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ tempest_services }}"
+  notify:
+    - Restart tempest container
diff --git a/ansible/roles/tempest/tasks/deploy.yml b/ansible/roles/tempest/tasks/deploy.yml
index 1f16915ad..dd26ecc34 100644
--- a/ansible/roles/tempest/tasks/deploy.yml
+++ b/ansible/roles/tempest/tasks/deploy.yml
@@ -1,4 +1,5 @@
 ---
 - include: config.yml
 
-- include: start.yml
+- name: Flush handlers
+  meta: flush_handlers
diff --git a/ansible/roles/tempest/tasks/pull.yml b/ansible/roles/tempest/tasks/pull.yml
index a31b62535..003cae0b0 100644
--- a/ansible/roles/tempest/tasks/pull.yml
+++ b/ansible/roles/tempest/tasks/pull.yml
@@ -3,4 +3,8 @@
   kolla_docker:
     action: "pull_image"
     common_options: "{{ docker_common_options }}"
-    image: "{{ tempest_image_full }}"
+    image: "{{ item.value.image }}"
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ tempest_services }}"
diff --git a/ansible/roles/tempest/tasks/reconfigure.yml b/ansible/roles/tempest/tasks/reconfigure.yml
index 0aeab552f..e078ef131 100644
--- a/ansible/roles/tempest/tasks/reconfigure.yml
+++ b/ansible/roles/tempest/tasks/reconfigure.yml
@@ -1,64 +1,2 @@
 ---
-- name: Ensuring the containers up
-  kolla_docker:
-    name: "{{ item.name }}"
-    action: "get_container_state"
-  register: container_state
-  failed_when: container_state.Running == false
-  when: inventory_hostname in groups[item.group]
-  with_items:
-    - { name: tempest, group: tempest }
-
-- include: config.yml
-
-- name: Check the configs
-  command: docker exec {{ item.name }} /usr/local/bin/kolla_set_configs --check
-  changed_when: false
-  failed_when: false
-  register: check_results
-  when: inventory_hostname in groups[item.group]
-  with_items:
-    - { name: tempest, group: tempest }
-
-# NOTE(jeffrey4l): when config_strategy == 'COPY_ALWAYS'
-# and container env['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE',
-# just remove the container and start again
-- name: Containers config strategy
-  kolla_docker:
-    name: "{{ item.name }}"
-    action: "get_container_env"
-  register: container_envs
-  when: inventory_hostname in groups[item.group]
-  with_items:
-    - { name: tempest, group: tempest }
-
-- name: Remove the containers
-  kolla_docker:
-    name: "{{ item[0]['name'] }}"
-    action: "remove_container"
-  register: remove_containers
-  when:
-    - config_strategy == "COPY_ONCE" or item[1]['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE'
-    - item[2]['rc'] == 1
-    - inventory_hostname in groups[item[0]['group']]
-  with_together:
-    - [{ name: tempest, group: tempest }]
-    - "{{ container_envs.results }}"
-    - "{{ check_results.results }}"
-
-- include: start.yml
-  when: remove_containers.changed
-
-- name: Restart containers
-  kolla_docker:
-    name: "{{ item[0]['name'] }}"
-    action: "restart_container"
-  when:
-    - config_strategy == 'COPY_ALWAYS'
-    - item[1]['KOLLA_CONFIG_STRATEGY'] != 'COPY_ONCE'
-    - item[2]['rc'] == 1
-    - inventory_hostname in groups[item[0]['group']]
-  with_together:
-    - [{ name: tempest, group: tempest }]
-    - "{{ container_envs.results }}"
-    - "{{ check_results.results }}"
+- include: deploy.yml
diff --git a/ansible/roles/tempest/tasks/start.yml b/ansible/roles/tempest/tasks/start.yml
deleted file mode 100644
index 97b6552c5..000000000
--- a/ansible/roles/tempest/tasks/start.yml
+++ /dev/null
@@ -1,11 +0,0 @@
----
-- name: Starting tempest container
-  kolla_docker:
-    action: "start_container"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ tempest_image_full }}"
-    name: "tempest"
-    volumes:
-      - "{{ node_config_directory }}/tempest/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "kolla_logs:/var/log/kolla/"
diff --git a/ansible/roles/tempest/tasks/upgrade.yml b/ansible/roles/tempest/tasks/upgrade.yml
index 1f16915ad..dd26ecc34 100644
--- a/ansible/roles/tempest/tasks/upgrade.yml
+++ b/ansible/roles/tempest/tasks/upgrade.yml
@@ -1,4 +1,5 @@
 ---
 - include: config.yml
 
-- include: start.yml
+- name: Flush handlers
+  meta: flush_handlers
-- 
GitLab