---
-
  name: "Creating VoltMX installation directories"
  ansible.builtin.file:
    mode: u=rwX,g=rX,o=rX
    path: "{{ item }}"
    state: directory
    owner: "{{ service_user }}"
    group: "{{ service_group }}"
  with_items:
    - "{{ installer_dir }}"
    - "{{ installer_dir }}/voltmx"
    - "{{ installer_dir }}/voltmx/archives"
    - "{{ installer_dir }}/voltmx/core"

-
  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 at {{ installer_dir }}/voltmx/core"
  register: voltmx_extracted
  ansible.builtin.stat:
    path: "{{ installer_dir }}/voltmx/core/VoltMXFoundry_Installer_Linux"
    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: "Configure MariaDB to accept Remote Connections"
  become: true
  mysql_user:
    name: "{{ mariadb_admin_user }}"
    password: "{{ mariadb_admin_pass }}"
    login_user: "{{ mariadb_admin_user }}"
    login_password: "{{ mariadb_admin_pass }}"
    priv: "*.*:ALL"
    host: "%"
    login_unix_socket: /var/run/mysqld/mysqld.sock

-
  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: u=rwX,g=rX,o=rX
    validate_certs: false
    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: u=rwX,g=rX,o=rX
    validate_certs: false
    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: "Creating VoltMX installation directories"
  ansible.builtin.file:
    mode: u=rwX,g=rX,o=rX
    path: "{{ item }}"
    state: directory
    owner: "{{ service_user }}"
    group: "{{ service_group }}"
  with_items:
    - "{{ installer_dir }}/voltmx"
    - "{{ installer_dir }}/voltmx/archives"
    - "{{ installer_dir }}/voltmx/core"


-
  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 }} {{ 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/core/installer.properties"
    mode: a+x
    owner: "{{ service_user }}"
    group: "{{ service_group }}"
    src: "installer.properties.j2"

-
  name: "Setting /etc/hosts"
  replace:
    path: "/etc/hosts"
    regexp: "{{ item.regex }}"
    replace: "{{ item.replace }}"
  with_items:
    - { regex: "(^127.0.0.1)(.*)$", replace: '127.0.0.1  localhost '}
    - { regex: "(^127.0.1.1)(.*)$", replace: '127.0.1.1  localhost '}

-
  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/install-foundry.sh"

-
  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 }}"

-
  name: "Installing VoltMX Via Silent Installer"
  when: not voltmx_server_installed.stat.exists
  ansible.builtin.shell: "{{ item }}"
  become: true
  args:
    chdir: "{{ installer_dir }}/voltmx/core/"
    executable: /bin/bash
    creates: "{{ completed_dir }}/voltmxsetup"
  with_items:
    - "./install-foundry.sh installer.properties"


#-
#  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: "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"
    state: touch

-
  name: "Checking VoltMX is listening on port {{ voltmx_port_forwards[0].port }}"
  ansible.builtin.wait_for:
    port: "{{ voltmx_port_forwards[0].port }}"
    delay: 5
    timeout: 60
    msg: "Timeout waiting for {{ voltmx_port_forwards[0].port }} to respond"
  register: port_check
