---

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

- name: "Setup DNS configuration for {{ kibana_id }}"
  include_role:
    name: sma_digitalocean
    tasks_from: domain
  vars:
    record_data: "{{ stage_server_ip }}"
    record_name: "{{ kibana_id }}"

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

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

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

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

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

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

- name: "Start {{ kibana_id }}"
  community.docker.docker_compose:
    project_src: '{{ service_base_path }}/{{ kibana_id }}'
    state: present
    pull: true
  tags:
    - update_config
    - update_deployment

- name: "Create ES objects for technical users"
  include_tasks: add_technical_users.yml
  loop: '{{ kibana_technical_users }}'
  loop_control:
    loop_var: technical_user
  args:
    apply:
      tags:
        - kibana-technical-users
  tags:
    - kibana-technical-users
