From 8b0fa7728ccb330559416c0bebf64a4a36df89e9 Mon Sep 17 00:00:00 2001 From: Mauricio Lima Date: Mon, 23 Jan 2017 11:44:37 -0300 Subject: [PATCH] Optimize reconfiguration for sahara Co-Authored-By: caoyuan Change-Id: Iefc3ffc03a15a2e8eea49e91297ce75a5690010b Partially-implements: blueprint better-reconfigure --- ansible/roles/sahara/defaults/main.yml | 22 ++++++ ansible/roles/sahara/handlers/main.yml | 46 ++++++++++++ .../roles/sahara/tasks/bootstrap_service.yml | 12 ++-- ansible/roles/sahara/tasks/config.yml | 70 +++++++++++++------ ansible/roles/sahara/tasks/deploy.yml | 5 +- ansible/roles/sahara/tasks/pull.yml | 16 ++--- ansible/roles/sahara/tasks/reconfigure.yml | 69 +----------------- ansible/roles/sahara/tasks/start.yml | 26 ------- ansible/roles/sahara/tasks/upgrade.yml | 3 +- 9 files changed, 134 insertions(+), 135 deletions(-) create mode 100644 ansible/roles/sahara/handlers/main.yml delete mode 100644 ansible/roles/sahara/tasks/start.yml diff --git a/ansible/roles/sahara/defaults/main.yml b/ansible/roles/sahara/defaults/main.yml index d545f68346..478847d895 100644 --- a/ansible/roles/sahara/defaults/main.yml +++ b/ansible/roles/sahara/defaults/main.yml @@ -1,6 +1,28 @@ --- project_name: "sahara" +sahara_services: + sahara-api: + container_name: sahara_api + group: sahara-api + enabled: true + image: "{{ sahara_api_image_full }}" + volumes: + - "{{ node_config_directory }}/sahara-api/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "sahara:/var/lib/sahara/" + - "kolla_logs:/var/log/kolla/" + sahara-engine: + container_name: sahara_engine + group: sahara-engine + enabled: true + image: "{{ sahara_engine_image_full }}" + volumes: + - "{{ node_config_directory }}/sahara-engine/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "sahara:/var/lib/sahara/" + - "kolla_logs:/var/log/kolla/" + #################### # Database diff --git a/ansible/roles/sahara/handlers/main.yml b/ansible/roles/sahara/handlers/main.yml new file mode 100644 index 0000000000..79a9a57f73 --- /dev/null +++ b/ansible/roles/sahara/handlers/main.yml @@ -0,0 +1,46 @@ +--- +- name: Restart sahara-api container + vars: + service_name: "sahara-api" + service: "{{ sahara_services[service_name] }}" + config_json: "{{ sahara_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + sahara_conf: "{{ sahara_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ sahara_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + sahara_api_container: "{{ check_sahara_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 sahara_conf.changed | bool + or policy_json.changed | bool + or sahara_api_container.changed | bool + +- name: Restart sahara-engine container + vars: + service_name: "sahara-engine" + service: "{{ sahara_services[service_name] }}" + config_json: "{{ sahara_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + sahara_conf: "{{ sahara_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ sahara_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + sahara_engine_container: "{{ check_sahara_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 sahara_conf.changed | bool + or policy_json.changed | bool + or sahara_engine_container.changed | bool diff --git a/ansible/roles/sahara/tasks/bootstrap_service.yml b/ansible/roles/sahara/tasks/bootstrap_service.yml index e4b88fa4c4..f0bd0bcadf 100644 --- a/ansible/roles/sahara/tasks/bootstrap_service.yml +++ b/ansible/roles/sahara/tasks/bootstrap_service.yml @@ -1,5 +1,7 @@ --- - name: Running Sahara bootstrap container + vars: + sahara_api: "{{ sahara_services['sahara-api'] }}" kolla_docker: action: "start_container" common_options: "{{ docker_common_options }}" @@ -7,15 +9,11 @@ environment: KOLLA_BOOTSTRAP: KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}" - image: "{{ sahara_api_image_full }}" + image: "{{ sahara_api.image }}" labels: BOOTSTRAP: name: "bootstrap_sahara" restart_policy: "never" - volumes: - - "{{ node_config_directory }}/sahara-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - - "sahara:/var/lib/sahara/" + volumes: "{{ sahara_api.volumes }}" run_once: True - delegate_to: "{{ groups['sahara-api'][0] }}" + delegate_to: "{{ groups[sahara_api.group][0] }}" diff --git a/ansible/roles/sahara/tasks/config.yml b/ansible/roles/sahara/tasks/config.yml index 59cda53143..5634ab3246 100644 --- a/ansible/roles/sahara/tasks/config.yml +++ b/ansible/roles/sahara/tasks/config.yml @@ -1,37 +1,46 @@ --- - name: Ensuring config directories exist file: - path: "{{ node_config_directory }}/{{ item }}" + path: "{{ node_config_directory }}/{{ item.key }}" state: "directory" recurse: yes - with_items: - - "sahara-api" - - "sahara-engine" + when: inventory_hostname in groups[item.value.group] + with_dict: "{{ sahara_services }}" - name: Copying over config.json files for services template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "sahara-api" - - "sahara-engine" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: sahara_config_jsons + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ sahara_services }}" + notify: + - Restart sahara-api container + - Restart sahara-engine container - name: Copying over sahara.conf merge_configs: vars: - service_name: "{{ item }}" + service_name: "{{ item.key }}" sources: - "{{ role_path }}/templates/sahara.conf.j2" - "{{ node_custom_config }}/global.conf" - "{{ node_custom_config }}/database.conf" - "{{ node_custom_config }}/messaging.conf" - "{{ node_custom_config }}/sahara.conf" - - "{{ node_custom_config }}/sahara/{{ item }}.conf" + - "{{ node_custom_config }}/sahara/{{ item.key }}.conf" - "{{ node_custom_config }}/sahara/{{ inventory_hostname }}/sahara.conf" - dest: "{{ node_config_directory }}/{{ item }}/sahara.conf" - with_items: - - "sahara-api" - - "sahara-engine" + dest: "{{ node_config_directory }}/{{ item.key }}/sahara.conf" + register: sahara_confs + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ sahara_services }}" + notify: + - Restart sahara-api container + - Restart sahara-engine container - name: Check if policies shall be overwritten local_action: stat path="{{ node_custom_config }}/sahara/policy.json" @@ -40,9 +49,30 @@ - name: Copying over existing policy.json template: src: "{{ node_custom_config }}/sahara/policy.json" - dest: "{{ node_config_directory }}/{{ item }}/policy.json" - with_items: - - "sahara-api" - - "sahara-engine" + dest: "{{ node_config_directory }}/{{ item.key }}/policy.json" + register: sahara_policy_jsons when: - sahara_policy.stat.exists + - sahara_policy.stat.exists + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ sahara_services }}" + notify: + - Restart sahara-api container + - Restart sahara-engine container + +- name: Check sahara 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_sahara_containers + when: + - action != "config" + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ sahara_services }}" + notify: + - Restart sahara-api container + - Restart sahara-engine container diff --git a/ansible/roles/sahara/tasks/deploy.yml b/ansible/roles/sahara/tasks/deploy.yml index f109cf7360..05655b739e 100644 --- a/ansible/roles/sahara/tasks/deploy.yml +++ b/ansible/roles/sahara/tasks/deploy.yml @@ -9,6 +9,5 @@ - include: bootstrap.yml when: inventory_hostname in groups['sahara-api'] -- include: start.yml - when: inventory_hostname in groups['sahara-api'] or - inventory_hostname in groups['sahara-engine'] +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/sahara/tasks/pull.yml b/ansible/roles/sahara/tasks/pull.yml index 8d0037f485..162ad1f034 100644 --- a/ansible/roles/sahara/tasks/pull.yml +++ b/ansible/roles/sahara/tasks/pull.yml @@ -1,14 +1,10 @@ --- -- name: Pulling sahara-engine image +- name: Pulling sahara images kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ sahara_engine_image_full }}" - when: inventory_hostname in groups['sahara-engine'] - -- name: Pulling sahara-api image - kolla_docker: - action: "pull_image" - common_options: "{{ docker_common_options }}" - image: "{{ sahara_api_image_full }}" - when: inventory_hostname in groups['sahara-api'] + image: "{{ item.value.image }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ sahara_services }}" diff --git a/ansible/roles/sahara/tasks/reconfigure.yml b/ansible/roles/sahara/tasks/reconfigure.yml index 890f10645b..e078ef1318 100644 --- a/ansible/roles/sahara/tasks/reconfigure.yml +++ b/ansible/roles/sahara/tasks/reconfigure.yml @@ -1,69 +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: sahara_api, group: sahara-api } - - { name: sahara_engine, group: sahara-engine } - -- 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: sahara_api, group: sahara-api } - - { name: sahara_engine, group: sahara-engine } - -# 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: sahara_api, group: sahara-api } - - { name: sahara_engine, group: sahara-engine } - -- 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: sahara_api, group: sahara-api }, - { name: sahara_engine, group: sahara-engine },] - - "{{ 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: - - inventory_hostname in groups[item[0]['group']] - - config_strategy == 'COPY_ALWAYS' - - item[1]['KOLLA_CONFIG_STRATEGY'] != 'COPY_ONCE' - - item[2]['rc'] == 1 - with_together: - - [{ name: sahara_api, group: sahara-api }, - { name: sahara_engine, group: sahara-engine },] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" +- include: deploy.yml diff --git a/ansible/roles/sahara/tasks/start.yml b/ansible/roles/sahara/tasks/start.yml deleted file mode 100644 index bb7b22d6bf..0000000000 --- a/ansible/roles/sahara/tasks/start.yml +++ /dev/null @@ -1,26 +0,0 @@ ---- -- name: Starting sahara-api container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ sahara_api_image_full }}" - name: "sahara_api" - volumes: - - "{{ node_config_directory }}/sahara-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "sahara:/var/lib/sahara/" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['sahara-api'] - -- name: Starting sahara-engine container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ sahara_engine_image_full }}" - name: "sahara_engine" - volumes: - - "{{ node_config_directory }}/sahara-engine/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "sahara:/var/lib/sahara/" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['sahara-engine'] diff --git a/ansible/roles/sahara/tasks/upgrade.yml b/ansible/roles/sahara/tasks/upgrade.yml index 308053080c..c38db1adf4 100644 --- a/ansible/roles/sahara/tasks/upgrade.yml +++ b/ansible/roles/sahara/tasks/upgrade.yml @@ -3,4 +3,5 @@ - include: bootstrap_service.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers