# 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.
---

- name: Initialize variables
  set_fact:
    cobbler_container_status: false
    cobbler_image_status: false
    cobbler_config_status: false
    backup_map_status: false
    new_node_status: false
  tags: install

- name: Check if any backup file exists
  block:
  - name: Check status of backup file
    stat:
      path: "{{ role_path }}/files/backup_mapping_file.csv"
    register: backup_map

  - name: Set status for backup file
    set_fact:
      backup_map_status: true
    when: backup_map.stat.exists
  rescue:
  - name: Message
    debug:
      msg: "All nodes are new"
      verbosity: 2

- name: Get K8s namespaces
  command: kubectl get namespaces
  changed_when: false
  register: k8s_namespaces

- name: Create namespace network-config
  command: kubectl create namespace cobbler
  changed_when: true
  when: "'cobbler' not in k8s_namespaces.stdout"

- name: Inspect the cobbler image
  command: "buildah images {{ cobbler_image_name }}"
  register: cobbler_image_result
  failed_when: false
  changed_when: false
  tags: install

- name: Check cobbler pod status on the machine
  command: kubectl get pods -n cobbler
  register: cobbler_pod_result
  failed_when: false
  changed_when: false
  tags: install

- name: Update cobbler image status
  set_fact:
    cobbler_image_status: true
  when: "'No such image' not in cobbler_image_result.stderr"
  tags: install

- name: Update cobbler container status
  set_fact:
    cobbler_container_status: true
  when: "'cobbler' in cobbler_pod_result.stdout"
  tags: install

- name: Get cobbler pod name
  command: 'kubectl get pod -n cobbler -l app=cobbler -o jsonpath="{.items[0].metadata.name}"'
  changed_when: false
  register: cobbler_pod_name
  when: cobbler_container_status
  tags: install

- name: Fetch cobbler profile list
  command: "kubectl exec --stdin --tty -n cobbler {{ cobbler_pod_name.stdout }} -- cobbler profile list"
  changed_when: false
  register: cobbler_profile_list
  failed_when: false
  when: cobbler_container_status

- name: Check crontab list
  command: "kubectl exec --stdin --tty -n cobbler {{ cobbler_pod_name.stdout }} -- crontab -l"
  changed_when: false
  register: crontab_list
  failed_when: false
  when: cobbler_container_status

- name: Update cobbler config status
  set_fact:
    cobbler_config_status: true
  when:
    - cobbler_container_status
    - "'CentOS' in cobbler_profile_list.stdout"
    - "'* * * * * /usr/bin/ansible-playbook /root/tftp.yml' in crontab_list.stdout"
    - "'*/5 * * * * /usr/bin/ansible-playbook /root/inventory_creation.yml' in crontab_list.stdout"