---
-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    -
      name: "Set up facts for long lines"
      ansible.builtin.set_fact:
        war_builder_path: "/war-builder/"
        voltmx_webapps_path: "{{ voltmx_installation_dir }}/tomcat/webapps/"
        voltmx_jar_command: "jar -xvf {{ voltmx_installation_dir }}/tomcat/webapps/"
        voltmx_replace_regexp: >
          (\s+){{ voltmx_port_forwards[0].url }}\.{{ settings.hostname }}\.{{ settings.domain.split(".")[0] }}\.
          {{ settings.domain.split(".")[1] }}:{{ voltmx_port_forwards[0].guest }}(\s+.*)?$     (\s+){{ voltmx_port_forwards[0].url }}\.
          {{ settings.hostname }}\.{{ settings.domain.split(".")[0] }}\.{{ settings.domain.split(".")[1] }}:{{ voltmx_port_forwards[0].guest }}(\s+.*)?$
        voltmx_replace_with: '\1{{ voltmx_port_forwards[0].url }}.{{ settings.hostname }}.{{ settings.domain }}:443\2'
        voltmx_replace_with_proxy: '\1{{ voltmx_port_forwards[1].url }}.{{ settings.hostname }}.{{ settings.domain }}:443\2'
    
    -
      name: "Checking if the VoltMX Installer archive is at /voltmx/archives/{{ voltmx_installer_archive }}"
      register: voltmx_server_installer_check
      ansible.builtin.stat:
        path: "{{ installer_dir }}/voltmx/archives/{{ voltmx_installer_archive }}"
        checksum_algorithm: sha256
    
    -
      name: "Checking if VoltMX has been installed"
      register: voltmx_server_installed
      ansible.builtin.stat:
        path: "{{ completed_dir }}/voltmxsetup"
        checksum_algorithm: sha256
    
    -
      name: "Checking if the VoltMX was extracted extracted installer is in voltmx/core at {{ installer_dir }}"
      register: voltmx_extracted
      ansible.builtin.stat:
        path: "{{ installer_dir }}/voltmx/core"
        checksum_algorithm: sha256
    
    -
      name: "Increase maximum SQL connections"
      ansible.builtin.lineinfile:
        path: /etc/mysql/mariadb.conf.d/50-server.cnf
        regexp: '^#max_connections '
        line: max_connections = 300
    
    -
      name: "Restarting MariaDB"
      ansible.builtin.systemd:
        name: mariadb
        state: restarted
    
    -
      name: "Downloading VoltMX from {{ installer_base_url }} using Bearer token"
      register: voltlsresult_bearer
      ansible.builtin.get_url:
        mode: '0755'
        url: "{{ installer_base_url }}/{{ installer_url_app_path }}/{{ voltmx_installer_archive }}"
        dest: "{{ installer_dir }}/voltmx/archives/{{ voltmx_installer_archive }}"
        headers:
          Authorization: "Bearer {{ secrets.installer_url_bearer }}"
      when: 
        - not voltmx_server_installer_check.stat.exists 
        - not voltmx_server_installed.stat.exists 
        - not voltmx_extracted.stat.exists
        - secrets.installer_url_bearer is defined
        - secrets.installer_url_bearer != ''
      failed_when: false

    -
      name: "Downloading VoltMX from {{ installer_base_url }} using basic auth"
      register: voltlsresult
      until: "voltlsresult is not failed"
      retries: 3
      ansible.builtin.get_url:
        mode: '0755'
        url: "{{ installer_base_url }}/{{ installer_url_app_path }}/{{ voltmx_installer_archive }}"
        dest: "{{ installer_dir }}/voltmx/archives/{{ voltmx_installer_archive }}"
        username: "{{ installer_url_user }}"
        password: "{{ secrets.installer_url_pass }}"
      when: 
        - not voltmx_server_installer_check.stat.exists 
        - not voltmx_server_installed.stat.exists 
        - not voltmx_extracted.stat.exists
        - (secrets.installer_url_bearer is not defined or secrets.installer_url_bearer == '' or voltlsresult_bearer is failed)
    
    -
      name: "Extracting voltmx from {{ voltmx_installer_archive }}"
      when: not voltmx_server_installed.stat.exists or not voltmx_extracted.stat.exists
      ansible.builtin.unarchive:
        src: "{{ installer_dir }}/voltmx/archives/{{ voltmx_installer_archive }}"
        dest: "{{ installer_dir }}/voltmx/core"
        remote_src: true
    
    -
      name: "Grabbing External IP address"
      ansible.builtin.shell: |
        set -o pipefail
        ip addr show public_adapter | awk '/inet / {print $2}' | cut -d'/' -f1
      args:
        executable: /bin/bash
      register: netoutput
      changed_when: false
    
    -
      name: "Adding VoltMX Host Records"
      ansible.builtin.lineinfile:
        path: "/etc/hosts"
        search_string: '{{ netoutput.stdout }}'
        line: >
          {{ netoutput.stdout }} {{ settings.hostname }} {{ settings.hostname }}.{{ settings.domain }}
          console.{{ settings.hostname }}.{{ settings.domain }}
          {{ voltmx_port_forwards[0].url }}.{{ settings.hostname }}.{{ settings.domain }}
    
    -
      name: "Adding Voltmx silent install response file"
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.template:
        dest: "{{ installer_dir }}/voltmx/installer.properties"
        mode: a+x
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
        src: "installer.properties.j2"
    
    -
      name: "Setting Installer to be Executable"
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.file:
        mode: a+x
        path: "{{ item }}"
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
      with_items:
        - "{{ installer_dir }}/voltmx/core/VoltMXFoundry_Installer_Linux/VoltMXFoundryInstaller-9.5.1.1_GA.bin"
    
    -
      name: "Creating VoltMX App Directory"
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.file:
        path: "{{ item }}"
        state: directory
        mode: '0755'
        owner: "{{ service_user }}"
        group: "{{ service_group }}"
      loop:
        - "{{ voltmx_installation_dir }}"
        - /war-builder/
    -
      name: "Copying Original installer.properties to /tmp/installer.properties.orig"
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.copy:
        remote_src: true
        mode: "0644"
        src: "{{ installer_dir }}/voltmx/core/VoltMXFoundry_Installer_Linux/installer.properties"
        dest: /tmp/installer.properties.orig
    
    -
      name: "Removing Original installer.properties"
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.file:
        path: "{{ installer_dir }}/voltmx/core/VoltMXFoundry_Installer_Linux/installer.properties"
        state: absent
    
    -
      name: "Installing VoltMX Via Silent Installer"
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.shell: "{{ item }}"
      become: true
      become_user: "{{ service_user }}"
      args:
        chdir: "{{ installer_dir }}/voltmx/core/VoltMXFoundry_Installer_Linux"
        executable: /bin/bash
        creates: "{{ completed_dir }}/voltmxsetup"
      with_items:
        - "./VoltMXFoundryInstaller-9.5.1.1_GA.bin -f {{ installer_dir }}/voltmx/installer.properties -i silent"
    
    -
      name: "Import SSL certificate into cacerts"
      when: not voltmx_server_installed.stat.exists
      become_user: "{{ service_user }}"
      community.general.java_cert:
        executable: "/local/voltmx-foundry/jre/bin/keytool"
        cert_path: "{{ cert_dir }}/crt/default-signed.crt"
        keystore_path: /local/voltmx-foundry/jre/lib/security/cacerts
        keystore_pass: changeit
        state: present
        cert_alias: "{{ settings.hostname }}.{{ settings.domain }}"
        trust_cacert: true
    
    -
      name: Pause for 60 seconds to let VoltMX fully configure
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.pause:
        seconds: 60
    
    -
      name: "Placing VoltMX Service scripts"
      become: true
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.template:
        owner: "{{ item.owner }}"
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        mode: "a+x"
      loop:
        - { src: 'voltmx.service.j2', dest: '/etc/systemd/system/voltmx.service', owner: '{{ service_user }}' }
    
    -
      name: "Stopping VoltMX via systemd Service"
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.service:
        name: voltmx
        state: stopped
        enabled: true
    
    -
      name: Pause for 60 seconds to let VoltMX fully configure
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.pause:
        seconds: 60
    
    -
      name: "Setting up VoltMX for Reverse Proxy"
      when: not voltmx_server_installed.stat.exists and not voltmx_port_forwards[1].guest is defined
      become: true
      become_user: "{{ service_user }}"
      block:
        -
          name: "Set Tomcat to accept HTTPS proxied connections from HAProxy"
          ansible.builtin.lineinfile:
            path: "{{ voltmx_installation_dir }}/tomcat/conf/server.xml"
            regexp: '<Connector server="HCL" port="{{ voltmx_port_forwards[0].guest }}"'
            line: >
              <Connector server="HCL" port="{{ voltmx_port_forwards[0].guest }}" protocol="HTTP/1.1"
              maxHttpHeaderSize="8192" maxThreads="150" enableLookups="false" acceptCount="25"
              disableUploadTimeout="true" tcpNoDelay="true" compression="on" compressionMinSize="512000"
              compressableMimeType="text/html,text/xml,text/css,text/plain,text/javascript,application/x-javascript,application/javascript,application/json,application/xml"
              connectionTimeout="20000" proxyName="{{ voltmx_port_forwards[1].url }}.{{ settings.hostname }}.{{ settings.domain }}"
              proxyPort="443" scheme="https" secure="true" />
    
        -
          name: "Creating War-Builder"
          ansible.builtin.shell: ls *.war | cut -d '.' -f1 | xargs -I {} bash -c "sudo mkdir -p /war-builder/{} && sudo chmod 777 -R /war-builder/"
          changed_when: false
          args:
            chdir: "{{ voltmx_installation_dir }}/tomcat/webapps/"
    
        -
          name: "Copying orignal war files"
          ansible.builtin.shell: ls *.war | xargs -I {} bash -c "cp {} /war-builder/"
          changed_when: false
          args:
            chdir: "{{ voltmx_installation_dir }}/tomcat/webapps/"
    
        -
          name: "Expanding war files for editing"
          become_user: "{{ service_user }}"
          changed_when: false
          ansible.builtin.shell: |
              ls *.war | cut -d '.' -f1 | xargs -I {} bash -c "sudo mkdir -p {{ war_builder_path }}{} && sudo chmod 777 -R \
              {{ war_builder_path }} && cd {} && {{ voltmx_jar_command }}{}.war"
          args:
            executable: "/bin/bash"
            chdir: "{{ war_builder_path }}"
          environment:
            PATH: "{{ ansible_env.PATH }}:{{ service_home_dir }}/.sdkman/candidates/java/current/bin"
    
        -
          name: "Registering Files with URLs to modify"
          ansible.builtin.shell: |
            grep -rnw voltmx.demo.startcloud | cut -d ':' -f1
          args:
            chdir: "{{ war_builder_path }}"
          changed_when: false
          register: voltmx_urls_to_modify
    
        -
          name: "Replacing URLs"
          ansible.builtin.replace:
            path: "{{ war_builder_path }}{{ item }}"
            regexp: "{{ voltmx_replace_regexp }}"
            replace: "{{ voltmx_replace_with }}"
          loop: voltmx_urls_to_modify.stdout_lines | unique
    
        -
          name: "Creating new war files"
          changed_when: false
          become_user: "{{ service_user }}"
          ansible.builtin.shell: ls *.war | cut -d '.' -f1 | xargs -I {} bash -c "cd {} && jar -cvf {{ voltmx_installation_dir }}/tomcat/webapps/{}.war *"
          args:
            executable: "/bin/bash"
            chdir: /war-builder/
          environment:
            PATH: "{{ ansible_env.PATH }}:{{ service_home_dir }}/.sdkman/candidates/java/current/bin"
    
    -
      name: "Setting up VoltMX for Reverse Proxy"
      when: not voltmx_server_installed.stat.exists and voltmx_port_forwards[1].guest is defined
      become: true
      become_user: "{{ service_user }}"
      block:
        -
          name: "Set Tomcat to accept HTTPS proxied connections from HAProxy"
          ansible.builtin.lineinfile:
            path: "{{ voltmx_installation_dir }}/tomcat/conf/server.xml"
            regexp: '(.*<Connector.*port="{{ voltmx_port_forwards[1].guest }}")(.*\/>)'
            line: '\1 proxyName=\"{{ voltmx_port_forwards[1].url }}.{{ settings.hostname }}.{{ settings.domain }}\" proxyPort=\"443\" \2'
    
        -
          name: "Creating War-Builder"
          changed_when: false
          ansible.builtin.shell: ls *.war | cut -d '.' -f1 | xargs -I {} bash -c "sudo mkdir -p /war-builder/{} && sudo chmod 777 -R /war-builder/"
          args:
            chdir: "{{ voltmx_installation_dir }}/tomcat/webapps/"
    
        -
          name: "Copying orignal war files"
          changed_when: false
          ansible.builtin.shell: ls *.war | xargs -I {} bash -c "cp {} /war-builder/"
          args:
            chdir: "{{ voltmx_installation_dir }}/tomcat/webapps/"
    
        -
          name: "Expanding war files for editing"
          become_user: "{{ service_user }}"
          changed_when: false
          ansible.builtin.shell: |
            ls *.war | cut -d '.' -f1 | xargs -I {} bash -c "sudo mkdir -p {{ war_builder_path }}{} && sudo chmod 777 -R \
            {{ war_builder_path }} && cd {} && {{ voltmx_jar_command }}{}.war"
          args:
            executable: "/bin/bash"
            chdir: "{{ war_builder_path }}"
          environment:
            PATH: "{{ ansible_env.PATH }}:{{ service_home_dir }}/.sdkman/candidates/java/current/bin"
    
        -
          name: "Registering Files with URLs to modify"
          ansible.builtin.shell: |
            grep -rnw voltmx.demo.startcloud | cut -d ':' -f1
          args:
            chdir: /war-builder/
          changed_when: false
          register: voltmx_urls_to_modify
    
        -
          name: "Replacing URLs"
          ansible.builtin.replace:
            path: "{{ war_builder_path }}{{ item }}"
            regexp: "{{ voltmx_replace_regexp }}"
            replace: "{{ voltmx_replace_with }}"
          loop: voltmx_urls_to_modify.stdout_lines | unique
    
        -
          name: "Creating new war files"
          become_user: "{{ service_user }}"
          ansible.builtin.shell: ls *.war | cut -d '.' -f1 | xargs -I {} bash -c "cd {} && jar -cvf {{ voltmx_installation_dir }}/tomcat/webapps/{}.war *"
          args:
            executable: "/bin/bash"
            chdir: /war-builder/
          changed_when: false
          environment:
            PATH: "{{ ansible_env.PATH }}:{{ service_home_dir }}/.sdkman/candidates/java/current/bin"
    
    -
      name: "Getting process IDs of the process in case of any lingering services"
      when: not voltmx_server_installed.stat.exists
      community.general.pids:
        name: java
      register: pids_of_java
    
    -
      name: "Printing the process IDs obtained"
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.debug:
        msg: "PIDS of java:{{ pids_of_java.pids | join(',') }}"
    
    -
      name: "Starting VoltMX via systemd Service"
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.service:
        name: voltmx
        state: started
        enabled: true
    
    -
      name: "Marking VoltMX as Installed"
      when: not voltmx_server_installed.stat.exists
      ansible.builtin.file:
        path: "{{ completed_dir }}/voltmxsetup"
        mode: "0644"
        state: touch
    
    -
      name: "Checking VoltMX is listening on port {{ voltmx_port_forwards[0].guest }}"
      ansible.builtin.wait_for:
        port: "{{ voltmx_port_forwards[0].guest }}"
        delay: 5
        timeout: 60
        msg: "Timeout waiting for {{ voltmx_port_forwards[0].guest }} to respond"
      register: port_check
