From 05e874040cd096cb8704897bc95546b8cc612d76 Mon Sep 17 00:00:00 2001
From: caoyuan <cao.yuan@99cloud.net>
Date: Mon, 23 Jan 2017 23:27:48 +0800
Subject: [PATCH] Optimize reconfigure action for vmtp

Change-Id: I9f76c8e7520cabcc0a35394c5a2ef48962e3939d
Partially-implements: blueprint better-reconfigure
---
 ansible/roles/vmtp/defaults/main.yml     | 12 +++++
 ansible/roles/vmtp/handlers/main.yml     | 17 +++++++
 ansible/roles/vmtp/tasks/config.yml      | 30 +++++++++++-
 ansible/roles/vmtp/tasks/deploy.yml      |  4 +-
 ansible/roles/vmtp/tasks/pull.yml        |  7 ++-
 ansible/roles/vmtp/tasks/reconfigure.yml | 58 +-----------------------
 ansible/roles/vmtp/tasks/start.yml       | 11 -----
 ansible/roles/vmtp/tasks/upgrade.yml     |  3 +-
 8 files changed, 68 insertions(+), 74 deletions(-)
 create mode 100644 ansible/roles/vmtp/handlers/main.yml
 delete mode 100644 ansible/roles/vmtp/tasks/start.yml

diff --git a/ansible/roles/vmtp/defaults/main.yml b/ansible/roles/vmtp/defaults/main.yml
index 1899d8be0b..d16de6c305 100644
--- a/ansible/roles/vmtp/defaults/main.yml
+++ b/ansible/roles/vmtp/defaults/main.yml
@@ -1,6 +1,18 @@
 ---
 project_name: "vmtp"
 
+vmtp_services:
+  vmtp:
+    container_name: "vmtp"
+    image: "{{ vmtp_image_full }}"
+    enabled: true
+    group: "vmtp"
+    volumes:
+      - "{{ node_config_directory }}/vmtp/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "kolla_logs:/var/log/kolla"
+
+
 ####################
 # Docker
 ####################
diff --git a/ansible/roles/vmtp/handlers/main.yml b/ansible/roles/vmtp/handlers/main.yml
new file mode 100644
index 0000000000..abc8fe1a69
--- /dev/null
+++ b/ansible/roles/vmtp/handlers/main.yml
@@ -0,0 +1,17 @@
+- name: Restart vmtp container
+  vars:
+    service_name: "vmtp"
+    service: "{{ vmtp_services[service_name] }}"
+    vmtp_container: "{{ check_vmtp_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 }}"
+    volumes: "{{ service.volumes|reject('equalto', '')|list }}"
+  when:
+    - action != "config"
+    - inventory_hostname in groups[service.group]
+    - service.enabled | bool
+    - vmtp_confs.changed | bool
+      or vmtp_container.changed | bool
diff --git a/ansible/roles/vmtp/tasks/config.yml b/ansible/roles/vmtp/tasks/config.yml
index c63c93e806..b62b709007 100644
--- a/ansible/roles/vmtp/tasks/config.yml
+++ b/ansible/roles/vmtp/tasks/config.yml
@@ -1,9 +1,13 @@
 ---
 - name: Ensuring config directories exist
   file:
-    path: "{{ node_config_directory }}/vmtp"
+    path: "{{ node_config_directory }}/{{ item.key }}"
     state: "directory"
     recurse: yes
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ vmtp_services }}"
 
 - name: Register binary python path
   command: echo /usr/lib/python2.7/site-packages
@@ -16,11 +20,35 @@
   when: kolla_install_type != 'binary'
 
 - name: Copying over configuration file for vmtp
+  vars:
+    service: "{{ vmtp_services['vmtp'] }}"
   merge_yaml:
     sources:
       - "{{ role_path }}/templates/{{ item }}.j2"
       - "{{ node_custom_config }}/{{ item }}"
       - "{{ node_custom_config }}/vmtp/{{ item }}"
     dest: "{{ python_path }}/vmtp/{{ item }}"
+  register: vmtp_confs
+  when:
+    - inventory_hostname in groups[service.group]
+    - service.enabled | bool
   with_items:
     - "cfg.default.yaml"
