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

- name: Check if the folder for custom meter definitions exist
  stat:
    path: "{{ node_custom_config }}/ceilometer/{{ ceilometer_custom_meters_local_folder }}"
  delegate_to: localhost
  register: ceilometer_custom_meters_folder

- name: Set variable that indicates if we have a folder for custom meter YAML files
  set_fact:
    custom_meter_folder_exist: "{{ ceilometer_custom_meters_folder.stat.exists and ceilometer_custom_meters_folder.stat.isdir }}"

- name: Find all *.yaml files in custom meter definitions folder (if the folder exist)
  find:
    paths: "{{ node_custom_config }}/ceilometer/{{ ceilometer_custom_meters_local_folder }}"
    patterns: "*.yaml"
  delegate_to: localhost
  register: ceilometer_custom_meters_folder_found_files
  when: custom_meter_folder_exist

- name: Set the variable that control the copy of custom meter definitions
  set_fact:
    should_copy_custom_meter_definitions: "{{ custom_meter_folder_exist and ceilometer_custom_meters_folder_found_files.matched > 0 }}"

- name: Create default folder for custom meter definitions
  file:
    path: "{{ node_config_directory }}/{{ item.key }}/meters.d"
    state: "directory"
    owner: "{{ config_owner_user }}"
    group: "{{ config_owner_group }}"
    mode: "0770"
  become: true
  with_dict: "{{ ceilometer_services }}"
  when:
    - should_copy_custom_meter_definitions
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool

- name: Copying custom meter definitions to Ceilometer
  copy:
    src: "{{ node_custom_config }}/ceilometer/{{ ceilometer_custom_meters_local_folder }}/"
    dest: "{{ node_config_directory }}/{{ item.key }}/meters.d"
    force: True
    mode: "0660"
  become: true
  when:
    - should_copy_custom_meter_definitions
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ ceilometer_services }}"
  notify:
    - "Restart {{ item.key }} container"

- name: Check if the folder ["{{ node_custom_config }}/ceilometer/{{ ceilometer_dynamic_pollsters_local_folder }}"] for dynamic pollsters definitions exist
  stat:
    path: "{{ node_custom_config }}/ceilometer/{{ ceilometer_dynamic_pollsters_local_folder }}"
  delegate_to: localhost
  register: ceilometer_dynamic_pollsters_folder
  run_once: True

- name: Set the variable that control the copy of dynamic pollsters definitions
  set_fact:
    should_copy_dynamic_pollster_definitions: "{{ ceilometer_dynamic_pollsters_folder.stat.exists and ceilometer_dynamic_pollsters_folder.stat.isdir }}"

- name: Clean default folder for dynamic pollsters definitions
  file:
    state: absent
    path: "{{ node_config_directory }}/ceilometer-central/pollsters.d/"

- name: Create default folder for dynamic pollsters definitions
  file:
    path: "{{ node_config_directory }}/ceilometer-central/pollsters.d"
    state: "directory"
    owner: "{{ config_owner_user }}"
    group: "{{ config_owner_group }}"
    mode: "0770"
  become: true
  when:
    - should_copy_dynamic_pollster_definitions
    - inventory_hostname in groups['ceilometer-central']

- name: Copying dynamic pollsters definitions
  copy:
    src: "{{ node_custom_config }}/ceilometer/{{ ceilometer_dynamic_pollsters_local_folder }}/"
    dest: "{{ node_config_directory }}/ceilometer-central/pollsters.d/"
    mode: "0660"
  become: true
  when:
    - should_copy_dynamic_pollster_definitions
    - inventory_hostname in groups['ceilometer-central']
  notify:
    - "Restart ceilometer-central container"

- name: Check if custom polling.yaml exists
  stat:
    path: "{{ node_custom_config }}/ceilometer/polling.yaml"
  delegate_to: localhost
  register: ceilometer_polling_file

