# 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: Include control_plane_common vars
  include_vars: ../../control_plane_common/vars/main.yml
  run_once: true

- name: Include custom_iso vars
  include_vars: ../../control_plane_customiso/vars/main.yml
  run_once: true

- name: Check {{ management_station_ip_file }} file is present
  stat:
    path: "{{ role_path }}/files/{{ management_station_ip_file }}"
  register: ip_file_check
  run_once: true
  
- name: Fetch management station ip from {{ management_station_ip_file }}
  command: cat {{ role_path }}/files/{{ management_station_ip_file }}
  changed_when: false
  register: fetch_ip
  run_once: true
  when: ip_file_check.stat.exists

- name: Missing {{ management_station_ip_file }}
  fail:
    msg: "{{ missing_ip_file_fail_msg }}"
  when: not ip_file_check.stat.exists
  
- name: Set management_station_ip
  set_fact:
    management_station_ip: "{{ fetch_ip.stdout }}"

- name: Initialize variables
  set_fact:
    raid_type: false
    raid_controller_sensor: ""
    raid_enclosure_name: ""
    drives_id: ""
    enterprise_license: false
    datacenter_license: false
    provision_status: false
    model_status: false
    idrac_license_name: ""
    deploy_os_status: false

- block:
    - name: Check tower_cli.cfg is encrypted
      command: cat "{{ playbook_dir }}/roles/webui_awx/files/{{ awx_vars_filename }}"
      changed_when: false
      register: awx_content
      run_once: true

    - name: Decrpyt tower_cli.cfg
      command: >-
        ansible-vault decrypt "{{ playbook_dir }}/roles/webui_awx/files/{{ awx_vars_filename }}"
        --vault-password-file "{{ playbook_dir }}/roles/webui_awx/files/{{ awx_vaultname }}"
      changed_when: false
      run_once: true
      when: "'$ANSIBLE_VAULT;' in awx_content.stdout"

    - name: Fetch awx host
      command: grep "host:" "{{ playbook_dir }}/roles/webui_awx/files/{{ awx_vars_filename }}"
      register: fetch_awx_host
      changed_when: false
      run_once: true

    - name: Fetch awx username
      command: grep "username:" "{{ playbook_dir }}/roles/webui_awx/files/{{ awx_vars_filename }}"
      register: fetch_awx_username
      changed_when: false
      run_once: true
      no_log: true

    - name: Fetch awx password
      command: grep "password:" "{{ playbook_dir }}/roles/webui_awx/files/{{ awx_vars_filename }}"
      register: fetch_awx_password
      changed_when: false
      run_once: true
      no_log: true

    - name: Set awx variables
      set_fact:
        awx_host: "{{ fetch_awx_host.stdout | regex_replace('host: ','') }}"
        awx_username: "{{ fetch_awx_username.stdout | regex_replace('username: ','') }}"
        awx_password: "{{ fetch_awx_password.stdout | regex_replace('password: ','') }}"
      no_log: true

    - name: Encrypt tower_cli.cfg
      command: >-
        ansible-vault encrypt "{{ playbook_dir }}/roles/webui_awx/files/{{ awx_vars_filename }}"
        --vault-password-file "{{ playbook_dir }}/roles/webui_awx/files/{{ awx_vaultname }}"
      changed_when: false
      run_once: true
      when: "'$ANSIBLE_VAULT;' in awx_content.stdout"
      
    - name: Get inventory list
      command: >-
        awx --conf.host "{{ awx_host }}" --conf.username "{{ awx_username }}" --conf.password "{{ awx_password }}"
        inventory list -f human --filter "name"
      register: inventory_list
      run_once: true
      changed_when: false
      no_log: true

    - name: Create provisioned_idrac inventory
      command: >-
        awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
        inventory create --name "{{ provisioned_idrac_inventory_name }}" --organization "{{ awx_organization }}"
      register: create_inventory
      run_once: true
      changed_when: true
      no_log: true
      when: provisioned_idrac_inventory_name not in inventory_list.stdout

    - name: Fetch provisioned_idrac inventory
      command: >-
        awx --conf.host {{ awx_host }} --conf.username {{ awx_username }} --conf.password {{ awx_password }}
        hosts list --inventory "{{ provisioned_idrac_inventory_name }}" -f human --filter "name"
      register: fetch_inventory
      run_once: true
      changed_when: false
      no_log: true

    - name: Set provision status
      set_fact:
        provision_status: true
      when: inventory_hostname in fetch_inventory.stdout

    - name: Removing hosts already provisioned
      debug:
        msg: "{{ provision_skip_msg }}"
      when: provision_status

  when: awx_search_key in hostname.stdout

