---
- name: Ensuring config directories exist
  file:
    path: "{{ node_config_directory }}/{{ item }}"
    state: "directory"
    owner: "{{ config_owner_user }}"
    group: "{{ config_owner_group }}"
    mode: "0770"
  become: true
  with_items:
    - "kolla-toolbox"
    - "cron"
    - "cron/logrotate"

- name: Ensuring fluentd config directories exist
  file:
    path: "{{ node_config_directory }}/{{ item }}"
    state: "directory"
    recurse: yes
  become: true
  with_items:
    - "fluentd"
    - "fluentd/input"
    - "fluentd/output"
    - "fluentd/format"
    - "fluentd/filter"
  when: enable_fluentd | 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
  register: common_config_jsons
  when: item.value.enabled | bool
  with_dict: "{{ common_services }}"
  notify:
    - "Restart {{ item.key }} container"

- name: Copying over fluentd input config files
  template:
    src: "conf/input/{{ item }}.conf.j2"
    dest: "{{ node_config_directory }}/fluentd/input/{{ item }}.conf"
    mode: "0660"
  become: true
  register: fluentd_input
  when: enable_fluentd | bool
  with_items:
    - "00-global"
    - "01-syslog"
    - "02-mariadb"
    - "03-rabbitmq"
    - "04-openstack-wsgi"
    - "05-libvirt"
    - "06-zookeeper"
    - "07-kafka"
  notify:
    - Restart fluentd container

- name: Copying over fluentd output config files
  template:
    src: "conf/output/{{ item.name }}.conf.j2"
    dest: "{{ node_config_directory }}/fluentd/output/{{ item.name }}.conf"
    mode: "0660"
  become: true
  register: fluentd_output
  when:
    - enable_fluentd | bool
    - item.enabled | bool
  with_items:
    - name: "00-local"
      enabled: true
    - name: "01-es"
      enabled: "{{ enable_elasticsearch | bool or
                ( elasticsearch_address != kolla_internal_vip_address ) }}"
  notify:
    - Restart fluentd container

- name: Find custom fluentd output config files
  local_action:
    module: find
    path: "{{ node_custom_config }}/fluentd/output"
    pattern: "*.conf"
  run_once: True
  register: find_custom_fluentd_outputs
  when:
    - enable_fluentd | bool

- name: Copying over custom fluentd output config files
  template:
    src: "{{ item.path }}"
    dest: "{{ node_config_directory }}/fluentd/output/{{ item.path | basename }}"
    mode: "0660"
  become: true
  register: fluentd_output_custom
  when:
    - enable_fluentd | bool
  with_items: "{{ find_custom_fluentd_outputs.files }}"
  notify:
    - Restart fluentd container

- name: Copying over fluentd format config files
  template:
    src: "conf/format/{{ item }}.conf.j2"
    dest: "{{ node_config_directory }}/fluentd/format/{{ item }}.conf"
    mode: "0660"
  become: true
  register: fluentd_format
  with_items:
    - "apache_access"
    - "wsgi_access"
  when: enable_fluentd | bool
  notify:
    - Restart fluentd container

- name: Copying over fluentd filter config files
  template:
    src: "conf/filter/{{ item.src }}.conf.j2"
    dest: "{{ node_config_directory }}/fluentd/filter/{{ item.dest }}.conf"
    mode: "0660"
  become: true
  register: fluentd_filter
  with_items:
    - src: 00-record_transformer
      dest: 00-record_transformer
    - src: "{{ '01-rewrite-0.14' if kolla_base_distro in ['debian', 'ubuntu'] else '01-rewrite-0.12' }}"
      dest: 01-rewrite
  when: enable_fluentd | bool
  notify:
    - Restart fluentd container

- name: Find custom fluentd filter config files
  local_action:
    module: find
    path: "{{ node_custom_config }}/fluentd/filter"
    pattern: "*.conf"
  run_once: True
  register: find_custom_fluentd_filters
  when: enable_fluentd | bool

- name: Copying over custom fluentd filter config files
  template:
    src: "{{ item.path }}"
    dest: "{{ node_config_directory }}/fluentd/filter/{{ item.path | basename }}"
    mode: "0660"
  become: true
  register: fluentd_filter_custom
  with_items: "{{ find_custom_fluentd_filters.files }}"
  when: enable_fluentd | bool
  notify:
    - Restart fluentd container

- name: Copying over td-agent.conf
  template:
    src: "td-agent.conf.j2"
    dest: "{{ node_config_directory }}/{{ item }}/td-agent.conf"
    mode: "0660"
  become: true
  register: fluentd_td_agent
  with_items:
    - "fluentd"
  when: enable_fluentd | bool
  notify:
    - Restart fluentd container

