#  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: Create custom ISO
  command: >-
    mkisofs -o {{ role_path }}/files/{{ unattended_iso_filename }} -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4
    -boot-info-table -eltorito-alt-boot -e images/efiboot.img -no-emul-boot -J -R -V "CentOS 7 x86_64"  {{ tmp_iso_dir }}
  changed_when: true
  register: custom_iso_status
  tags: install
  args:
    chdir: "{{ tmp_iso_dir }}"

- name: Custom ISO creation status check
  assert:
    that:
      - "'Total directory bytes:' in custom_iso_status.stderr"
      - "'Path table size(bytes):' in custom_iso_status.stderr"
      - "'Max brk space used' in custom_iso_status.stderr"
      - "'extents written' in custom_iso_status.stderr"
    success_msg: "{{ custom_iso_success_msg }}"
    fail_msg: "{{ custom_iso_fail_msg }}"
  register: iso_success_check

- name: Remove the kickstart file
  file:
    path: "{{ role_path }}/files/{{ kickstart_file }}"
    state: absent
  tags: install

- name: Include control_plane_repo vars
  include_vars: ../../control_plane_repo/vars/main.yml

- name: Copy ISO file to nfs share
  copy:
    src: "{{ role_path }}/files/{{ unattended_iso_filename }}"
    dest: "{{ nfs_share_offline_repo }}/{{ unattended_iso_filename }}"
    mode: preserve
  tags: install

- name: Fetch ansible-playbook location
  command: whereis ansible-playbook
  changed_when: false
  register: ansible_playbook_location
  tags: install

- name: Schedule task
  cron:
    name: "Add idrac IP to nfs exports"
    minute: "*/10"
    job: "if ! out=`{{ ansible_playbook_location.stdout.split(' ')[1] }} {{ role_path }}/files/add_nfs_client.yml`; then echo $out >> {{ cron_error_log }}; fi"
  tags: install