- name: Copying over polling.yaml
  copy:
    src: "{{ node_custom_config }}/ceilometer/polling.yaml"
    dest: "{{ node_config_directory }}/{{ item.key }}/polling.yaml"
    force: True
    mode: "0660"
  become: true
  when:
    - ceilometer_polling_file.stat.exists
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ ceilometer_services }}"
  notify:
    - "Restart {{ item.key }} container"

- name: Set ceilometer polling file's path
  set_fact:
    ceilometer_polling_file_path: "{{ ceilometer_polling_file.stat.path }}"
  when:
    - ceilometer_polling_file.stat.exists

- name: Check custom gnocchi_resources.yaml exists
  stat:
    path: "{{ node_custom_config }}/ceilometer/gnocchi_resources.yaml"
  delegate_to: localhost
  register: ceilometer_gnocchi_resources_file

- name: Copying over gnocchi_resources.yaml
  copy:
    src: "{{ node_custom_config }}/ceilometer/gnocchi_resources.yaml"
    dest: "{{ node_config_directory }}/{{ item.key }}/gnocchi_resources.yaml"
    force: True
    mode: "0660"
  become: true
  when:
    - ceilometer_gnocchi_resources_file.stat.exists
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ ceilometer_services }}"
  notify:
    - "Restart {{ item.key }} container"

- name: Set ceilometer gnocchi_resources file's path
  set_fact:
    ceilometer_gnocchi_resources_file_path: "{{ ceilometer_gnocchi_resources_file.stat.path }}"
  when:
    - ceilometer_gnocchi_resources_file.stat.exists

- name: Check if policies shall be overwritten
  stat:
    path: "{{ item }}"
  delegate_to: localhost
  run_once: True
  register: ceilometer_policy
  with_first_found:
    - files: "{{ supported_policy_format_list }}"
      paths:
        - "{{ node_custom_config }}/ceilometer/"
      skip: true

- name: Set ceilometer policy file
  set_fact:
    ceilometer_policy_file: "{{ ceilometer_policy.results.0.stat.path | basename }}"
    ceilometer_policy_file_path: "{{ ceilometer_policy.results.0.stat.path }}"
  when:
    - ceilometer_policy.results

- include_tasks: copy-certs.yml
  when:
    - kolla_copy_ca_into_containers | bool

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

- name: Copying over ceilometer.conf
  vars:
    service_name: "{{ item.key }}"
  merge_configs:
    sources:
      - "{{ role_path }}/templates/ceilometer.conf.j2"
      - "{{ node_custom_config }}/global.conf"
      - "{{ node_custom_config }}/ceilometer.conf"
      - "{{ node_custom_config }}/ceilometer/{{ item.key }}.conf"
      - "{{ node_custom_config }}/ceilometer/{{ inventory_hostname }}/ceilometer.conf"
    dest: "{{ node_config_directory }}/{{ item.key }}/ceilometer.conf"
    mode: "0660"
  become: true
  when:
    - item.value.enabled | bool
    - inventory_hostname in groups[item.value.group]
  with_dict: "{{ ceilometer_services }}"
  notify:
    - "Restart {{ item.key }} container"

- name: Check custom event_definitions.yaml exists
  stat:
    path: "{{ node_custom_config }}/ceilometer/event_definitions.yaml"
  delegate_to: localhost
  register: ceilometer_event_definitions_file

- name: Copying over event_definitions.yaml
  vars:
    service: "{{ ceilometer_services['ceilometer-notification'] }}"
  copy:
    src: "{{ node_custom_config }}/ceilometer/event_definitions.yaml"
    dest: "{{ node_config_directory }}/ceilometer-notification/event_definitions.yaml"
    force: True
    mode: "0660"
  become: true
  register: ceilometer_event_definitions_overwriting
  when:
    - ceilometer_event_definitions_file.stat.exists
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
  notify:
    - Restart ceilometer-notification container

