# 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: 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:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"

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


- name: drop and re-create the staging db entirely
  hosts: db-koji01.stg.iad2.fedoraproject.org
  user: root
  become: yes
  become_user: postgres
  become_method: sudo
  tags:
  - db-sync
  vars:
  - builder_groups:
    - name: buildvm_stg
      arches: i386 x86_64
      extra_channels:
      - container
      - createrepo
      - livemedia
    - name: buildvm_aarch64_stg
      arches: aarch64
    - name: buildvm_ppc64le_stg
      arches: ppc64le
    - name: buildvm_s390x_stg
      arches: s390x
  # Users allowed to use content generators, only in staging
  - cg_users:
    - user_name: mbs/mbs.stg.fedoraproject.org
      cg_name: module-build-service
    - user_name: obudai
      cg_name: osbuild

  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:
  - template: src=templates/koji-reset-staging.sql dest=/var/lib/pgsql/koji-reset-staging.sql
    tags:
    - db-sync-script

  - postgresql_user: name=koji password={{ kojiPassword }}
  - postgresql_user: name=backup
  - postgresql_db: name=koji state=absent
# buildroot_listing is excluded from the sync to save some time
# TODO: install mbuffer on db-koji.stg
  - name: Import the prod db.  This will take quite a while.  Go get a snack!
    shell: >
      curl -s -S https://infrastructure.fedoraproject.org/infra/db-dumps/koji.dump.xz |
      mbuffer -q -s 8M -m 128M -l /tmp/mbuffer-curl-to-xz.log |
      xzcat |
      mbuffer -q -s 16M -m 256M -l /tmp/mbuffer-xz-to-sed.log |
      sed '/COPY buildroot_listing /,/\./d' |
      mbuffer -q -s 16M -m 512M -l /tmp/mbuffer-sed-to-psql.log |
      psql -v ON_ERROR_STOP=1
  - name: repoint all the prod rpm entries at the secondary volume (and other stuff)
    shell: psql -1 -v ON_ERROR_STOP=1 koji </var/lib/pgsql/koji-reset-staging.sql
  - name: vacuum database
    shell: psql -v ON_ERROR_STOP=1 koji <<<"VACUUM ANALYZE"

- 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:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"
  tags:
  - wipe-fs

  tasks:
  - name: stop nfs-server
    service: name=nfs-server state=stopped
  - name: unmount /mnt/fedora_koji/koji/ostree
    mount: name=/mnt/fedora_koji/koji/ostree state=unmounted
  - name: unmount /mnt/fedora_koji/koji/compose/ostree
    mount: name=/mnt/fedora_koji/koji/compose/ostree state=unmounted
  - name: unmount /mnt/fedora_koji
    mount: name=/mnt/fedora_koji state=unmounted
  - name: wipe fedora_koji volume
    filesystem: dev=/dev/GuestVolGroup00/fedora_koji fstype=xfs force=yes
  - name: mount newly-created empty koji filesystem
    mount: name=/mnt/fedora_koji src=/dev/GuestVolGroup00/fedora_koji fstype=xfs state=mounted
  - name: recreate koji directory structure
    file: name={{item}} state=directory owner=apache group=apache setype=httpd_sys_rw_content_t
    with_items:
    - /mnt/fedora_koji/koji
    - /mnt/koji/compose
    - /mnt/koji/compose/updates
    - /mnt/koji/packages
    - /mnt/koji/repos
    - /mnt/koji/repos-dist
    - /mnt/koji/scratch
    - /mnt/koji/vol
    - /mnt/koji/work
# TODO recreate directories under /mnt/koji/compose (which ones?) that composer expects to exist
  - name: symlink production volume
    file: src=/mnt/fedora_koji_prod/koji dest=/mnt/koji/vol/prod state=link
  - name: restart nfs-server
    service: name=nfs-server state=started
  - name: restart httpd
    service: name=httpd state=started
  - name: restart kojira
    service: name=kojira state=started

- name: remount NFS on builders, composers, bodhi and odcs
  hosts: builders_stg:releng_compose_stg:bodhi_backend_stg:odcs_backend_stg:odcs_frontend_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"
  tags:
  - remount-nfs

  tasks:
  - name: remount koji nfs
    shell: umount /mnt/fedora_koji && mount /mnt/fedora_koji
    args:
      warn: false
    when: "'.s390.' not in inventory_hostname"

- name: restart kojid on staging builders
  hosts: builders_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"
  tags:
  - restart-kojid

  tasks:
  - name: restart kojid
    service: name=kojid state=restarted

# TODO run createrepo for selected tags
#     stgkoji regen-repo --nowait f29-build
# TODO include playbooks to sync apps that use koji (koschei/bodhi/mbs/...)
