From d56c58685e613eb186218b3f23989692fdb587c9 Mon Sep 17 00:00:00 2001 From: caoyuan Date: Thu, 19 Jan 2017 23:43:25 +0800 Subject: [PATCH] Optimize reconfigure action for rabbitmq Change-Id: I14e1619f8fd33be5b0652982131c2a4a4b3e1292 Partially-implements: blueprint better-reconfigure --- ansible/roles/rabbitmq/defaults/main.yml | 13 ++++ ansible/roles/rabbitmq/handlers/main.yml | 20 ++++++ ansible/roles/rabbitmq/tasks/config.yml | 45 +++++++++++--- ansible/roles/rabbitmq/tasks/deploy.yml | 3 +- ansible/roles/rabbitmq/tasks/pull.yml | 7 ++- ansible/roles/rabbitmq/tasks/reconfigure.yml | 64 +------------------- ansible/roles/rabbitmq/tasks/start.yml | 12 ---- ansible/roles/rabbitmq/tasks/upgrade.yml | 6 +- 8 files changed, 83 insertions(+), 87 deletions(-) create mode 100644 ansible/roles/rabbitmq/handlers/main.yml delete mode 100644 ansible/roles/rabbitmq/tasks/start.yml diff --git a/ansible/roles/rabbitmq/defaults/main.yml b/ansible/roles/rabbitmq/defaults/main.yml index 4fb19feb48..82dc09712e 100644 --- a/ansible/roles/rabbitmq/defaults/main.yml +++ b/ansible/roles/rabbitmq/defaults/main.yml @@ -1,6 +1,19 @@ --- project_name: "rabbitmq" +rabbitmq_services: + rabbitmq: + container_name: rabbitmq + group: rabbitmq + enabled: true + image: "{{ rabbitmq_image_full }}" + volumes: + - "{{ node_config_directory }}/rabbitmq/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "rabbitmq:/var/lib/rabbitmq/" + - "kolla_logs:/var/log/kolla/" + + #################### # Docker #################### diff --git a/ansible/roles/rabbitmq/handlers/main.yml b/ansible/roles/rabbitmq/handlers/main.yml new file mode 100644 index 0000000000..d74057cba4 --- /dev/null +++ b/ansible/roles/rabbitmq/handlers/main.yml @@ -0,0 +1,20 @@ +--- +- name: Restart rabbitmq container + vars: + service_name: "rabbitmq" + service: "{{ rabbitmq_services[service_name] }}" + config_json: "{{ rabbitmq_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + rabbitmq_container: "{{ check_rabbitmq_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 rabbitmq_confs.changed | bool + or rabbitmq_container.changed | bool diff --git a/ansible/roles/rabbitmq/tasks/config.yml b/ansible/roles/rabbitmq/tasks/config.yml index e104fb8bc9..69daaaf7e5 100644 --- a/ansible/roles/rabbitmq/tasks/config.yml +++ b/ansible/roles/rabbitmq/tasks/config.yml @@ -1,25 +1,56 @@ --- - name: Ensuring config directories exist file: - path: "{{ node_config_directory }}/{{ item }}" + path: "{{ node_config_directory }}/{{ item.key }}" state: "directory" recurse: yes - with_items: - - "rabbitmq" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ rabbitmq_services }}" - name: Copying over config.json files for services template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "rabbitmq" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: rabbitmq_config_jsons + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ rabbitmq_services }}" + notify: + - Restart rabbitmq container - name: Copying over rabbitmq configs + vars: + service: "{{ rabbitmq_services['rabbitmq'] }}" template: src: "{{ item }}.j2" dest: "{{ node_config_directory }}/rabbitmq/{{ item }}" + register: rabbitmq_confs + when: + - inventory_hostname in groups[service.group] + - service.enabled | bool with_items: - "rabbitmq-env.conf" - "rabbitmq.config" - "rabbitmq-clusterer.config" - "definitions.json" + notify: + - Restart rabbitmq container + +- name: Check rabbitmq 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_rabbitmq_containers + when: + - action != "config" + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ rabbitmq_services }}" + notify: + - Restart rabbitmq container diff --git a/ansible/roles/rabbitmq/tasks/deploy.yml b/ansible/roles/rabbitmq/tasks/deploy.yml index 98daa4021c..9eca42dec5 100644 --- a/ansible/roles/rabbitmq/tasks/deploy.yml +++ b/ansible/roles/rabbitmq/tasks/deploy.yml @@ -3,4 +3,5 @@ - include: bootstrap.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/rabbitmq/tasks/pull.yml b/ansible/roles/rabbitmq/tasks/pull.yml index cb289fabf5..4ebed4c038 100644 --- a/ansible/roles/rabbitmq/tasks/pull.yml +++ b/ansible/roles/rabbitmq/tasks/pull.yml @@ -3,5 +3,8 @@ kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ rabbitmq_image_full }}" - when: inventory_hostname in groups['rabbitmq'] + image: "{{ item.value.image }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ rabbitmq_services }}" diff --git a/ansible/roles/rabbitmq/tasks/reconfigure.yml b/ansible/roles/rabbitmq/tasks/reconfigure.yml index 7524002b77..e078ef1318 100644 --- a/ansible/roles/rabbitmq/tasks/reconfigure.yml +++ b/ansible/roles/rabbitmq/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: rabbitmq, group: rabbitmq } - -- 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: rabbitmq, group: rabbitmq } - -# 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: rabbitmq, group: rabbitmq } - -- 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: rabbitmq, group: rabbitmq }] - - "{{ 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: rabbitmq, group: rabbitmq }] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" +- include: deploy.yml diff --git a/ansible/roles/rabbitmq/tasks/start.yml b/ansible/roles/rabbitmq/tasks/start.yml deleted file mode 100644 index 9501545ff9..0000000000 --- a/ansible/roles/rabbitmq/tasks/start.yml +++ /dev/null @@ -1,12 +0,0 @@ ---- -- name: Starting rabbitmq container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ rabbitmq_image_full }}" - name: "rabbitmq" - volumes: - - "{{ node_config_directory }}/rabbitmq/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "rabbitmq:/var/lib/rabbitmq/" - - "kolla_logs:/var/log/kolla/" diff --git a/ansible/roles/rabbitmq/tasks/upgrade.yml b/ansible/roles/rabbitmq/tasks/upgrade.yml index e76d3b8727..c02c94dbf1 100644 --- a/ansible/roles/rabbitmq/tasks/upgrade.yml +++ b/ansible/roles/rabbitmq/tasks/upgrade.yml @@ -26,12 +26,14 @@ - rabbitmq_hostname != (gospel_node.stdout | from_json).hostname - rabbitmq_differs['result'] -- include: start.yml +- name: Flush handlers + meta: flush_handlers when: - rabbitmq_hostname == (gospel_node.stdout | from_json).hostname - rabbitmq_differs['result'] -- include: start.yml +- name: Flush handlers + meta: flush_handlers when: - rabbitmq_hostname != (gospel_node.stdout | from_json).hostname - rabbitmq_differs['result']