From d0100432fc99bd94121b0da8537f6319c45bf0bb Mon Sep 17 00:00:00 2001
From: Mauricio Lima <mauriciolimab@gmail.com>
Date: Mon, 23 Jan 2017 11:41:57 -0300
Subject: [PATCH] Optimize reconfiguration for karbor

Co-Authored-By: caoyuan <cao.yuan@99cloud.net>
Change-Id: Ic5b3b7b1f8009203c8ff870a75b150704eb54e6d
Partially-implements: blueprint better-reconfigure
---
 ansible/roles/karbor/defaults/main.yml        | 30 +++++++
 ansible/roles/karbor/handlers/main.yml        | 66 +++++++++++++++
 .../roles/karbor/tasks/bootstrap_service.yml  | 11 ++-
 ansible/roles/karbor/tasks/config.yml         | 82 +++++++++++++------
 ansible/roles/karbor/tasks/deploy.yml         |  6 +-
 ansible/roles/karbor/tasks/pull.yml           | 23 ++----
 ansible/roles/karbor/tasks/reconfigure.yml    | 71 +---------------
 ansible/roles/karbor/tasks/start.yml          | 36 --------
 ansible/roles/karbor/tasks/upgrade.yml        |  3 +-
 9 files changed, 170 insertions(+), 158 deletions(-)
 create mode 100644 ansible/roles/karbor/handlers/main.yml
 delete mode 100644 ansible/roles/karbor/tasks/start.yml

diff --git a/ansible/roles/karbor/defaults/main.yml b/ansible/roles/karbor/defaults/main.yml
index 1b2e659be1..ed7b417131 100644
--- a/ansible/roles/karbor/defaults/main.yml
+++ b/ansible/roles/karbor/defaults/main.yml
@@ -1,6 +1,36 @@
 ---
 project_name: "karbor"
 
+karbor_services:
+  karbor-api:
+    container_name: karbor_api
+    group: karbor-api
+    enabled: true
+    image: "{{ karbor_api_image_full }}"
+    volumes:
+      - "{{ node_config_directory }}/karbor-api/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "kolla_logs:/var/log/kolla/"
+  karbor-protection:
+    container_name: karbor_protection
+    group: karbor-protection
+    enabled: true
+    image: "{{ karbor_protection_image_full }}"
+    volumes:
+      - "{{ node_config_directory }}/karbor-protection/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "kolla_logs:/var/log/kolla/"
+  karbor-operationengine:
+    container_name: karbor_operationengine
+    group: karbor-operationengine
+    enabled: true
+    image: "{{ karbor_operationengine_image_full }}"
+    volumes:
+      - "{{ node_config_directory }}/karbor-operationengine/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "kolla_logs:/var/log/kolla/"
+
+
 ####################
 # Database
 ####################
