From 9af94ff055b85dc5989a75509a3b41bf0ccfb35e Mon Sep 17 00:00:00 2001 From: caoyuan Date: Wed, 18 Jan 2017 21:20:20 +0800 Subject: [PATCH] Optimize reconfigure action for rally Change-Id: I2fb2f4cd88638dfc5ccc261a0b113094cba60715 Partially-implements: blueprint better-reconfigure --- ansible/roles/rally/defaults/main.yml | 11 ++++ ansible/roles/rally/handlers/main.yml | 23 +++++++ .../roles/rally/tasks/bootstrap_service.yml | 11 ++-- ansible/roles/rally/tasks/config.yml | 58 +++++++++++++---- ansible/roles/rally/tasks/deploy.yml | 5 +- ansible/roles/rally/tasks/pull.yml | 6 +- ansible/roles/rally/tasks/reconfigure.yml | 64 +------------------ ansible/roles/rally/tasks/start.yml | 11 ---- ansible/roles/rally/tasks/upgrade.yml | 3 +- 9 files changed, 96 insertions(+), 96 deletions(-) create mode 100644 ansible/roles/rally/handlers/main.yml delete mode 100644 ansible/roles/rally/tasks/start.yml diff --git a/ansible/roles/rally/defaults/main.yml b/ansible/roles/rally/defaults/main.yml index ced0999cb0..ecba9ac67e 100644 --- a/ansible/roles/rally/defaults/main.yml +++ b/ansible/roles/rally/defaults/main.yml @@ -1,6 +1,17 @@ --- project_name: "rally" +rally_services: + rally: + container_name: "rally" + image: "{{ rally_image_full }}" + enabled: true + group: "rally" + volumes: + - "{{ node_config_directory }}/rally/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + ######## # Docker diff --git a/ansible/roles/rally/handlers/main.yml b/ansible/roles/rally/handlers/main.yml new file mode 100644 index 0000000000..d07ea45965 --- /dev/null +++ b/ansible/roles/rally/handlers/main.yml @@ -0,0 +1,23 @@ +- name: Restart rally container + vars: + service_name: "rally" + service: "{{ rally_services[service_name] }}" + config_json: "{{ rally_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + rally_conf: "{{ rally_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ rally_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + rally_container: "{{ check_rally_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|reject('equalto', '')|list }}" + when: + - action != "config" + - inventory_hostname in groups[service.group] + - service.enabled | bool + - config_json.changed | bool + or rally_conf.changed | bool + or policy_json.changed | bool + or rally_container.changed | bool diff --git a/ansible/roles/rally/tasks/bootstrap_service.yml b/ansible/roles/rally/tasks/bootstrap_service.yml index 47a7c8f7f8..cc8aaccf75 100644 --- a/ansible/roles/rally/tasks/bootstrap_service.yml +++ b/ansible/roles/rally/tasks/bootstrap_service.yml @@ -1,5 +1,7 @@ --- - name: Running rally bootstrap container + vars: + rally: "{{ rally_services['rally'] }}" kolla_docker: action: "start_container" common_options: "{{ docker_common_options }}" @@ -7,14 +9,11 @@ environment: KOLLA_BOOTSTRAP: KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}" - image: "{{ rally_image_full }}" + image: "{{ rally.image }}" labels: BOOTSTRAP: name: "bootstrap_rally" restart_policy: "never" - volumes: - - "{{ node_config_directory }}/rally/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" + volumes: "{{ rally.volumes }}" run_once: True - delegate_to: "{{ groups['rally'][0] }}" + delegate_to: "{{ groups[rally.group][0] }}" diff --git a/ansible/roles/rally/tasks/config.yml b/ansible/roles/rally/tasks/config.yml index 3304915de3..48a4012a4e 100644 --- a/ansible/roles/rally/tasks/config.yml +++ b/ansible/roles/rally/tasks/config.yml @@ -1,29 +1,39 @@ --- - name: Ensuring config directories exist file: - path: "{{ node_config_directory }}/{{ item }}" + path: "{{ node_config_directory }}/{{ item.key }}" state: "directory" recurse: yes - with_items: - - "rally" + when: inventory_hostname in groups[item.value.group] + with_dict: "{{ rally_services }}" - name: Copying over config.json files for services template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "rally" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: rally_config_jsons + when: + - item.value.enabled | bool + - inventory_hostname in groups[item.value.group] + with_dict: "{{ rally_services }}" + notify: + - Restart rally container - name: Copying over rally.conf merge_configs: vars: - project_name: "rally" + service_name: "{{ item.key }}" sources: - "{{ role_path }}/templates/rally.conf.j2" - "{{ node_custom_config }}/rally.conf" - dest: "{{ node_config_directory }}/{{ item }}/rally.conf" - with_items: - - "rally" + dest: "{{ node_config_directory }}/{{ item.key }}/{{ item.key }}.conf" + register: rally_confs + when: + - item.value.enabled | bool + - inventory_hostname in groups[item.value.group] + with_dict: "{{ rally_services }}" + notify: + - Restart rally container - name: Check if policies shall be overwritten local_action: stat path="{{ node_custom_config }}/rally/policy.json" @@ -32,6 +42,28 @@ - name: Copying over existing policy.json template: src: "{{ node_custom_config }}/rally/policy.json" - dest: "{{ node_config_directory }}/rally/policy.json" + dest: "{{ node_config_directory }}/{{ item.key }}/policy.json" + register: rally_policy_jsons when: - rally_policy.stat.exists + - rally_policy.stat.exists + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ rally_services }}" + notify: + - Restart rally container + +- name: Check rally 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_rally_containers + when: + - action != "config" + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ rally_services }}" + notify: + - Restart rally container diff --git a/ansible/roles/rally/tasks/deploy.yml b/ansible/roles/rally/tasks/deploy.yml index 1dc8c04e30..9eca42dec5 100644 --- a/ansible/roles/rally/tasks/deploy.yml +++ b/ansible/roles/rally/tasks/deploy.yml @@ -1,4 +1,7 @@ --- - include: config.yml + - include: bootstrap.yml -- include: start.yml + +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/rally/tasks/pull.yml b/ansible/roles/rally/tasks/pull.yml index bcada160d8..171e7ee3e7 100644 --- a/ansible/roles/rally/tasks/pull.yml +++ b/ansible/roles/rally/tasks/pull.yml @@ -3,4 +3,8 @@ kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ rally_image_full }}" + image: "{{ item.value.image }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ rally_services }}" diff --git a/ansible/roles/rally/tasks/reconfigure.yml b/ansible/roles/rally/tasks/reconfigure.yml index 79c3dde5ac..e078ef1318 100644 --- a/ansible/roles/rally/tasks/reconfigure.yml +++ b/ansible/roles/rally/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: rally, group: rally } - -- 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: rally, group: rally } - -# 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: rally, group: rally } - -- 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: rally, group: rally }] - - "{{ 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: rally, group: rally }] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" +- include: deploy.yml diff --git a/ansible/roles/rally/tasks/start.yml b/ansible/roles/rally/tasks/start.yml deleted file mode 100644 index f95000fa17..0000000000 --- a/ansible/roles/rally/tasks/start.yml +++ /dev/null @@ -1,11 +0,0 @@ ---- -- name: Starting rally container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ rally_image_full }}" - name: "rally" - volumes: - - "{{ node_config_directory }}/rally/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" diff --git a/ansible/roles/rally/tasks/upgrade.yml b/ansible/roles/rally/tasks/upgrade.yml index 308053080c..c38db1adf4 100644 --- a/ansible/roles/rally/tasks/upgrade.yml +++ b/ansible/roles/rally/tasks/upgrade.yml @@ -3,4 +3,5 @@ - include: bootstrap_service.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers