# 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: Get all images
  command: "buildah images"
  register: images_result
  failed_when: false
  changed_when: false

- name: Update the permission of init_k8s_pod.sh
  file:
    path: "{{ role_path }}/files/init_k8s_pod.sh"
    mode: "{{ slurm_telemetry_code_dir_mode }}"
  when: slurm_telemetry_image not in images_result.stdout

- name: Create slurm_telemetry image
  command: buildah bud -t {{ slurm_telemetry_image }}:{{ slurm_telemetry_image_tag }} -f {{ role_path }}/files/Dockerfile
  args:
    chdir: "{{ role_path }}/files/"
  changed_when: true
  when: slurm_telemetry_image not in images_result.stdout

- name: Deploy slurm_telemetry pod
  command: kubectl apply -f {{ role_path }}/files/k8s_slurm_telemetry.yml
  changed_when: true

- name: Wait for slurm_telemetry pod to come to ready state
  command: kubectl wait --for=condition=ready -n {{ namespace }} pod -l app=slurm-telemetry --timeout=4m
  changed_when: true

- name: Delete input config file
  file:
    path: "{{ role_path }}/files/monster/config.yml"
    state: absent

- name: Delete init k8s pod file
  file:
    path: "{{ role_path }}/files/init_k8s_pod.sh"
    state: absent