- name: Kill elasticsearch process
  become: yes
  shell: |
    set -o pipefail
    ps -ef | grep -v grep | grep -w elasticsearch | awk '{print $2}' | while read curr_ps_id
    do
        echo "process ${curr_ps_id} still running"
        echo "$(ps -ef | grep -v grep | grep ${curr_ps_id})"
        kill -9 ${curr_ps_id}
    done
  args:
    executable: /bin/bash
  changed_when: false

- name: Find searchguard bundle for removal
  become: yes
  find:
    paths: "{{ insight_home }}/app/third-party/elasticsearch/"
    patterns: "^search-guard-.+\\.zip$"
    use_regex: yes
    file_type: file
  register: check_searchguard_bundle_result

- name: Remove searchguard plugin
  become: yes
  become_user: elasticsearch
  ignore_errors: yes
  shell: |
    {{ insight_es_script_path }}/elasticsearch-plugin remove {{ check_searchguard_bundle_result.files[0].path }}
  environment:
    ES_JAVA_HOME: "{{ insight_es_java_home }}"
    ES_PATH_CONF: "{{ insight_es_conf_base }}/config"
  register: remove_searchguard_result
  when: check_searchguard_bundle_result.matched == 1

- name: Delete elasticsearch home dir
  become: yes
  file:
    path: "{{ insight_es_home }}"
    state: absent

- name: Create elasticsearch home dir
  become: yes
  file:
    path: "{{ insight_es_home }}"
    state: directory
    owner: elasticsearch
    group: elasticsearch
    mode: 0755

- name: Find elasticsearch package
  become: yes
  find:
    paths: "{{ insight_home }}/app/third-party/elasticsearch"
    patterns: "^elasticsearch-.+\\.tar.gz$"
    use_regex: yes
    file_type: file
  register: check_elasticsearch_package_result

- name: Set elasticsearch package file name
  set_fact:
    insight_elasticsearch_package: "{{ check_elasticsearch_package_result.files[0].path }}"
  when: check_elasticsearch_package_result.matched > 0

- name: Extract elasticsearch package
  become: yes
  unarchive:
    src: "{{ insight_elasticsearch_package }}"
    dest: "{{ insight_es_home }}"
    remote_src: yes
    extra_opts:
      - --strip-components=1
      - --exclude=config
    owner: elasticsearch
    group: elasticsearch
    creates: "{{ insight_es_java_home }}"
  register: unarchive_result
  when: check_elasticsearch_package_result.matched > 0

- name: Generate HA elasticsearch.yml template file
  become: yes
  template:
    src: templates/ha/{{ insight_ha_node_type }}.elasticsearch.yml.j2
    dest: "{{ insight_es_conf_base }}/elasticsearch.yml"
    owner: elasticsearch
    group: elasticsearch
    mode: 0644
  when: unarchive_result.extract_results.rc | default(128) == 0

- name: Create empty unicast_hosts.txt file
  become: yes
  file:
    path: "{{ insight_es_conf_base }}/unicast_hosts.txt"
    state: touch
    owner: elasticsearch
    group: elasticsearch
    mode: 0644

- name: Upgrade searchguard plugin
  import_tasks: upgrade-searchguard.yml

- name: Start elasticsearch
  become: yes
  become_user: elasticsearch
  shell: |
    nohup {{ insight_es_script_path }}/elasticsearch -d
  environment:
    ES_JAVA_HOME: "{{ insight_es_java_home }}"
    ES_PATH_CONF: "{{ insight_es_conf_base }}/"
  when: unarchive_result.extract_results.rc | default(128) == 0
  register: start_elastcsearch

- name: Wait for elasticsearch to start
  pause:
    seconds: 30
  when: start_elasticsearch.changed

- name: Check if elasticsearch is running
  wait_for:
    host: localhost
    port: "{{ insight_es_transport_port }}"
    delay: 5
    connect_timeout: 1

- name: Init searchguard plugin
  become: yes
  become_user: elasticsearch
  shell: |
    ./sgadmin.sh -p {{ insight_es_transport_port }} -cacert root-ca.pem \
    -cert sgadmin.pem -key sgadmin.key -cd {{ insight_es_searchgaurd_home }}/sgconfig/ -nhnv -icl
  args:
    chdir: "{{ insight_es_searchgaurd_home }}/tools/"
  environment:
    JAVA_HOME: "{{ insight_es_java_home }}"
  register: install_searchguard_result
  when: check_searchguard_bundle_result.matched == 1
