#  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: Get ME4 volume
  shell: >
    set -o pipefail && \
    lsscsi -s | grep ME4
  changed_when: false
  register: me4_output
  failed_when: false

- name: ME4 volume check
  fail:
    msg: "{{ me4_volume_fail_msg }}"
  when: me4_output is failed or (me4_output.stdout | regex_findall('ME4') | length) != 2

- name: Set ME4 data facts
  set_fact:
    me4_k8s_volume_data: "{{ me4_output.stdout.split('\n')[0].split(' ') | select() }}"
    me4_slurm_volume_data: "{{ me4_output.stdout.split('\n')[1].split(' ') | select() }}"

- name: Add ME4 volume data to dummy host
  add_host:
    name:   "NFS_NODE_TOKEN_HOLDER"
    me4_k8s_volume: "{{ me4_k8s_volume_data[-2] }}"
    me4_slurm_volume: "{{ me4_slurm_volume_data[-2] }}"

- name: Get all mounted partitions
  command: df -h
  changed_when: false
  register: mounted_partitions

- name: Create partition on ME4 volumes
  command: "parted -a optimal {{ item }} --script -- mklabel gpt mkpart primary 0% {{ powervault_me4_disk_partition_size }}"
  changed_when: true
  with_items:
    - "{{ hostvars['NFS_NODE_TOKEN_HOLDER']['me4_k8s_volume'] }}"
    - "{{ hostvars['NFS_NODE_TOKEN_HOLDER']['me4_slurm_volume'] }}"
  when:
    - hostvars['NFS_NODE_TOKEN_HOLDER']['me4_k8s_volume'] not in mounted_partitions.stdout
    - hostvars['NFS_NODE_TOKEN_HOLDER']['me4_slurm_volume'] not in mounted_partitions.stdout

- name: Update kernel with new partition changes
  command: partprobe
  changed_when: false

- name: Check ME4 mounted partitions
  shell: >
    set -o pipefail && \
    mount | grep me4
  failed_when: false
  changed_when: false
  args:
    warn: false
  register: me4_mounted_partitions

- name: Set file system on partition
  shell: >
    set -o pipefail && \
    echo y | mkfs -t ext4 {{ item }}1
  with_items:
    - "{{ hostvars['NFS_NODE_TOKEN_HOLDER']['me4_k8s_volume'] }}"
    - "{{ hostvars['NFS_NODE_TOKEN_HOLDER']['me4_slurm_volume'] }}"
  when:
    - me4_nfs_share_k8s not in me4_mounted_partitions.stdout
    - me4_nfs_share_slurm not in me4_mounted_partitions.stdout

- name: Creating NFS share directories
  file:
    path: "{{ item }}"
    state: directory
    mode: "{{ nfs_share_dir_mode }}"
  with_items:
    - "{{ me4_nfs_share_k8s }}"
    - "{{ me4_nfs_share_slurm }}"

- name: Mount K8s partition on K8s NFS share
  command: "mount {{ hostvars['NFS_NODE_TOKEN_HOLDER']['me4_k8s_volume'] }}1 {{ me4_nfs_share_k8s }}"
  changed_when: true
  args:
    warn: false
  when: me4_nfs_share_k8s not in me4_mounted_partitions.stdout

- name: Mount Slurm partition on Slurm NFS share
  command: "mount {{ hostvars['NFS_NODE_TOKEN_HOLDER']['me4_slurm_volume'] }}1 {{ me4_nfs_share_slurm }}"
  changed_when: true
  args:
    warn: false
  when: me4_nfs_share_slurm not in me4_mounted_partitions.stdout

- name: Configuring auto mount K8s partition on reboot
  lineinfile:
    path: "{{ fstab_file_path }}"
    line: "{{ hostvars['NFS_NODE_TOKEN_HOLDER']['me4_k8s_volume'] }}1            {{ me4_nfs_share_k8s }}      ext4            defaults        0     0"

- name: Configuring auto mount Slurm partition on reboot
  lineinfile:
    path: "{{ fstab_file_path }}"
    line: "{{ hostvars['NFS_NODE_TOKEN_HOLDER']['me4_slurm_volume'] }}1            {{ me4_nfs_share_slurm }}      ext4            defaults        0     0"