# Copyright 2022 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: Creating directory for deploying awx-operator
  file:
    path: "{{ awx_operator_folder }}"
    state: directory
    mode: "{{ file_perm }}"

- name: Check for awx-operator status
  stat:
    path: "{{ awx_manager_file }}"
  register: awx_operator_repo

- name: Cloning awx-operator from github
  git:
    repo: "{{ awx_operator_link }}"
    dest: "{{ awx_operator_folder }}"
    version: "{{ awx_tag }}"
  when: not awx_operator_repo.stat.exists

- name: Modifying livenessprobe value
  replace:
    path: "{{ awx_manager_file }}"
    regexp: "{{ liveness_probe_initial }}"
    replace: "{{ liveness_probe_final }}"

- name: Modifying readinessprobe value
  replace:
    path: "{{ awx_manager_file }}"
    regexp: "{{ readiness_probe_initial }}"
    replace: "{{ readiness_probe_final }}"

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

- name: Setting the current namespace for kubectl
  command: "kubectl config set-context --current --namespace={{ awx_namespace }}"
  changed_when: false

- name: Installing jq package
  package:
    name: jq
    state: present

- name: Deploying awx-operator
  command: make deploy
  changed_when: false
  args:
    chdir: "{{ awx_operator_folder }}"
  environment:
    NAMESPACE: "{{ awx_namespace }}"

- name: Waiting for awx operator deployment {This might take 10-15 minutes}
  block:
    - name: Waiting for awx-operator deployment to be up and running
      command: kubectl wait --for=condition=available deployment awx-operator-controller-manager -n {{ awx_namespace }} --timeout={{ awx_operator_time }}
      changed_when: false 
  rescue:
    - name: Display failure message
      debug:
        msg: "{{ operator_deployment_failure }}"

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

- 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 (It may take 5-10min)
  command: "buildah bud -t custom-awx-ee {{ awx_ee_docker_file }}"
  changed_when: false
  when: "'custom-awx-ee' not in docker_images.stdout"
  retries: "{{ min_retries }}"

- 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: 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: 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: Fails if the pods go into ImagePullBackOff state
  block:
    - name: Waiting for awx deployment to be up and running
      command: kubectl wait --for=condition=available deployment awx -n {{ awx_namespace }} --timeout={{ awx_deployment_time }}
      changed_when: false
  rescue:
    - name: Display failure message
      debug:
        msg: "{{ deployment_failure_msg }}"