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

Co-Authored-By: Jeffrey Zhang <jeffrey.zhang@99cloud.net>
Change-Id: Ieb44258cd99744a92ef1aa958e3bdccf9519f212
Partially-implements: blueprint better-reconfigure
---
 ansible/roles/horizon/defaults/main.yml       | 29 +++++++++
 ansible/roles/horizon/handlers/main.yml       | 21 ++++++
 .../roles/horizon/tasks/bootstrap_service.yml | 10 +--
 ansible/roles/horizon/tasks/config.yml        | 59 +++++++++++++++--
 ansible/roles/horizon/tasks/deploy.yml        |  3 +-
 ansible/roles/horizon/tasks/precheck.yml      |  4 +-
 ansible/roles/horizon/tasks/pull.yml          |  9 ++-
 ansible/roles/horizon/tasks/reconfigure.yml   | 64 +------------------
 ansible/roles/horizon/tasks/start.yml         | 29 ---------
 ansible/roles/horizon/tasks/upgrade.yml       |  3 +-
 10 files changed, 121 insertions(+), 110 deletions(-)
 create mode 100644 ansible/roles/horizon/handlers/main.yml
 delete mode 100644 ansible/roles/horizon/tasks/start.yml

diff --git a/ansible/roles/horizon/defaults/main.yml b/ansible/roles/horizon/defaults/main.yml
index b2ddaa559d..ed12ef204a 100644
--- a/ansible/roles/horizon/defaults/main.yml
+++ b/ansible/roles/horizon/defaults/main.yml
@@ -1,6 +1,35 @@
 ---
 project_name: "horizon"
 
+horizon_services:
+  horizon:
+    container_name: horizon
+    group: horizon
+    enabled: true
+    image: "{{ horizon_image_full }}"
+    environment:
+      ENABLE_CLOUDKITTY: "{{ 'yes' if enable_horizon_cloudkitty | bool else 'no' }}"
+      ENABLE_FREEZER: "{{ 'yes' if enable_horizon_freezer | bool else 'no' }}"
+      ENABLE_IRONIC: "{{ 'yes' if enable_horizon_ironic | bool else 'no' }}"
+      ENABLE_KARBOR: "{{ 'yes' if enable_horizon_karbor | bool else 'no' }}"
+      ENABLE_MAGNUM: "{{ 'yes' if enable_horizon_magnum | bool else 'no' }}"
+      ENABLE_MANILA: "{{ 'yes' if enable_horizon_manila | bool else 'no' }}"
+      ENABLE_MISTRAL: "{{ 'yes' if enable_horizon_mistral | bool else 'no' }}"
+      ENABLE_MURANO: "{{ 'yes' if  enable_horizon_murano | bool else 'no' }}"
+      ENABLE_NEUTRON_LBAAS: "{{ 'yes' if enable_horizon_neutron_lbaas | bool else 'no' }}"
+      ENABLE_SAHARA: "{{ 'yes' if enable_horizon_sahara | bool else 'no' }}"
+      ENABLE_SEARCHLIGHT: "{{ 'yes' if enable_horizon_searchlight | bool else 'no' }}"
+      ENABLE_SENLIN: "{{ 'yes' if enable_horizon_senlin | bool else 'no' }}"
+      ENABLE_SOLUM: "{{ 'yes' if enable_horizon_solum | bool else 'no' }}"
+      ENABLE_TACKER: "{{ 'yes' if enable_horizon_tacker | bool else 'no' }}"
+      ENABLE_TROVE: "{{ 'yes' if enable_horizon_trove | bool else 'no' }}"
+      ENABLE_WATCHER: "{{ 'yes' if enable_horizon_watcher | bool else 'no' }}"
+    volumes:
+      - "{{ node_config_directory }}/horizon/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "kolla_logs:/var/log/kolla/"
+
+
 ####################
 # Database
 ####################