+  notify:
+    - Restart vmtp container
+
+- name: Check vmtp 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_vmtp_containers
+  when:
+    - action != "config"
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ vmtp_services }}"
+  notify:
+    - Restart vmtp container
diff --git a/ansible/roles/vmtp/tasks/deploy.yml b/ansible/roles/vmtp/tasks/deploy.yml
index d4daf108c1..5268b68025 100644
--- a/ansible/roles/vmtp/tasks/deploy.yml
+++ b/ansible/roles/vmtp/tasks/deploy.yml
@@ -2,5 +2,5 @@
 - include: config.yml
   when: inventory_hostname in groups['vmtp']
 
-- include: start.yml
-  when: inventory_hostname in groups['vmtp']
+- name: Flush handlers
+  meta: flush_handlers
diff --git a/ansible/roles/vmtp/tasks/pull.yml b/ansible/roles/vmtp/tasks/pull.yml
index 0be3c5eece..dc06d6d812 100644
--- a/ansible/roles/vmtp/tasks/pull.yml
+++ b/ansible/roles/vmtp/tasks/pull.yml
@@ -3,5 +3,8 @@
   kolla_docker:
     action: "pull_image"
     common_options: "{{ docker_common_options }}"
-    image: "{{ vmtp_image_full }}"
-  when: inventory_hostname in groups['vmtp']
+    image: "{{ item.value.image }}"
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ vmtp_services }}"
diff --git a/ansible/roles/vmtp/tasks/reconfigure.yml b/ansible/roles/vmtp/tasks/reconfigure.yml
index 3cf5f29402..e078ef1318 100644
--- a/ansible/roles/vmtp/tasks/reconfigure.yml
+++ b/ansible/roles/vmtp/tasks/reconfigure.yml
@@ -1,58 +1,2 @@
 ---
-- name: Ensure container is up
-  kolla_docker:
-    name: "vmtp"
-    action: "get_container_state"
-  register: container_state
-  failed_when: container_state.Running == false
-  when: inventory_hostname in groups['vmtp']
-
-- include: config.yml
-
-- name: Check configuration
-  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['vmtp']
-  with_items:
-    - { name: vmtp, group: vmtp }
-
-# 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: "vmtp"
-    action: "get_container_env"
-  register: container_envs
-  when: inventory_hostname in groups['vmtp']
-
-- name: Remove the containers
-  kolla_docker:
-    name: "vmtp"
-    action: "remove_container"
-  register: remove_containers
-  when:
-    - config_strategy == "COPY_ONCE" or item[0]['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE'
-    - item[1]['rc'] == 1
-    - inventory_hostname in groups['vmtp']
-  with_together:
-    - "{{ container_envs.results }}"
-    - "{{ check_results.results }}"
-
-- include: start.yml
-  when: remove_containers.changed
-
-- name: Restart containers
-  kolla_docker:
-    name: "vmtp"
-    action: "restart_container"
-  when:
-    - config_strategy == 'COPY_ALWAYS'
-    - item[0]['KOLLA_CONFIG_STRATEGY'] != 'COPY_ONCE'
-    - item[1]['rc'] == 1
-    - inventory_hostname in groups['vmtp']
-  with_together:
-    - "{{ container_envs.results }}"
-    - "{{ check_results.results }}"
+- include: deploy.yml
diff --git a/ansible/roles/vmtp/tasks/start.yml b/ansible/roles/vmtp/tasks/start.yml
deleted file mode 100644
index c3a2b19b73..0000000000
--- a/ansible/roles/vmtp/tasks/start.yml
+++ /dev/null
@@ -1,11 +0,0 @@
----
-- name: Starting vmtp container
-  kolla_docker:
-    action: "start_container"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ vmtp_image_full }}"
-    name: "vmtp"
-    volumes:
-      - "{{ node_config_directory }}/vmtp/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "kolla_logs:/var/log/kolla"
diff --git a/ansible/roles/vmtp/tasks/upgrade.yml b/ansible/roles/vmtp/tasks/upgrade.yml
index 1f16915ad9..dd26ecc34d 100644
--- a/ansible/roles/vmtp/tasks/upgrade.yml
+++ b/ansible/roles/vmtp/tasks/upgrade.yml
@@ -1,4 +1,5 @@
 ---
 - include: config.yml
 
-- include: start.yml
+- name: Flush handlers
+  meta: flush_handlers
-- 
GitLab