- name: Copying over cron logrotate config files
  template:
    src: "cron-logrotate-{{ item.name }}.conf.j2"
    dest: "{{ node_config_directory }}/cron/logrotate/{{ item.name }}.conf"
    mode: "0660"
  become: true
  register: cron_confs
  when: item.enabled | bool
  with_items:
    - { name: "ansible", enabled: "yes" }
    - { name: "aodh", enabled: "{{ enable_aodh }}" }
    - { name: "barbican", enabled: "{{ enable_barbican }}" }
    - { name: "blazar", enabled: "{{ enable_blazar }}" }
    - { name: "ceilometer", enabled: "{{ enable_ceilometer }}" }
    - { name: "ceph", enabled: "{{ enable_ceph }}" }
    - { name: "chrony", enabled: "{{ enable_chrony }}" }
    - { name: "cinder", enabled: "{{ enable_cinder }}" }
    - { name: "cloudkitty", enabled: "{{ enable_cloudkitty }}" }
    - { name: "collectd", enabled: "{{ enable_collectd }}" }
    - { name: "congress", enabled: "{{ enable_congress }}" }
    - { name: "designate", enabled: "{{ enable_designate }}" }
    - { name: "elasticsearch", enabled: "{{ enable_elasticsearch }}" }
    - { name: "etcd", enabled: "{{ enable_etcd }}" }
    - { name: "freezer", enabled: "{{ enable_freezer }}" }
    - { name: "glance", enabled: "{{ enable_glance }}" }
    - { name: "global", enabled: "yes" }
    - { name: "gnocchi", enabled: "{{ enable_gnocchi }}" }
    - { name: "grafana", enabled: "{{ enable_grafana }}" }
    - { name: "haproxy", enabled: "{{ enable_haproxy }}" }
    - { name: "heat", enabled: "{{ enable_heat }}" }
    - { name: "influxdb", enabled: "{{ enable_influxdb }}" }
    - { name: "ironic", enabled: "{{ enable_ironic }}" }
    - { name: "ironic-inspector", enabled: "{{ enable_ironic }}" }
    - { name: "iscsid", enabled: "{{ enable_iscsid }}" }
    - { name: "kafka", enabled: "{{ enable_kafka }}" }
    - { name: "karbor", enabled: "{{ enable_karbor }}" }
    - { name: "keepalived", enabled: "{{ enable_haproxy }}" }
    - { name: "keystone", enabled: "{{ enable_keystone }}" }
    - { name: "kibana", enabled: "{{ enable_kibana }}" }
    - { name: "kuryr", enabled: "{{ enable_kuryr }}" }
    - { name: "magnum", enabled: "{{ enable_magnum }}" }
    - { name: "manila", enabled: "{{ enable_manila }}" }
    - { name: "mariadb", enabled: "{{ enable_mariadb }}" }
    - { name: "mistral", enabled: "{{ enable_mistral }}" }
    - { name: "monasca", enabled: "{{ enable_monasca }}" }
    - { name: "mongodb", enabled: "{{ enable_mongodb }}" }
    - { name: "murano", enabled: "{{ enable_murano }}" }
    - { name: "neutron", enabled: "{{ enable_neutron }}" }
    - { name: "nova", enabled: "{{ enable_nova }}" }
    - { name: "octavia", enabled: "{{ enable_octavia }}" }
    - { name: "outward-rabbitmq", enabled: "{{ enable_outward_rabbitmq }}" }
    - { name: "panko", enabled: "{{ enable_panko }}" }
    - { name: "rabbitmq", enabled: "{{ enable_rabbitmq }}" }
    - { name: "rally", enabled: "{{ enable_rally }}" }
    - { name: "sahara", enabled: "{{ enable_sahara }}" }
    - { name: "searchlight", enabled: "{{ enable_searchlight }}" }
    - { name: "senlin", enabled: "{{ enable_senlin }}" }
    - { name: "skydive", enabled: "{{ enable_skydive }}" }
    - { name: "solum", enabled: "{{ enable_solum }}" }
    - { name: "swift", enabled: "{{ enable_swift }}" }
    - { name: "tacker", enabled: "{{ enable_tacker }}" }
    - { name: "tempest", enabled: "{{ enable_tempest }}" }
    - { name: "trove", enabled: "{{ enable_trove }}" }
    - { name: "vitrage", enabled: "{{ enable_vitrage }}" }
    - { name: "watcher", enabled: "{{ enable_watcher }}" }
    - { name: "zookeeper", enabled: "{{ enable_zookeeper }}" }
    - { name: "zun", enabled: "{{ enable_zun }}" }
  notify:
    - Restart cron container

- name: Ensuring config directories have correct owner and permission
  become: true
  file:
    path: "{{ node_config_directory }}/{{ item.key }}"
    owner: "{{ config_owner_user }}"
    group: "{{ config_owner_group }}"
    mode: "0770"
  ignore_errors: "{{ ansible_check_mode }}"
  when:
    - item.value.enabled | bool
    - item.key != "kolla-toolbox"
  with_dict: "{{ common_services }}"

- name: Check common 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 }}"
    privileged: "{{ item.value.privileged | default(False) }}"
    environment: "{{ item.value.environment }}"
  register: check_common_containers
  when:
    - kolla_action != "config"
    - item.value.enabled | bool
  with_dict: "{{ common_services }}"
  notify:
    - "Restart {{ item.key }} container"
