---

### tags:
###   configure_realm

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

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

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

- name: "Deploy docker templates for {{ keycloak_id }}"
  include_role:
    name: hetzner-ansible-sma-deploy
    tasks_from: templates
  vars:
    current_config: "keycloak-compact"
    current_base_path: "{{ service_base_path }}"
    current_destination: "{{ keycloak_id }}"
    current_owner: "{{ docker_owner }}"
    current_group: "{{ docker_group }}"

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

- name: "Start {{ keycloak_id }}"
  community.docker.docker_compose:
    project_src: '{{ service_base_path }}/{{ keycloak_id }}'
    state: present

- name: "Setting local keycloak url"
  set_fact:  
    shared_service_url_keycloak: "http://localhost:{{ service_port_keycloak_external }}"

- name: "Wait for <localhost:{{ service_port_keycloak_external }}>"
  wait_for:
    host: "localhost"
    port: '{{ service_port_keycloak_external }}'
    delay: 30