- block:
    - name: Show status of the Lifecycle Controller
      dellemc.openmanage.idrac_lifecycle_controller_status_info:
        idrac_ip: "{{ inventory_hostname }}"
        idrac_user: "{{ idrac_username }}"
        idrac_password: "{{ idrac_password }}"
      register: lc_check_status

    - name: LC not available
      fail:
        msg: "{{ lc_check_fail_msg }}"
      when: not lc_check_status.lc_status_info.LCReady
      register: lc_fail

    - name: Get system inventory
      dellemc.openmanage.idrac_system_info:
        idrac_ip: "{{ inventory_hostname }}"
        idrac_user: "{{ idrac_username }}"
        idrac_password: "{{ idrac_password }}"
      register: idrac_info

    - name: Set enterprise license status
      set_fact:
        enterprise_license: true
        idrac_license_name: "{{ idrac_info.system_info.License[my_idx1].LicenseDescription }}"
      with_items: "{{ idrac_info.system_info.License }}"
      when:
        - '"iDRAC" in idrac_info.system_info.License[my_idx1].LicenseDescription'
        - '"Enterprise" in idrac_info.system_info.License[my_idx1].LicenseDescription'
        - '"License" in idrac_info.system_info.License[my_idx1].LicenseDescription'
        - '"Healthy" in idrac_info.system_info.License[my_idx1].PrimaryStatus'
      loop_control:
        index_var: my_idx1

    - name: Set datacenter license status
      set_fact:
        datacenter_license: true
        idrac_license_name: "{{ idrac_info.system_info.License[my_idx1].LicenseDescription }}"
      with_items: "{{ idrac_info.system_info.License }}"
      when:
        - '"iDRAC" in idrac_info.system_info.License[my_idx2].LicenseDescription'
        - '"Datacenter" in idrac_info.system_info.License[my_idx2].LicenseDescription'
        - '"License" in idrac_info.system_info.License[my_idx2].LicenseDescription'
        - '"Healthy" in idrac_info.system_info.License[my_idx2].PrimaryStatus'
      loop_control:
        index_var: my_idx2

    - name: Change provision mode in absence of license
      set_fact:
        provision_method: "pxe"
      when: not (enterprise_license or datacenter_license)

    - name: Firmware version of iDRAC9 not supported
      debug:
        msg: "{{ idrac9_firmware_not_supported_msg }}"
      when:
        - '"iDRAC9" in idrac_license_name'
        - idrac_info.system_info.iDRAC[0].FirmwareVersion < idrac9_supported_version

    - name: Firmware version of iDRAC8 not supported
      debug:
        msg: "{{ idrac8_firmware_not_supported_msg }}"
      when:
        - '"iDRAC8" in idrac_license_name'
        - idrac_info.system_info.iDRAC[0].FirmwareVersion < idrac8_supported_version

    - name: Check NFS share access
      dellemc.openmanage.idrac_server_config_profile:
        idrac_ip: "{{ inventory_hostname }}"
        idrac_user: "{{ idrac_username }}"
        idrac_password: "{{ idrac_password }}"
        share_name: "{{ management_station_ip }}:{{ nfs_share_offline_repo }}"
        command: "export"
        scp_components: "BIOS"
        scp_file: "{{ nfs_check_file }}"
        export_format: XML
        export_use: Default
        job_wait: true
      register: nfs_check
      ignore_errors: true
      until: not nfs_check.failed
      retries: "{{ retries_count }}"

    - name: Missing entries in nfs exports
      fail:
        msg: "{{ missing_exports_fail_msg }}"
      when:
        - nfs_check_key in nfs_check.msg or
          nfs_check_key in nfs_check.scp_status.Status
  when: not provision_status
