# This playbook syncs the production fas with staging and manages all
# the steps we need to keep our setup intact.
#

- name: bring staging services down
  hosts: fas01.stg.phx2.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
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"

  tasks:
  - service: name=httpd state=stopped

- name: dump the prod db out
  hosts: db-fas01.phx2.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:
  - name: Dumping the production db. This might take a minute. Go out to the lobby!
    shell: pg_dump -C fas2 |xz -c > /var/tmp/fas2.dump.xz
#
# pull dump from prod to batcave
#
  - fetch:
       src: /var/tmp/fas2.dump.xz
       dest: /var/tmp/
       flat: yes

- name: drop and re-create the staging db entirely
  hosts: db-fas01.stg.phx2.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:
#
# push dump to stg from batcave
#
  - copy:
       src: /var/tmp/fas2.dump.xz
       dest: /var/tmp/fas2.dump.xz

  - command: unxz /var/tmp/fas2.dump.xz
             creates=/var/tmp/fas2.dump

  - command: dropdb fas2
  - command: createdb fas2
  - name: Import the prod db.  This will take quite a while.  Go get a snack!
    shell: cat /var/tmp/fas2.dump | psql fas2
  - command: rm -f /var/tmp/fas2.dump /var/tmp/fas2.dump.xz

- name: bring staging services up
  hosts: fas01.stg.phx2.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
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"

  tasks:
  - service: name=httpd state=started

- name: Remove the prod db dump now that we are done
  hosts: db-fas01.phx2.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:
  - name: Remove the db dump on the prod server
    command: rm -f /var/tmp/fas2.dump.xz

- name: Remove the prod db dump on batcave
  hosts: batcave01.phx2.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
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"

  tasks:
#
# delete tmp dump on batcave01
#
  - command: rm -f /var/tmp/fas2.dump.xz
