From 2363a8e46930afe5cd3643def32102cb8e700861 Mon Sep 17 00:00:00 2001 From: caoyuan Date: Thu, 19 Jan 2017 17:58:04 +0800 Subject: [PATCH] Optimize reconfigure action for cloudkitty Change-Id: I7389a437d54db9c6dfb4706b2fe541a4c4702de5 Partially-implements: blueprint better-reconfigure --- ansible/roles/cloudkitty/defaults/main.yml | 21 ++++++ ansible/roles/cloudkitty/handlers/main.yml | 46 ++++++++++++ .../cloudkitty/tasks/bootstrap_service.yml | 11 ++- ansible/roles/cloudkitty/tasks/config.yml | 72 +++++++++++++------ ansible/roles/cloudkitty/tasks/deploy.yml | 5 +- ansible/roles/cloudkitty/tasks/pull.yml | 16 ++--- .../roles/cloudkitty/tasks/reconfigure.yml | 66 +---------------- ansible/roles/cloudkitty/tasks/start.yml | 24 ------- ansible/roles/cloudkitty/tasks/upgrade.yml | 3 +- 9 files changed, 135 insertions(+), 129 deletions(-) create mode 100644 ansible/roles/cloudkitty/handlers/main.yml delete mode 100644 ansible/roles/cloudkitty/tasks/start.yml diff --git a/ansible/roles/cloudkitty/defaults/main.yml b/ansible/roles/cloudkitty/defaults/main.yml index a344866f20..f7fbf3bc94 100644 --- a/ansible/roles/cloudkitty/defaults/main.yml +++ b/ansible/roles/cloudkitty/defaults/main.yml @@ -1,6 +1,27 @@ --- project_name: "cloudkitty" +cloudkitty_services: + cloudkitty-api: + container_name: "cloudkitty_api" + group: "cloudkitty-api" + image: "{{ cloudkitty_api_image_full }}" + enabled: True + volumes: + - "{{ node_config_directory }}/cloudkitty-api/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + cloudkitty-processor: + container_name: "cloudkitty_processor" + group: "cloudkitty-processor" + image: "{{ cloudkitty_processor_image_full }}" + enabled: True + volumes: + - "{{ node_config_directory }}/cloudkitty-processor/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + + #################### # Database #################### diff --git a/ansible/roles/cloudkitty/handlers/main.yml b/ansible/roles/cloudkitty/handlers/main.yml new file mode 100644 index 0000000000..82a67724a5 --- /dev/null +++ b/ansible/roles/cloudkitty/handlers/main.yml @@ -0,0 +1,46 @@ +--- +- name: Restart cloudkitty-api container + vars: + service_name: "cloudkitty-api" + service: "{{ cloudkitty_services[service_name] }}" + config_json: "{{ cloudkitty_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + cloudkitty_conf: "{{ cloudkitty_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ cloudkitty_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + cloudkitty_api_container: "{{ check_cloudkitty_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 cloudkitty_conf.changed | bool + or policy_json.changed | bool + or cloudkitty_api_container.changed | bool + +- name: Restart cloudkitty-processor container + vars: + service_name: "cloudkitty-processor" + service: "{{ cloudkitty_services[service_name] }}" + config_json: "{{ cloudkitty_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + cloudkitty_conf: "{{ cloudkitty_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ cloudkitty_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + cloudkitty_processor_container: "{{ check_cloudkitty_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 cloudkitty_conf.changed | bool + or policy_json.changed | bool + or cloudkitty_processor_container.changed | bool diff --git a/ansible/roles/cloudkitty/tasks/bootstrap_service.yml b/ansible/roles/cloudkitty/tasks/bootstrap_service.yml index b6cdfac0a9..db958ddc5d 100644 --- a/ansible/roles/cloudkitty/tasks/bootstrap_service.yml +++ b/ansible/roles/cloudkitty/tasks/bootstrap_service.yml @@ -1,5 +1,7 @@ --- - name: Running Cloudkitty bootstrap container + vars: + cloudkitty_api: "{{ cloudkitty_services['cloudkitty-api'] }}" kolla_docker: action: "start_container" common_options: "{{ docker_common_options }}" @@ -7,14 +9,11 @@ environment: KOLLA_BOOTSTRAP: KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}" - image: "{{ cloudkitty_api_image_full }}" + image: "{{ cloudkitty_api.image }}" labels: BOOTSTRAP: name: "bootstrap_cloudkitty" restart_policy: "never" - volumes: - - "{{ node_config_directory }}/cloudkitty-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" + volumes: "{{ cloudkitty_api.volumes }}" run_once: True - delegate_to: "{{ groups['cloudkitty-api'][0] }}" + delegate_to: "{{ groups[cloudkitty_api.group][0] }}" diff --git a/ansible/roles/cloudkitty/tasks/config.yml b/ansible/roles/cloudkitty/tasks/config.yml index d614334ca4..da333341f5 100644 --- a/ansible/roles/cloudkitty/tasks/config.yml +++ b/ansible/roles/cloudkitty/tasks/config.yml @@ -1,37 +1,48 @@ --- - name: Ensuring config directories exist file: - path: "{{ node_config_directory }}/{{ item }}" + path: "{{ node_config_directory }}/{{ item.key }}" state: "directory" recurse: yes - with_items: - - "cloudkitty-api" - - "cloudkitty-processor" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ cloudkitty_services }}" - name: Copying over config.json files for services template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "cloudkitty-api" - - "cloudkitty-processor" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: cloudkitty_config_jsons + when: + - item.value.enabled | bool + - inventory_hostname in groups[item.value.group] + with_dict: "{{ cloudkitty_services }}" + notify: + - Restart cloudkitty-api container + - Restart cloudkitty-processor container - name: Copying over cloudkitty.conf merge_configs: vars: - service_name: "{{ item }}" + service_name: "{{ item.key }}" sources: - "{{ role_path }}/templates/cloudkitty.conf.j2" - "{{ node_custom_config }}/global.conf" - "{{ node_custom_config }}/database.conf" - "{{ node_custom_config }}/messaging.conf" - "{{ node_custom_config }}/cloudkitty.conf" - - "{{ node_custom_config }}/cloudkitty/{{ item }}.conf" + - "{{ node_custom_config }}/cloudkitty/{{ item.key }}.conf" - "{{ node_custom_config }}/cloudkitty/{{ inventory_hostname }}/cloudkitty.conf" - dest: "{{ node_config_directory }}/{{ item }}/cloudkitty.conf" - with_items: - - "cloudkitty-api" - - "cloudkitty-processor" + dest: "{{ node_config_directory }}/{{ item.key }}/cloudkitty.conf" + register: cloudkitty_confs + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ cloudkitty_services }}" + notify: + - Restart cloudkitty-api container + - Restart cloudkitty-processor container - name: Check if policies shall be overwritten local_action: stat path="{{ node_custom_config }}/cloudkitty/policy.json" @@ -40,9 +51,30 @@ - name: Copying over existing policy.json template: src: "{{ node_custom_config }}/cloudkitty/policy.json" - dest: "{{ node_config_directory }}/{{ item }}/policy.json" - with_items: - - "cloudkitty-api" - - "cloudkitty-processor" + dest: "{{ node_config_directory }}/{{ item.key }}/policy.json" + register: cloudkitty_policy_jsons when: - cloudkitty_policy.stat.exists + - cloudkitty_policy.stat.exists + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ cloudkitty_services }}" + notify: + - Restart cloudkitty-api container + - Restart cloudkitty-processor container + +- name: Check cloudkitty 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_cloudkitty_containers + when: + - action != "config" + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ cloudkitty_services }}" + notify: + - Restart cloudkitty-api container + - Restart cloudkitty-processor container diff --git a/ansible/roles/cloudkitty/tasks/deploy.yml b/ansible/roles/cloudkitty/tasks/deploy.yml index 5cff1aee2a..f066c0dfbb 100644 --- a/ansible/roles/cloudkitty/tasks/deploy.yml +++ b/ansible/roles/cloudkitty/tasks/deploy.yml @@ -9,6 +9,5 @@ - include: bootstrap.yml when: inventory_hostname in groups['cloudkitty-api'] -- include: start.yml - when: inventory_hostname in groups['cloudkitty-api'] or - inventory_hostname in groups['cloudkitty-processor'] +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/cloudkitty/tasks/pull.yml b/ansible/roles/cloudkitty/tasks/pull.yml index efd7bd7df5..a490cc195e 100644 --- a/ansible/roles/cloudkitty/tasks/pull.yml +++ b/ansible/roles/cloudkitty/tasks/pull.yml @@ -1,14 +1,10 @@ --- -- name: Pulling cloudkitty-api image +- name: Pulling cloudkitty images kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ cloudkitty_api_image_full }}" - when: inventory_hostname in groups['cloudkitty-api'] - -- name: Pulling cloudkitty-processor image - kolla_docker: - action: "pull_image" - common_options: "{{ docker_common_options }}" - image: "{{ cloudkitty_processor_image_full }}" - when: inventory_hostname in groups['cloudkitty-processor'] + image: "{{ item.value.image }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ cloudkitty_services }}" diff --git a/ansible/roles/cloudkitty/tasks/reconfigure.yml b/ansible/roles/cloudkitty/tasks/reconfigure.yml index 6cd265dfc1..e078ef1318 100644 --- a/ansible/roles/cloudkitty/tasks/reconfigure.yml +++ b/ansible/roles/cloudkitty/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: cloudkitty_api, group: cloudkitty-api } - - { name: cloudkitty_processor, group: cloudkitty-processor } - -- 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: cloudkitty_api, group: cloudkitty-api } - - { name: cloudkitty_processor, group: cloudkitty-processor } - -- 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: cloudkitty_api, group: cloudkitty-api } - - { name: cloudkitty_processor, group: cloudkitty-processor } - -- 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: cloudkitty_api, group: cloudkitty-api }, - { name: cloudkitty_processor, group: cloudkitty-processor }] - - "{{ 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: cloudkitty_api, group: cloudkitty-api }, - { name: cloudkitty_processor, group: cloudkitty-processor }] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" +- include: deploy.yml diff --git a/ansible/roles/cloudkitty/tasks/start.yml b/ansible/roles/cloudkitty/tasks/start.yml deleted file mode 100644 index 5b4741d73b..0000000000 --- a/ansible/roles/cloudkitty/tasks/start.yml +++ /dev/null @@ -1,24 +0,0 @@ ---- -- name: Starting cloudkitty-processor container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ cloudkitty_processor_image_full }}" - name: "cloudkitty_processor" - volumes: - - "{{ node_config_directory }}/cloudkitty-processor/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['cloudkitty-processor'] - -- name: Starting cloudkitty-api container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ cloudkitty_api_image_full }}" - name: "cloudkitty_api" - volumes: - - "{{ node_config_directory }}/cloudkitty-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['cloudkitty-api'] diff --git a/ansible/roles/cloudkitty/tasks/upgrade.yml b/ansible/roles/cloudkitty/tasks/upgrade.yml index 308053080c..c38db1adf4 100644 --- a/ansible/roles/cloudkitty/tasks/upgrade.yml +++ b/ansible/roles/cloudkitty/tasks/upgrade.yml @@ -3,4 +3,5 @@ - include: bootstrap_service.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers