---

### params:
###   current_service: gitea
###   current_server_ip : 1.2.3.4
###   current_dns_entry : dev-gitea-01
###   current_dns_entries : ['dev-gitea']
###   service_base_path : /etc/smardigo/
###   current_service_id : dev-gitea-01-gitea
###   current_service_docker : <docker config object>
###   docker_owner : root
###   docker_group : root

### tags:
###   update_deployment

- name: "Updating DNS for <{{ current_dns_entry }}> to <{{ current_server_ip }}>"
  include_role:
    name: sma_digitalocean
    tasks_from: domain
  vars:
    record_data: "{{ current_server_ip }}"
    record_name: "{{ current_dns_entry }}"

- name: "Updating public DNS for <{{ current_host }}>"
  include_role:
    name: sma_digitalocean
    tasks_from: domain
  vars:
    record_data: "{{ item.ip }}"
    record_name: "{{ item.name }}"
  loop: "{{ current_dns_entries }}"

- name: "Checking if <{{ current_service_id }}/docker-compose.yml> exists"
  stat:
    path: '{{ service_base_path }}/{{ current_service_id }}/docker-compose.yml'
  register: check_docker_compose_file
  tags:
    - update_deployment

- name: "Stopping <{{ current_service_id }}>"
  community.docker.docker_compose:
    project_src: '{{ service_base_path }}/{{ current_service_id }}'
    state: absent
  when: check_docker_compose_file.stat.exists
  tags:
    - update_deployment

- name: "Deploying docker templates for <{{ current_service_id }}>"
  include_role:
    name: sma_deploy
    tasks_from: templates
  vars:
    current_config: "_docker"
    current_base_path: "{{ service_base_path }}"
    current_destination: "{{ current_service_id }}"
    current_owner: "{{ docker_owner }}"
    current_group: "{{ docker_group }}"
    current_docker: "{{ current_service_docker }}"

- name: "Deploying service templates for <{{ current_service_id }}>"
  include_role:
    name: sma_deploy
    tasks_from: templates
  vars:
    current_config: "{{ current_service }}"
    current_base_path: "{{ service_base_path }}"
    current_destination: "{{ current_service_id }}"
    current_owner: "{{ docker_owner }}"
    current_group: "{{ docker_group }}"

- name: "Starting <{{ current_service_id }}>"
  community.docker.docker_compose:
    project_src: '{{ service_base_path }}/{{ current_service_id }}'
    state: present
    pull: true
  tags:
    - update_deployment
