---

### tags:
###   update_certs
###   update_config
###   update_deployment

- name: "Check if {{ elastic_id }}/docker-compose.yml exists"
  stat:
    path: '{{ service_base_path }}/{{ elastic_id }}/docker-compose.yml'
  register: check_docker_compose_file
  tags:
    - update_config
    - update_deployment

- name: "Stop {{ elastic_id }}"
  community.docker.docker_compose:
    project_src: '{{ service_base_path }}/{{ elastic_id }}'
    state: absent
  when: check_docker_compose_file.stat.exists
  tags:
    - update_config
    - update_deployment

- name: "Deploy docker templates for {{ elastic_id }}"
  include_role:
    name: sma_deploy
    tasks_from: templates
  vars:
    current_config: "_docker"
    current_base_path: "{{ service_base_path }}"
    current_destination: "{{ elastic_id }}"
    current_owner: "{{ docker_owner }}"
    current_group: "{{ docker_group }}"
    current_docker: "{{ elastic_docker }}"
  tags:
    - update_config

- name: "Deploy service templates for {{ elastic_id }}"
  include_role:
    name: sma_deploy
    tasks_from: templates
  vars:
    current_config: "elastic"
    current_base_path: "{{ service_base_path }}"
    current_destination: "{{ elastic_id }}"
    current_owner: "{{ docker_owner }}"
    current_group: "{{ docker_group }}"
  tags:
    - update_config

- name: "Deploy certificate templates for {{ elastic_id }}"
  include_role:
    name: sma_deploy
    tasks_from: templates
  vars:
    current_config: "elastic-certs/{{ stage }}-certs"
    current_base_path: "{{ service_base_path }}"
    current_destination: "{{ elastic_id }}/certs"
    current_owner: "{{ docker_owner }}"
    current_group: "{{ docker_group }}"
    cleanup_destination: "true"
  tags:
    - update_certs
    - update_config

- name: "Restart {{ elastic_id }}"
  community.docker.docker_compose:
    project_src: '{{ service_base_path }}/{{ elastic_id }}'
    restarted: yes
    build: no
  tags:
    - never
    - update_certs

- name: "Update {{ elastic_id }}"
  community.docker.docker_compose:
    project_src: '{{ service_base_path }}/{{ elastic_id }}'
    state: present
    pull: yes
  tags:
    - update_config
    - update_deployment

- name: "Waiting for running elastic instance"
  become: false
  ansible.builtin.wait_for:
    timeout: 180
    port: "{{ service_port_elasticsearch }}"
    host: localhost

- name: "Check if elastic cluster API reachable"
  become: false
  uri:
    url: "https://{{ elastic_api_endpoint }}/_cluster/health?pretty"
    method: GET
    status_code: [200]
    user: "{{ elastic_admin_username }}"
    password: "{{ elastic_admin_password }}"
    force_basic_auth: yes
    ca_path: '{{ service_base_path }}/{{ elastic_id }}/certs/ca/ca.crt'
  register: check_elastic_api_reachable
  delay: 5
  retries: 15
  until: check_elastic_api_reachable.status in [200]

- name: "Configure | create default ilm templates"
  include_tasks: create_ilm_tmpl.yml
  loop: '{{ elastic_default_ilm_templates }}'
  loop_control:
    loop_var: ilm_tmpl
  tags:
    - elastic-ilmtmpls
  when:
    - inventory_hostname == groups['elastic'][0]

- name: "Configure | create default index templates"
  include_tasks: create_index_tmpl.yml
  loop: '{{ elastic_default_index_templates }}'
  loop_control:
    loop_var: idx_tmpl
  tags:
    - elastic-idxtmpls
  when:
    - inventory_hostname == groups['elastic'][0]