diff --git a/ansible/roles/karbor/handlers/main.yml b/ansible/roles/karbor/handlers/main.yml
new file mode 100644
index 0000000000..a179280c39
--- /dev/null
+++ b/ansible/roles/karbor/handlers/main.yml
@@ -0,0 +1,66 @@
+---
+- name: Restart karbor-api container
+  vars:
+    service_name: "karbor-api"
+    service: "{{ karbor_services[service_name] }}"
+    config_json: "{{ karbor_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    karbor_conf: "{{ karbor_confs.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    karbor_api_container: "{{ check_karbor_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 karbor_conf.changed | bool
+      or openstack_infra_conf.changed | bool
+      or karbor_api_container.changed | bool
+
+- name: Restart karbor-protection container
+  vars:
+    service_name: "karbor-protection"
+    service: "{{ karbor_services[service_name] }}"
+    config_json: "{{ karbor_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    karbor_conf: "{{ karbor_confs.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    karbor_protection_container: "{{ check_karbor_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 karbor_conf.changed | bool
+      or openstack_infra_conf.changed | bool
+      or karbor_protection_container.changed | bool
+
+- name: Restart karbor-operationengine container
+  vars:
+    service_name: "karbor-operationengine"
+    service: "{{ karbor_services[service_name] }}"
+    config_json: "{{ karbor_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    karbor_conf: "{{ karbor_confs.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    karbor_operationengine_container: "{{ check_karbor_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 karbor_conf.changed | bool
+      or openstack_infra_conf.changed | bool
+      or karbor_operationengine_container.changed | bool
diff --git a/ansible/roles/karbor/tasks/bootstrap_service.yml b/ansible/roles/karbor/tasks/bootstrap_service.yml
index f2cb6f9362..f42931280b 100644
--- a/ansible/roles/karbor/tasks/bootstrap_service.yml
+++ b/ansible/roles/karbor/tasks/bootstrap_service.yml
@@ -1,5 +1,7 @@
 ---
 - name: Running Karbor bootstrap container
+  vars:
+    karbor_api: "{{ karbor_services['karbor-api'] }}"
   kolla_docker:
     action: "start_container"
     common_options: "{{ docker_common_options }}"
@@ -7,14 +9,11 @@
     environment:
       KOLLA_BOOTSTRAP:
       KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
-    image: "{{ karbor_api_image_full }}"
+    image: "{{ karbor_api.image }}"
     labels:
       BOOTSTRAP:
     name: "bootstrap_karbor"
     restart_policy: "never"
-    volumes:
-      - "{{ node_config_directory }}/karbor-api/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "kolla_logs:/var/log/kolla/"
+    volumes: "{{ karbor_api.volumes }}"
   run_once: True
-  delegate_to: "{{ groups['karbor-api'][0] }}"
+  delegate_to: "{{ groups[karbor_api.group][0] }}"
diff --git a/ansible/roles/karbor/tasks/config.yml b/ansible/roles/karbor/tasks/config.yml
index 204b966629..5ff24ebb93 100644
--- a/ansible/roles/karbor/tasks/config.yml
+++ b/ansible/roles/karbor/tasks/config.yml
@@ -1,47 +1,81 @@
 ---
 - name: Ensuring config directories exist
   file:
-    path: "{{ node_config_directory }}/{{ item }}"
-    path: "{{ node_config_directory }}/{{ item }}/providers.d"
+    path: "{{ node_config_directory }}/{{ item.key }}/providers.d"
     state: "directory"
     recurse: yes
-  with_items:
-    - "karbor-api"
-    - "karbor-protection"
-    - "karbor-operationengine"
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ karbor_services }}"
 
 - name: Copying over config.json files for services
   template:
-    src: "{{ item }}.json.j2"
-    dest: "{{ node_config_directory }}/{{ item }}/config.json"
-  with_items:
-    - "karbor-api"
-    - "karbor-protection"
-    - "karbor-operationengine"
+    src: "{{ item.key }}.json.j2"
+    dest: "{{ node_config_directory }}/{{ item.key }}/config.json"
+  register: karbor_config_jsons
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ karbor_services }}"
+  notify:
+    - Restart karbor-api container
+    - Restart karbor-protection container
+    - Restart karbor-operationengine container
 
 - name: Copying over karbor.conf
   merge_configs:
     vars:
-      service_name: "{{ item }}"
+      service_name: "{{ item.key }}"
     sources:
       - "{{ role_path }}/templates/karbor.conf.j2"
       - "{{ node_config_directory }}/config/global.conf"
       - "{{ node_config_directory }}/config/database.conf"
       - "{{ node_config_directory }}/config/messaging.conf"
       - "{{ node_config_directory }}/config/karbor.conf"
-      - "{{ node_config_directory }}/config/karbor/{{ item }}.conf"
+      - "{{ node_config_directory }}/config/karbor/{{ item.key }}.conf"
       - "{{ node_config_directory }}/config/karbor/{{ inventory_hostname }}/karbor.conf"
-    dest: "{{ node_config_directory }}/{{ item }}/karbor.conf"
-  with_items:
-    - "karbor-api"
-    - "karbor-protection"
-    - "karbor-operationengine"
+    dest: "{{ node_config_directory }}/{{ item.key }}/karbor.conf"
+  register: karbor_confs
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ karbor_services }}"
+  notify:
+    - Restart karbor-api container
+    - Restart karbor-protection container
+    - Restart karbor-operationengine container
 
 - name: Copying over openstack-infra.conf
+  vars:
+    service: "{{ item.key }}"
   template:
     src: "providers.d/openstack-infra.conf.j2"
-    dest: "{{ node_config_directory }}/{{ item }}/providers.d/openstack-infra.conf"
-  with_items:
-    - "karbor-api"
-    - "karbor-protection"
-    - "karbor-operationengine"
+    dest: "{{ node_config_directory }}/{{ item.key }}/providers.d/openstack-infra.conf"
+  register: openstack_infra_conf
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ karbor_services }}"
+  notify:
+    - Restart karbor-api container
+    - Restart karbor-protection container
+    - Restart karbor-operationengine container
+
+- name: Check karbor 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_karbor_containers
+  when:
+    - action != "config"
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ karbor_services }}"
+  notify:
+    - Restart karbor-api container
+    - Restart karbor-protection container
+    - Restart karbor-operationengine container
diff --git a/ansible/roles/karbor/tasks/deploy.yml b/ansible/roles/karbor/tasks/deploy.yml
index 4cb15ce2f6..17fc56ea48 100644
--- a/ansible/roles/karbor/tasks/deploy.yml
+++ b/ansible/roles/karbor/tasks/deploy.yml
@@ -10,7 +10,5 @@
 - include: bootstrap.yml
   when: inventory_hostname in groups['karbor-api']
 
