# Nuke and rebuild the xapian search index for fedora-packages
#
# "install_packages_indexer" is a host_var that instructs this to only run on
# one of the multiple packages nodes.  The cache that this rebuilds is shared
# between nodes with gluster.

- name: enter maintenance mode
  hosts: packages:packages_stg
  user: root
  gather_facts: False

  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - "{{ private }}/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml

  tasks:
  - name: tell nagios to shush for these hosts
    nagios: action=downtime minutes=300 service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.iad2.fedoraproject.org
    ignore_errors: true

  - name: stop the cache warmer
    service: name={{item}} state=stopped
    with_items:
    - fedmsg-hub

- name: Rebuild that search index on the side and install it (just staging)
  hosts: packages_stg
  user: root
  become: True
  become_user: apache
  gather_facts: False

  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - "{{ private }}/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml

  tasks:
  - name: Pull in the list of packages from pkgdb.  Go get a snack. (2 hours)
    command: /usr/bin/fcomm-index-packages --index-db-dest=/var/cache/fedoracommunity/packages/xapian --icons-dest /var/cache/fedoracommunity/packages/icons --mdapi-url=https://apps.stg.fedoraproject.org/mdapi --icons-url=https://dl.fedoraproject.org/pub/alt/screenshots 
    when: install_packages_indexer

- name: Rebuild that search index on the side and install it. (just prod)
  hosts: packages
  tags: rebuild-prod-index
  user: root
  become: True
  become_user: apache
  gather_facts: False

  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - "{{ private }}/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml

  tasks:
  - name: Pull in the list of packages from pkgdb.  Go get a snack. (2 hours)
    command: /usr/bin/fcomm-index-packages --index-db-dest=/var/cache/fedoracommunity/packages/xapian --icons-dest /var/cache/fedoracommunity/packages/icons --mdapi-url=https://apps.fedoraproject.org/mdapi --icons-url=https://dl.fedoraproject.org/pub/alt/screenshots 
    when: install_packages_indexer

- name: leave maintenance mode
  hosts: packages:packages_stg
  user: root
  gather_facts: False

  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - "{{ private }}/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml

  tasks:
  - name: Make sure the perms are straight
    file: path=/var/cache/fedoracommunity/packages/ state=directory owner=apache group=fedmsg mode="g+rw" recurse=yes

  - name: Restart the cache worker
    service: name={{item}} state=started
    with_items:
    - fedmsg-hub

  - name: tell nagios to start bothering us again
    nagios: action=unsilence service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.iad2.fedoraproject.org
    ignore_errors: true
