---
- name: Authenticate with OpenShift via user and password
  shell: "oc login {{ openshift_host }} -u {{ openshift_user }} -p {{ openshift_password }}"
  when:
  - openshift_user is defined
  - openshift_password is defined
  - openshift_token is not defined

- name: Authenticate with OpenShift via token
  shell: "oc login {{ openshift_host }} --token {{ openshift_token }}"
  when: openshift_token is defined

- name: Get Project Detail
  shell: "oc get project {{ awx_openshift_project }}"
  register: project_details
  ignore_errors: yes

- name: Get Postgres Service Detail
  shell: "oc describe svc postgresql -n {{ awx_openshift_project }}"
  register: postgres_svc_details
  ignore_errors: yes
  when: "pg_hostname is not defined or pg_hostname == ''"

- name: Create AWX Openshift Project
  shell: "oc new-project {{ awx_openshift_project }}"
  when: project_details.rc != 0

# This might could/should be optional based on certain circumstances
- name: Mark Openshift User as Admin
  shell: "oc adm policy add-role-to-user admin {{ openshift_user }} -n {{ awx_openshift_project }}"

# TODO: This is duplicated in the kubernetes role, probably needs to be moved to the image_build role
- name: Manage AWX Container Images
  block:
    - name: Get docker registry password from oc if needed
      block:
        - name: Set docker registry password
          shell: oc whoami -t
          register: docker_registry_password_shell
        - name: Set docker registry password
          set_fact:
            docker_registry_password: "{{ docker_registry_password_shell.stdout }}"
      when: docker_registry_password is not defined

    - name: Authenticate with Docker registry
      docker_login:
        registry: "{{ docker_registry }}"
        username: "{{ docker_registry_username }}"
        password: "{{ docker_registry_password }}"
        reauthorize: yes
      when: docker_registry is defined and docker_registry_password is defined
      delegate_to: localhost

    - name: Wait for Openshift
      pause:
        seconds: 30

    - name: Tag and push web image to registry
      docker_image:
        name: "{{ awx_web_image }}"
        repository: "{{ docker_registry }}/{{ docker_registry_repository }}/{{ awx_web_image }}"
        tag: "{{ awx_version }}"
        push: yes
      when: docker_registry is defined
      delegate_to: localhost

    - name: Wait for the registry to settle
      pause:
        seconds: 10

    - name: Tag and push task image to registry
      docker_image:
        name: "{{ awx_task_image }}"
        repository: "{{ docker_registry }}/{{ docker_registry_repository }}/{{ awx_task_image }}"
        tag: "{{ awx_version }}"
        push: yes
      when: docker_registry is defined
      delegate_to: localhost

    # Note this is the one bit that is Openshift specific
    - name: Enable image stream lookups for awx images
      shell: "oc set image-lookup --all -n {{ awx_openshift_project }}"

    - name: Set full web image path
      set_fact:
        awx_web_openshift_image: "{{ awx_web_image }}:{{ awx_version }}"
      when: awx_web_openshift_image is not defined

    - name: Set full task image path
      set_fact:
        awx_task_openshift_image: "{{ awx_task_image }}:{{ awx_version }}"
      when: awx_task_openshift_image is not defined
  when: dockerhub_base is not defined

- name: Set DockerHub Image Paths
  set_fact:
    awx_web_openshift_image: "{{ dockerhub_base }}/awx_web:{{ dockerhub_version }}"
    awx_task_openshift_image: "{{ dockerhub_base }}/awx_task:{{ dockerhub_version }}"
  when: dockerhub_base is defined

- name: Deploy and Activate Postgres
  shell: "oc new-app --template=postgresql-persistent -e MEMORY_LIMIT={{ pg_memory_limit|default('512') }}Mi -e NAMESPACE=openshift -e DATABASE_SERVICE_NAME=postgresql  -e POSTGRESQL_USER={{ pg_username }} -e POSTGRESQL_PASSWORD={{ pg_password }} -e POSTGRESQL_DATABASE={{ pg_database }} -e VOLUME_CAPACITY={{ pg_volume_capacity|default('5')}}Gi -e POSTGRESQL_VERSION=9.5 -n {{ awx_openshift_project }}"
  when: (pg_hostname is not defined or pg_hostname == '') and (postgres_svc_details is defined and postgres_svc_details.rc != 0)
  register: openshift_pg_activate

- name: Wait for Postgres to activate
  pause:
    seconds: 15
  when: openshift_pg_activate|changed

- name: Set openshift base path
  set_fact:
    openshift_base_path: "{{ awx_local_base_config_path|default('/tmp') }}/awx-config"

- name: Ensure directory exists
  file:
    path: "{{ openshift_base_path  }}"
    state: directory

- name: Template Openshift AWX Config
  template:
    src: configmap.yml.j2
    dest: "{{ openshift_base_path }}/configmap.yml"
    mode: '0600'

- name: Template Openshift AWX Deployment
  template:
    src: deployment.yml.j2
    dest: "{{ openshift_base_path }}/deployment.yml"
    mode: '0600'

- name: Template Openshift AWX etcd2
  template:
    src: etcd.yml.j2
    dest: "{{ openshift_base_path }}/etcd.yml"
    mode: '0600'

- name: Apply etcd deployment
  shell: "oc apply -f {{ openshift_base_path }}/etcd.yml"

- name: Apply Configmap
  shell: "oc apply -f {{ openshift_base_path }}/configmap.yml"

- name: Apply Deployment
  shell: "oc apply -f {{ openshift_base_path }}/deployment.yml"
