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

# /*---------------------------------------------------------------------------8
# |                                                                            |
# |             SAP: Register BOM                                              |
# |         SAP SCS: deploy SCS Parameter file install template                |
# |         SAP SCS: Install                                                   |
# |                                                                            |
# +------------------------------------4--------------------------------------*/

---
- name:                                "SCS HA Install: Set Python version {{ distribution_id }}"
  ansible.builtin.set_fact:
    python_version: "python2"
  when: (ansible_distribution | lower ~ ansible_distribution_major_version) in ['sles_sap12']

- name:                                 "SCS HA Install: Set BOM facts"
  ansible.builtin.set_fact:
    sap_inifile:                        "{{ bom_base_name }}-scsha-{{ ansible_hostname }}.params"
    sap_inifile_template:               "scsha-inifile-param.j2"
    dir_params:                         "{{ tmp_directory }}/.{{ sid_to_be_deployed.sid | lower }}-params"

#   0x) Create hidden directory for parameter files
- name:                                "SCS HA Install: Create directories"
  ansible.builtin.file:
    path:                              "{{ item.path }}"
    state:                             directory
    mode:                              '{{ item.mode }}'
  loop:
    - { mode: '0755', path: '{{ dir_params }}' }
    - { mode: '0755', path: '{{ tmp_directory }}/{{ sid_to_be_deployed.sid | upper }}' }
    - { mode: '0755', path: '/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}' }

- name:                                "SCS HA Install: reset"
  ansible.builtin.file:
    path:                              "/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}/sap_deployment_scs.txt"
    state:                             absent
  when:                                reinstall

- name:                                "SCS HA Install: check if installed"
  ansible.builtin.stat:
    path:                              "/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}/sap_deployment_scs.txt"
  register:                            scs_installed
  when:                                node_tier == 'scs'

    # Returns bom object
- name:                                "SCS HA Install: Register BoM"
  ansible.builtin.include_role:
    name:                              roles-sap/3.3.1-bom-utility
    tasks_from:                        bom-register
  vars:
    bom_name:                          "{{ bom_base_name }}"
    task_prefix:                       "SCS HA Install: "
    sa_enabled:                        true


- name:                                "SCS HA Install: register variables"
  ansible.builtin.set_fact:
    scs_bom_id:                        "{{ bom.product_ids.scs_ha }}"
    scs_bom_instance_type:             "{% if bom.InstanceType is defined %}{{ bom.InstanceType }}{% else %}ABAP{% endif %}"
    public:                            true

- name:                                "SCS Install: register additional variables"
  ansible.builtin.set_fact:
    installGateway:                    "{% if scs_bom_instance_type == 'ABAP' %}true{% else %}false{% endif %}"

