---

### tags:
###   update_certs
###   update_deployment
###   update_connections
###   update_configuration

- name: "Setup DNS configuration for <{{ connect_id }}> to <{{ stage_server_ip }}>"
  include_role:
    name: sma_digitalocean
    tasks_from: domain
  vars:
    record_data: "{{ stage_server_ip }}"
    record_name: "{{ connect_id }}"

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

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

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

- name: "Deploy service templates for {{ connect_id }}"
  include_role:
    name: sma_deploy
    tasks_from: templates
  vars:
    current_config: "connect"
    current_base_path: "{{ service_base_path }}"
    current_destination: "{{ connect_id }}"
    current_owner: "{{ docker_owner }}"
    current_group: "{{ docker_group }}"

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

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

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