Optimize reconfiguration for mongodb

Change-Id: If3828170c00a9c5327b94d9253be93aefa147db9
Partially-implements: blueprint better-reconfigure
This commit is contained in:
caoyuan 2017-02-15 10:10:03 +08:00
parent a9e5836cde
commit 40b635b9c5
8 changed files with 101 additions and 104 deletions

View File

@ -1,6 +1,19 @@
---
project_name: "mongodb"
mongodb_services:
mongodb:
container_name: mongodb
group: mongodb
enabled: true
privileged: True
image: "{{ mongodb_image_full }}"
volumes:
- "{{ node_config_directory }}/mongodb/:{{ container_config_directory }}/:ro"
- "/etc/localtime:/etc/localtime:ro"
- "kolla_logs:/var/log/kolla/"
- "mongodb:/var/lib/mongodb"
####################
# Docker

View File

@ -0,0 +1,38 @@
---
- name: Restart mongodb container
vars:
service_name: "mongodb"
service: "{{ mongodb_services[service_name] }}"
config_json: "{{ mongodb_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
mongodb_conf: "{{ mongodb_confs.results|selectattr('item.key', 'equalto', service_name)|first }}"
mongodb_container: "{{ check_mongodb_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 }}"
when:
- action != "config"
- inventory_hostname in groups[service.group]
- service.enabled | bool
- config_json.changed | bool
or mongodb_conf.changed | bool
or mongodb_container.changed | bool
notify:
- Waiting for the mongodb startup
- Checking current replication status
- name: Waiting for the mongodb startup
wait_for: host={{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }} port={{ mongodb_port }}
- name: Checking current replication status
command: "docker exec -t mongodb mongo {{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }} --quiet --eval rs.status().ok"
register: mongodb_replication_status
changed_when: false
delegate_to: "{{ groups['mongodb'][0] }}"
run_once: True
- include: "bootstrap_cluster.yml"
when: mongodb_replication_status.stdout != "1"

View File

@ -1,22 +1,51 @@
---
- name: Ensuring config directories exist
file:
path: "{{ node_config_directory }}/{{ item }}"
path: "{{ node_config_directory }}/{{ item.key }}"
state: "directory"
recurse: yes
with_items:
- "mongodb"
when:
- inventory_hostname in groups[item.value.group]
- item.value.enabled | bool
with_dict: "{{ mongodb_services }}"
- name: Copying over config.json files for services
template:
src: "{{ item }}.json.j2"
dest: "{{ node_config_directory }}/{{ item }}/config.json"
with_items:
- "mongodb"
src: "{{ item.key }}.json.j2"
dest: "{{ node_config_directory }}/{{ item.key }}/config.json"
register: mongodb_config_jsons
when:
- inventory_hostname in groups[item.value.group]
- item.value.enabled | bool
with_dict: "{{ mongodb_services }}"
notify:
- Restart mongodb container
- name: Copying over mongodb.conf
template:
src: "{{ item }}.j2"
dest: "{{ node_config_directory }}/mongodb/{{ item }}"
with_items:
- "mongodb.conf"
src: "{{ item.key }}.conf.j2"
dest: "{{ node_config_directory }}/mongodb/{{ item.key }}.conf"
register: mongodb_confs
when:
- inventory_hostname in groups[item.value.group]
- item.value.enabled | bool
with_dict: "{{ mongodb_services }}"
notify:
- Restart mongodb container
- name: Check mongodb containers
kolla_docker:
action: "compare_container"
common_options: "{{ docker_common_options }}"
name: "{{ item.value.container_name }}"
image: "{{ item.value.image }}"
privileged: "{{ item.value.privileged|default(False) }}"
volumes: "{{ item.value.volumes }}"
register: check_mongodb_containers
when:
- action != "config"
- inventory_hostname in groups[item.value.group]
- item.value.enabled | bool
with_dict: "{{ mongodb_services }}"
notify:
- Restart mongodb container

View File

@ -3,4 +3,5 @@
- include: bootstrap.yml
- include: start.yml
- name: Flush handlers
meta: flush_handlers

View File

@ -3,5 +3,8 @@
kolla_docker:
action: "pull_image"
common_options: "{{ docker_common_options }}"
image: "{{ mongodb_image_full }}"
when: inventory_hostname in groups['mongodb']
image: "{{ item.value.image }}"
when:
- inventory_hostname in groups[item.value.group]
- item.value.enabled | bool
with_dict: "{{ mongodb_services }}"

View File

@ -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: mongodb, group: mongodb }
- 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: mongodb, group: mongodb }
# 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: mongodb, group: mongodb }
- 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: mongodb, group: mongodb }]
- "{{ 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: mongodb, group: mongodb }]
- "{{ container_envs.results }}"
- "{{ check_results.results }}"
- include: deploy.yml

View File

@ -1,26 +0,0 @@
---
- name: Starting mongodb container
kolla_docker:
action: "start_container"
common_options: "{{ docker_common_options }}"
image: "{{ mongodb_image_full }}"
name: "mongodb"
privileged: True
volumes:
- "{{ node_config_directory }}/mongodb/:{{ container_config_directory }}/:ro"
- "/etc/localtime:/etc/localtime:ro"
- "kolla_logs:/var/log/kolla/"
- "mongodb:/var/lib/mongodb"
- name: Waiting for the mongodb startup
wait_for: host={{ api_interface_address }} port={{ mongodb_port }}
- name: Checking current replication status
command: "docker exec -t mongodb mongo {{ api_interface_address }} --quiet --eval rs.status().ok"
register: mongodb_replication_status
changed_when: false
delegate_to: "{{ groups['mongodb'][0] }}"
run_once: True
- include: "bootstrap_cluster.yml"
when: mongodb_replication_status.stdout != "1"

View File

@ -1,4 +1,5 @@
---
- include: config.yml
- include: start.yml
- name: Flush handlers
meta: flush_handlers