- name:                                "SCS HA Install"
  block:

    - name:                            "SCS Install: check media exists"
      ansible.builtin.stat:
        path:                          "{{ target_media_location }}/SWPM/sapinst"
      register:                        sapinst_found

    - name:                            "ErrorHandling"
      ansible.builtin.fail:
        msg:                           "INSTALL:0003:Unable to find sapinst, please check that the installation media is mounted"
      when: not sapinst_found.stat.exists

    - name:                            "SCS HA Install: Template processing - Create ini file {{ sap_inifile }} from {{ sap_inifile_template }}"
      ansible.builtin.template:
        src:                           "{{ sap_inifile_template }}"
        dest:                          "{{ dir_params }}/{{ sap_inifile }}"
        mode:                          0644
        force:                         true
      vars:
        sap_scs_hostname:              "{{ scs_virtual_hostname }}"
        param_directory:               "{{ dir_params }}"
        set_ascsInstallGateway:        "{{ installGateway | bool | lower }}"
        set_ascsInstallWebDispatcher:  "{{ ascsInstallWebDispatcher | default(false) | bool | lower }}"

    - name:                            "SCS HA Install: check if installed"
      ansible.builtin.debug:
        msg:                           "SCS HA Install flag: {{ scs_installed }}"
        verbosity:                     2

    - name:                            "SCS HA Install: install variables"
      ansible.builtin.debug:
        msg:
          - "INIFILE:    {{ sap_inifile }}"
          - "PRODUCT ID: {{ bom.product_ids.scs_ha }}"
          - "HOST:       {{ scs_virtual_hostname }}"
        verbosity:                     2

    - name:                            "SCS HA Install: SCS - Assign ownership"
      ansible.builtin.file:
        path:                          "{{ item.path }}"
        owner:                         "{{ sid_to_be_deployed.sid | lower }}adm"
        group:                         sapsys
        recurse:                       true
        state:                         directory
      loop:
        - { path: '/sapmnt/{{ sid_to_be_deployed.sid | upper }}' }
        - { path: '/usr/sap/{{ sid_to_be_deployed.sid | upper }}/SYS' }
        - { path: '/usr/sap/{{ sid_to_be_deployed.sid | upper }}/{{ instance_type }}{{ scs_instance_number }}' }

    - name:                            "SCS HA Install: SCS - comment out required message server ports in /etc/services"
      ansible.builtin.replace:
        path:                          /etc/services
        regexp:                        '^([a-z\-]+\s+36{{ scs_instance_number }}/.*)$'
        replace:                       '# \1'
        backup:                        true

    - name:                            "SCS HA Install: SCS - comment out required message server ports in /etc/services for JAVA"
      ansible.builtin.replace:
        path:                          /etc/services
        regexp:                        '^([a-z\-]+\s+39{{ scs_instance_number }}/.*)$'
        replace:                       '# \1'
        backup:                        true
      when:                            instance_type=='SCS'

    - name:                            "SCS HA Install: SCS Second port for JAVA"
      ansible.builtin.set_fact:
        second_port:                   "{{ scs_instance_number | int }}"

    - name:                            "SCS HA Install: SCS Second port for JAVA"
      ansible.builtin.set_fact:
        second_port_str:               '{{ "%02d" | format(((second_port | int) + 1)) }}'

    - name:                            "SCS HA Install: SCS - comment out required message server ports in /etc/services for JAVA"
      ansible.builtin.replace:
        path:                          /etc/services
        regexp:                        '^([a-z\-]+\s+39{{ second_port_str }}/.*)$'
        replace:                       '# \1'
        backup:                        true
      when:                            instance_type=='SCS'

    # *====================================4=======================================8
    # |  SAP SCS: Install                                                          |
    # | 2230669 - System Provisioning Using a Parameter Input File                 |
    # *====================================4=======================================8

    - name:                            "SCS HA Install: Create temp directory for sid"
      ansible.builtin.file:
        path:                          "{{ tmp_directory }}/{{ sid_to_be_deployed.sid | upper }}"
        state:                         directory
        mode:                          0755

    - name:                            "SCS HA Installation"
      block:

        - name:                        "SCS HA Install: Create Symlink"
          ansible.builtin.import_tasks:  ../../../roles-sap-os/2.10-sap-notes/tasks/2.10.3119751.yaml
          when:
            - ansible_os_family == 'RedHat'

        - name:                        "SCS HA Install: Start"
          ansible.builtin.debug:
            msg:                       "Starting sapinst for {{ sid_to_be_deployed.sid | upper }}. Please wait"

        - name:                        "SCS HA Install: SAPInst"
          ansible.builtin.shell: |
                                       umask {{ custom_umask | default('022') }} ;
                                       ./sapinst SAPINST_INPUT_PARAMETERS_URL={{ dir_params }}/{{ sap_inifile }}     \
                                                 SAPINST_EXECUTE_PRODUCT_ID={{ bom.product_ids.scs_ha }}             \
                                                 SAPINST_SKIP_DIALOGS=true                                           \
                                                 SAPINST_START_GUISERVER=false                                       \
                                                 SAPINST_USE_HOSTNAME={{ scs_virtual_hostname }}                     \
                                                 IS_HOST_LOCAL_USING_STRING_COMPARE=true
          args:
            chdir:                     "{{ target_media_location }}/SWPM"
            creates:                   "/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}/sap_deployment_scs.txt"
          environment:
            SAPSYSTEMNAME:             "{{ sid_to_be_deployed.sid | upper }}"
            TMPDIR:                    "{{ tmp_directory }}/{{ sid_to_be_deployed.sid | upper }}"
          register:                    scs_installation
          failed_when:                 scs_installation.rc > 0
          when:                        node_tier == 'scs'
      rescue:
        # ToDO: cleanup file systems on failed install.
        # until then we validate if certain files and folders exist and take
        # actions based on them
        - name:                        "RESCUE - SCS HA Install: Check '/usr/sap/<SAPSID>/SYS/exe/uc/linuxx86_64' exists"
          ansible.builtin.stat:
            path:                      '/usr/sap/{{ sid_to_be_deployed.sid | upper }}/SYS/exe/uc/linuxx86_64'
          register:                    stat_sys_exe_uc
          when:                        node_tier == 'scs'

        - name:                        "RESCUE - SCS HA Install: Check '/usr/sap/<SAPSID>/SYS/exe/uc/linuxx86_64' is empty"
          ansible.builtin.find:
            path:                      '/usr/sap/{{ sid_to_be_deployed.sid | upper }}/SYS/exe/uc/linuxx86_64'
            patterns:                  "*"
            file_type:                 directory
          register:                    find_sys_exe_uc_result
          when:
            - node_tier == 'scs'
            - stat_sys_exe_uc.stat.exists

        # At this stage the DIR_CT_RUN,'/usr/sap/<SAPSID>/SYS/exe/uc/linuxx86_64'
        # is expected to be empty so we delete it.
        # nw.directoryIsNotEmptyUnattended
        - name:                        "RESCUE - SCS HA Install: Show installation result"
          ansible.builtin.debug:
            var:                       scs_installation
          when:                        scs_installation is defined

        - name:                        "RESCUE - SCS HA Install: Cleanup '/usr/sap/{{ sid_to_be_deployed.sid | upper }}/SYS/exe/uc/linuxx86_64'"
          ansible.builtin.file:
            path:                      "/usr/sap/{{ sid_to_be_deployed.sid | upper }}/SYS/exe/uc/linuxx86_64"
            state:                     absent
          when:
            - node_tier == 'scs'
            - find_sys_exe_uc_result is defined
            - find_sys_exe_uc_result | length > 0

        - name:                        "RESCUE - SCS HA Install: SCS - Assign ownership"
          ansible.builtin.file:
            path:                      "{{ item.path }}"
            owner:                     "{{ sid_to_be_deployed.sid | lower }}adm"
            group:                     sapsys
            recurse:                   true
            state:                     directory
          loop:
            - { path: '/sapmnt/{{ sid_to_be_deployed.sid | upper }}' }
            - { path: '/usr/sap/{{ sid_to_be_deployed.sid | upper }}/SYS' }
            - { path: '/usr/sap/{{ sid_to_be_deployed.sid | upper }}/{{ instance_type }}{{ scs_instance_number }}' }
          when:                        node_tier == 'scs'

        # Add IS_HOST_LOCAL_USING_STRING_COMPARE=true to fix The host with the
        # name <host> is not a valid virtual host on the local host
        # SAP Note# 2279110

        - name:                        "RESCUE - SCS HA Install: SAPINST"
          ansible.builtin.shell: |
                                       umask {{ custom_umask | default('022') }} ;
                                       ./sapinst SAPINST_INPUT_PARAMETERS_URL={{ dir_params }}/{{ sap_inifile }}     \
                                                 SAPINST_EXECUTE_PRODUCT_ID={{ bom.product_ids.scs_ha }}             \
                                                 SAPINST_SKIP_DIALOGS=true                                           \
                                                 SAPINST_START_GUISERVER=false                                       \
                                                 SAPINST_USE_HOSTNAME={{ scs_virtual_hostname }}                     \
                                                 IS_HOST_LOCAL_USING_STRING_COMPARE=true
          args:
            chdir:                     "{{ target_media_location }}/SWPM"
            creates:                   "/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}/sap_deployment_scs.txt"
          environment:
            SAPSYSTEMNAME:             "{{ sid_to_be_deployed.sid | upper }}"
            TMPDIR:                    "{{ tmp_directory }}/{{ sid_to_be_deployed.sid | upper }}"
          register:                    scs_installation
          failed_when:                 scs_installation.rc > 0
          when:                        node_tier == 'scs'

    - name:                            "SCS HA Install: Installation failure results"
      block:
        - name:                        "SCS HA Install: Installation results (debug)"
          ansible.builtin.debug:
            var:                       scs_installation
            verbosity:                 2

        - name:                        "SCS HA Install: Save installation results"
          ansible.builtin.set_fact:
            scs_installation_succeeded: false

        - name:                        "SCS HA Install: Installation results"
          ansible.builtin.fail:
            msg:                       SCS HA installation failed"

      when:                            scs_installation.rc > 0

    - name:                            "SCS HA Install: Installation results"
      block:
        - name:                        "SCS HA Install: Installation results"
          ansible.builtin.debug:
            msg:                       "SCS HA Installation succeeded"

        - name:                        "SCS HA Install: Save installation results"
          ansible.builtin.set_fact:
            scs_installation_succeeded: true

        - name:                        "SCS HA Install: Cleanup ini file {{ ansible_hostname }}"
          ansible.builtin.file:
            path:                      "{{ dir_params }}/{{ sap_inifile }}"
            state:                     absent

        - name:                        "SCS HA Install: flag"
          ansible.builtin.file:
            path:                      "/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}/sap_deployment_scs.txt"
            state:                     touch
            mode:                      0755

      when:                            scs_installation.rc == 0

  when:
    - node_tier == 'scs'
    - not scs_installed.stat.exists

- name:                                "SCS Install status"
  block:

    - name:                            "SCS HA Install: Status"
      ansible.builtin.debug:
        msg:                           "SCS is already installed"

    - name:                            "SCS HA Install: return value"
      ansible.builtin.set_fact:
        scs_already_installed:         true
  when:
    - node_tier == 'scs'
    - scs_installed.stat.exists

- name:                                "SCS HA Install: ENSA1 set defaults"
  ansible.builtin.set_fact:
    ensa1:                             false
  when: ensa1 is not defined

- name:                                "SCS HA Install: ENSA2 set defaults"
  ansible.builtin.set_fact:
    ensa2:                             false
  when: ensa2 is not defined

- name:                                "SCS HA Install: Enqueue Server Setup"
  block:
    - name:                            "SCSERS - Get the variable to determine ENSA version"
      become_user:                     "{{ sid_to_be_deployed.sid | lower }}adm"
      become:                          true
      ansible.builtin.shell:           "{{ sapcontrolscs_command }} -host {{ scs_virtual_hostname }} -function GetProcessList"
      environment:
        PATH:                          /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/sap/{{ sid_to_be_deployed.sid | upper }}/SYS/exe/uc/linuxx86_64:/usr/sap/{{ sid_to_be_deployed.sid | upper }}/SYS/exe/run:/home/{{ sid_to_be_deployed.sid | lower }}adm
        DIR_LIBRARY:                   /usr/sap/{{ sid_to_be_deployed.sid | upper }}/SYS/exe/run
        LD_LIBRARY_PATH:               /usr/sap/{{ sid_to_be_deployed.sid | upper }}/SYS/exe/run:/usr/sap/{ sid_to_be_deployed.sid | upper }}/SYS/exe/uc/linuxx86_64
        SAPSYSTEMNAME:                 "{{ sid_to_be_deployed.sid | upper }}"
      vars:
        allow_world_readable_tmpfiles: true
        ansible_python_interpreter:    "{{ python_version }}"
      register:                        enq_server
      failed_when:                     enq_server.rc > 3
      changed_when:                    false

    # enserver, EnqueueServer
    - name:                            "SCS HA Install: Is ENSA1"
      ansible.builtin.set_fact:
        ensa1:                         "{{ enq_server.stdout_lines | select('search', 'enserver') | length > 0 }}"

    # logic added to identify java based installations
    - name:                            "SCS HA Install: Is ENSA1 - JAVA"
      ansible.builtin.set_fact:
        ensa1:                         "{{ enq_server.stdout_lines | select('search', 'enrepserver') | length > 0 }}"
      when:
        - not ensa1

    # enq_server, Enqueue Server 2
    - name:                            "SCS HA Install: Is ENSA2"
      ansible.builtin.set_fact:
        ensa2:                         "{{ enq_server.stdout_lines | select('search', 'enq_server') | length > 0 }}"

    - name:                            "SCS HA Install: Print the value of ENSA1/ENSA2"
      ansible.builtin.debug:
        msg:
          - "ENSA1: {{ ensa1 }}"
          - "ENSA2: {{ ensa2 }}"
  rescue:
    - name:                            "SCS HA Install: Set default to ENSA2 for HANA if message server is down"
      ansible.builtin.set_fact:
        ensa2:                         true
      when:                            platform == 'HANA'

    - name:                            "SCS HA Install: Set default to ENSA1 for AnyDB if message server is down"
      ansible.builtin.set_fact:
        ensa1:                         true
      when:                            platform != 'HANA'

  when:
    - inventory_hostname == primary_instance_name

- name:                                "SCS HA Install: Set default to ENSA2 for HANA if message server is down"
  ansible.builtin.set_fact:
    ensa2:                             true
  when:
    - platform == 'HANA'
    - scs_bom_instance_type == 'ABAP'
    - not ensa1
    - not ensa2
    - inventory_hostname == primary_instance_name

- name:                                "SCS HA Install: Set default to ENSA for HANA (JAVA) if message server is down"
  ansible.builtin.set_fact:
    ensa1:                             true
  when:
    - platform == 'HANA'
    - scs_bom_instance_type == 'JAVA'
    - not ensa1
    - not ensa2
    - inventory_hostname == primary_instance_name

- name:                                "SCS HA Install: Set default to ENSA1 for AnyDB if message server is down"
  ansible.builtin.set_fact:
    ensa1:                             true
  when:
    - platform != 'HANA'
    - not ensa1
    - not ensa2
    - inventory_hostname == primary_instance_name

- name:                                "SCS HA Install: Print the value of ENSA1/ENSA2"
  ansible.builtin.debug:
    msg:
      - "ENSA1: {{ ensa1 }}"
      - "ENSA2: {{ ensa2 }}"

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