---
# file: roles/sonarqube/tasks/configure.yml

- name: "init vars"
  ansible.builtin.set_fact:
    sonar_ce_javaOpts: ""
    sonar_es_javaOpts: ""
    sonar_web_javaOpts: ""
    sonar_ce_javaAdditionalOpts: ""

- name: "init javaOpts vars"
  ansible.builtin.set_fact:
    sonar_ce_javaOpts: "{{ sonar_ce_javaOpts + '-Xms' + sonarqube_java_heap_size + ' -Xmx' + sonarqube_java_heap_size }}"
    sonar_es_javaOpts: "{{ sonar_es_javaOpts + '-Xms' + sonarqube_java_heap_size + ' -Xmx' + sonarqube_java_heap_size }}"
    sonar_web_javaOpts: "{{ sonar_web_javaOpts + '-Xms' + sonarqube_java_heap_size + ' -Xmx' + sonarqube_java_heap_size }}"
  when: sonarqube_java_heap_size is defined

- name: "sonar_ce_javaOpts append jmx config"
  ansible.builtin.set_fact:
    sonar_ce_javaOpts: "{{ sonar_ce_javaOpts + ' ' + sonarqube_ce_jmx_config }}"

- name: "sonar_web_javaAdditionalOpts init with jmx config"
  ansible.builtin.set_fact:
    sonar_web_javaAdditionalOpts: "{{ sonarqube_web_jmx_config }}"

# IT-9595
- name: "append sonar_web_javaAdditionalOpts"
  ansible.builtin.set_fact:
    sonar_web_javaAdditionalOpts: "{{ sonar_web_javaAdditionalOpts + ' -javaagent:/opt/sonarqube/stateful/extensions/plugins/sonarqube-community-branch-plugin-' + sonarqube_community_branch_plugin_version + '.jar=web' }}"
  when: sonarqube_community_branch_plugin_version is defined

# IT-9595
- name: "append sonar_web_javaAdditionalOpts"
  ansible.builtin.set_fact:
    sonar_web_javaAdditionalOpts: "{{ sonar_web_javaAdditionalOpts + ' -javaagent:/opt/sonarqube/stateful/extensions/plugins/sonarqube-community-branch-plugin-' + sonarqube_talas_community_branch_plugin_version + '.talas.jar=web' }}"
  when: sonarqube_talas_community_branch_plugin_version is defined

# IT-9595
- name: "append sonar_ce_javaAdditionalOpts"
  ansible.builtin.set_fact:
    sonar_ce_javaAdditionalOpts: "{{ sonar_ce_javaAdditionalOpts + ' -javaagent:/opt/sonarqube/stateful/extensions/plugins/sonarqube-community-branch-plugin-' + sonarqube_community_branch_plugin_version + '.jar=ce' }}"
  when: sonarqube_community_branch_plugin_version is defined

# IT-9595
- name: "append sonar_ce_javaAdditionalOpts"
  ansible.builtin.set_fact:
    sonar_ce_javaAdditionalOpts: "{{ sonar_ce_javaAdditionalOpts + ' -javaagent:/opt/sonarqube/stateful/extensions/plugins/sonarqube-community-branch-plugin-' + sonarqube_talas_community_branch_plugin_version + '.talas.jar=ce' }}"
  when: sonarqube_talas_community_branch_plugin_version is defined

- name: "manage /opt/sonarqube/stateless/conf/sonar.properties"
  ansible.builtin.template:
    src: "sonar.properties.j2"
    dest: "/opt/sonarqube/stateless/conf/sonar.properties"
    owner: sonarqube
    group: sonarqube
    backup: true
  notify: restart sonarqube

- name: "{{ nexus_basedir }}/engine/etc/jmxremote.access {{ nexus_basedir }}/engine/etc/jmxremote.password"
  template:
    src: "{{ item }}.j2"
    dest: "/opt/sonarqube/stateless/conf//{{ item }}"
    owner: sonarqube
    group: sonarqube
    mode: 0400
    backup: true
  loop:
    - jmxremote.access
    - jmxremote.password
  notify: restart sonarqube
