# Utility playbook to upgrade pagure in stg and prod.
# You can use it to update stg by simply calling ``-l pagure_stg`` to your
# ansible command.
# To specify that you want to use the testing repo specify ``-e "testing=True"``
# to your ansible command.

# Main task to upgrade pagure
- name: upgrade pagure
  hosts: pagure:pagure_stg
  user: root
  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  vars:
    testing: False
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"

  pre_tasks:
  - name: tell nagios to shush w.r.t. the frontend
    nagios:
        action=downtime
        minutes=15
        service=host
        host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.iad2.fedoraproject.org
    ignore_errors: true

##
## To do here. Call a play which can set status to say pagure is down.
##

  tasks:
  - name: yum update pagure packages from main repo
    yum: name="pagure*"
         state=latest
         update_cache=yes
    when: not testing

  - name: yum update pagure* packages from testing repo
    yum: name="pagure*"
         state=latest
         enablerepo=infrastructure-tags-stg
         update_cache=yes
    when: testing

  # This is needed so the alembic will work.
  - name: stop the general pagure workers
    service:
      name: "{{ item }}"
      state: stopped
    with_items:
      - httpd
      - pagure_ev
      - pagure_gitolite_worker
      - pagure_logcom
      - pagure_milter
      - pagure_webhook
      - pagure_worker

  - name: stop the specific workers on pagure.io
    service:
      name: "{{ item }}"
      state: stopped
    with_items:
      - pagure_ci
      - pagure_fast_worker
      - pagure_loadjson
      - pagure_logcom
      - pagure_medium_worker
      - pagure_mirror
      - pagure_slow_worker
    when: inventory_hostname.startswith('pagure')

  - name: Upgrade the database
    command: /usr/bin/alembic -c /etc/pagure/alembic.ini upgrade head
    args:
      chdir: /etc/pagure/
    environment:
        PAGURE_CONFIG: /etc/pagure/pagure.cfg

  post_tasks:

  - name: start the general pagure workers
    service:
      name: "{{ item }}"
      state: started
    with_items:
      - httpd
      - pagure_ev
      - pagure_gitolite_worker
      - pagure_logcom
      - pagure_milter
      - pagure_webhook
      - pagure_worker

  - name: start_pagure_workers on pagure servers
    service:
      name: "{{ item }}"
      state: started
    with_items:
      - pagure_ci
      - pagure_fast_worker
      - pagure_loadjson
      - pagure_logcom
      - pagure_medium_worker
      - pagure_mirror
      - pagure_slow_worker
    when: inventory_hostname.startswith('pagure')

##
## ToDo: Put in an include to pull in setting status back
## 


  - name: tell nagios to unshush w.r.t. the frontend
    nagios:
        action=unsilence
        service=host
        host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.iad2.fedoraproject.org
    ignore_errors: true
