###
# This Playbook orchestrate creation, validation, report generation
# and upload of validation logs.
###
## This section takes the ansible host group 'just_created'
## and runs validations against them
---
- hosts: all
  gather_facts: True
  remote_user: "{{ lookup('env', 'ADMIN_USER_NAME') }}"
  become: true
  vars:
    ansible_become_pass: "{{ lookup('env', 'ADMIN_PASSWORD') }}"
    is_rhui_package_present: false

  tasks:
    - assert:
        that:
          - "offer_type is defined"
          - "rhel_version is defined"
          - "build_on_rhui4 is defined"
          - "isCVM is defined"

    - set_fact:
        rhui3_regions: "{{ lookup('file', 'files/rhui3-regions').split() }}"
        rhui4_regions: "{{ lookup('file', 'files/rhui4-regions').split() }}"
        out_folder: /tmp/out
        err_folder: /tmp/err
        license_type: "{{ 'byos' if 'byos' in inventory_hostname else 'payg' }}"
        repo_type: "{{'base' if 'base' in offer_type or 'beta' in offer_type else ('byol' if 'byol' in offer_type else 'eus')}}"

    - name: Delete validation data from previous run
      shell: |
        rm -rf /tmp/out && rm -rf /tmp/err
        mkdir -p {{ out_folder }}
        mkdir -p {{ err_folder }}
      ignore_errors: yes

    - name: Run per VM validations
      include_tasks: validation-playbooks/per-vm-validation.yaml
      ignore_errors: yes

    - name: Run per region validations for RHUI3
      include_tasks: validation-playbooks/per-region-validation.yaml
      loop: "{{ rhui3_regions }}"
      when: item != "" and license_type != 'byos' and build_on_rhui4 == 'false' and is_rhui_package_present
      ignore_errors: yes

    - name: Run per region validations for RHUI4
      include_tasks: validation-playbooks/per-region-validation.yaml
      loop: "{{ rhui4_regions }}"
      when: item != "" and license_type != 'byos' and build_on_rhui4 == 'true' and is_rhui_package_present
      ignore_errors: yes

    ## ########################################################################## 
    ## below python script generates a logs.json which contains a lot of 
    ## information about the host. Currently we don't have the code to 
    ## parse that and decide if it is success or failure. Once that part is done,
    ## uncomment the code below.

    # Run the validate.py script
    # - name: Copy validation python scripts to host
    #   copy: src=./validations/ dest=/tmp/validations/

    # - name: Run validation script
    #   shell: python /tmp/validations/validate.py
    ## ##########################################################################

    - name: Recursively find /tmp/out files
      find:
        paths: /tmp/out
        recurse: yes
      register: out_files_to_copy

    - name: Recursively find /tmp/err files
      find:
        paths: /tmp/err
        recurse: yes
      register: err_files_to_copy

    - name: Merge the two lists
      set_fact:
        files_list: "{{ out_files_to_copy.files + err_files_to_copy.files }}"

    - name: Fetch all the log files
      fetch: 
        src: "{{ item.path }}"
        dest: ./validation_results
      loop: "{{ files_list }}"