# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

---
# /*----------------------------------------------------------------------------8
# |                                                                            |
# |                         PREPARATION FOR BOM PROCESSING                     |
# |                                                                            |
# |                         RESET LOGGING                                      |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

- hosts:                               localhost
  name:                                Get Storage account details from KeyVault
  gather_facts:                        true
  vars_files:
    - vars/ansible-input-api.yaml      # API Input template with defaults
  tasks:

    - name:                            Create Progress folder
      ansible.builtin.file:
        path:                          "{{ _workspace_directory }}/.progress"
        state:                         directory
        mode:                          0755
      tags:
        - always

    - name:                            Remove bom-processing-done flag
      ansible.builtin.file:
        path:                          "{{ _workspace_directory }}/.progress/bom-processing-done"
        state:                         absent
      tags:
        - always

    - name:                            Include 0.3.sap-installation-media-storage-details role
      ansible.builtin.import_role:
        name:                          roles-misc/0.3.sap-installation-media-storage-details
      vars:
        tier:                          bom_download
      tags:
        - always

# /*----------------------------------------------------------------------------8
# |                                                                            |
# |                         PLAYBOOK FOR BOM PROCESSING                        |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

- hosts:                               "{{ sap_sid | upper }}_SCS : {{ sap_sid | upper }}_DB"

  name:                                BOM Processing
  remote_user:                         "{{ orchestration_ansible_user }}"
  gather_facts:                        true
  vars_files:
    - vars/ansible-input-api.yaml      # API Input template with defaults

  tasks:
# -------------------------------------+---------------------------------------8
#
# Build the list of tasks to be executed in order here.
#
# -------------------------------------+---------------------------------------8
    - name:                            "Perform BoM processing"
      when:
        # run on SCS or the when no SCS is defined run on the first DB server to support HANA only install
        - "'scs' in supported_tiers or (
            platform == 'HANA' and
            (groups[sap_sid | upper ~ '_SCS'] is not defined or groups[sap_sid | upper ~ '_SCS'] | length == 0) and
            inventory_hostname == (groups[sap_sid | upper ~ '_DB'] | sort)[0]
          )"
      block:

        - name:                        Set facts
          ansible.builtin.set_fact:
            tier:                      preparation
            sapbits_location_base_path: "{{ hostvars.localhost.sapbits_location_base_path }}"
            sa_enabled:                true
          tags:
            - always

        - name:                        Set facts
          when:                        hostvars.localhost.sapbits_sas_token is defined
          ansible.builtin.set_fact:
            sapbits_sas_token:         "{{ hostvars.localhost.sapbits_sas_token }}"
          tags:
            - always

        - name:                        Set facts
          when:                        hostvars.localhost.sapbits_sas_token is defined
          ansible.builtin.set_fact:
            sapbits_sas_token:         "{{ hostvars.localhost.sapbits_sas_token }}"
          tags:
            - always

        - name:                        3.3-bom-processing role for Linux
          become:                      true
          when:                        ansible_os_family != "Windows"
          block:
            - name:                    Include the 3.3-bom-processing role
              ansible.builtin.import_role:
                name:                  "roles-sap/3.3-bom-processing"
              vars:
                tier:                  bom_download
                always_upload_jinja_templates: false
                primary_instance_name: ansible_play_hosts[0]
                sa_enabled:            true
              tags:
                - 3.3-bom-processing

        - name:                        3.3-bom-processing role for Windows
          when:                        ansible_os_family == "Windows"
          block:
            - name:                    Include the 3.3-bom-processing role
              ansible.builtin.import_role:
                name:                  "roles-sap/windows/3.3-bom-processing"
              vars:
                tier:                  bom_download
                always_upload_jinja_templates: false
                primary_instance_name: ansible_play_hosts[0]
              tags:
                - 3.3-bom-processing

        - name:                        Create bom-processing flag
          delegate_to:                 localhost
          become:                      false
          ansible.builtin.file:
            path:                      "{{ _workspace_directory }}/.progress/bom-processing"
            state:                     touch
            mode:                      0755
          tags:
            - always

...
# /*---------------------------------------------------------------------------8
# |                                   END                                      |
# +------------------------------------4--------------------------------------*/
