# 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:
    infiniband_container_status: false
    infiniband_container_image_status: false
    infiniband_container_config_status: false
    infiniband_backup_map_status: false
    infiniband_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: infiniband_backup_map

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

- name: Inspect the infiniband_container image
  command: "buildah images {{ infiniband_image_name }}"
  register: infiniband_container_image_result
  ignore_errors: true
  changed_when: false
  tags: install

- name: Check infiniband_container status on the machine
  command: kubectl get pods -n network-config
  register: infiniband_container_result
  ignore_errors: true
  changed_when: false
  tags: install

- name: Update infiniband_container image status
  set_fact:
    infiniband_container_image_status: true
  when: "'No such image' not in infiniband_container_image_result.stderr"
  tags: install

- name: Update infiniband_container container status
  set_fact:
    infiniband_container_status: true
  when: "'infiniband-container' in infiniband_container_result.stdout"
  tags: install

- name: Update infiniband_container  status
  set_fact:
    infiniband_container_config_status: true
  when:
    - infiniband_container_status == true