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

- 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

- name: Inspect the infiniband_container image
  command: "buildah images"
  register: infiniband_container_image_result
  failed_when: false
  changed_when: false

- name: Create directories if they don't exist
  block:
    - name: Checking directory
      stat:
        path: "{{ item }}"
      register: stat_result
      with_items:
        - "{{ subnet_manager.cache_directory }}"
        - "{{ subnet_manager.log_directory }}"
      loop_control:
        label: "{{ item }}"

    - name: Creating directories
      file:
        path: "{{ item.item }}"
        state: directory
        mode: "{{ folder_perm }}"
        group: root
        owner: root
      when: not item.stat.exists
      loop: "{{ stat_result.results }}"
      loop_control:
        label: "{{ item.item }}"

- name: Copy opensm configuration file
  copy:
    src: "{{ opensm_conf_file }}"
    dest: "{{ opensm_conf_file_dest }}"
    mode: preserve
    force: yes

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

- name: Update infiniband_container image status
  set_fact:
    infiniband_container_image_status: true
  when: infiniband_image_name in infiniband_container_image_result.stdout

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