From 5a47049615b6e72dea6369cd4bc6d5b77dc76360 Mon Sep 17 00:00:00 2001
From: Mauricio Lima <mauriciolimab@gmail.com>
Date: Mon, 23 Jan 2017 11:43:20 -0300
Subject: [PATCH] Optimize reconfiguration for magnum

Co-Authored-By: caoyuan <cao.yuan@99cloud.net>
Change-Id: I478bc30b4fcf61507298f16001370de3e129a11f
Partially-implements: blueprint better-reconfigure
---
 ansible/roles/magnum/defaults/main.yml        | 22 ++++++
 ansible/roles/magnum/handlers/main.yml        | 46 ++++++++++++
 .../roles/magnum/tasks/bootstrap_service.yml  | 11 ++-
 ansible/roles/magnum/tasks/config.yml         | 70 +++++++++++++------
 ansible/roles/magnum/tasks/deploy.yml         |  5 +-
 ansible/roles/magnum/tasks/pull.yml           | 16 ++---
 ansible/roles/magnum/tasks/reconfigure.yml    | 69 +-----------------
 ansible/roles/magnum/tasks/start.yml          | 25 -------
 ansible/roles/magnum/tasks/upgrade.yml        |  3 +-
 9 files changed, 134 insertions(+), 133 deletions(-)
 create mode 100644 ansible/roles/magnum/handlers/main.yml
 delete mode 100644 ansible/roles/magnum/tasks/start.yml

diff --git a/ansible/roles/magnum/defaults/main.yml b/ansible/roles/magnum/defaults/main.yml
index c44c254eb8..c2695732be 100644
--- a/ansible/roles/magnum/defaults/main.yml
+++ b/ansible/roles/magnum/defaults/main.yml
@@ -1,6 +1,28 @@
 ---
 project_name: "magnum"
 
+magnum_services:
+  magnum-api:
+    container_name: magnum_api
+    group: magnum-api
+    enabled: true
+    image: "{{ magnum_api_image_full }}"
+    volumes:
+      - "{{ node_config_directory }}/magnum-api/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "kolla_logs:/var/log/kolla/"
+  magnum-conductor:
+    container_name: magnum_conductor
+    group: magnum-conductor
+    enabled: true
+    image: "{{ magnum_conductor_image_full }}"
+    volumes:
+      - "{{ node_config_directory }}/magnum-conductor/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "magnum:/var/lib/magnum/"
+      - "kolla_logs:/var/log/kolla/"
+
+
 ####################
 # Database
 ####################
