---

### tags:
###   update_certs
###   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: "Creating iam user token"
  smardigo_user_token:
    secret:  "{{ iam_jwt_secret }}"
    user_id: "{{ connect_client_admin_username }}"
    realm: "{{ current_realm_name }}"
    client_id: "{{ connect_oidc_client_id }}"
  register: iam_user_token_result
  delegate_to: 127.0.0.1
  become: false
  tags:
    - update_deployment

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

- name: "Setup hcloud firewalls for <{{ inventory_hostname }}>"
  include_role:
    name: hetzner-ansible-hcloud
    tasks_from: configure-firewall2
  loop: "{{ server_hcloud_firewall_objects }}"
  loop_control:
    loop_var: firewall_object
  when:
    - server_hcloud_firewall_objects is defined

- name: "Setup DNS configuration for <{{ connect_external_subdomain }}> to <{{ stage_server_ip }}>"
  include_role:
    name: hetzner-ansible-dns
  vars:
    record_data: "{{ stage_server_ip }}"
    record_name: "{{ connect_external_subdomain }}"
  when:
    - connect_external_subdomain is defined

- name: "Setup DNS configuration for <{{ connect_id }}> to <{{ stage_server_ip }}>"
  include_role:
    name: hetzner-ansible-dns
  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: hetzner-ansible-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 }}"
  tags:
    - update_deployment

- name: "Deploy service templates for {{ connect_id }}"
  include_role:
    name: hetzner-ansible-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 }}"
  tags:
    - update_deployment

- name: "Deploy certificate templates for {{ connect_id }}"
  include_role:
    name: hetzner-ansible-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:
    - never
    - update_certs

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