From 927c89571fd818c715389d5b8501cadf32c56b3a Mon Sep 17 00:00:00 2001 From: caoyuan Date: Fri, 20 Jan 2017 23:36:43 +0800 Subject: [PATCH] Optimize reconfiguration for senlin Change-Id: I6516db6e1cb0ccd3de6c525e686e041619f83a36 Partially-implements: blueprint better-reconfigure --- ansible/roles/senlin/defaults/main.yml | 20 ++++++ ansible/roles/senlin/handlers/main.yml | 44 ++++++++++++ .../roles/senlin/tasks/bootstrap_service.yml | 11 ++- ansible/roles/senlin/tasks/config.yml | 70 +++++++++++++------ ansible/roles/senlin/tasks/deploy.yml | 5 +- ansible/roles/senlin/tasks/pull.yml | 16 ++--- ansible/roles/senlin/tasks/reconfigure.yml | 66 +---------------- ansible/roles/senlin/tasks/start.yml | 24 ------- ansible/roles/senlin/tasks/upgrade.yml | 3 +- 9 files changed, 130 insertions(+), 129 deletions(-) create mode 100644 ansible/roles/senlin/handlers/main.yml delete mode 100644 ansible/roles/senlin/tasks/start.yml diff --git a/ansible/roles/senlin/defaults/main.yml b/ansible/roles/senlin/defaults/main.yml index 811ad25c78..a2a48b6a01 100644 --- a/ansible/roles/senlin/defaults/main.yml +++ b/ansible/roles/senlin/defaults/main.yml @@ -1,6 +1,26 @@ --- project_name: "senlin" +senlin_services: + senlin-api: + container_name: senlin_api + group: senlin-api + enabled: true + image: "{{ senlin_api_image_full }}" + volumes: + - "{{ node_config_directory }}/senlin-api/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + senlin-engine: + container_name: senlin_engine + group: senlin-engine + enabled: true + image: "{{ senlin_engine_image_full }}" + volumes: + - "{{ node_config_directory }}/senlin-engine/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + #################### # Database #################### diff --git a/ansible/roles/senlin/handlers/main.yml b/ansible/roles/senlin/handlers/main.yml new file mode 100644 index 0000000000..9e85395801 --- /dev/null +++ b/ansible/roles/senlin/handlers/main.yml @@ -0,0 +1,44 @@ +--- +- name: Restart senlin-api container + vars: + service_name: "senlin-api" + service: "{{ senlin_services[service_name] }}" + config_json: "{{ senlin_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + senlin_conf: "{{ senlin_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ senlin_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + senlin_api_container: "{{ check_senlin_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: + - inventory_hostname in groups[service.group] + - service.enabled | bool + - config_json.changed | bool + or senlin_conf.changed | bool + or policy_json.changed | bool + or senlin_api_container.changed | bool + +- name: Restart senlin-engine container + vars: + service_name: "senlin-engine" + service: "{{ senlin_services[service_name] }}" + config_json: "{{ senlin_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + senlin_conf: "{{ senlin_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ senlin_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + senlin_engine_container: "{{ check_senlin_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: + - inventory_hostname in groups[service.group] + - service.enabled | bool + - config_json.changed | bool + or senlin_conf.changed | bool + or policy_json.changed | bool + or senlin_engine_container.changed | bool diff --git a/ansible/roles/senlin/tasks/bootstrap_service.yml b/ansible/roles/senlin/tasks/bootstrap_service.yml index ff3504d957..1fe89fb214 100644 --- a/ansible/roles/senlin/tasks/bootstrap_service.yml +++ b/ansible/roles/senlin/tasks/bootstrap_service.yml @@ -1,5 +1,7 @@ --- - name: Running Senlin bootstrap container + vars: + senlin_api: "{{ senlin_services['senlin-api'] }}" kolla_docker: action: "start_container" common_options: "{{ docker_common_options }}" @@ -7,14 +9,11 @@ environment: KOLLA_BOOTSTRAP: KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}" - image: "{{ senlin_api_image_full }}" + image: "{{ senlin_api.image }}" labels: BOOTSTRAP: name: "bootstrap_senlin" restart_policy: "never" - volumes: - - "{{ node_config_directory }}/senlin-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" + volumes: "{{ senlin_api.volumes }}" run_once: True - delegate_to: "{{ groups['senlin-api'][0] }}" + delegate_to: "{{ groups[senlin_api.group][0] }}" diff --git a/ansible/roles/senlin/tasks/config.yml b/ansible/roles/senlin/tasks/config.yml index 54773529b4..50b50419b1 100644 --- a/ansible/roles/senlin/tasks/config.yml +++ b/ansible/roles/senlin/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: - - "senlin-api" - - "senlin-engine" + when: inventory_hostname in groups[item.value.group] + with_dict: "{{ senlin_services }}" - name: Copying over config.json files for services template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "senlin-api" - - "senlin-engine" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: senlin_config_jsons + when: + - item.value.enabled | bool + - inventory_hostname in groups[item.value.group] + with_dict: "{{ senlin_services }}" + notify: + - Restart senlin-api container + - Restart senlin-engine container - name: Copying over senlin.conf merge_configs: vars: - service_name: "{{ item }}" + service_name: "{{ item.key }}" sources: - "{{ role_path }}/templates/senlin.conf.j2" - "{{ node_custom_config }}/global.conf" - "{{ node_custom_config }}/database.conf" - "{{ node_custom_config }}/messaging.conf" - "{{ node_custom_config }}/senlin.conf" - - "{{ node_custom_config }}/senlin/{{ item }}.conf" + - "{{ node_custom_config }}/senlin/{{ item.key }}.conf" - "{{ node_custom_config }}/senlin/{{ inventory_hostname }}/senlin.conf" - dest: "{{ node_config_directory }}/{{ item }}/senlin.conf" - with_items: - - "senlin-api" - - "senlin-engine" + dest: "{{ node_config_directory }}/{{ item.key }}/senlin.conf" + register: senlin_confs + when: + - item.value.enabled | bool + - inventory_hostname in groups[item.value.group] + with_dict: "{{ senlin_services }}" + notify: + - Restart senlin-api container + - Restart senlin-engine container - name: Check if policies shall be overwritten local_action: stat path="{{ node_custom_config }}/senlin/policy.json" @@ -40,9 +49,30 @@ - name: Copying over existing policy.json template: src: "{{ node_custom_config }}/senlin/policy.json" - dest: "{{ node_config_directory }}/{{ item }}/policy.json" - with_items: - - "senlin-api" - - "senlin-engine" + dest: "{{ node_config_directory }}/{{ item.key }}/policy.json" + register: senlin_policy_jsons when: - senlin_policy.stat.exists + - senlin_policy.stat.exists + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ senlin_services }}" + notify: + - Restart senlin-api container + - Restart senlin-engine container + +- name: Check senlin 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_senlin_containers + when: + - action != 'genconfig' + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ senlin_services }}" + notify: + - Restart senlin-api container + - Restart senlin-engine container diff --git a/ansible/roles/senlin/tasks/deploy.yml b/ansible/roles/senlin/tasks/deploy.yml index 46aedd6e1f..7e02e41d90 100644 --- a/ansible/roles/senlin/tasks/deploy.yml +++ b/ansible/roles/senlin/tasks/deploy.yml @@ -9,6 +9,5 @@ - include: bootstrap.yml when: inventory_hostname in groups['senlin-api'] -- include: start.yml - when: inventory_hostname in groups['senlin-api'] or - inventory_hostname in groups['senlin-engine'] +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/senlin/tasks/pull.yml b/ansible/roles/senlin/tasks/pull.yml index 3a21aee052..11faee637c 100644 --- a/ansible/roles/senlin/tasks/pull.yml +++ b/ansible/roles/senlin/tasks/pull.yml @@ -1,14 +1,10 @@ --- -- name: Pulling senlin-api image +- name: Pulling senlin images kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ senlin_api_image_full }}" - when: inventory_hostname in groups['senlin-api'] - -- name: Pulling senlin-engine image - kolla_docker: - action: "pull_image" - common_options: "{{ docker_common_options }}" - image: "{{ senlin_engine_image_full }}" - when: inventory_hostname in groups['senlin-engine'] + image: "{{ item.value.image }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ senlin_services }}" diff --git a/ansible/roles/senlin/tasks/reconfigure.yml b/ansible/roles/senlin/tasks/reconfigure.yml index ffd25dc392..e078ef1318 100644 --- a/ansible/roles/senlin/tasks/reconfigure.yml +++ b/ansible/roles/senlin/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: senlin_api, group: senlin-api } - - { name: senlin_engine, group: senlin-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: senlin_api, group: senlin-api } - - { name: senlin_engine, group: senlin-engine } - -- 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: senlin_api, group: senlin-api } - - { name: senlin_engine, group: senlin-engine } - -- 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: senlin_api, group: senlin-api }, - { name: senlin_engine, group: senlin-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: - - 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: senlin_api, group: senlin-api }, - { name: senlin_engine, group: senlin-engine }] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" +- include: deploy.yml diff --git a/ansible/roles/senlin/tasks/start.yml b/ansible/roles/senlin/tasks/start.yml deleted file mode 100644 index f539bb3cd5..0000000000 --- a/ansible/roles/senlin/tasks/start.yml +++ /dev/null @@ -1,24 +0,0 @@ ---- -- name: Starting senlin-engine container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ senlin_engine_image_full }}" - name: "senlin_engine" - volumes: - - "{{ node_config_directory }}/senlin-engine/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['senlin-engine'] - -- name: Starting senlin-api container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ senlin_api_image_full }}" - name: "senlin_api" - volumes: - - "{{ node_config_directory }}/senlin-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['senlin-api'] diff --git a/ansible/roles/senlin/tasks/upgrade.yml b/ansible/roles/senlin/tasks/upgrade.yml index 308053080c..c38db1adf4 100644 --- a/ansible/roles/senlin/tasks/upgrade.yml +++ b/ansible/roles/senlin/tasks/upgrade.yml @@ -3,4 +3,5 @@ - include: bootstrap_service.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers