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

- name:                                "ORACLE Post Processing: Set Variables"
  ansible.builtin.set_fact:
    PAS:                               'pas'
    pga_temp_l4tb_std:                 "{{ ((ansible_memory_mb.real.total * 0.60 ) * 0.2) | round | int }}"
    pga_temp_l4tb_dis:                 "{{ ((ansible_memory_mb.real.total * 0.80 ) * 0.2) | round | int }}"
    # pga_temp_g4tb:                       "{{ ((ansible_memory_mb.real.total*0.85)*0.2) |round |int }}"

# PGA & SGA for RAM < 4TB
- name:                                "ORACLE Post Processing: Set the SGA and PGA Sizes for RAM < 4TB"
  ansible.builtin.set_fact:
    main_mem1:                         "{{ ansible_memory_mb.real.total }}"
    ora_sga:                           "{{ (ansible_memory_mb.real.total * 0.60) | round | int }}"
    ora_pga:                           "{{ [(pga_temp_l4tb_std | int), 4194304] | min }}"

  when:
    - ansible_memory_mb.real.total < 4194304
    - "PAS not in supported_tiers"
- name:                                "ORACLE Post Processing: Set the SGA and PGA Sizes for RAM < 4TB Single Node Deployments"
  ansible.builtin.set_fact:
    main_mem1:                         "{{ ansible_memory_mb.real.total }}"
    ora_sga:                           "{{ (ansible_memory_mb.real.total * 0.75) | round | int }}"
    ora_pga:                           "{{ [(pga_temp_l4tb_std | int), 4194304] | min }}"
  when:
    - ansible_memory_mb.real.total < 4194304
    - "PAS in supported_tiers"

- name:                                "ORACLE Post Processing: Show Main Memory"
  ansible.builtin.debug:
    msg:
      - "Total memory:         {{ main_mem1 }}"
      - "sga_max_size:         {{ ora_sga }}"
      - "pga_aggregate_target: {{ ora_pga }}"

- name:                                "ORACLE Post Processing: Set the SGA and PGA Sizes for RAM > 4TB"
  ansible.builtin.set_fact:
    ora_sga:                           "{{ (ansible_memory_mb.real.total * 0.65) | round | int }}"
    ora_pga:                           "{{ ((ansible_memory_mb.real.total*0.65)*0.2) | round | int }}"
  when:
    - ansible_memory_mb.real.total > 4194304
    - supported_tiers != "pas"
    - supported_tiers == "scs"

# Block to check if a reboot has been performed already after updating SGA & PGS
- name:                                "ORACLE Post Processing: DBLoad: - check if DBLoad is performed for {{ sid_to_be_deployed.sid | upper }}"
  ansible.builtin.stat:
    path:                              "/etc/sap_deployment_automation/{{ sid_to_be_deployed.sid | upper }}/ora_sga_updated.txt"
  register:                            sga_update_status

- name:                                "ORACLE Post Processing: Oracle SGA & PGA: create updatesga.sql"
  become:                              true
  become_user:                         "{{ oracle_user_name }}"
  ansible.builtin.blockinfile:
    create: true
    path: /etc/sap_deployment_automation/{{ db_sid | upper }}/updatesga.sql
    marker_begin: "-- BEGIN"
    marker_end:   "-- END"
    block: |
         ALTER SYSTEM SET sga_max_size={{ ora_sga }}M SCOPE=spfile;
         ALTER SYSTEM SET pga_aggregate_target={{ ora_pga }}M SCOPE=spfile;
         ALTER SYSTEM SET use_large_pages=only SCOPE=spfile;
         alter system set LOCAL_LISTENER="(ADDRESS=(PROTOCOL=TCP)(HOST={{ ansible_hostname }})(PORT=1521))" scope=both;
         SHUTDOWN IMMEDIATE;
         exit;
    mode: '0755'

- name:                                "ORACLE Post Processing: Oracle Startup: create startup.sql"
  become:                              true
  become_user:                         "{{ oracle_user_name }}"
  ansible.builtin.blockinfile:
    create: true
    path: /etc/sap_deployment_automation/{{ db_sid | upper }}/startup.sql
    marker_begin: "-- BEGIN"
    marker_end:   "-- END"
    block: |
         STARTUP;
         exit
    mode: '0755'


