From 6defea46cbed77ccb13ccd8e52e8624e83777dcb Mon Sep 17 00:00:00 2001 From: Mauricio Lima Date: Mon, 23 Jan 2017 11:08:23 -0300 Subject: [PATCH] Optimize reconfiguration for collectd Co-Authored-By: caoyuan Change-Id: I0604d262965b27b8bf6c560238811f81c830ea1a Partially-implements: blueprint better-reconfigure --- ansible/roles/collectd/defaults/main.yml | 15 +++++ ansible/roles/collectd/handlers/main.yml | 21 +++++++ ansible/roles/collectd/tasks/config.yml | 54 +++++++++++++---- ansible/roles/collectd/tasks/deploy.yml | 3 +- ansible/roles/collectd/tasks/pull.yml | 6 +- ansible/roles/collectd/tasks/reconfigure.yml | 61 +------------------- ansible/roles/collectd/tasks/start.yml | 14 ----- ansible/roles/collectd/tasks/upgrade.yml | 3 +- 8 files changed, 90 insertions(+), 87 deletions(-) create mode 100644 ansible/roles/collectd/handlers/main.yml delete mode 100644 ansible/roles/collectd/tasks/start.yml diff --git a/ansible/roles/collectd/defaults/main.yml b/ansible/roles/collectd/defaults/main.yml index 93cfd4cf3c..c6b9047453 100644 --- a/ansible/roles/collectd/defaults/main.yml +++ b/ansible/roles/collectd/defaults/main.yml @@ -1,4 +1,19 @@ --- +collectd_services: + collectd: + container_name: collectd + group: collectd + enabled: true + image: "{{ collectd_image_full }}" + privileged: True + volumes: + - "{{ node_config_directory }}/collectd/:{{ container_config_directory }}/:ro" + - "{{node_config_directory }}/collectd/collectd.conf.d/:/etc/collectd/collectd.conf.d/" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + - "/sys/:/sys/:ro" + - "/dev/:/dev/:ro" + #################### # Docker #################### diff --git a/ansible/roles/collectd/handlers/main.yml b/ansible/roles/collectd/handlers/main.yml new file mode 100644 index 0000000000..52d0d5851e --- /dev/null +++ b/ansible/roles/collectd/handlers/main.yml @@ -0,0 +1,21 @@ +--- +- name: Restart collectd container + vars: + service_name: "collectd" + service: "{{ collectd_services[service_name] }}" + config_json: "{{ collectd_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + collectd_container: "{{ check_collectd_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 }}" + privileged: "{{ service.privileged | default(False) }}" + volumes: "{{ service.volumes }}" + when: + - action != "config" + - inventory_hostname in groups[service.group] + - service.enabled | bool + - config_json.changed | bool + or collectd_conf.changed | bool + or collectd_container.changed | bool diff --git a/ansible/roles/collectd/tasks/config.yml b/ansible/roles/collectd/tasks/config.yml index 2470cd3a5b..eddcd8122a 100644 --- a/ansible/roles/collectd/tasks/config.yml +++ b/ansible/roles/collectd/tasks/config.yml @@ -1,28 +1,39 @@ --- - name: Ensuring collectd config directories exist file: - path: "{{ node_config_directory }}/{{ item }}" + path: "{{ node_config_directory }}/{{ item.key }}" state: "directory" recurse: yes - with_items: - - "collectd" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ collectd_services }}" - name: Ensuring Plugin directory exist file: - path: "{{ node_config_directory }}/{{ item }}/collectd.conf.d" + path: "{{ node_config_directory }}/{{ item.key }}/collectd.conf.d" state: "directory" recurse: yes - with_items: - - "collectd" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ collectd_services }}" - name: Copying over config.json files for services template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "collectd" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: collectd_config_jsons + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ collectd_services }}" + notify: + - Restart collectd container - name: Copying over collectd.conf for services + vars: + service: "{{ collectd_services['collectd'] }}" template: src: "{{ item }}" dest: "{{ node_config_directory }}/collectd/collectd.conf" @@ -31,3 +42,26 @@ - "{{ node_custom_config }}/collectd/collectd.conf" - "{{ node_custom_config }}/collectd.conf" - "collectd.conf.j2" + register: collectd_conf + when: + - inventory_hostname in groups[service.group] + - service.enabled | bool + notify: + - Restart collectd container + +- name: Check collectd containers + kolla_docker: + action: "compare_container" + common_options: "{{ docker_common_options }}" + name: "{{ item.value.container_name }}" + image: "{{ item.value.image }}" + privileged: "{{ item.value.privileged | default(False) }}" + volumes: "{{ item.value.volumes }}" + register: check_collectd_containers + when: + - action != "config" + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ collectd_services }}" + notify: + - Restart collectd container diff --git a/ansible/roles/collectd/tasks/deploy.yml b/ansible/roles/collectd/tasks/deploy.yml index 1f16915ad9..dd26ecc34d 100644 --- a/ansible/roles/collectd/tasks/deploy.yml +++ b/ansible/roles/collectd/tasks/deploy.yml @@ -1,4 +1,5 @@ --- - include: config.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/collectd/tasks/pull.yml b/ansible/roles/collectd/tasks/pull.yml index d182b27b67..1120000481 100644 --- a/ansible/roles/collectd/tasks/pull.yml +++ b/ansible/roles/collectd/tasks/pull.yml @@ -3,4 +3,8 @@ kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ collectd_image_full }}" + image: "{{ item.value.image }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ collectd_services }}" diff --git a/ansible/roles/collectd/tasks/reconfigure.yml b/ansible/roles/collectd/tasks/reconfigure.yml index 602850983e..e078ef1318 100644 --- a/ansible/roles/collectd/tasks/reconfigure.yml +++ b/ansible/roles/collectd/tasks/reconfigure.yml @@ -1,61 +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: collectd, group: collectd } - -- 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: collectd, group: collectd } - -- 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: collectd, group: collectd } - -- 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: collectd, group: collectd }] - - "{{ 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: collectd, group: collectd }] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" +- include: deploy.yml diff --git a/ansible/roles/collectd/tasks/start.yml b/ansible/roles/collectd/tasks/start.yml deleted file mode 100644 index f73067fe03..0000000000 --- a/ansible/roles/collectd/tasks/start.yml +++ /dev/null @@ -1,14 +0,0 @@ ---- -- name: Starting collectd container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ collectd_image_full }}" - name: "collectd" - privileged: True - volumes: - - "{{ node_config_directory }}/collectd/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - - "/sys/:/sys/:ro" - - "/dev/:/dev/:ro" diff --git a/ansible/roles/collectd/tasks/upgrade.yml b/ansible/roles/collectd/tasks/upgrade.yml index 1f16915ad9..dd26ecc34d 100644 --- a/ansible/roles/collectd/tasks/upgrade.yml +++ b/ansible/roles/collectd/tasks/upgrade.yml @@ -1,4 +1,5 @@ --- - include: config.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers