---
- name: upgrade copr frontend
  hosts: copr_front_dev_aws:copr_front_aws
  user: root
  gather_facts: True

  vars:
    cache_file: /var/lib/copr/.ansible-copr-frontend-version

  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml

  tasks:
  - name: Generic upgrade tasks for copr servers
    import_tasks: _generic_tasks.yml

  - name: Check for copr-frontend update
    shell: dnf clean expire-cache ; dnf check-update copr-frontend
    register: frontend_has_update
    changed_when: "frontend_has_update.rc != 0"
    failed_when: false

  - name: make httpd stopped
    service: name=httpd state=stopped
    register: httpd_stopped
    when: frontend_has_update.changed

  - name: Upgrade copr-frontend packages
    dnf:
      state: latest
      name:
      - copr-frontend
      - copr-frontend-fedora
      - copr-selinux
      - python3-copr-common

  - name: upgrade db to head, logs in /var/log/copr-frontend/migrations.log
    shell: alembic-3 upgrade head &> /var/log/copr-frontend/migrations.log
    become: yes
    become_user: copr-fe
    args:
      chdir: /usr/share/copr/coprs_frontend/
    when: frontend_has_update.changed

  - name: make httpd started
    service: name=httpd state=started
    when: httpd_stopped.changed

  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"
