- name: check to see if a mash is going on before we do anything...
  hosts: bodhi-backend:bodhi-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:
  - include_vars: dir=/srv/web/infra/ansible/vars/all/ ignore_files=README
  - name: Check for the existance of a mashing lock.
    shell: "curl https://bodhi{{env_suffix}}.fedoraproject.org/composes/"
    register: composes

  - name: Fail if we found that a mash was in progress
    fail:
      msg: "There are composes in progress."
    any_errors_fatal: true
    when: "composes.stdout != '{\"composes\": []}' and env != 'staging'"

- name: push packages out
  hosts: bodhi-backend:bodhi-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"

  tasks:
  - name: clean all metadata {%if testing%}(with infrastructure-testing on){%endif%}
    command: dnf clean all {%if testing%} --enablerepo=infrastructure-tags-stg {%endif%}
    check_mode: no
  - name: dnf update bodhi-server packages from main repo
    dnf: name="{{ item }}" state=latest
    with_items:
     - bodhi-docs
     - bodhi-server
  - name: dnf update bodhi-server packages from testing repo
    dnf: name="{{ item }}" state=latest enablerepo=infrastructure-tags-stg
    with_items:
     - bodhi-docs
     - bodhi-server
    when: testing

- name: find out if there are migrations to run
  hosts: bodhi-backend:bodhi-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:
  - name: Find out what the current migration version is
    command: /usr/bin/alembic-3 -c /etc/bodhi/alembic.ini current
    register: current_migration_version

- name: Stop the front end if there are migrations to run
  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"

  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.phx2.fedoraproject.org
    ignore_errors: true

  tasks:
  - set_fact:
      # This will be a bool that indicates whether we need to run migrations or not.
      migrations: "'(head)' not in hostvars['bodhi-backend01{{ env_suffix }}.phx2.fedoraproject.org']['current_migration_version'].stdout"
  - name: Scale down to 0 pods
    command: oc -n bodhi scale dc/bodhi-web --replicas=0
    when: migrations

- name: verify the backends, stop them, and then upgrade the db
  hosts: bodhi-backend:bodhi-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 w.r.t. the backend
    nagios: action=downtime minutes=15 service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.phx2.fedoraproject.org
    ignore_errors: true

  roles:
  - bodhi2/backend

  tasks:
  - name: Stop the fedmsg-hub-3 backend
    service: name="fedmsg-hub-3" state=stopped

  - name: Upgrade the database
    command: /usr/bin/alembic-3 -c /etc/bodhi/alembic.ini upgrade head
    args:
      chdir: /usr/share/bodhi/
    when: inventory_hostname.startswith(('bodhi-backend01.phx2', 'bodhi-backend01.stg.phx2'))

  - name: And... start the backend again
    service: name="fedmsg-hub-3" state=started

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

- name: restart the frontend
  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 up pods
    command: oc -n bodhi scale dc/bodhi-web --replicas={{ hostvars[groups['bodhi2'][0]]['openshift_pods'] }}
    when: env != "staging"
  - name: Scale up pods
    command: oc -n bodhi scale dc/bodhi-web --replicas={{ hostvars[groups['bodhi2-stg'][0]]['openshift_pods'] }}
    when: env == "staging"

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