---
- name: Create heketi DB volume
  command: "{{ glusterfs_heketi_client }} setup-openshift-heketi-storage --image {{ glusterfs_heketi_image }} --listfile /tmp/heketi-storage.json"
  register: setup_storage

- name: Copy heketi-storage list
  shell: "{{ openshift_client_binary }} --config={{ mktemp.stdout }}/admin.kubeconfig rsh --namespace={{ glusterfs_namespace }} {{ deploy_heketi_pod.results.results[0]['items'][0]['metadata']['name'] }} cat /tmp/heketi-storage.json > {{ mktemp.stdout }}/heketi-storage.json"

# Need `command` here because heketi-storage.json contains multiple objects.
- name: Copy heketi DB to GlusterFS volume
  command: "{{ openshift_client_binary }} --config={{ mktemp.stdout }}/admin.kubeconfig create -f {{ mktemp.stdout }}/heketi-storage.json -n {{ glusterfs_namespace }}"
  when: setup_storage.rc == 0

- name: Wait for copy job to finish
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    kind: job
    state: list
    name: "heketi-storage-copy-job"
  register: heketi_job
  until:
  - "'results' in heketi_job.results and heketi_job.results.results | count > 0"
  # Pod's 'Complete' status must be True
  - "heketi_job.results.results | lib_utils_oo_collect(attribute='status.conditions') | lib_utils_oo_collect(attribute='status', filters={'type': 'Complete'}) | map('bool') | select | list | count == 1"
  delay: 10
  retries: "{{ (glusterfs_timeout | int / 10) | int }}"
  failed_when:
  - "'results' in heketi_job.results"
  - "heketi_job.results.results | count > 0"
  # Fail when pod's 'Failed' status is True
  - "heketi_job.results.results | lib_utils_oo_collect(attribute='status.conditions') | lib_utils_oo_collect(attribute='status', filters={'type': 'Failed'}) | map('bool') | select | list | count == 1"
  when: setup_storage.rc == 0

- name: Delete deploy resources
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    kind: "{{ item.kind }}"
    name: "{{ item.name | default(omit) }}"
    selector: "{{ item.selector | default(omit) }}"
    state: absent
  with_items:
  - kind: "template,route,service,jobs,dc,secret"
    selector: "deploy-heketi"
  - kind: "svc"
    name: "heketi-storage-endpoints"
  - kind: "secret"
    name: "heketi-{{ glusterfs_name | default }}-topology-secret"

- name: Generate heketi endpoints
  template:
    src: "heketi-endpoints.yml.j2"
    dest: "{{ mktemp.stdout }}/heketi-endpoints.yml"

- name: Generate heketi service
  template:
    src: "heketi-service.yml.j2"
    dest: "{{ mktemp.stdout }}/heketi-service.yml"

- name: Create heketi endpoints
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    state: present
    kind: endpoints
    name: "heketi-db-{{ glusterfs_name }}-endpoints"
    files:
    - "{{ mktemp.stdout }}/heketi-endpoints.yml"

- name: Create heketi service
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    state: present
    kind: service
    name: "heketi-db-{{ glusterfs_name }}-endpoints"
    files:
    - "{{ mktemp.stdout }}/heketi-service.yml"

- name: Copy heketi template
  copy:
    src: "heketi-template.yml"
    dest: "{{ mktemp.stdout }}/heketi-template.yml"

- name: Create heketi template
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    kind: template
    name: heketi
    state: present
    files:
    - "{{ mktemp.stdout }}/heketi-template.yml"

- name: Deploy heketi pod
  oc_process:
    namespace: "{{ glusterfs_namespace }}"
    template_name: "heketi"
    create: True
    params:
      IMAGE_NAME: "{{ glusterfs_heketi_image }}"
      HEKETI_ROUTE: "{{ glusterfs_heketi_url | default(['heketi-',glusterfs_name]|join) }}"
      HEKETI_USER_KEY: "{{ glusterfs_heketi_user_key }}"
      HEKETI_ADMIN_KEY: "{{ glusterfs_heketi_admin_key }}"
      HEKETI_EXECUTOR: "{{ glusterfs_heketi_executor }}"
      HEKETI_FSTAB: "{{ glusterfs_heketi_fstab }}"
      CLUSTER_NAME: "{{ glusterfs_name }}"

- name: Wait for heketi pod
  oc_obj:
    namespace: "{{ glusterfs_namespace }}"
    kind: pod
    state: list
    selector: "glusterfs=heketi-{{ glusterfs_name }}-pod"
  register: heketi_pod
  until:
  - "heketi_pod.results.results[0]['items'] | count > 0"
  # Pod's 'Ready' status must be True
  - "heketi_pod.results.results[0]['items'] | lib_utils_oo_collect(attribute='status.conditions') | lib_utils_oo_collect(attribute='status', filters={'type': 'Ready'}) | map('bool') | select | list | count == 1"
  delay: 10
  retries: "{{ (glusterfs_timeout | int / 10) | int }}"

- name: Set heketi-cli command
  set_fact:
    glusterfs_heketi_client: "{{ openshift_client_binary }} --config={{ mktemp.stdout }}/admin.kubeconfig rsh --namespace={{ glusterfs_namespace }} {{ heketi_pod.results.results[0]['items'][0]['metadata']['name'] }} {{ glusterfs_heketi_cli }} -s http://localhost:8080 --user admin --secret '{{ glusterfs_heketi_admin_key }}'"

- name: Verify heketi service
  command: "{{ glusterfs_heketi_client }} cluster list"
  changed_when: False