- name: Copying over event_definitions.yaml for notification service
  vars:
    service: "{{ ceilometer_services['ceilometer-notification'] }}"
  template:
    src: "event_definitions.yaml.j2"
    dest: "{{ node_config_directory }}/ceilometer-notification/event_definitions.yaml"
    mode: "0660"
  become: true
  register: ceilometer_event_definitions
  when:
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
    - not ceilometer_event_definitions_file.stat.exists
  notify:
    - Restart ceilometer-notification container

- name: Check custom event_pipeline.yaml exists
  stat:
    path: "{{ node_custom_config }}/ceilometer/event_pipeline.yaml"
  delegate_to: localhost
  register: ceilometer_event_pipeline_file

- name: Copying over event_pipeline.yaml
  vars:
    service: "{{ ceilometer_services['ceilometer-notification'] }}"
  copy:
    src: "{{ node_custom_config }}/ceilometer/event_pipeline.yaml"
    dest: "{{ node_config_directory }}/ceilometer-notification/event_pipeline.yaml"
    force: True
    mode: "0660"
  become: true
  register: ceilometer_event_pipeline_overwriting
  when:
    - ceilometer_event_pipeline_file.stat.exists
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
  notify:
    - Restart ceilometer-notification container

- name: Copying over event_pipeline.yaml for notification service
  vars:
    service: "{{ ceilometer_services['ceilometer-notification'] }}"
  template:
    src: "event_pipeline.yaml.j2"
    dest: "{{ node_config_directory }}/ceilometer-notification/event_pipeline.yaml"
    mode: "0660"
  become: true
  when:
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
    - not ceilometer_event_pipeline_file.stat.exists
  notify:
    - Restart ceilometer-notification container

- name: Check custom pipeline.yaml exists
  stat:
    path: "{{ node_custom_config }}/ceilometer/pipeline.yaml"
  delegate_to: localhost
  register: ceilometer_pipeline_file

- name: Copying over pipeline.yaml
  vars:
    services_require_pipeline:
      - ceilometer-compute
      - ceilometer-central
      - ceilometer-notification
  copy:
    src: "{{ node_custom_config }}/ceilometer/pipeline.yaml"
    dest: "{{ node_config_directory }}/{{ item.key }}/pipeline.yaml"
    force: True
    mode: "0660"
  become: true
  register: ceilometer_pipeline_overwriting
  when:
    - ceilometer_pipeline_file.stat.exists
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
    - item.key in services_require_pipeline
  with_dict: "{{ ceilometer_services }}"
  notify:
    - "Restart {{ item.key }} container"

- name: Copying over pipeline.yaml file
  vars:
    services_require_pipeline:
      - ceilometer-compute
      - ceilometer-central
      - ceilometer-notification
  template:
    src: "pipeline.yaml.j2"
    dest: "{{ node_config_directory }}/{{ item.key }}/pipeline.yaml"
    mode: "0660"
  become: true
  when:
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
    - item.key in services_require_pipeline
    - not ceilometer_pipeline_file.stat.exists
  with_dict: "{{ ceilometer_services }}"
  notify:
    - "Restart {{ item.key }} container"

- name: Copying VMware vCenter CA file
  become: true
  vars:
    service: "{{ ceilometer_services['ceilometer-compute'] }}"
  copy:
    src: "{{ node_custom_config }}/vmware_ca"
    dest: "{{ node_config_directory }}/ceilometer-compute/vmware_ca"
    mode: "0660"
  when:
    - nova_compute_virt_type == "vmware"
    - not vmware_vcenter_insecure | bool
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
  notify:
    - Restart ceilometer-compute container

- name: Copying over existing policy file
  template:
    src: "{{ ceilometer_policy_file_path }}"
    dest: "{{ node_config_directory }}/{{ item.key }}/{{ ceilometer_policy_file }}"
    mode: "0660"
  become: true
  when:
    - ceilometer_policy_file is defined
    - inventory_hostname in groups[item.value.group]
    - item.value.enabled | bool
  with_dict: "{{ ceilometer_services }}"
  notify:
    - "Restart {{ item.key }} container"
