---
- name: Verify that the console is running
  oc_obj:
    namespace: openshift-web-console
    kind: deployment
    state: list
    name: webconsole
  register: console_deployment
  until:
  - console_deployment.module_results is defined
  - console_deployment.module_results.results is defined
  - console_deployment.module_results.results[0] is defined
  - console_deployment.module_results.results[0].status is defined
  - console_deployment.module_results.results[0].status.readyReplicas is defined
  - console_deployment.module_results.results[0].status.readyReplicas > 0
  retries: 60
  delay: 10
  changed_when: false
  # Ignore errors so we can log troubleshooting info on failures.
  ignore_errors: yes

# Log the result of `oc status`, `oc get pods`, `oc get events`, and `oc logs deployment/webconsole` for troubleshooting failures.
- when: (console_deployment.module_results.results[0].status.readyReplicas is not defined) or (console_deployment.module_results.results[0].status.readyReplicas == 0)
  block:
  - name: Check status in the openshift-web-console namespace
    command: >
      {{ openshift_client_binary }} status --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift-web-console
    register: endpoint_status
    ignore_errors: true
  - debug:
      msg: "{{ endpoint_status.stdout_lines }}"
  - name: Get pods in the openshift-web-console namespace
    command: >
      {{ openshift_client_binary }} get pods --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift-web-console -o wide
    register: endpoint_pods
    ignore_errors: true
  - debug:
      msg: "{{ endpoint_pods.stdout_lines }}"
  - name: Get events in the openshift-web-console namespace
    command: >
      {{ openshift_client_binary }} get events --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift-web-console
    register: endpoint_events
    ignore_errors: true
  - debug:
      msg: "{{ endpoint_events.stdout_lines }}"
  - name: Get console pod logs
    command: >
      {{ openshift_client_binary }} logs deployment/webconsole --tail=50 --config={{ openshift.common.config_base }}/master/admin.kubeconfig -n openshift-web-console
    register: endpoint_log
    ignore_errors: true
  - debug:
      msg: "{{ endpoint_log.stdout_lines }}"

- when: (console_deployment.module_results.results[0].status.readyReplicas is not defined) or (console_deployment.module_results.results[0].status.readyReplicas == 0)
  block:
  - name: Report console errors
    fail:
      msg: Console install failed.