diff --git a/ansible/roles/magnum/handlers/main.yml b/ansible/roles/magnum/handlers/main.yml
new file mode 100644
index 0000000000..2b15347164
--- /dev/null
+++ b/ansible/roles/magnum/handlers/main.yml
@@ -0,0 +1,46 @@
+---
+- name: Restart magnum-api container
+  vars:
+    service_name: "magnum-api"
+    service: "{{ magnum_services[service_name] }}"
+    config_json: "{{ magnum_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    magnum_conf: "{{ magnum_confs.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    policy_json: "{{ magnum_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    magnum_api_container: "{{ check_magnum_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 }}"
+  when:
+    - action != "config"
+    - inventory_hostname in groups[service.group]
+    - service.enabled | bool
+    - config_json.changed | bool
+      or magnum_conf.changed | bool
+      or policy_json.changed | bool
+      or magnum_api_container.changed | bool
+
+- name: Restart magnum-conductor container
+  vars:
+    service_name: "magnum-conductor"
+    service: "{{ magnum_services[service_name] }}"
+    config_json: "{{ magnum_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    magnum_conf: "{{ magnum_confs.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    policy_json: "{{ magnum_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    magnum_conductor_container: "{{ check_magnum_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 }}"
+  when:
+    - action != "config"
+    - inventory_hostname in groups[service.group]
+    - service.enabled | bool
+    - config_json.changed | bool
+      or magnum_conf.changed | bool
+      or policy_json.changed | bool
+      or magnum_conductor_container.changed | bool
diff --git a/ansible/roles/magnum/tasks/bootstrap_service.yml b/ansible/roles/magnum/tasks/bootstrap_service.yml
index 2f5e2b5031..6cb9122df6 100644
--- a/ansible/roles/magnum/tasks/bootstrap_service.yml
+++ b/ansible/roles/magnum/tasks/bootstrap_service.yml
@@ -1,5 +1,7 @@
 ---
 - name: Running Magnum bootstrap container
+  vars:
+    magnum_api: "{{ magnum_services['magnum-api'] }}"
   kolla_docker:
     action: "start_container"
     common_options: "{{ docker_common_options }}"
@@ -7,14 +9,11 @@
     environment:
       KOLLA_BOOTSTRAP:
       KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
-    image: "{{ magnum_api_image_full }}"
+    image: "{{ magnum_api.image }}"
     labels:
       BOOTSTRAP:
     name: "bootstrap_magnum"
     restart_policy: "never"
-    volumes:
-      - "{{ node_config_directory }}/magnum-api/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "kolla_logs:/var/log/kolla"
+    volumes: "{{ magnum_api.volumes }}"
   run_once: True
-  delegate_to: "{{ groups['magnum-api'][0] }}"
+  delegate_to: "{{ groups[magnum_api.group][0] }}"
diff --git a/ansible/roles/magnum/tasks/config.yml b/ansible/roles/magnum/tasks/config.yml
index 2fa679bc61..514740fbc4 100644
--- a/ansible/roles/magnum/tasks/config.yml
+++ b/ansible/roles/magnum/tasks/config.yml
@@ -1,37 +1,46 @@
 ---
 - name: Ensuring config directories exist
   file:
-    path: "{{ node_config_directory }}/{{ item }}"
+    path: "{{ node_config_directory }}/{{ item.key }}"
     state: "directory"
     recurse: yes
-  with_items:
-    - "magnum-api"
-    - "magnum-conductor"
+  when: inventory_hostname in groups[item.value.group]
+  with_dict: "{{ magnum_services }}"
 
 - name: Copying over config.json files for services
   template:
-    src: "{{ item }}.json.j2"
-    dest: "{{ node_config_directory }}/{{ item }}/config.json"
-  with_items:
-    - "magnum-api"
-    - "magnum-conductor"
+    src: "{{ item.key }}.json.j2"
+    dest: "{{ node_config_directory }}/{{ item.key }}/config.json"
+  register: magnum_config_jsons
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ magnum_services }}"
+  notify:
+    - Restart magnum-api container
+    - Restart magnum-conductor container
 
 - name: Copying over magnum.conf
   merge_configs:
     vars:
-      service_name: "magnum-api"
+      service_name: "{{ item.key }}"
     sources:
       - "{{ role_path }}/templates/magnum.conf.j2"
       - "{{ node_custom_config }}/global.conf"
       - "{{ node_custom_config }}/database.conf"
       - "{{ node_custom_config }}/messaging.conf"
       - "{{ node_custom_config }}/magnum.conf"
-      - "{{ node_custom_config }}/magnum/{{ item }}.conf"
+      - "{{ node_custom_config }}/magnum/{{ item.key }}.conf"
       - "{{ node_custom_config }}/magnum/{{ inventory_hostname }}/magnum.conf"
-    dest: "{{ node_config_directory }}/{{ item }}/magnum.conf"
-  with_items:
-    - "magnum-api"
-    - "magnum-conductor"
+    dest: "{{ node_config_directory }}/{{ item.key }}/magnum.conf"
+  register: magnum_confs
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ magnum_services }}"
+  notify:
+    - Restart magnum-api container
+    - Restart magnum-conductor container
 
 - name: Check if policies shall be overwritten
   local_action: stat path="{{ node_custom_config }}/magnum/policy.json"
@@ -40,9 +49,30 @@
 - name: Copying over existing policy.json
   template:
     src: "{{ node_custom_config }}/magnum/policy.json"
-    dest: "{{ node_config_directory }}/{{ item }}/policy.json"
-  with_items:
-    - "magnum-api"
-    - "magnum-conductor"
+    dest: "{{ node_config_directory }}/{{ item.key }}/policy.json"
+  register: magnum_policy_jsons
   when:
-    magnum_policy.stat.exists
+    - magnum_policy.stat.exists
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ magnum_services }}"
+  notify:
+    - Restart magnum-api container
+    - Restart magnum-conductor container
+
+- name: Check magnum 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_magnum_containers
+  when:
+    - action != "config"
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ magnum_services }}"
+  notify:
+    - Restart magnum-api container
+    - Restart magnum-conductor container
diff --git a/ansible/roles/magnum/tasks/deploy.yml b/ansible/roles/magnum/tasks/deploy.yml
index 50e56814ac..68d5007e8e 100644
--- a/ansible/roles/magnum/tasks/deploy.yml
+++ b/ansible/roles/magnum/tasks/deploy.yml
@@ -9,6 +9,5 @@
 - include: bootstrap.yml
   when: inventory_hostname in groups['magnum-api']
 
