- name: Verify the badges backend and stop it
  hosts:
   - badges_backend
   - badges_backend_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
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"
  pre_tasks:
  - name: tell nagios to shush
    nagios: action=downtime minutes=120 service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.iad2.fedoraproject.org
    ignore_errors: true
  roles:
  - badges/backend
  tasks:
  - service: name="fedmsg-hub" state=stopped

- name: Stop datagrepper
  hosts: 
    - os_masters[0]
    - os_masters_stg[0]
  user: root
  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - /srv/private/ansible/vars.yml
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"
  tasks:
  - name: Scale down datagrepper to 0 pods
    command: oc -n datagrepper scale dc/datagrepper --replicas=0

- name: Stop datanommer
  hosts:
    - os_masters[0]
    - os_masters_stg[0]
  user: root
  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - /srv/private/ansible/vars.yml
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"
  tasks:
  - name: Scale down datanommer to 0 pods
    command: oc -n datanommer scale dc/datanommer --replicas=0

- name: Upgrade the database
  role: openshift/object
  app: datanommer
  template: job.yml
  objectname: job.yml

- name: Wait for the db-upgrade completion
  command: oc get jobs/db-upgrade -o jsonpath='{@.status.succeeded}'
  register: status
  until: status.stdout | int == 1
  retries: 5
  delay: 30

- name: Delete the job in case it finished
  role: openshift/object-delete
  app: datanommer
  objecttype: job
  objectname: db-upgrade
  when: status.stdout | int == 1

- name: Start the datanommer again
  hosts:
    - os_masters[0]
    - os_masters_stg[0]
  user: root
  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - /srv/private/ansible/vars.yml
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  tasks:
  - name: Scale up datanommer pods
    command: oc -n datanommer scale dc/db-datanommer --replicas=1

- name: Start the datagrepper again
  hosts:
    - os_masters[0]
    - os_masters_stg[0]
  user: root
  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - /srv/private/ansible/vars.yml
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  tasks:
  - name: Scale up datagrepper pods
    command: oc -n datagrepper scale dc/datagrepper --replicas=1

- name: Restart the last backend piece (badges)
  hosts:
  - badges-backend
  - badges-backend-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
  tasks:
  - service: name="fedmsg-hub" state=started
  post_tasks:
  - name: tell nagios to unshush
    nagios: action=unsilence service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.iad2.fedoraproject.org
    ignore_errors: true
