---
- name: Ensuring config directories exist
  file:
    path: "{{ node_config_directory }}/{{ item.key }}"
    state: "directory"
    owner: "{{ config_owner_user }}"
    group: "{{ config_owner_group }}"
    mode: "0770"
    recurse: yes
  become: true
  when:
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ monasca_services }}"

- name: Copying over config.json files for services
  template:
    src: "{{ item.key }}/{{ item.key }}.json.j2"
    dest: "{{ node_config_directory }}/{{ item.key }}/config.json"
    mode: "0660"
  become: true
  register: monasca_config_jsons
  when:
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ monasca_services }}"
  notify:
    - "Restart {{ item.key }} container"

- name: Copying over monasca-api config
  vars:
    service: "{{ monasca_services['monasca-api'] }}"
  merge_configs:
    sources:
      - "{{ role_path }}/templates/monasca-api/{{ item }}.j2"
      - "{{ node_custom_config }}/monasca/{{ item }}"
      - "{{ node_custom_config }}/monasca/{{ inventory_hostname }}/{{ item }}"
    dest: "{{ node_config_directory }}/monasca-api/{{ item }}"
    mode: "0660"
  become: true
  register: monasca_api_confs
  with_items:
    - api-config.conf
    - api-config.ini
  when:
    - inventory_hostname in groups[service['group']]
    - service.enabled | bool
  notify:
    - Restart monasca-api container

- name: Copying over monasca-api wsgi config
  vars:
    service: "{{ monasca_services['monasca-api'] }}"
  template:
    src: "{{ role_path }}/templates/monasca-api/wsgi-api.conf.j2"
    dest: "{{ node_config_directory }}/monasca-api/wsgi-api.conf"
    mode: "0660"
  become: true
  register: monasca_api_wsgi_confs
  when:
    - inventory_hostname in groups[service['group']]
    - service.enabled | bool
  notify:
    - Restart monasca-api container

- name: Copying over monasca-log-api config
  vars:
    service: "{{ monasca_services['monasca-log-api'] }}"
  merge_configs:
    sources:
      - "{{ role_path }}/templates/monasca-log-api/{{ item }}.j2"
      - "{{ node_custom_config }}/monasca/{{ item }}"
      - "{{ node_custom_config }}/monasca/{{ inventory_hostname }}/{{ item }}"
    dest: "{{ node_config_directory }}/monasca-log-api/{{ item }}"
    mode: "0660"
  become: true
  register: monasca_log_api_confs
  with_items:
    - log-api.conf
    - log-api-paste.ini
  when:
    - inventory_hostname in groups[service['group']]
    - service.enabled | bool
  notify:
    - Restart monasca-log-api container

- name: Copying over monasca-log-api wsgi config
  vars:
    service: "{{ monasca_services['monasca-log-api'] }}"
  template:
    src: "{{ role_path }}/templates/monasca-log-api/wsgi-log-api.conf.j2"
    dest: "{{ node_config_directory }}/monasca-log-api/wsgi-log-api.conf"
    mode: "0660"
  become: true
  register: monasca_log_api_wsgi_confs
  when:
    - inventory_hostname in groups[service['group']]
    - service.enabled | bool
  notify:
    - Restart monasca-log-api container

- name: Copying over monasca-log-transformer config
  vars:
    service: "{{ monasca_services['monasca-log-transformer'] }}"
  template:
    src: "{{ item }}"
    dest: "{{ node_config_directory }}/monasca-log-transformer/log-transformer.conf"
    mode: "0660"
  become: true
  register: monasca_log_transformer_confs
  with_first_found:
    - "{{ node_custom_config }}/monasca/{{ inventory_hostname }}/log-transformer.conf"
    - "{{ node_custom_config }}/monasca/log-transformer.conf"
    - "{{ role_path }}/templates/monasca-log-transformer/log-transformer.conf.j2"
  when:
    - inventory_hostname in groups[service['group']]
    - service.enabled | bool
  notify:
    - Restart monasca-log-transformer container

- name: Copying over monasca-log-persister config
  vars:
    service: "{{ monasca_services['monasca-log-persister'] }}"
  template:
    src: "{{ item }}"
    dest: "{{ node_config_directory }}/monasca-log-persister/log-persister.conf"
    mode: "0660"
  become: true
  register: monasca_log_persister_confs
  with_first_found:
    - "{{ node_custom_config }}/monasca/{{ inventory_hostname }}/log-persister.conf"
    - "{{ node_custom_config }}/monasca/log-persister.conf"
    - "{{ role_path }}/templates/monasca-log-persister/log-persister.conf.j2"
  when:
    - inventory_hostname in groups[service['group']]
    - service.enabled | bool
  notify:
    - Restart monasca-log-persister container

- name: Copying over monasca-log-persister elasticsearch template
  vars:
    service: "{{ monasca_services['monasca-log-persister'] }}"
  template:
    src: "{{ item }}"
    dest: "{{ node_config_directory }}/monasca-log-persister/elasticsearch-template.json"
    mode: "0660"
  become: true
  register: monasca_log_persister_elasticsearch_template
  with_first_found:
    - "{{ node_custom_config }}/monasca/{{ inventory_hostname }}/elasticsearch-template.json"
    - "{{ node_custom_config }}/monasca/elasticsearch-template.json"
    - "{{ role_path }}/templates/monasca-log-persister/elasticsearch-template.json"
  when:
    - inventory_hostname in groups[service['group']]
    - service.enabled | bool
  notify:
    - Restart monasca-log-persister container


- name: Check monasca containers
  become: true
  kolla_docker:
    action: "compare_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ item.value.container_name }}"
    image: "{{ item.value.image }}"
    volumes: "{{ item.value.volumes }}"
    dimensions: "{{ item.value.dimensions }}"
  register: check_monasca_containers
  when:
    - kolla_action != "config"
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ monasca_services }}"
  notify:
    - "Restart {{ item.key }} container"
