# This playbook syncs the production bodhi instance with staging.


- name: bring staging services down (httpd)
  hosts: bodhi2_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=httpd state=stopped

- name: bring staging services down (OpenShift web services)
  hosts: os-control01.stg.iad2.fedoraproject.org
  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:
  - command: oc -n bodhi scale dc/bodhi-web --replicas=0
  - command: oc -n bodhi scale dc/bodhi-consumer --replicas=0
  - command: oc -n bodhi scale dc/bodhi-celery --replicas=0

- name: bring staging services down (messaging)
  hosts: 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:
  - service:
      name: "{{ item }}"
      state: stopped
    with_items:
    - fm-consumer@config.service
    - bodhi-celery

# Here's the meaty part in the middle
- name: drop and re-create the staging db entirely
  hosts: db01.stg.iad2.fedoraproject.org
  user: root
  become: yes
  become_user: postgres
  become_method: sudo

  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:
  - copy: src=/srv/web/infra/db-dumps/bodhi2.dump.xz dest=/var/tmp/bodhi2.dump.xz
      owner=postgres group=postgres
  - command: unxz /var/tmp/bodhi2.dump.xz
             creates=/var/tmp/bodhi2.dump
  - command: dropdb bodhi2
  - command: createdb -O bodhi2 bodhi2
  - name: Import the prod db.
    shell: cat /var/tmp/bodhi2.dump | psql bodhi2
  - file: path=/var/tmp/bodhi2.dump state=absent

- name: bring staging services up (OpenShift web services)
  hosts: os-control01.stg.iad2.fedoraproject.org
  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:
  - command: oc -n bodhi scale dc/bodhi-web --replicas=1
  - command: oc -n bodhi scale dc/bodhi-consumer --replicas=1
  - command: oc -n bodhi scale dc/bodhi-celery --replicas=1

- name: bring staging services up (httpd)
  hosts: bodhi2_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=httpd state=started

- name: bring staging services up (messaging)
  hosts: 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:
  - service:
      name: "{{ item }}"
      state: stopped
    with_items:
    - fm-consumer@config.service
    - bodhi-celery
