# 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 KIiND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---

- name: Initialize variables
  set_fact:
    mngmnt_network_container_image_status: false
    backup_mngmnt_map_status: false
    new_mngmnt_node_status: false

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

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

- name: Inspect the mngmnt_network_container image
  command: "buildah images"
  register: mngmnt_network_container_image_result
  failed_when: false
  changed_when: false

- name: Check mngmnt_network_container status on the machine
  command: kubectl get pods -n network-config
  register: mngmnt_network_container_result
  failed_when: false
  changed_when: false

- name: Update mngmnt_network_container image status
  set_fact:
    mngmnt_network_container_image_status: true
  when: mngmnt_network_image_name in mngmnt_network_container_image_result.stdout

- name: Update mngmnt_network_container container status
  set_fact:
    mngmnt_network_container_status: true
  when: "'mngmnt-network-container' in mngmnt_network_container_result.stdout"