From cb1a2b4fafd5cefcee3011449e7fa1830c6f5650 Mon Sep 17 00:00:00 2001
From: caoyuan <cao.yuan@99cloud.net>
Date: Fri, 20 Jan 2017 17:09:23 +0800
Subject: [PATCH] Optimize reconfigure action for gnocchi

Change-Id: I1b81f46568cf37296b86755b501d167fba849408
Partially-implements: blueprint better-reconfigure
---
 ansible/roles/gnocchi/defaults/main.yml       | 32 +++++++
 ansible/roles/gnocchi/handlers/main.yml       | 71 ++++++++++++++
 .../roles/gnocchi/tasks/bootstrap_service.yml | 12 +--
 ansible/roles/gnocchi/tasks/config.yml        | 94 ++++++++++++++-----
 ansible/roles/gnocchi/tasks/deploy.yml        |  6 +-
 ansible/roles/gnocchi/tasks/pull.yml          | 23 ++---
 ansible/roles/gnocchi/tasks/reconfigure.yml   | 74 +--------------
 ansible/roles/gnocchi/tasks/start.yml         | 39 --------
 ansible/roles/gnocchi/tasks/upgrade.yml       |  3 +-
 9 files changed, 189 insertions(+), 165 deletions(-)
 create mode 100644 ansible/roles/gnocchi/handlers/main.yml
 delete mode 100644 ansible/roles/gnocchi/tasks/start.yml

diff --git a/ansible/roles/gnocchi/defaults/main.yml b/ansible/roles/gnocchi/defaults/main.yml
index cd80d1695d..8cf1541a49 100644
--- a/ansible/roles/gnocchi/defaults/main.yml
+++ b/ansible/roles/gnocchi/defaults/main.yml
@@ -1,6 +1,38 @@
 ---
 project_name: "gnocchi"
 
+gnocchi_services:
+  gnocchi-api:
+    container_name: gnocchi_api
+    group: gnocchi-api
+    enabled: true
+    image: "{{ gnocchi_api_image_full }}"
+    volumes:
+      - "{{ node_config_directory }}/gnocchi-api/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "gnocchi:/var/lib/gnocchi/"
+      - "kolla_logs:/var/log/kolla/"
+  gnocchi-metricd:
+    container_name: gnocchi_metricd
+    group: gnocchi-metricd
+    enabled: true
+    image: "{{ gnocchi_metricd_image_full }}"
+    volumes:
+      - "{{ node_config_directory }}/gnocchi-metricd/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "gnocchi:/var/lib/gnocchi/"
+      - "kolla_logs:/var/log/kolla/"
+  gnocchi-statsd:
+    container_name: gnocchi_statsd
+    group: gnocchi-statsd
+    enabled: true
+    image: "{{ gnocchi_statsd_image_full }}"
+    volumes:
+      - "{{ node_config_directory }}/gnocchi-statsd/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "gnocchi:/var/lib/gnocchi/"
+      - "kolla_logs:/var/log/kolla/"
+
 
 ####################
 # Ceph