diff --git a/ansible/roles/horizon/handlers/main.yml b/ansible/roles/horizon/handlers/main.yml
new file mode 100644
index 0000000000..f02501cd50
--- /dev/null
+++ b/ansible/roles/horizon/handlers/main.yml
@@ -0,0 +1,21 @@
+---
+- name: Restart horizon container
+  vars:
+    service_name: "horizon"
+    service: "{{ horizon_services[service_name] }}"
+  kolla_docker:
+    action: "recreate_or_restart_container"
+    common_options: "{{ docker_common_options }}"
+    name: "{{ service.container_name }}"
+    image: "{{ service.image }}"
+    environment: "{{ service.environment }}"
+    volumes: "{{ service.volumes }}"
+  when:
+    - action != "config"
+    - inventory_hostname in groups[service.group]
+    - service.enabled | bool
+    - horizon_config_json | changed
+      or horizon_conf | changed
+      or horizon_local_settings | changed
+      or policy_jsons | changed
+      or check_horizon_container | changed
diff --git a/ansible/roles/horizon/tasks/bootstrap_service.yml b/ansible/roles/horizon/tasks/bootstrap_service.yml
index c20280f6bd..998cdc5e10 100644
--- a/ansible/roles/horizon/tasks/bootstrap_service.yml
+++ b/ansible/roles/horizon/tasks/bootstrap_service.yml
@@ -1,5 +1,7 @@
 ---
 - name: Running Horizon bootstrap container
+  vars:
+    horizon: "{{ horizon_services['horizon'] }}"
   kolla_docker:
     action: "start_container"
     common_options: "{{ docker_common_options }}"
@@ -7,13 +9,11 @@
     environment:
       KOLLA_BOOTSTRAP:
       KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
-    image: "{{ horizon_image_full }}"
+    image: "{{ horizon.image }}"
     labels:
       BOOTSTRAP:
     name: "bootstrap_horizon"
     restart_policy: "never"
-    volumes:
-      - "{{ node_config_directory }}/horizon/:{{ container_config_directory }}/:ro"
-      - "kolla_logs:/var/log/kolla/"
+    volumes: "{{ horizon.volumes }}"
   run_once: True
-  delegate_to: "{{ groups['horizon'][0] }}"
+  delegate_to: "{{ groups[horizon.group][0] }}"
diff --git a/ansible/roles/horizon/tasks/config.yml b/ansible/roles/horizon/tasks/config.yml
index 34555340d1..e5f7f1cabc 100644
--- a/ansible/roles/horizon/tasks/config.yml
+++ b/ansible/roles/horizon/tasks/config.yml
@@ -1,29 +1,45 @@
 ---
 - name: Ensuring config directories exist
   file:
-    path: "{{ node_config_directory }}/{{ item }}"
+    path: "{{ node_config_directory }}/{{ item.key }}"
     state: "directory"
     recurse: yes
-  with_items:
-    - "horizon"
+  when: inventory_hostname in groups[item.value.group]
+  with_dict: "{{ horizon_services }}"
 
 - name: Copying over config.json files for services
+  vars:
+    horizon: "{{ horizon_services['horizon'] }}"
   template:
-    src: "{{ item }}.json.j2"
-    dest: "{{ node_config_directory }}/{{ item }}/config.json"
-  with_items:
-    - "horizon"
+    src: "horizon.json.j2"
+    dest: "{{ node_config_directory }}/horizon/config.json"
+  register: horizon_config_json
+  when:
+    - horizon.enabled | bool
+    - inventory_hostname in groups[horizon.group]
+  notify:
+    - Restart horizon container
 
 - name: Copying over horizon.conf
+  vars:
+    horizon: "{{ horizon_services['horizon'] }}"
   template:
     src: "{{ item }}"
     dest: "{{ node_config_directory }}/horizon/horizon.conf"
+  register: horizon_conf
   with_first_found:
     - "{{ node_custom_config }}/horizon/{{ inventory_hostname }}/horizon.conf"
     - "{{ node_custom_config }}/horizon/horizon.conf"
     - "horizon.conf.j2"
+  when:
+    - horizon.enabled | bool
+    - inventory_hostname in groups[horizon.group]
+  notify:
+    - Restart horizon container
 
 - name: Copying over local_settings