-- include: start.yml
-  when: inventory_hostname in groups['magnum-api'] or
-        inventory_hostname in groups['magnum-conductor']
+- name: Flush handlers
+  meta: flush_handlers
diff --git a/ansible/roles/magnum/tasks/pull.yml b/ansible/roles/magnum/tasks/pull.yml
index 3825c7520a..c79c6bcd36 100644
--- a/ansible/roles/magnum/tasks/pull.yml
+++ b/ansible/roles/magnum/tasks/pull.yml
@@ -1,14 +1,10 @@
 ---
-- name: Pulling magnum-api image
+- name: Pulling magnum images
   kolla_docker:
     action: "pull_image"
     common_options: "{{ docker_common_options }}"
-    image: "{{ magnum_api_image_full }}"
-  when: inventory_hostname in groups['magnum-api']
-
-- name: Pulling magnum-conductor image
-  kolla_docker:
-    action: "pull_image"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ magnum_conductor_image_full }}"
-  when: inventory_hostname in groups['magnum-conductor']
+    image: "{{ item.value.image }}"
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ magnum_services }}"
diff --git a/ansible/roles/magnum/tasks/reconfigure.yml b/ansible/roles/magnum/tasks/reconfigure.yml
index 9598ffb89d..e078ef1318 100644
--- a/ansible/roles/magnum/tasks/reconfigure.yml
+++ b/ansible/roles/magnum/tasks/reconfigure.yml
@@ -1,69 +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: magnum_api, group: magnum-api }
-    - { name: magnum_conductor, group: magnum-conductor }
-
-- 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: magnum_api, group: magnum-api }
-    - { name: magnum_conductor, group: magnum-conductor }
-
-# 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: magnum_api, group: magnum-api }
-    - { name: magnum_conductor, group: magnum-conductor }
-
-- name: Remove the containers
-  kolla_docker:
-    name: "{{ item[0]['name'] }}"
-    action: "remove_container"
-  register: remove_containers
-  when:
-    - inventory_hostname in groups[item[0]['group']]
-    - config_strategy == "COPY_ONCE" or item[1]['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE'
-    - item[2]['rc'] == 1
-  with_together:
-    - [{ name: magnum_api, group: magnum-api },
-       { name: magnum_conductor, group: magnum-conductor }]
-    - "{{ 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:
-    - inventory_hostname in groups[item[0]['group']]
-    - config_strategy == 'COPY_ALWAYS'
-    - item[1]['KOLLA_CONFIG_STRATEGY'] != 'COPY_ONCE'
-    - item[2]['rc'] == 1
-  with_together:
-    - [{ name: magnum_api, group: magnum-api },
-       { name: magnum_conductor, group: magnum-conductor }]
-    - "{{ container_envs.results }}"
-    - "{{ check_results.results }}"
+- include: deploy.yml
diff --git a/ansible/roles/magnum/tasks/start.yml b/ansible/roles/magnum/tasks/start.yml
deleted file mode 100644
index b43839484d..0000000000
--- a/ansible/roles/magnum/tasks/start.yml
+++ /dev/null
@@ -1,25 +0,0 @@
----
-- name: Starting magnum-api container
-  kolla_docker:
-    action: "start_container"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ magnum_api_image_full }}"
-    name: "magnum_api"
-    volumes:
-      - "{{ node_config_directory }}/magnum-api/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "kolla_logs:/var/log/kolla/"
-  when: inventory_hostname in groups['magnum-api']
-
-- name: Starting magnum-conductor container
-  kolla_docker:
-    action: "start_container"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ magnum_conductor_image_full }}"
-    name: "magnum_conductor"
-    volumes:
-      - "{{ node_config_directory }}/magnum-conductor/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "magnum:/var/lib/magnum/"
-      - "kolla_logs:/var/log/kolla/"
-  when: inventory_hostname in groups['magnum-conductor']
diff --git a/ansible/roles/magnum/tasks/upgrade.yml b/ansible/roles/magnum/tasks/upgrade.yml
index 308053080c..c38db1adf4 100644
--- a/ansible/roles/magnum/tasks/upgrade.yml
+++ b/ansible/roles/magnum/tasks/upgrade.yml
@@ -3,4 +3,5 @@
 
 - include: bootstrap_service.yml
 
-- include: start.yml
+- name: Flush handlers
+  meta: flush_handlers