###
# This Playbook runs validations which are specific to a VM and 
# doesn't depend on which region the VM is deployed in.
###

---
- name: Set image properties
  set_fact:
    err_folder: /tmp/err
    admin_user: "{{ lookup('env', 'ADMIN_USER_NAME') }}"

#TODO : commenting for now, as this check if failing for rhel7
# - name: Check if Grubenv file is symlink
#   shell: |
#     if [[ -L "/boot/grub2/grubenv" ]]; then   
#       echo "Grubenv file is symlink" >> {{err_folder}}/err_msgs.log
#     fi
#   ignore_errors: yes

- name: Compare RHEL version with the expected RHEL version in the pipeline
  lineinfile:
    path: "{{err_folder}}/err_msgs.log"
    line: "RHEL version mismatch: Expected RHEL version: {{rhel_version}}, current RHEL version: {{ansible_distribution_version}} "
    create: yes
    state: present
  when: "ansible_distribution_version != rhel_version"

- name: Check number of users on the machine
  shell: getent passwd {1000..60000} | grep -v {{ admin_user }} | wc -l
  register: users_on_machine
  ignore_errors: yes

- name: Execute command to list packages
  shell: |
    package_list=$( rpm -qa --qf ' "%{NAME}": { "version": " %{VERSION}", "release": "%{RELEASE}" \},' )
    package_list=${package_list%,}
    package_json="{  $package_list  }"
    echo $package_json | jq -r .
  register: package_list_output

- name: Set package facts
  set_fact:
    ansible_facts:
      packages: "{{ package_list_output.stdout | from_json }}"

- name: Check if cloud-init is installed
  lineinfile:
    path: "{{err_folder}}/err_msgs.log"
    line: "'cloud-init' not found"
    create: yes
    state: present
  when: "'cloud-init' not in ansible_facts.packages"

- name: Write to error msg if repolist failed
  lineinfile:
    path: "{{err_folder}}/err_msgs.log"
    line: "Additional users found on the machines"
    create: yes
    state: present
  when: users_on_machine.stdout_lines[0] != '0'

- name: check if ADE validation is valid for this vm
  include_tasks: validation-playbooks/ade_packages_validation.yaml
  ignore_errors: yes
  when: ansible_os_family == "RedHat" and (ansible_distribution_major_version == '8' or ansible_distribution_major_version == '7') and (repo_type == 'base' or repo_type =='beta')

- name: Check if blacklisted drivers are blacklisted by modprobe service
  include_tasks: validation-playbooks/blacklisted_drivers_validation.yaml
  ignore_errors: yes
  when: ansible_os_family == "RedHat"

- name: Check if the initramfs file is present
  stat: path=/boot/initramfs-{{ ansible_facts.kernel }}.img
  register: initramfs_present
  when: isCVM is false

- name: "Write to error msg if initramfs files are not present"
  lineinfile:
    path: "{{err_folder}}/err_msgs.log"
    line: "\n boot validation failed since initramfs file is not present."
    create: yes
    state: present 
  when: isCVM is false and initramfs_present.stat.exists == false

- name: Check for NVME/PCI Drivers in image
  when: isCVM is false and initramfs_present.stat.exists == true
  block:
  - name: Check if the nvme driver is present in all images
    shell: lsinitrd /boot/initramfs-{{ ansible_facts.kernel }}.img | grep nvme
    register: check_nvme_allimages

  - debug:
      var: check_nvme_allimages

  - name: "Write to error msg if some drivers are not present"
    lineinfile:
      path: "{{err_folder}}/err_msgs.log"
      line: "\n NVME validation failed since nvme driver is not present. "
      create: yes
      state: present 
    when: ("nvme" not in check_nvme_allimages.stdout)

  - name: Check if the pci driver is present in all images
    shell: lsinitrd /boot/initramfs-{{ ansible_facts.kernel }}.img | grep pci
    register: check_pci_allimages

  - debug:
      var: check_pci_allimages

  - name: "Write to error msg if some drivers are not present"
    lineinfile:
      path: "{{err_folder}}/err_msgs.log"
      line: "\n NVME validation failed since pci driver is not present. "
      create: yes
      state: present 
    when: ("pci" not in check_pci_allimages.stdout)

- name: Check for Rhui client package in the image
  block:
  - name: Execute command to list packages
    shell: |
      package_list=$( rpm -qa --qf ' "%{NAME}": { "version": " %{VERSION}", "release": "%{RELEASE}" \},' )
      package_list=${package_list%,}
      package_json="{ $package_list }"
      echo $package_json | jq -r .
    register: package_list_output

  - name: Set package facts
    set_fact:
      ansible_facts:
        packages: "{{ package_list_output.stdout | from_json }}"

  - name: Check for rhui package details
    set_fact:
      rhui_package: "{{ ansible_facts.packages | dict2items | selectattr('key', 'match', 'rhui-azure-rhel') }}"

  - name: Check for rhui package count
    set_fact:
      rhui_package_count : "{{ rhui_package | count }}"

  - name: Check for rhui package details
    debug:
      var: "{{ item }}"
    with_items:
      - rhui_package
      - rhui_package_count

  - name: Log error in case rhui package isn't installed
    lineinfile:
      path: "{{err_folder}}/err_msgs.log"
      line: "Rhui Client package is not installed in the image"
      create: yes
      state: present
    when: rhui_package_count == "0" and offer_type !='byol'

  - name: Set the variable if rhui client package is found
    set_fact:
      is_rhui_package_present : true
    when: rhui_package_count != "0"

- name: Check the repo-validation of RHEL Image according to the offertype and client package present in it.
  include_tasks: validation-playbooks/repo-validation-check.yaml
  ignore_errors: yes

- name: Check for the Accelerated Networking in Rhel 9 Images.
  when: isCVM is false and ansible_distribution_major_version == '9'
  block:
    - name: Check if the unmanaged devices are present network config of all Rhel 9 Images
      shell: NetworkManager --print-config
      register: check_network_config
  
    - debug:
        var: check_network_config
    
    - name: "Write to error msg if unmanaged drivers are not present"
      lineinfile:
        path: "{{err_folder}}/err_msgs.log"
        line: "\n Accelerated Networking Validation failed since unmanaged drivers are not present."
        create: yes
        state: present 
      when: ("unmanaged-devices=driver:mlx4_core;driver:mlx5_core" not in check_network_config.stdout_lines)    
    
    - name: Check if the accelerated networking config file is present in all Rhel 9 Images
      stat:
        path: /etc/NetworkManager/conf.d/99-azure-unmanaged-devices.conf
      register: check_99_azure_unmanaged_devices_config_file
    
    - debug:
        var: check_99_azure_unmanaged_devices_config_file
    
    - name: "Write to error msg if the file is not present"
      lineinfile:
        path: "./err_msgs.log"
        line: "\n Accelerated Networking Validation failed since unmanaged config file is not present. "
      when: not check_99_azure_unmanaged_devices_config_file.stat.exists    
    