- name:                                "ORACLE Post Processing: Oracle SGA Change Execution"
  become:                              true
  become_user:                         "{{ oracle_user_name }}"
  ansible.builtin.shell: |
                                       set -o pipefail
                                       sqlplus / as sysdba @updatesga.sql | tee updatesga.log
  register:                            updatesga_results
  failed_when:                         updatesga_results.rc != 0
  args:
    creates:                           /etc/sap_deployment_automation/{{ db_sid | upper }}/sgaupdated.txt
    chdir:                             /etc/sap_deployment_automation/{{ db_sid | upper }}
    executable:                        /bin/csh


- name:                                "ORACLE Post Processing: Create sgaupdated.txt"
  ansible.builtin.file:
    path:                              /etc/sap_deployment_automation/{{ db_sid | upper }}/sgaupdated.txt
    state:                             touch
    mode:                              '0755'
  when:                                updatesga_results.rc == 0

# Wait for creation of HugePages
# Reboot the VM to avoid the error "ORA-27102: out of memory"

- name:                                "ORACLE Post Processing: DB VM reboot"
  block:

    - name:                            "ORACLE Post Processing: Reboot after the Enabling HugePages"
      ansible.builtin.debug:
        msg:                           "Reboot after the Enabling HugePages"

    - name:                            "ORACLE Post Processing: Reboot after the Enabling HugePages"
      become:                          true
      become_user:                     root
      ansible.builtin.reboot:
        reboot_timeout:                300
      failed_when:                     false

    - name:                            "ORACLE Post Processing: Clear the failed state of hosts"
      ansible.builtin.meta:            clear_host_errors

  # Wait for Connection after reboot
    - name:                            "ORACLE Post Processing: Wait for Connection after reboot"
      ansible.builtin.wait_for_connection:
        delay:                         10
        timeout:                       300
  when:
    - not sga_update_status.stat.exists

- name:                                "ORACLE Post Processing: Start Oracle after SGA Change"
  become:                              true
  become_user:                         "{{ oracle_user_name }}"
  ansible.builtin.shell: |
                                       set -o pipefail
                                       sqlplus / as sysdba @startup.sql | tee startup.log
  register:                            dbstarted_results
  failed_when:                         dbstarted_results.rc != 0
  args:
    creates:                           /etc/sap_deployment_automation/{{ db_sid | upper }}/db_startup_completed.txt
    chdir:                             /etc/sap_deployment_automation/{{ db_sid | upper }}
    executable:                        /bin/csh

# Create a block for starting the oracle listener on primary node
- name:                                "Oracle Post-Processing - start lsnrctl on Primary"
  become:                              true
  become_user:                         "{{ oracle_user_name }}"
  ansible.builtin.shell:               lsnrctl start
  register:                            lsnrctl_start_primary_results
  failed_when:                         lsnrctl_start_primary_results.rc > 1
  args:
    creates:                           /etc/sap_deployment_automation/{{ db_sid | upper }}/lsnrctl_started.txt
    chdir:                             /etc/sap_deployment_automation/{{ db_sid | upper }}
    executable:                        /bin/csh

- name:                                "Oracle Post-Processing: Create lsnrctl_started.txt"
  become:                              true
  become_user:                         "{{ oracle_user_name }}"
  ansible.builtin.file:
    path:                              /etc/sap_deployment_automation/{{ db_sid | upper }}/lsnrctl_started.txt
    state:                             touch
    mode:                              '0755'
  when:
    - lsnrctl_start_primary_results.rc == 0

- name:                                "ORACLE Post Processing: Create db_startup_completed.txt"
  ansible.builtin.file:
    path:                              /etc/sap_deployment_automation/{{ db_sid | upper }}/db_startup_completed.txt
    state:                             touch
    mode:                              '0755'
  when:                                updatesga_results.rc == 0

- name:                                "ORACLE Post Processing: Create sgaupdated.txt"
  ansible.builtin.file:
    path:                              /etc/sap_deployment_automation/{{ db_sid | upper }}/ora_sga_updated.txt
    state:                             touch
    mode:                              '0755'
  when:                                updatesga_results.rc == 0
