From 7148b818e0c3ee9570dee860cbeda0fb94c76b2e Mon Sep 17 00:00:00 2001 From: caoyuan Date: Sun, 22 Jan 2017 17:05:11 +0800 Subject: [PATCH] Optimize reconfiguration for mistral Change-Id: Ieec58fe3759c8ce430a187357ca8282b0f81b7eb Partially-implements: blueprint better-reconfigure --- ansible/roles/mistral/defaults/main.yml | 30 ++++++++ ansible/roles/mistral/handlers/main.yml | 69 ++++++++++++++++++ .../roles/mistral/tasks/bootstrap_service.yml | 11 ++- ansible/roles/mistral/tasks/config.yml | 70 +++++++++++------- ansible/roles/mistral/tasks/deploy.yml | 6 +- ansible/roles/mistral/tasks/pull.yml | 23 ++---- ansible/roles/mistral/tasks/reconfigure.yml | 71 +------------------ ansible/roles/mistral/tasks/start.yml | 36 ---------- ansible/roles/mistral/tasks/upgrade.yml | 3 +- 9 files changed, 161 insertions(+), 158 deletions(-) create mode 100644 ansible/roles/mistral/handlers/main.yml delete mode 100644 ansible/roles/mistral/tasks/start.yml diff --git a/ansible/roles/mistral/defaults/main.yml b/ansible/roles/mistral/defaults/main.yml index cca068623d..5983e6556d 100644 --- a/ansible/roles/mistral/defaults/main.yml +++ b/ansible/roles/mistral/defaults/main.yml @@ -1,6 +1,36 @@ --- project_name: "mistral" +mistral_services: + mistral-api: + container_name: mistral_api + group: mistral-api + enabled: true + image: "{{ mistral_api_image_full }}" + volumes: + - "{{ node_config_directory }}/mistral-api/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + mistral-engine: + container_name: mistral_engine + group: mistral-engine + enabled: true + image: "{{ mistral_engine_image_full }}" + volumes: + - "{{ node_config_directory }}/mistral-engine/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + mistral-executor: + container_name: mistral_executor + group: mistral-executor + enabled: true + image: "{{ mistral_executor_image_full }}" + volumes: + - "{{ node_config_directory }}/mistral-executor/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + + #################### # Database #################### diff --git a/ansible/roles/mistral/handlers/main.yml b/ansible/roles/mistral/handlers/main.yml new file mode 100644 index 0000000000..b7d68951b6 --- /dev/null +++ b/ansible/roles/mistral/handlers/main.yml @@ -0,0 +1,69 @@ +--- +- name: Restart mistral-api container + vars: + service_name: "mistral-api" + service: "{{ mistral_services[service_name] }}" + config_json: "{{ mistral_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + mistral_conf: "{{ mistral_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ mistral_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + mistral_api_container: "{{ check_mistral_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 mistral_conf.changed | bool + or policy_json.changed | bool + or mistral_api_container.changed | bool + +- name: Restart mistral-engine container + vars: + service_name: "mistral-engine" + service: "{{ mistral_services[service_name] }}" + config_json: "{{ mistral_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + mistral_conf: "{{ mistral_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ mistral_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + mistral_engine_container: "{{ check_mistral_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 mistral_conf.changed | bool + or policy_json.changed | bool + or mistral_engine_container.changed | bool + +- name: Restart mistral-executor container + vars: + service_name: "mistral-executor" + service: "{{ mistral_services[service_name] }}" + config_json: "{{ mistral_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + mistral_conf: "{{ mistral_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ mistral_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + mistral_executor_container: "{{ check_mistral_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 mistral_conf.changed | bool + or policy_json.changed | bool + or mistral_executor_container.changed | bool diff --git a/ansible/roles/mistral/tasks/bootstrap_service.yml b/ansible/roles/mistral/tasks/bootstrap_service.yml index a4bdfa5e8d..58d4a796bb 100644 --- a/ansible/roles/mistral/tasks/bootstrap_service.yml +++ b/ansible/roles/mistral/tasks/bootstrap_service.yml @@ -1,5 +1,7 @@ --- - name: Running Mistral bootstrap container + vars: + mistral_api: "{{ mistral_services['mistral-api'] }}" kolla_docker: action: "start_container" common_options: "{{ docker_common_options }}" @@ -7,14 +9,11 @@ environment: KOLLA_BOOTSTRAP: KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}" - image: "{{ mistral_api_image_full }}" + image: "{{ mistral_api.image }}" labels: BOOTSTRAP: name: "bootstrap_mistral" restart_policy: "never" - volumes: - - "{{ node_config_directory }}/mistral-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" + volumes: "{{ mistral_api.volumes }}" run_once: True - delegate_to: "{{ groups['mistral-api'][0] }}" + delegate_to: "{{ groups[mistral_api.group][0] }}" diff --git a/ansible/roles/mistral/tasks/config.yml b/ansible/roles/mistral/tasks/config.yml index 62f12d18a7..4410e8384b 100644 --- a/ansible/roles/mistral/tasks/config.yml +++ b/ansible/roles/mistral/tasks/config.yml @@ -1,40 +1,44 @@ --- - name: Ensuring config directories exist file: - path: "{{ node_config_directory }}/{{ item }}" + path: "{{ node_config_directory }}/{{ item.key }}" state: "directory" recurse: yes - with_items: - - "mistral-api" - - "mistral-engine" - - "mistral-executor" + when: inventory_hostname in groups[item.value.group] + with_dict: "{{ mistral_services }}" - name: Copying over config.json files for services template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "mistral-api" - - "mistral-engine" - - "mistral-executor" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: mistral_config_jsons + when: + - item.value.enabled | bool + - inventory_hostname in groups[item.value.group] + with_dict: "{{ mistral_services }}" + notify: + - Restart {{ item.key }} container - name: Copying over mistral.conf merge_configs: vars: - service_name: "{{ item }}" + service_name: "{{ item.key }}" sources: - "{{ role_path }}/templates/mistral.conf.j2" - "{{ node_custom_config }}/global.conf" - "{{ node_custom_config }}/database.conf" - "{{ node_custom_config }}/messaging.conf" - "{{ node_custom_config }}/mistral.conf" - - "{{ node_custom_config }}/mistral/{{ item }}.conf" + - "{{ node_custom_config }}/mistral/{{ item.key }}.conf" - "{{ node_custom_config }}/mistral/{{ inventory_hostname }}/mistral.conf" - dest: "{{ node_config_directory }}/{{ item }}/mistral.conf" - with_items: - - "mistral-api" - - "mistral-engine" - - "mistral-executor" + dest: "{{ node_config_directory }}/{{ item.key }}/mistral.conf" + register: mistral_confs + when: + - item.value.enabled | bool + - inventory_hostname in groups[item.value.group] + with_dict: "{{ mistral_services }}" + notify: + - Restart {{ item.key }} container - name: Check if policies shall be overwritten local_action: stat path="{{ node_custom_config }}/mistral/policy.json" @@ -43,10 +47,28 @@ - name: Copying over existing policy.json template: src: "{{ node_custom_config }}/mistral/policy.json" - dest: "{{ node_config_directory }}/{{ item }}/policy.json" - with_items: - - "mistral-api" - - "mistral-engine" - - "mistral-executor" + dest: "{{ node_config_directory }}/{{ item.key }}/policy.json" + register: mistral_policy_jsons when: - mistral_policy.stat.exists + - mistral_policy.stat.exists + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ mistral_services }}" + notify: + - Restart {{ item.key }} container + +- name: Check mistral 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_mistral_containers + when: + - action != "config" + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ mistral_services }}" + notify: + - Restart {{ item.key }} container diff --git a/ansible/roles/mistral/tasks/deploy.yml b/ansible/roles/mistral/tasks/deploy.yml index 5f062e5c51..e8b3e135cd 100644 --- a/ansible/roles/mistral/tasks/deploy.yml +++ b/ansible/roles/mistral/tasks/deploy.yml @@ -10,7 +10,5 @@ - include: bootstrap.yml when: inventory_hostname in groups['mistral-api'] -- include: start.yml - when: inventory_hostname in groups['mistral-api'] or - inventory_hostname in groups['mistral-engine'] or - inventory_hostname in groups['mistral-executor'] +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/mistral/tasks/pull.yml b/ansible/roles/mistral/tasks/pull.yml index 6a17830b44..030787762a 100644 --- a/ansible/roles/mistral/tasks/pull.yml +++ b/ansible/roles/mistral/tasks/pull.yml @@ -1,21 +1,10 @@ --- -- name: Pulling mistral-api image +- name: Pulling mistral images kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ mistral_api_image_full }}" - when: inventory_hostname in groups['mistral-api'] - -- name: Pulling mistral-engine image - kolla_docker: - action: "pull_image" - common_options: "{{ docker_common_options }}" - image: "{{ mistral_engine_image_full }}" - when: inventory_hostname in groups['mistral-engine'] - -- name: Pulling mistral-executor image - kolla_docker: - action: "pull_image" - common_options: "{{ docker_common_options }}" - image: "{{ mistral_executor_image_full }}" - when: inventory_hostname in groups['mistral-executor'] + image: "{{ item.value.image }}" + with_dict: "{{ mistral_services }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool diff --git a/ansible/roles/mistral/tasks/reconfigure.yml b/ansible/roles/mistral/tasks/reconfigure.yml index d0d9e7ae97..e078ef1318 100644 --- a/ansible/roles/mistral/tasks/reconfigure.yml +++ b/ansible/roles/mistral/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: mistral_api, group: mistral-api } - - { name: mistral_engine, group: mistral-engine } - - { name: mistral_executor, group: mistral-executor } - -- 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: mistral_api, group: mistral-api } - - { name: mistral_engine, group: mistral-engine } - - { name: mistral_executor, group: mistral-executor } - -- 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: mistral_api, group: mistral-api } - - { name: mistral_engine, group: mistral-engine } - - { name: mistral_executor, group: mistral-executor } - -- 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: mistral_api, group: mistral-api }, - { name: mistral_engine, group: mistral-engine }, - { name: mistral_executor, group: mistral-executor }] - - "{{ 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: mistral_api, group: mistral-api }, - { name: mistral_engine, group: mistral-engine }, - { name: mistral_executor, group: mistral-executor }] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" +- include: deploy.yml diff --git a/ansible/roles/mistral/tasks/start.yml b/ansible/roles/mistral/tasks/start.yml deleted file mode 100644 index 54f9b63deb..0000000000 --- a/ansible/roles/mistral/tasks/start.yml +++ /dev/null @@ -1,36 +0,0 @@ ---- -- name: Starting mistral-engine container - kolla_docker: - action: "start_container" - name: "mistral_engine" - common_options: "{{ docker_common_options }}" - image: "{{ mistral_engine_image_full }}" - volumes: - - "{{ node_config_directory }}/mistral-engine/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['mistral-engine'] - -- name: Starting mistral-executor container - kolla_docker: - action: "start_container" - name: "mistral_executor" - common_options: "{{ docker_common_options }}" - image: "{{ mistral_executor_image_full }}" - volumes: - - "{{ node_config_directory }}/mistral-executor/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['mistral-executor'] - -- name: Starting mistral-api container - kolla_docker: - action: "start_container" - name: "mistral_api" - common_options: "{{ docker_common_options }}" - image: "{{ mistral_api_image_full }}" - volumes: - - "{{ node_config_directory }}/mistral-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['mistral-api'] diff --git a/ansible/roles/mistral/tasks/upgrade.yml b/ansible/roles/mistral/tasks/upgrade.yml index 308053080c..c38db1adf4 100644 --- a/ansible/roles/mistral/tasks/upgrade.yml +++ b/ansible/roles/mistral/tasks/upgrade.yml @@ -3,4 +3,5 @@ - include: bootstrap_service.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers