- hosts: all
  gather_facts: false
  tags:
    - openshift

  environment:
    VERSION: 2.7
    IMAGE_FULL_NAME: "{{ image_full_name }}"
    IMAGE_REGISTRY_URL: "{{ image_registry_url }}"
    IMAGE_NAMESPACE: "{{ image_namespace }}"
    IMAGE_NAME: "{{ image_name }}"
    IMAGE_TAG: "{{ image_tag }}"
    OPENSHIFT_CLUSTER_URL: "{{ openshift_cluster_url }}"
    OPENSHIFT_AUTH_TOKEN: "{{ openshift_auth_token }}"
    OPENSHIFT_USERNAME: "{{ openshift_username }}"
    OPENSHIFT_PROJECT_NAME: "{{ openshift_project_name }}"

  tasks:
    - name: "Run container and perform simple checks"
      block:
        - name: Log into the OpenShift cluster
          shell: oc login {{ openshift_cluster_url }} --token="{{ openshift_auth_token }}" --insecure-skip-tls-verify
        - name: Select the project {{ openshift_project_name }}
          shell: oc project {{ openshift_project_name }}
        - name: Import the image into OpenShift
          shell: oc import-image {{ image_name }} --from={{ image_full_name }} --insecure=true --confirm
        - name: Get imported image registry URL
          shell: oc get is {{ image_name }} --output=jsonpath='{ .status.dockerImageRepository }'
          register: imported_image_url
        - name: Start a dummy {{ image_namespace }}/{{ image_name }}:{{ image_tag }} container
          shell: oc run java-cmd-test -i --image={{ imported_image_url.stdout }} --command -- bash
        - name: Figure out a pod name for the started container
          shell: oc get pods --selector deploymentconfig=java-cmd-test --output=jsonpath='{ .items[*].metadata.name }'
          register: pod_name
        - name: Make sure 'Java' command is available in the container
          shell: oc exec {{ pod_name.stdout }} -- java -version
          register: java_cmd_run
        - debug:
            msg: "{{ java_cmd_run.stdout }}"
        - name: Make sure Red Hat OpenShift Dev Spaces binaries are present
          shell: oc exec {{ pod_name.stdout }} -- ls -la /home/user/devspaces/tomcat
          register: ls_cmd_run
        - debug:
            msg: "{{ ls_cmd_run.stdout }}"