-- include: start.yml
-  when: inventory_hostname in groups['karbor-api'] or
-        inventory_hostname in groups['karbor-protection'] or
-        inventory_hostname in groups['karbor-operationengine']
+- name: Flush handlers
+  meta: flush_handlers
diff --git a/ansible/roles/karbor/tasks/pull.yml b/ansible/roles/karbor/tasks/pull.yml
index f000a878d4..311f263d61 100644
--- a/ansible/roles/karbor/tasks/pull.yml
+++ b/ansible/roles/karbor/tasks/pull.yml
@@ -1,21 +1,10 @@
 ---
-- name: Pulling karbor-api image
+- name: Pulling karbor images
   kolla_docker:
     action: "pull_image"
     common_options: "{{ docker_common_options }}"
-    image: "{{ karbor_api_image_full }}"
-  when: inventory_hostname in groups['karbor-api']
-
-- name: Pulling karbor-protection image
-  kolla_docker:
-    action: "pull_image"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ karbor_protection_image_full }}"
-  when: inventory_hostname in groups['karbor-protection']
-
-- name: Pulling karbor-operationengine image
-  kolla_docker:
-    action: "pull_image"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ karbor_operationengine_image_full }}"
-  when: inventory_hostname in groups['karbor-operationengine']
+    image: "{{ item.value.image }}"
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ karbor_services }}"
diff --git a/ansible/roles/karbor/tasks/reconfigure.yml b/ansible/roles/karbor/tasks/reconfigure.yml
index 33776caa8a..e078ef1318 100644
--- a/ansible/roles/karbor/tasks/reconfigure.yml
+++ b/ansible/roles/karbor/tasks/reconfigure.yml
@@ -1,71 +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: karbor_api, group: karbor-api }
-    - { name: karbor_protection, group: karbor-protection }
-    - { name: karbor_operationengine, group: karbor-operationengine }
-
-- 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: karbor_api, group: karbor-api }
-    - { name: karbor_protection, group: karbor-protection }
-    - { name: karbor_operationengine, group: karbor-operationengine }
-
-- 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: karbor_api, group: karbor-api }
-    - { name: karbor_protection, group: karbor-protection }
-    - { name: karbor_operationengine, group: karbor-operationengine }
-
-- 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: karbor_api, group: karbor-api },
-       { name: karbor_protection, group: karbor-protection },
-       { name: karbor_operationengine, group: karbor-operationengine }]
-    - "{{ 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: karbor_api, group: karbor-api },
-       { name: karbor_protection, group: karbor-protection },
-       { name: karbor_operationengine, group: karbor-operationengine }]
-    - "{{ container_envs.results }}"
-    - "{{ check_results.results }}"
+- include: deploy.yml
diff --git a/ansible/roles/karbor/tasks/start.yml b/ansible/roles/karbor/tasks/start.yml
deleted file mode 100644
index f66173e083..0000000000
--- a/ansible/roles/karbor/tasks/start.yml
+++ /dev/null
@@ -1,36 +0,0 @@
----
-- name: Starting karbor-api container
-  kolla_docker:
-    action: "start_container"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ karbor_api_image_full }}"
-    name: "karbor_api"
-    volumes:
-      - "{{ node_config_directory }}/karbor-api/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "kolla_logs:/var/log/kolla/"
-  when: inventory_hostname in groups['karbor-api']
-
-- name: Starting karbor-protection container
-  kolla_docker:
-    action: "start_container"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ karbor_protection_image_full }}"
-    name: "karbor_protection"
-    volumes:
-      - "{{ node_config_directory }}/karbor-protection/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "kolla_logs:/var/log/kolla/"
-  when: inventory_hostname in groups['karbor-protection']
-
-- name: Starting karbor-operationengine container
-  kolla_docker:
-    action: "start_container"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ karbor_operationengine_image_full }}"
-    name: "karbor_operationengine"
-    volumes:
-      - "{{ node_config_directory }}/karbor-operationengine/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "kolla_logs:/var/log/kolla/"
-  when: inventory_hostname in groups['karbor-operationengine']
diff --git a/ansible/roles/karbor/tasks/upgrade.yml b/ansible/roles/karbor/tasks/upgrade.yml
index 308053080c..c38db1adf4 100644
--- a/ansible/roles/karbor/tasks/upgrade.yml
+++ b/ansible/roles/karbor/tasks/upgrade.yml
@@ -3,4 +3,5 @@
 
 - include: bootstrap_service.yml
 
-- include: start.yml
+- name: Flush handlers
+  meta: flush_handlers