+  vars:
+    horizon: "{{ horizon_services['horizon'] }}"
   template:
     src: "{{ item }}"
     dest: "{{ node_config_directory }}/horizon/local_settings"
@@ -31,6 +47,12 @@
     - "{{ node_custom_config }}/horizon/{{ inventory_hostname }}/local_settings"
     - "{{ node_custom_config }}/horizon/local_settings"
     - "local_settings.j2"
+  register: horizon_local_settings
+  when:
+    - horizon.enabled | bool
+    - inventory_hostname in groups[horizon.group]
+  notify:
+    - Restart horizon container
 
 - name: Check if policies shall be overwritten
   local_action: stat path="{{ node_custom_config }}/horizon/{{ item.name }}_policy.json"
@@ -52,10 +74,15 @@
     - { name: "trove", enabled: "{{ enable_trove }}" }
 
 - name: Copying over existing policy.json
+  vars:
+    horizon: "{{ horizon_services['horizon'] }}"
   template:
     src: "{{ node_custom_config }}/horizon/{{ item[0]['name'] }}_policy.json"
     dest: "{{ node_config_directory }}/horizon/{{ item[0]['name'] }}_policy.json"
+  register: policy_jsons
   when:
+    - horizon.enabled | bool
+    - inventory_hostname in groups[horizon.group]
     - item[0]['enabled'] | bool
     - item[1]['stat']['exists']
   with_together:
@@ -73,3 +100,21 @@
        { name: "senlin", enabled: "{{ enable_senlin }}" },
        { name: "trove", enabled: "{{ enable_trove }}" }]
     - "{{ custom_policy.results }}"
+
+- name: Check horizon container
+  vars:
+    horizon: "{{ horizon_services['horizon'] }}"
+  kolla_docker:
+    action: "compare_container"
+    common_options: "{{ docker_common_options }}"
+    name: "{{ horizon.container_name }}"
+    image: "{{ horizon.image }}"
+    environment: "{{ horizon.environment }}"
+    volumes: "{{ horizon.volumes }}"
+  register: check_horizon_container
+  when:
+    - action != "config"
+    - inventory_hostname in groups[horizon.group]
+    - horizon.enabled | bool
+  notify:
+    - Restart horizon container
diff --git a/ansible/roles/horizon/tasks/deploy.yml b/ansible/roles/horizon/tasks/deploy.yml
index 07d49b6ace..7c5f051eca 100644
--- a/ansible/roles/horizon/tasks/deploy.yml
+++ b/ansible/roles/horizon/tasks/deploy.yml
@@ -4,4 +4,5 @@
 - include: bootstrap.yml
   when: horizon_backend_database | bool
 
-- include: start.yml
+- name: Flush handlers
+  meta: flush_handlers
diff --git a/ansible/roles/horizon/tasks/precheck.yml b/ansible/roles/horizon/tasks/precheck.yml
index f81c023e86..9f76922572 100644
--- a/ansible/roles/horizon/tasks/precheck.yml
+++ b/ansible/roles/horizon/tasks/precheck.yml
@@ -6,6 +6,8 @@
   register: container_facts
 
 - name: Checking free port for Horizon
+  vars:
+    horizon: "{{ horizon_services['horizon'] }}"
   wait_for:
     host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
     port: "{{ horizon_port }}"
@@ -13,4 +15,4 @@
     state: stopped
   when:
     - container_facts['horizon'] is not defined
-    - inventory_hostname in groups['horizon']
+    - inventory_hostname in groups[horizon.group]
diff --git a/ansible/roles/horizon/tasks/pull.yml b/ansible/roles/horizon/tasks/pull.yml
index 99eab899de..e47822d1ae 100644
--- a/ansible/roles/horizon/tasks/pull.yml
+++ b/ansible/roles/horizon/tasks/pull.yml
@@ -1,7 +1,10 @@
 ---
-- name: Pulling horizon image
+- name: Pulling horizon images
   kolla_docker:
     action: "pull_image"
     common_options: "{{ docker_common_options }}"
