- name: apply koschei/frontend role
  hosts: "{{ koschei_web_hosts | default('koschei-web:koschei-web-stg') }}"
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  roles:
  - koschei/frontend

- name: apply koschei/backend role
  hosts: "{{ koschei_backend_hosts | default('koschei-backend:koschei-backend-stg') }}"
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  roles:
  - koschei/backend

- name: upgrade packages
  hosts: "{{ koschei_web_hosts | default('koschei-web:koschei-web-stg') }}:{{ koschei_backend_hosts | default('koschei-backend:koschei-backend-stg') }}"
  vars:
    fedora_repos:
    - updates
  tasks:
  - name: clean dnf metadata
    command: dnf {% for repo in fedora_repos %}--enablerepo {{ repo }} {% endfor %} clean all
    warn: False
  - name: create dnf metadata cache
    command: dnf {% for repo in fedora_repos %}--enablerepo {{ repo }} {% endfor %} makecache
    warn: False
  - name: lits installed koschei packages
    dnf:
      list: "koschei*"
      disablerepo: "*"
    register: koschei_packages
  - name: update koschei packages
    dnf:
      name: "{{ item.name }}"
      state: latest
      enablerepo: "{{ fedora_repos | join(',') }}"
    register: koschei_upgrade
    with_items: "{{ koschei_packages.results }}"

- name: stop backend services
  hosts: "{{ koschei_backend_hosts | default('koschei-backend:koschei-backend-stg') }}"
  tasks:
  - name: stop services
    service: name="{{ item }}" state=stopped
    with_items: "{{ koschei_backend_services }}"

- name: stop httpd
  hosts: "{{ koschei_web_hosts | default('koschei-web:koschei-web-stg') }}"
  tasks:
  - name: stop httpd
    service: name="httpd" state=stopped

- name: run DB migration
  hosts: "{{ koschei_backend_hosts | default('koschei-backend:koschei-backend-stg') }}"
  tasks:
  - name: run db migration
    become: yes
    become_user: koschei
    command: koschei-admin alembic upgrade head

- name: restart httpd
  hosts: "{{ koschei_web_hosts | default('koschei-web:koschei-web-stg') }}"
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  tasks:
  - name: start httpd
    service: name="httpd" state=started

- name: restart backend services
  hosts: "{{ koschei_backend_hosts | default('koschei-backend:koschei-backend-stg') }}"
  vars_files:
  - /srv/web/infra/ansible/vars/global.yml
  - /srv/private/ansible/vars.yml
  - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  tasks:
  - name: start services
    service: name="{{ item }}" state=started
    with_items: "{{ koschei_backend_services }}"

- name: Check whether backend update
  hosts: "{{ koschei_web_hosts | default('koschei-web:koschei-web-stg') }}:{{ koschei_backend_hosts | default('koschei-backend:koschei-backend-stg') }}"
  tasks:
  - assert:
      that: koschei_upgrade.changed
