# This playbook syncs the production koji instance with staging and manages all
# the steps we need to keep our setup intact.
#
# For a description of what we're doing, see
#   https://lists.fedoraproject.org/pipermail/infrastructure/2015-June/016377.html
# For a description of the koji 'secondary volumes' feature, see
#   https://lists.fedoraproject.org/pipermail/buildsys/2012-May/003892.html
# For a description of the sql migration we do, see
#   https://lists.fedoraproject.org/pipermail/buildsys/2015-June/004779.html


- name: grab the latest production backup
  hosts: db-koji01.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:
  - include: "{{ handlers }}/restart_services.yml"

  tasks:
  - fetch: src=/backups/koji-{{ansible_date_time['date']}}.dump.xz
           dest=/var/tmp/prod-koji-dump/
           fail_on_missing=yes

- name: sync config and sql migration script
  hosts: koji-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:
  - include: "{{ handlers }}/restart_services.yml"

  roles:
  - koji_hub

- name: bring staging services down
  hosts: koji-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:
  - include: "{{ handlers }}/restart_services.yml"

  tasks:
  - service: name=httpd state=stopped
  - service: name=kojid state=stopped
  - service: name=kojira state=stopped


- name: drop and re-create the staging db entirely
  hosts: db01.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:
  - include: "{{ handlers }}/restart_services.yml"

  tasks:
  - template: src=templates/koji-reset-staging.sql dest=/var/lib/pgsql/koji-reset-staging.sql
  - copy: src=/var/tmp/prod-koji-dump/db-koji01.phx2.fedoraproject.org/backups/koji-{{ansible_date_time['date']}}.dump.xz
          dest=/var/tmp/koji-{{ansible_date_time['date']}}.dump.xz
          owner=postgres group=postgres
  - command: unxz /var/tmp/koji-{{ansible_date_time['date']}}.dump.xz
             creates=/var/tmp/koji-{{ansible_date_time['date']}}.dump
  - command: dropdb koji
  - command: createdb -O koji koji
  - name: Import the prod db.  This will take quite a while.  Go get a snack!
    shell: cat /var/tmp/koji-{{ansible_date_time['date']}}.dump | psql koji
  - name: repoint all the prod rpm entries at the secondary volume (and other stuff)
    shell: psql koji < /var/lib/pgsql/koji-reset-staging.sql

# TODO -- nuke old staging content in /mnt/fedora_koji/koji/

- name: bring staging services up
  hosts: koji-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:
  - include: "{{ handlers }}/restart_services.yml"

  tasks:
  - service: name=httpd state=started
  - service: name=kojid state=started
  - service: name=kojira state=started

- name: Nuke the prod db dump that we cached on lockbox
  hosts: lockbox
  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: Nuke the prod db dump that we cached on lockbox
    file: dest=/var/tmp/prod-koji-dump/ state=absent
