From 91dbe0ec0ca7c4257d9696b237e42237e9f80b6d Mon Sep 17 00:00:00 2001 From: caoyuan Date: Sun, 22 Jan 2017 10:06:06 +0800 Subject: [PATCH] Optimize reconfiguration for barbican Change-Id: I0808b32115989ed55bf5401d2f5401930766d39a Partially-implements: blueprint better-reconfigure --- ansible/roles/barbican/defaults/main.yml | 29 +++++++ ansible/roles/barbican/handlers/main.yml | 71 +++++++++++++++++ .../barbican/tasks/bootstrap_service.yml | 12 ++- ansible/roles/barbican/tasks/config.yml | 78 ++++++++++++++----- ansible/roles/barbican/tasks/deploy.yml | 6 +- ansible/roles/barbican/tasks/pull.yml | 23 ++---- ansible/roles/barbican/tasks/reconfigure.yml | 74 +----------------- ansible/roles/barbican/tasks/start.yml | 37 --------- ansible/roles/barbican/tasks/upgrade.yml | 3 +- 9 files changed, 175 insertions(+), 158 deletions(-) create mode 100644 ansible/roles/barbican/handlers/main.yml delete mode 100644 ansible/roles/barbican/tasks/start.yml diff --git a/ansible/roles/barbican/defaults/main.yml b/ansible/roles/barbican/defaults/main.yml index 7fb8d628f9..592881a8d3 100644 --- a/ansible/roles/barbican/defaults/main.yml +++ b/ansible/roles/barbican/defaults/main.yml @@ -1,6 +1,35 @@ --- project_name: "barbican" +barbican_services: + barbican-api: + container_name: barbican_api + group: barbican-api + enabled: true + image: "{{ barbican_api_image_full }}" + volumes: + - "{{ node_config_directory }}/barbican-api/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "barbican:/var/lib/barbican/" + - "kolla_logs:/var/log/kolla/" + barbican-keystone-listener: + container_name: barbican_keystone_listener + group: barbican-keystone-listener + enabled: true + image: "{{ barbican_keystone_listener_image_full }}" + volumes: + - "{{ node_config_directory }}/barbican-keystone-listener/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" + barbican-worker: + container_name: barbican_worker + group: barbican-worker + enabled: true + image: "{{ barbican_worker_image_full }}" + volumes: + - "{{ node_config_directory }}/barbican-worker/:{{ container_config_directory }}/:ro" + - "/etc/localtime:/etc/localtime:ro" + - "kolla_logs:/var/log/kolla/" #################### # Database diff --git a/ansible/roles/barbican/handlers/main.yml b/ansible/roles/barbican/handlers/main.yml new file mode 100644 index 0000000000..1f40186be7 --- /dev/null +++ b/ansible/roles/barbican/handlers/main.yml @@ -0,0 +1,71 @@ +--- +- name: Restart barbican-api container + vars: + service_name: "barbican-api" + service: "{{ barbican_services[service_name] }}" + config_json: "{{ barbican_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + barbican_conf: "{{ barbican_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ barbican_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + barbican_api_container: "{{ check_barbican_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 barbican_conf.changed | bool + or policy_json.changed | bool + or barbican_api_ini.changed | bool + or barbican_api_paste.changed | bool + or barbican_api_container.changed | bool + +- name: Restart barbican-keystone-listener container + vars: + service_name: "barbican-keystone-listener" + service: "{{ barbican_services[service_name] }}" + config_json: "{{ barbican_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + barbican_conf: "{{ barbican_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ barbican_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + barbican_keystone_listener_container: "{{ check_barbican_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 barbican_conf.changed | bool + or policy_json.changed | bool + or barbican_keystone_listener_container.changed | bool + +- name: Restart barbican-worker container + vars: + service_name: "barbican-worker" + service: "{{ barbican_services[service_name] }}" + config_json: "{{ barbican_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + barbican_conf: "{{ barbican_confs.results|selectattr('item.key', 'equalto', service_name)|first }}" + policy_json: "{{ barbican_policy_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}" + barbican_worker_container: "{{ check_barbican_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 barbican_conf.changed | bool + or policy_json.changed | bool + or barbican_worker_container.changed | bool diff --git a/ansible/roles/barbican/tasks/bootstrap_service.yml b/ansible/roles/barbican/tasks/bootstrap_service.yml index 04ece90fdb..862b727c0a 100644 --- a/ansible/roles/barbican/tasks/bootstrap_service.yml +++ b/ansible/roles/barbican/tasks/bootstrap_service.yml @@ -1,5 +1,7 @@ --- - name: Running barbican bootstrap container + vars: + barbican_api: "{{ barbican_services['barbican-api'] }}" kolla_docker: action: "start_container" common_options: "{{ docker_common_options }}" @@ -7,15 +9,11 @@ environment: KOLLA_BOOTSTRAP: KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}" - image: "{{ barbican_api_image_full }}" + image: "{{ barbican_api.image }}" labels: BOOTSTRAP: name: "bootstrap_barbican" restart_policy: "never" - volumes: - - "{{ node_config_directory }}/barbican-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "barbican:/var/lib/barbican/" - - "kolla_logs:/var/log/kolla/" + volumes: "{{ barbican_api.volumes }}" run_once: True - delegate_to: "{{ groups['barbican-api'][0] }}" + delegate_to: "{{ groups[barbican_api.group][0] }}" diff --git a/ansible/roles/barbican/tasks/config.yml b/ansible/roles/barbican/tasks/config.yml index 0b71c1ce5f..4d26d4c2b7 100644 --- a/ansible/roles/barbican/tasks/config.yml +++ b/ansible/roles/barbican/tasks/config.yml @@ -11,46 +11,68 @@ - name: Copying over config.json files for services template: - src: "{{ item }}.json.j2" - dest: "{{ node_config_directory }}/{{ item }}/config.json" - with_items: - - "barbican-api" - - "barbican-worker" - - "barbican-keystone-listener" + src: "{{ item.key }}.json.j2" + dest: "{{ node_config_directory }}/{{ item.key }}/config.json" + register: barbican_config_jsons + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ barbican_services }}" + notify: + - Restart {{ item.key }} container - name: Copying over barbican-api.ini + vars: + service: "{{ barbican_services['barbican-api'] }}" merge_configs: sources: - "{{ role_path }}/templates/barbican-api.ini.j2" - "{{ node_custom_config }}/barbican-api/barbican-api.ini" - "{{ node_custom_config }}/barbican-api/{{ inventory_hostname }}/barbican-api.ini" dest: "{{ node_config_directory }}/barbican-api/vassals/barbican-api.ini" + register: barbican_api_ini + when: + - inventory_hostname in groups['barbican-api'] + - service.enabled | bool + notify: + - Restart barbican-api container - name: Copying over barbican-api-paste.ini + vars: + service: "{{ barbican_services['barbican-api'] }}" merge_configs: sources: - "{{ role_path }}/templates/barbican-api-paste.ini.j2" - "{{ node_custom_config }}/barbican-api/barbican-api-paste.ini" - "{{ node_custom_config }}/barbican-api/{{ inventory_hostname }}/barbican-api-paste.ini" dest: "{{ node_config_directory }}/barbican-api/barbican-api-paste.ini" + when: + - inventory_hostname in groups['barbiacn-api'] + - service.enabled | bool + register: barbican_api_paste + notify: + - Restart barbican-api container - name: Copying over barbican.conf merge_configs: vars: - service_name: "{{ item }}" + service_name: "{{ item.key }}" sources: - "{{ role_path }}/templates/barbican.conf.j2" - "{{ node_custom_config }}/global.conf" - "{{ node_custom_config }}/database.conf" - "{{ node_custom_config }}/messaging.conf" - "{{ node_custom_config }}/barbican.conf" - - "{{ node_custom_config }}/barbican/{{ item }}.conf" + - "{{ node_custom_config }}/barbican/{{ item.key }}.conf" - "{{ node_custom_config }}/barbican/{{ inventory_hostname }}/barbican.conf" - dest: "{{ node_config_directory }}/{{ item }}/barbican.conf" - with_items: - - "barbican-api" - - "barbican-keystone-listener" - - "barbican-worker" + dest: "{{ node_config_directory }}/{{ item.key }}/barbican.conf" + register: barbican_confs + when: + - item.value.enabled | bool + - inventory_hostname in groups[item.value.group] + with_dict: "{{ barbican_services }}" + notify: + - Restart {{ item.key }} container - name: Check if policies shall be overwritten local_action: stat path="{{ node_custom_config }}/barbican/policy.json" @@ -59,10 +81,28 @@ - name: Copying over existing policy.json template: src: "{{ node_custom_config }}/barbican/policy.json" - dest: "{{ node_config_directory }}/{{ item }}/policy.json" - with_items: - - "barbican-api" - - "barbican-keystone-listener" - - "barbican-worker" + dest: "{{ node_config_directory }}/{{ item.key }}/policy.json" + register: barbican_policy_jsons when: - barbican_policy.stat.exists + - barbican_policy.stat.exists + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ barbican_services }}" + notify: + - Restart {{ item.key }} container + +- name: Check barbican 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_barbican_containers + when: + - action != "config" + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ barbican_services }}" + notify: + - Restart {{ item.key }} container diff --git a/ansible/roles/barbican/tasks/deploy.yml b/ansible/roles/barbican/tasks/deploy.yml index 4b9f3ebcc3..3951ff2902 100644 --- a/ansible/roles/barbican/tasks/deploy.yml +++ b/ansible/roles/barbican/tasks/deploy.yml @@ -10,7 +10,5 @@ - include: bootstrap.yml when: inventory_hostname in groups['barbican-api'] -- include: start.yml - when: inventory_hostname in groups['barbican-api'] or - inventory_hostname in groups['barbican-worker'] or - inventory_hostname in groups['barbican-keystone-listener'] +- name: Flush handlers + meta: flush_handlers diff --git a/ansible/roles/barbican/tasks/pull.yml b/ansible/roles/barbican/tasks/pull.yml index 284f8970da..adacfbb765 100644 --- a/ansible/roles/barbican/tasks/pull.yml +++ b/ansible/roles/barbican/tasks/pull.yml @@ -1,21 +1,10 @@ --- -- name: Pulling barbican-api image +- name: Pulling barbican images kolla_docker: action: "pull_image" common_options: "{{ docker_common_options }}" - image: "{{ barbican_api_image_full }}" - when: inventory_hostname in groups['barbican-api'] - -- name: Pulling barbican-keystone-listener image - kolla_docker: - action: "pull_image" - common_options: "{{ docker_common_options }}" - image: "{{ barbican_keystone_listener_image_full }}" - when: inventory_hostname in groups['barbican-keystone-listener'] - -- name: Pulling barbican-worker image - kolla_docker: - action: "pull_image" - common_options: "{{ docker_common_options }}" - image: "{{ barbican_worker_image_full }}" - when: inventory_hostname in groups['barbican-worker'] + image: "{{ item.value.image }}" + when: + - inventory_hostname in groups[item.value.group] + - item.value.enabled | bool + with_dict: "{{ barbican_services }}" diff --git a/ansible/roles/barbican/tasks/reconfigure.yml b/ansible/roles/barbican/tasks/reconfigure.yml index 84f378665f..e078ef1318 100644 --- a/ansible/roles/barbican/tasks/reconfigure.yml +++ b/ansible/roles/barbican/tasks/reconfigure.yml @@ -1,74 +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: barbican_api, group: barbican-api } - - { name: barbican_keystone_listener, group: barbican-keystone-listener } - - { name: barbican_worker, group: barbican-worker } - -- 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: barbican_api, group: barbican-api } - - { name: barbican_keystone_listener, group: barbican-keystone-listener } - - { name: barbican_worker, group: barbican-worker } - -# 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: barbican_api, group: barbican-api } - - { name: barbican_keystone_listener, group: barbican-keystone-listener } - - { name: barbican_worker, group: barbican-worker } - -- 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: barbican_api, group: barbican-api }, - { name: barbican_keystone_listener, group: barbican-keystone-listener }, - { name: barbican_worker, group: barbican-worker }] - - "{{ 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: barbican_api, group: barbican-api }, - { name: barbican_keystone_listener, group: barbican-keystone-listener }, - { name: barbican_worker, group: barbican-worker }] - - "{{ container_envs.results }}" - - "{{ check_results.results }}" +- include: deploy.yml diff --git a/ansible/roles/barbican/tasks/start.yml b/ansible/roles/barbican/tasks/start.yml deleted file mode 100644 index c862beaaf9..0000000000 --- a/ansible/roles/barbican/tasks/start.yml +++ /dev/null @@ -1,37 +0,0 @@ ---- -- name: Starting barbican-api container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ barbican_api_image_full }}" - name: "barbican_api" - volumes: - - "{{ node_config_directory }}/barbican-api/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "barbican:/var/lib/barbican/" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['barbican-api'] - -- name: Starting barbican-keystone-listener container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ barbican_keystone_listener_image_full }}" - name: "barbican_keystone_listener" - volumes: - - "{{ node_config_directory }}/barbican-keystone-listener/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['barbican-keystone-listener'] - -- name: Starting barbican-worker container - kolla_docker: - action: "start_container" - common_options: "{{ docker_common_options }}" - image: "{{ barbican_worker_image_full }}" - name: "barbican_worker" - volumes: - - "{{ node_config_directory }}/barbican-worker/:{{ container_config_directory }}/:ro" - - "/etc/localtime:/etc/localtime:ro" - - "kolla_logs:/var/log/kolla/" - when: inventory_hostname in groups['barbican-worker'] diff --git a/ansible/roles/barbican/tasks/upgrade.yml b/ansible/roles/barbican/tasks/upgrade.yml index 308053080c..c38db1adf4 100644 --- a/ansible/roles/barbican/tasks/upgrade.yml +++ b/ansible/roles/barbican/tasks/upgrade.yml @@ -3,4 +3,5 @@ - include: bootstrap_service.yml -- include: start.yml +- name: Flush handlers + meta: flush_handlers