# Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---

# Tasks for installing AWX

- name: Get all namespaces
  command: kubectl get ns
  changed_when: false
  register: namespaces

- name: Create namespace
  command: "kubectl create namespace {{ awx_namespace }}"
  changed_when: true
  when: "'awx' not in namespaces.stdout"

- name: Get K8s pods
  command: "kubectl get pods -n {{ awx_namespace }}"
  changed_when: false
  register: k8s_pods

- name: Deploy awx-operator
  command: "kubectl apply -f {{ awx_operator_yml_file_path }}"
  changed_when: true
  when: '"awx-operator" not in k8s_pods.stdout'

- name: Get K8s persistent volumes
  command: "kubectl get pv -n {{ awx_namespace }}"
  changed_when: false
  register: k8s_pvs

- name: Configure host volume as playbooks directory path
  replace:
    path: "{{ awx_pv_yml_file_path }}"
    regexp: 'path: "/etc"'
    replace: 'path: "{{ playbook_dir | dirname | dirname }}"'
  when: "'awx-projects-pv' not in k8s_pvs.stdout"

- name: Create persistent volume and volumeclaim for projects
  command: "kubectl apply -f {{ awx_pv_yml_file_path }}"
  changed_when: true
  when: "'awx-projects-pv' not in k8s_pvs.stdout"

- name: Create persistent volume for postgres
  command: "kubectl apply -f {{ awx_postgres_pv_file_path }}"
  changed_when: true
  when: "'awx-postgres-pv' not in k8s_pvs.stdout"

- name: Get the docker images
  command: buildah images
  changed_when: false
  register: docker_images

- name: Build the custom-awx-ee image from the docker file
  command: "buildah bud -t custom-awx-ee {{ awx_ee_docker_file }}"
  changed_when: false
  when: "'custom-awx-ee' not in docker_images.stdout"

- name: Waiting for awx-operator deployment to be up and running
  shell: "kubectl wait --for=condition=available deployment/awx-operator -n {{ awx_namespace }} --timeout=600s"
  changed_when: false

- name: Deploy awx
  command: "kubectl apply -f {{ awx_yml_file_path }}"
  changed_when: true
  when: not k8s_pods.stdout | regex_search('awx-([A-Za-z0-9]{10})-([A-Za-z0-9]{5})')

- name: Install awxkit using pip3
  pip:
    name: awxkit
    state: present

- name: Install awx collection
  command: "ansible-galaxy collection install awx.awx:{{ awx_version }}"
  changed_when: true
  register: installation_status

- name: Wait for awx pods to get created
  wait_for:
    timeout: "{{ awx_wait_time }}"
  when: not k8s_pods.stdout | regex_search('awx-([A-Za-z0-9]{10})-([A-Za-z0-9]{5})')

- name: Waiting for awx deployment to be up and running
  shell: "kubectl wait --for=condition=available deployment/awx -n {{ awx_namespace }} --timeout=1200s"
  changed_when: false