-    image: "{{ horizon_image_full }}"
-  when: inventory_hostname in groups['horizon']
+    image: "{{ item.value.image }}"
+  when:
+    - inventory_hostname in groups[item.value.group]
+    - item.value.enabled | bool
+  with_items: "{{ horizon_services }}"
diff --git a/ansible/roles/horizon/tasks/reconfigure.yml b/ansible/roles/horizon/tasks/reconfigure.yml
index e4ae77c07e..56169f9727 100644
--- a/ansible/roles/horizon/tasks/reconfigure.yml
+++ b/ansible/roles/horizon/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: horizon, group: horizon }
-
-- 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: horizon, group: horizon }
-
-# 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: horizon, group: horizon }
-
-- 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: horizon, group: horizon }]
-    - "{{ 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: horizon, group: horizon }]
-    - "{{ container_envs.results }}"
-    - "{{ check_results.results }}"
+- include: "deploy.yml"
diff --git a/ansible/roles/horizon/tasks/start.yml b/ansible/roles/horizon/tasks/start.yml
deleted file mode 100644
index ea974b8ac3..0000000000
--- a/ansible/roles/horizon/tasks/start.yml
+++ /dev/null
@@ -1,29 +0,0 @@
----
-- name: Starting horizon container
-  kolla_docker:
-    action: "start_container"
-    common_options: "{{ docker_common_options }}"
-    image: "{{ horizon_image_full }}"
-    name: "horizon"
-    environment:
-      ENABLE_CLOUDKITTY: "{{ 'yes' if enable_horizon_cloudkitty | bool else 'no' }}"
-      ENABLE_FREEZER: "{{ 'yes' if enable_horizon_freezer | bool else 'no' }}"
-      ENABLE_IRONIC: "{{ 'yes' if enable_horizon_ironic | bool else 'no' }}"
-      ENABLE_KARBOR: "{{ 'yes' if enable_horizon_karbor | bool else 'no' }}"
-      ENABLE_MAGNUM: "{{ 'yes' if enable_horizon_magnum | bool else 'no' }}"
-      ENABLE_MANILA: "{{ 'yes' if enable_horizon_manila | bool else 'no' }}"
-      ENABLE_MISTRAL: "{{ 'yes' if enable_horizon_mistral | bool else 'no' }}"
-      ENABLE_MURANO: "{{ 'yes' if  enable_horizon_murano | bool else 'no' }}"
-      ENABLE_NEUTRON_LBAAS: "{{ 'yes' if enable_horizon_neutron_lbaas | bool else 'no' }}"
-      ENABLE_SAHARA: "{{ 'yes' if enable_horizon_sahara | bool else 'no' }}"
-      ENABLE_SEARCHLIGHT: "{{ 'yes' if enable_horizon_searchlight | bool else 'no' }}"
-      ENABLE_SENLIN: "{{ 'yes' if enable_horizon_senlin | bool else 'no' }}"
-      ENABLE_SOLUM: "{{ 'yes' if enable_horizon_solum | bool else 'no' }}"
-      ENABLE_TACKER: "{{ 'yes' if enable_horizon_tacker | bool else 'no' }}"
-      ENABLE_TROVE: "{{ 'yes' if enable_horizon_trove | bool else 'no' }}"
-      ENABLE_WATCHER: "{{ 'yes' if enable_horizon_watcher | bool else 'no' }}"
-    volumes:
-      - "{{ node_config_directory }}/horizon/:{{ container_config_directory }}/:ro"
-      - "/etc/localtime:/etc/localtime:ro"
-      - "kolla_logs:/var/log/kolla/"
-  when: inventory_hostname in groups['horizon']
diff --git a/ansible/roles/horizon/tasks/upgrade.yml b/ansible/roles/horizon/tasks/upgrade.yml
index 1f16915ad9..dd26ecc34d 100644
--- a/ansible/roles/horizon/tasks/upgrade.yml
+++ b/ansible/roles/horizon/tasks/upgrade.yml
@@ -1,4 +1,5 @@
 ---
 - include: config.yml
 
-- include: start.yml
+- name: Flush handlers
+  meta: flush_handlers