# This playbook will run the update_index management command. It is intended solely for usage by services using
# Haystack.
#
# Required variables for this playbook:
#
#   - APPLICATION_PATH                - the top-level path of the Django application; the application lives underneath
#                                       this directory in a directory with the same name as APPLICATION_NAME.
#   - APPLICATION_NAME                - The name of the application that we are migrating.
#   - APPLICATION_USER                - user which is meant to run the application
#   - HIPCHAT_TOKEN                   - API token to send messages to hipchat
#   - HIPCHAT_ROOM                    - ID or name of the room to send the notification
#   - HIPCHAT_URL                     - URL of the hipchat API  (defaults to v1 of the api)
#
# Example command line to run this playbook:
#    ansible-playbook -vvvv -i "localhost," -c local \
#       -e @overrides.yml \
#       update_index.yml
#



- hosts: all
  vars:
    HIPCHAT_URL: https://api.hipchat.com/v2/
    COMMAND_PREFIX: " . {{ APPLICATION_PATH }}/{{ APPLICATION_NAME }}_env; /edx/bin/python.{{ APPLICATION_NAME }} /edx/bin/manage.{{ APPLICATION_NAME }}"
  gather_facts: False
  tasks:

  - name: Update index
    shell: '{{ COMMAND_PREFIX }} update_index'
    become_user: "{{ APPLICATION_USER }}"

  - name: Send Hipchat notification
    hipchat:
      api: "{{ HIPCHAT_URL }}"
      token: "{{ HIPCHAT_TOKEN }}"
      room: "{{ HIPCHAT_ROOM }}"
      msg: "Search index updated."
    ignore_errors: yes
    when: HIPCHAT_TOKEN is defined