---

- name: Stop the service during upgrades
  service: name=elasticsearch state=stopped
  when: es_install_mode == 'upgrade'
  tags: es

- name: Stop seafile to free elasticsearch port
  service: name=seafile state=stopped
  when: es_seafile_bundled_es.stat.exists and es_seafile_service.stat.exists
  tags: es

- when: es_install_mode != 'none'
  block:
    - name: Download Elasticsearch {{ es_version }}
      get_url:
        url: "{{ es_archive_url }}"
        dest: "{{ es_root_dir }}/tmp"
        checksum: sha512:{{ es_archive_sha512 }}

    - name: Extract the archive
      unarchive:
        src: "{{ es_root_dir }}/tmp/elasticsearch-{{ es_version }}-linux-x86_64.tar.gz"
        dest: "{{ es_root_dir }}/tmp"
        remote_src: True

    - name: Move Elasticsearch to its final dir
      synchronize:
        src: "{{ es_root_dir }}/tmp/elasticsearch-{{ es_version }}/"
        dest: "{{ es_root_dir }}/app/"
        delete: True
        compress: False
      delegate_to: "{{ inventory_hostname }}"

    - name: Populate config dir
      synchronize:
        src: "{{ es_root_dir }}/tmp/elasticsearch-{{ es_version }}/config/"
        dest: "{{ es_root_dir }}/etc/"
        delete: True
        compress: False
      delegate_to: "{{ inventory_hostname }}"

    - name: Set permissions
      shell: |
        {% if ansible_selinux.status == 'enabled' %}
        restorecon -R {{ es_root_dir }}
        {% endif %}
        chown -R root:root {{ es_root_dir }}/app
        chown -R :{{ es_user }} {{ es_root_dir }}/etc
        chown -R {{ es_user }}:{{ es_user }} {{ es_root_dir }}/data/*
        find {{ es_root_dir }}/etc -type d -exec chmod 770 "{}" \;
        find {{ es_root_dir }}/etc -type f -exec chmod 660 "{}" \;
        find {{ es_root_dir }}/app -type d -exec chmod 755 "{}" \;
        find {{ es_root_dir }}/app -type f -exec chmod 644 "{}" \;
        find {{ es_root_dir }}/app/jdk/bin -type f -exec chmod 755 "{}" \;
        chmod 755 {{ es_root_dir }}/app/jdk/lib/jspawnhelper
        find {{ es_root_dir }}/app/bin -type f -exec chmod 755 "{}" \;
        find {{ es_root_dir }}/app/modules/x-pack-ml/platform/linux-x86_64/bin/ -type f -exec chmod 755 "{}" \;
      args:
        warn: False

  tags: es

- name: Deploy pre and post backup script
  template: src={{ item }}-backup.j2 dest=/etc/backup/{{ item }}.d/es mode=750
  loop:
    - pre
    - post
  tags: es

- name: Deploy tmpfile fragment
  template: src=tmpfiles.conf.j2 dest=/etc/tmpfiles.d/elasticsearch.conf
  register: es_tmpfiles
  tags: es

- name: Create tmpfiles
  command: systemd-tmpfiles --create
  when: es_tmpfiles.changed
  tags: es

- name: Deploy systemd unit
  template: src=elasticsearch.service.j2 dest=/etc/systemd/system/elasticsearch.service
  register: es_unit
  notify: restart elasticsearch
  tags: es

- name: Remove previous systemd unit snippet
  file: path=/etc/systemd/system/elasticsearch.service.d/ansible.conf state=absent
  register: es_unit_snip
  notify: restart elasticsearch
  tags: es

- name: Reload systemd
  systemd: daemon_reload=True
  when: es_unit.changed or es_unit_snip.changed
  tags: es

