---

### tags:
###   update_deployment

- name: "Creating smardigo user token"
  smardigo_user_token:
    secret:  "{{ connect_jwt_secret }}"
    user_id: "{{ connect_client_admin_username }}"
  register: smardigo_user_token_result
  delegate_to: 127.0.0.1
  become: false
  tags:
    - update_deployment

- name: "Setting smardigo_auth_token_value as fact"
  set_fact:
    smardigo_auth_token_value: "{{ smardigo_user_token_result.token }}"
  tags:
    - update_deployment

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

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

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

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

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

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