diff --git a/ansible/roles/gnocchi/handlers/main.yml b/ansible/roles/gnocchi/handlers/main.yml
new file mode 100644
index 0000000000..b957d33425
--- /dev/null
+++ b/ansible/roles/gnocchi/handlers/main.yml
@@ -0,0 +1,71 @@
+---
+- name: Restart gnocchi-api container
+  vars:
+    service_name: "gnocchi-api"
+    service: "{{ gnocchi_services[service_name] }}"
+    config_json: "{{ gnocchi_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    gnocchi_conf: "{{ gnocchi_confs.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    policy_json: "{{ gnocchi_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    gnocchi_api_container: "{{ check_gnocchi_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 gnocchi_conf.changed | bool
+      or policy_json.changed | bool
+      or gnocchi_api_paste_ini.changed | bool
+      or gnocchi_wsgi_conf.changed | bool
+      or gnocchi_api_container.changed | bool
+
+- name: Restart gnocchi-metricd container
+  vars:
+    service_name: "gnocchi-metricd"
+    service: "{{ gnocchi_services[service_name] }}"
+    config_json: "{{ gnocchi_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    gnocchi_conf: "{{ gnocchi_confs.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    policy_json: "{{ gnocchi_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    gnocchi_metricd_container: "{{ check_gnocchi_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 gnocchi_conf.changed | bool
+      or policy_json.changed | bool
+      or gnocchi_metricd_container.changed | bool
+
+- name: Restart gnocchi-statsd container
+  vars:
+    service_name: "gnocchi-statsd"
+    service: "{{ gnocchi_services[service_name] }}"
+    config_json: "{{ gnocchi_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    gnocchi_conf: "{{ gnocchi_confs.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    policy_json: "{{ gnocchi_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
+    gnocchi_statsd_container: "{{ check_gnocchi_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 gnocchi_conf.changed | bool
+      or policy_json.changed | bool
+      or gnocchi_statsd_container.changed | bool
diff --git a/ansible/roles/gnocchi/tasks/bootstrap_service.yml b/ansible/roles/gnocchi/tasks/bootstrap_service.yml
index 91e140cf7c..58b014d242 100644
--- a/ansible/roles/gnocchi/tasks/bootstrap_service.yml
+++ b/ansible/roles/gnocchi/tasks/bootstrap_service.yml
@@ -1,5 +1,7 @@
 ---
 - name: Running gnocchi bootstrap container
+  vars:
+    gnocchi_api: "{{ gnocchi_services['gnocchi-api'] }}"
   kolla_docker:
     action: "start_container"
     common_options: "{{ docker_common_options }}"
@@ -7,15 +9,11 @@
     environment:
       KOLLA_BOOTSTRAP:
       KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
-    image: "{{ gnocchi_api_image_full }}"
+    image: "{{ gnocchi_api.image }}"
     labels:
       BOOTSTRAP:
     name: "bootstrap_gnocchi"
     restart_policy: "never"
-    volumes:
-      - "{{ node_config_directory }}/gnocchi-api/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "gnocchi:/var/lib/gnocchi/"
-      - "kolla_logs:/var/log/kolla/"
+    volumes: "{{ gnocchi_api.volumes }}"
   run_once: True
-  delegate_to: "{{ groups['gnocchi-api'][0] }}"
+  delegate_to: "{{ groups[gnocchi_api.group][0] }}"
diff --git a/ansible/roles/gnocchi/tasks/config.yml b/ansible/roles/gnocchi/tasks/config.yml
index 5e74dca266..5a5f31f801 100644
--- a/ansible/roles/gnocchi/tasks/config.yml
+++ b/ansible/roles/gnocchi/tasks/config.yml
@@ -1,55 +1,79 @@
 ---
 - name: Ensuring config directories exist
   file:
-    path: "{{ node_config_directory }}/{{ item }}"
+    path: "{{ node_config_directory }}/{{ item.key }}"
     state: "directory"
     recurse: yes
-  with_items:
-    - "gnocchi-api"
-    - "gnocchi-statsd"
-    - "gnocchi-metricd"
+  when: inventory_hostname in groups[item.value.group]
+  with_dict: "{{ gnocchi_services }}"
 
 - name: Copying over config.json files for services
   template:
-    src: "{{ item }}.json.j2"
-    dest: "{{ node_config_directory }}/{{ item }}/config.json"
-  with_items:
-    - "gnocchi-api"
-    - "gnocchi-metricd"
-    - "gnocchi-statsd"
+    src: "{{ item.key }}.json.j2"
+    dest: "{{ node_config_directory }}/{{ item.key }}/config.json"
+  register: gnocchi_config_jsons
+  when:
+    - item.value.enabled | bool
+    - inventory_hostname in groups[item.value.group]
+  with_dict: "{{ gnocchi_services }}"
+  notify:
+    - Restart gnocchi-api container
+    - Restart gnocchi-metricd container
+    - Restart gnocchi-statsd container
 
 - name: Copying over api-paste.ini
+  vars:
+    service: "{{ gnocchi_services['gnocchi-api'] }}"
   merge_configs:
     sources:
       - "{{ role_path }}/templates/api-paste.ini.j2"
       - "{{ node_custom_config }}/gnocchi/api-paste.ini"
       - "{{ node_custom_config }}/gnocchi/{{ inventory_hostname }}/api-paste.ini"
     dest: "{{ node_config_directory }}/gnocchi-api/api-paste.ini"
+  register: gnocchi_api_paste_ini
+  when:
+    - inventory_hostname in groups['gnocchi-api']
+    - service.enabled | bool
+  notify:
+    - Restart gnocchi-api container
 
 - name: Copying over gnocchi.conf
   merge_configs:
     vars:
-      service_name: "{{ item }}"
+      service_name: "{{ item.key }}"
     sources:
       - "{{ role_path }}/templates/gnocchi.conf.j2"
       - "{{ node_custom_config }}/global.conf"
       - "{{ node_custom_config }}/database.conf"
       - "{{ node_custom_config }}/messaging.conf"
       - "{{ node_custom_config }}/gnocchi.conf"
-      - "{{ node_custom_config }}/gnocchi/{{ item }}.conf"
+      - "{{ node_custom_config }}/gnocchi/{{ item.key }}.conf"
       - "{{ node_custom_config }}/gnocchi/{{ inventory_hostname }}/gnocchi.conf"
-    dest: "{{ node_config_directory }}/{{ item }}/gnocchi.conf"
-  with_items:
-    - "gnocchi-api"
-    - "gnocchi-statsd"
-    - "gnocchi-metricd"
+    dest: "{{ node_config_directory }}/{{ item.key }}/gnocchi.conf"
+  register: gnocchi_confs
+  when:
+    - item.value.enabled | bool
+    - inventory_hostname in groups[item.value.group]
+  with_dict: "{{ gnocchi_services }}"
+  notify:
+    - Restart gnocchi-api container
+    - Restart gnocchi-metricd container
+    - Restart gnocchi-statsd container
 
 - name: Copying over wsgi-gnocchi.conf
+  vars:
+    service: "{{ gnocchi_services['gnocchi-api'] }}"
   template:
     src: "wsgi-gnocchi.conf.j2"
     dest: "{{ node_config_directory }}/{{ item }}/wsgi-gnocchi.conf"
+  register: gnocchi_wsgi_conf
+  when:
+    - inventory_hostname in groups['gnocchi-api']
+    - service.enabled | bool
   with_items:
     - "gnocchi-api"
+  notify:
+    - Restart gnocchi-api container
 
 - name: Check if policies shall be overwritten
   local_action: stat path="{{ node_custom_config }}/gnocchi/policy.json"
@@ -58,10 +82,32 @@
 - name: Copying over existing policy.json
   template:
     src: "{{ node_custom_config }}/gnocchi/policy.json"
-    dest: "{{ node_config_directory }}/{{ item }}/policy.json"
-  with_items:
-    - "gnocchi-api"
-    - "gnocchi-statsd"
-    - "gnocchi-metricd"
+    dest: "{{ node_config_directory }}/{{ item.key }}/policy.json"
+  register: gnocchi_policy_jsons
   when:
-    gnocchi_policy.stat.exists
+    - gnocchi_policy.stat.exists
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ gnocchi_services }}"
+  notify:
+    - Restart gnocchi-api container
+    - Restart gnocchi-metricd container
+    - Restart gnocchi-statsd container
+
+- name: Check gnocchi 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_gnocchi_containers
+  when:
+    - action != "config"
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ gnocchi_services }}"
+  notify:
+    - Restart gnocchi-api container
+    - Restart gnocchi-metricd container
+    - Restart gnocchi-statsd container
diff --git a/ansible/roles/gnocchi/tasks/deploy.yml b/ansible/roles/gnocchi/tasks/deploy.yml
index a8ff9f66d5..18de8752ee 100644
--- a/ansible/roles/gnocchi/tasks/deploy.yml
+++ b/ansible/roles/gnocchi/tasks/deploy.yml
@@ -20,7 +20,5 @@
 - include: bootstrap.yml
   when: inventory_hostname in groups['gnocchi-api']
 
-- include: start.yml
-  when: inventory_hostname in groups['gnocchi-statsd'] or
-        inventory_hostname in groups['gnocchi-api'] or
-        inventory_hostname in groups['gnocchi-metricd']
+- name: Flush handlers
+  meta: flush_handlers
diff --git a/ansible/roles/gnocchi/tasks/pull.yml b/ansible/roles/gnocchi/tasks/pull.yml
index caefd8aaf6..caf44ab530 100644
--- a/ansible/roles/gnocchi/tasks/pull.yml
+++ b/ansible/roles/gnocchi/tasks/pull.yml
@@ -1,21 +1,10 @@
 ---
-- name: Pulling gnocchi-api image
+- name: Pulling gnocchi images
   kolla_docker:
     action: "pull_image"
     common_options: "{{ docker_common_options }}"
-    image: "{{ gnocchi_api_image_full }}"
-  when: inventory_hostname in groups['gnocchi-api']
-
-- name: Pulling gnocchi-metricd image
-  kolla_docker:
-    action: "pull_image"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ gnocchi_metricd_image_full }}"
-  when: inventory_hostname in groups['gnocchi-metricd']
-
-- name: Pulling gnocchi-statsd image
-  kolla_docker:
-    action: "pull_image"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ gnocchi_statsd_image_full }}"
-  when: inventory_hostname in groups['gnocchi-statsd']
+    image: "{{ item.value.image }}"
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_dict: "{{ gnocchi_services }}"
diff --git a/ansible/roles/gnocchi/tasks/reconfigure.yml b/ansible/roles/gnocchi/tasks/reconfigure.yml
index 39798455c8..e078ef1318 100644
--- a/ansible/roles/gnocchi/tasks/reconfigure.yml
+++ b/ansible/roles/gnocchi/tasks/reconfigure.yml
@@ -1,74 +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: gnocchi_api, group: gnocchi-api }
-    - { name: gnocchi_metricd, group: gnocchi-metricd }
-    - { name: gnocchi_statsd, group: gnocchi-statsd }
-
-- 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: gnocchi_api, group: gnocchi-api }
-    - { name: gnocchi_metricd, group: gnocchi-metricd }
-    - { name: gnocchi_statsd, group: gnocchi-statsd }
-
-# 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: gnocchi_api, group: gnocchi-api }
-    - { name: gnocchi_metricd, group: gnocchi-metricd }
-    - { name: gnocchi_statsd, group: gnocchi-statsd }
-
-- 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: gnocchi_api, group: gnocchi-api },
-       { name: gnocchi_metricd, group: gnocchi-metricd },
-       { name: gnocchi_statsd, group: gnocchi-statsd }]
-    - "{{ 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: gnocchi_api, group: gnocchi-api },
-       { name: gnocchi_metricd, group: gnocchi-metricd },
-       { name: gnocchi_statsd, group: gnocchi-statsd }]
-    - "{{ container_envs.results }}"
-    - "{{ check_results.results }}"
+- include: deploy.yml
diff --git a/ansible/roles/gnocchi/tasks/start.yml b/ansible/roles/gnocchi/tasks/start.yml
deleted file mode 100644
index 5d0231fe47..0000000000
--- a/ansible/roles/gnocchi/tasks/start.yml
+++ /dev/null
@@ -1,39 +0,0 @@
----
-- name: Starting gnocchi-api container
-  kolla_docker:
-    action: "start_container"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ gnocchi_api_image_full }}"
-    name: "gnocchi_api"
-    volumes:
-      - "{{ node_config_directory }}/gnocchi-api/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "gnocchi:/var/lib/gnocchi/"
-      - "kolla_logs:/var/log/kolla/"
-  when: inventory_hostname in groups['gnocchi-api']
-
-- name: Starting gnocchi-metricd container
-  kolla_docker:
-    action: "start_container"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ gnocchi_metricd_image_full }}"
-    name: "gnocchi_metricd"
-    volumes:
-      - "{{ node_config_directory }}/gnocchi-metricd/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "gnocchi:/var/lib/gnocchi/"
-      - "kolla_logs:/var/log/kolla/"
-  when: inventory_hostname in groups['gnocchi-metricd']
-
-- name: Starting gnocchi-statsd container
-  kolla_docker:
-    action: "start_container"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ gnocchi_statsd_image_full }}"
-    name: "gnocchi_statsd"
-    volumes:
-      - "{{ node_config_directory }}/gnocchi-statsd/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "gnocchi:/var/lib/gnocchi/"
-      - "kolla_logs:/var/log/kolla/"
-  when: inventory_hostname in groups['gnocchi-statsd']
diff --git a/ansible/roles/gnocchi/tasks/upgrade.yml b/ansible/roles/gnocchi/tasks/upgrade.yml
index 308053080c..c38db1adf4 100644
--- a/ansible/roles/gnocchi/tasks/upgrade.yml
+++ b/ansible/roles/gnocchi/tasks/upgrade.yml
@@ -3,4 +3,5 @@
 
 - include: bootstrap_service.yml
 
-- include: start.yml
+- name: Flush handlers
+  meta: flush_handlers