From d7ab65b852d282cb82ef1c2ff8b8b40c5561c790 Mon Sep 17 00:00:00 2001 From: caoyuan Date: Sat, 21 Jan 2017 14:58:53 +0800 Subject: [PATCH] Optimize reconfiguration for searchlight Change-Id: If36ba287a09ff1c5e5c767b733e79f1c1753be10 Partially-implements: blueprint better-reconfigure --- ansible/roles/searchlight/defaults/main.yml | 20 ++++++ ansible/roles/searchlight/handlers/main.yml | 44 +++++++++++++ .../searchlight/tasks/bootstrap_service.yml | 11 ++-- ansible/roles/searchlight/tasks/config.yml | 64 ++++++++++++------ ansible/roles/searchlight/tasks/deploy.yml | 4 +- ansible/roles/searchlight/tasks/pull.yml | 16 ++--- .../roles/searchlight/tasks/reconfigure.yml | 66 +------------------ ansible/roles/searchlight/tasks/start.yml | 24 ------- ansible/roles/searchlight/tasks/upgrade.yml | 3 +- 9 files changed, 125 insertions(+), 127 deletions(-) create mode 100644 ansible/roles/searchlight/handlers/main.yml delete mode 100644 ansible/roles/searchlight/tasks/start.yml diff --git a/ansible/roles/searchlight/defaults/main.yml b/ansible/roles/searchlight/defaults/main.yml index 3030943633..a742489e86 100644 --- a/ansible/roles/searchlight/defaults/main.yml +++ b/ansible/roles/searchlight/defaults/main.yml @@ -1,6 +1,26 @@ --- project_name: "searchlight" +searchlight_services: + searchlight-api: + container_name: searchlight_api + group: searchlight-api + enabled: true + image: "{{ searchlight_api_image_full }}" + volumes: + - "{{ node_config_directory }}/searchlight-api/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + searchlight-listener: + container_name: searchlight_listener + group: searchlight-listener + enabled: true + image: "{{ searchlight_listener_image_full }}" + volumes: + - "{{ node_config_directory }}/searchlight-listener/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + #################### # Elasticsearch #################### diff --git a/ansible/roles/searchlight/handlers/main.yml b/ansible/roles/searchlight/handlers/main.yml new file mode 100644 index 0000000000..e37ec9acfa --- /dev/null +++ b/ansible/roles/searchlight/handlers/main.yml @@ -0,0 +1,44 @@ +--- +- name: Restart searchlight-api container + vars: + service_name: "searchlight-api" + service: "{{ searchlight_services[service_name] }}" + config_json: "{{ searchlight_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + searchlight_conf: "{{ searchlight_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ searchlight_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + searchlight_api_container: "{{ check_searchlight_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: + - inventory_hostname in groups[service.group] + - service.enabled | bool + - config_json.changed | bool + or searchlight_conf.changed | bool + or policy_json.changed | bool + or searchlight_api_container.changed | bool + +- name: Restart searchlight-listener container + vars: + service_name: "searchlight-listener" + service: "{{ searchlight_services[service_name] }}" + config_json: "{{ searchlight_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + searchlight_conf: "{{ searchlight_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ searchlight_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + searchlight_listener_container: "{{ check_searchlight_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: + - inventory_hostname in groups[service.group] + - service.enabled | bool + - config_json.changed | bool + or searchlight_conf.changed | bool + or policy_json.changed | bool + or searchlight_listener_container.changed | bool diff --git a/ansible/roles/searchlight/tasks/bootstrap_service.yml b/ansible/roles/searchlight/tasks/bootstrap_service.yml index 7f114924ba..b02b8b2b9f 100644 --- a/ansible/roles/searchlight/tasks/bootstrap_service.yml +++ b/ansible/roles/searchlight/tasks/bootstrap_service.yml @@ -1,5 +1,7 @@ --- - name: Running Searchlight bootstrap container + vars: + searchlight_api: "{{ searchlight_services['searchlight-api'] }}" kolla_docker: action: "start_container" common_options: "{{ docker_common_options }}" @@ -7,14 +9,11 @@ environment: KOLLA_BOOTSTRAP: KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}" - image: "{{ searchlight_api_image_full }}" + image: "{{ searchlight_api.image }}" labels: BOOTSTRAP: name: "bootstrap_searchlight" restart_policy: "never" - volumes: - - "{{ node_config_directory }}/searchlight-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" + volumes: "{{ searchlight_api.volumes }}" run_once: True - delegate_to: "{{ groups['searchlight-api'][0] }}" + delegate_to: "{{ groups[searchlight_api.group][0] }}" diff --git a/ansible/roles/searchlight/tasks/config.yml b/ansible/roles/searchlight/tasks/config.yml index 1bec9a7ac5..d1f960158b 100644 --- a/ansible/roles/searchlight/tasks/config.yml +++ b/ansible/roles/searchlight/tasks/config.yml @@ -1,35 +1,40 @@ --- - name: Ensuring config directories exist file: - path: "{{ node_config_directory }}/{{ item }}" + path: "{{ node_config_directory }}/{{ item.key }}" state: "directory" recurse: yes - with_items: - - "searchlight-api" - - "searchlight-listener" + with_dict: "{{ searchlight_services }}" - name: Copying over config.json files for services template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "searchlight-api" - - "searchlight-listener" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: searchlight_config_jsons + when: + - item.value.enabled | bool + - inventory_hostname in groups[item.value.group] + with_dict: "{{ searchlight_services }}" - name: Copying over searchlight.conf merge_configs: vars: - service_name: "{{ item }}" + service_name: "{{ item.key }}" sources: - "{{ role_path }}/templates/searchlight.conf.j2" - "{{ node_custom_config }}/global.conf" - "{{ node_custom_config }}/messaging.conf" - "{{ node_custom_config }}/searchlight.conf" - "{{ node_custom_config }}/searchlight/{{ inventory_hostname }}/searchlight.conf" - dest: "{{ node_config_directory }}/{{ item }}/searchlight.conf" - with_items: - - "searchlight-api" - - "searchlight-listener" + dest: "{{ node_config_directory }}/{{ item.key }}/searchlight.conf" + register: searchlight_confs + when: + - item.value.enabled | bool + - inventory_hostname in groups[item.value.group] + with_dict: "{{ searchlight_services }}" + notify: + - Restart searchlight-api container + - Restart searchlight-listener container - name: Check if policies shall be overwritten local_action: stat path="{{ node_custom_config }}/searchlight/policy.json" @@ -38,9 +43,30 @@ - name: Copying over existing policy.json template: src: "{{ node_custom_config }}/searchlight/policy.json" - dest: "{{ node_config_directory }}/{{ item }}/policy.json" - with_items: - - "searchlight-api" - - "searchlight-listener" + dest: "{{ node_config_directory }}/{{ item.key }}/policy.json" + register: searchlight_policy_jsons when: - searchlight_policy.stat.exists + - searchlight_policy.stat.exists + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ searchlight_services }}" + notify: + - Restart searchlight-api container + - Restart searchlight-listener container + +- name: Check searchlight 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_searchlight_containers + when: + - action != 'genconfig' + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ searchlight_services }}" + notify: + - Restart searchlight-api container + - Restart searchlight-listener container diff --git a/ansible/roles/searchlight/tasks/deploy.yml b/ansible/roles/searchlight/tasks/deploy.yml index ea73ba9f38..6c0058ec88 100644 --- a/ansible/roles/searchlight/tasks/deploy.yml +++ b/ansible/roles/searchlight/tasks/deploy.yml @@ -8,5 +8,5 @@ - include: bootstrap.yml when: inventory_hostname in groups['searchlight'] -- include: start.yml - when: inventory_hostname in groups['searchlight'] +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/searchlight/tasks/pull.yml b/ansible/roles/searchlight/tasks/pull.yml index 7389653810..d6b6e78432 100644 --- a/ansible/roles/searchlight/tasks/pull.yml +++ b/ansible/roles/searchlight/tasks/pull.yml @@ -1,14 +1,10 @@ --- -- name: Pulling searchlight-api image +- name: Pulling searchlight images kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ searchlight_api_image_full }}" - when: inventory_hostname in groups['searchlight-api'] - -- name: Pulling searchlight-listener image - kolla_docker: - action: "pull_image" - common_options: "{{ docker_common_options }}" - image: "{{ searchlight_listener_image_full }}" - when: inventory_hostname in groups['searchlight-listener'] + image: "{{ item.value.image }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ searchlight_services }}" diff --git a/ansible/roles/searchlight/tasks/reconfigure.yml b/ansible/roles/searchlight/tasks/reconfigure.yml index c1a1403fc8..e078ef1318 100644 --- a/ansible/roles/searchlight/tasks/reconfigure.yml +++ b/ansible/roles/searchlight/tasks/reconfigure.yml @@ -1,66 +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: searchlight_api, group: searchlight-api } - - { name: searchlight_listener, group: searchlight-listener } - -- 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: searchlight_api, group: searchlight-api } - - { name: searchlight_listener, group: searchlight-listener } - -- 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: searchlight_api, group: searchlight-api } - - { name: searchlight_listener, group: searchlight-listener } - -- 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: searchlight_api, group: searchlight-api }, - { name: searchlight_listener, group: searchlight-listener }] - - "{{ 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: searchlight_api, group: searchlight-api }, - { name: searchlight_listener, group: searchlight-listener }] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" +- include: deploy.yml diff --git a/ansible/roles/searchlight/tasks/start.yml b/ansible/roles/searchlight/tasks/start.yml deleted file mode 100644 index f97e789d0f..0000000000 --- a/ansible/roles/searchlight/tasks/start.yml +++ /dev/null @@ -1,24 +0,0 @@ ---- -- name: Starting searchlight-api container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ searchlight_api_image_full }}" - name: "searchlight_api" - volumes: - - "{{ node_config_directory }}/searchlight-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['searchlight-api'] - -- name: Starting searchlight-listener container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ searchlight_listener_image_full }}" - name: "searchlight_listener" - volumes: - - "{{ node_config_directory }}/searchlight-listener/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['searchlight-listener'] diff --git a/ansible/roles/searchlight/tasks/upgrade.yml b/ansible/roles/searchlight/tasks/upgrade.yml index 308053080c..c38db1adf4 100644 --- a/ansible/roles/searchlight/tasks/upgrade.yml +++ b/ansible/roles/searchlight/tasks/upgrade.yml @@ -3,4 +3,5 @@ - include: bootstrap_service.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers