- name: check to see if a compose 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 running composes
    shell: "curl https://bodhi{{env_suffix}}.fedoraproject.org/composes/"
    register: composes

  - name: Fail if we found that a compose 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: update bodhi-server packages from main repo
    dnf:
      name:
      - bodhi-server
      - bodhi-composer
      state: latest
      update_cache: true
  - name: update bodhi-server packages from testing repo
    dnf:
      name:
      - bodhi-server
      - bodhi-composer
      state: latest
      enablerepo: infrastructure-tags-stg
      update_cache: true
    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_control[0]:os_control_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.iad2.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 }}.iad2.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.iad2.fedoraproject.org
    ignore_errors: true

  roles:
  - bodhi2/backend

  tasks:
  - name: Stop the backend services
    service:
        name: "{{ item }}"
        state: stopped
    with_items:
    - fm-consumer@config.service
    - bodhi-celery

  - name: Upgrade the database
    command: /usr/bin/alembic-3 -c /etc/bodhi/alembic.ini upgrade head
    args:
      chdir: /usr/share/bodhi/

  - name: Start the backend services
    service:
        name: "{{ item }}"
        state: started
    with_items:
    - fm-consumer@config.service
    - bodhi-celery

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

- name: restart the frontend
  hosts: os_control[0]:os_control_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={{ bodhi_openshift_pods }}
    when: env != "staging"
  - name: Scale up pods
    command: oc -n bodhi scale dc/bodhi-web --replicas={{ bodhi_openshift_pods }}
    when: env == "staging"
  # Redeploy the build config as it may request a newer version in the Dockerfile
  # Only do it on prod because staging tracks a branch
  - import_role:
      name: openshift/object
    vars:
      app: bodhi
      template: buildconfig.yml
      objectname: buildconfig.yml
    when: env != "staging"
  # Start a build with the new buildconfig
  # Only do it on prod because staging builds are triggered by Github's webhook
  - import_role:
      name: openshift/start-build
    vars:
      app: bodhi
      buildname: bodhi-base
    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.iad2.fedoraproject.org
    ignore_errors: true
