---
- name: install tmpfiles.d Taskotron config
  copy: src=tmpfiles-taskotron.conf dest=/etc/tmpfiles.d/taskotron.conf owner=root group=root mode=0600
  when: deployment_type != 'local'

- name: ensure packages required for libtaskotron are installed
  dnf: name={{ item }} state=present
  with_items:
    - PyYAML
    - libtaskotron
  when: deployment_type == 'local'

- name: ensure packages required for libtaskotron are installed
  dnf: name={{ item }} state=present
  with_items:
    - PyYAML
    - libtaskotron
    - testcloud
    - selinux-policy-devel
  when: deployment_type in ['dev', 'stg', 'prod']

- name: ensure packages required for taskotron tasks are installed
  dnf: name={{ item }} state=present
  with_items:
    - python-solv
    - python-librepo
    - rpmlint
  when: ansible_cmdline.ostree is not defined

- name: generate taskotron.yaml config file
  template: src={{ item }} dest=/etc/taskotron/taskotron.yaml owner=root group=root mode=0644
  with_first_found:
    - taskotron.yaml.j2.{{ deployment_type }}
    - taskotron.yaml.j2

- name: generate namespaces.yaml config file
  template: src={{ item }} dest=/etc/taskotron/namespaces.yaml owner=root group=root mode=0644
  with_first_found:
    - namespaces.yaml.j2.{{ deployment_type }}
    - namespaces.yaml.j2

- name: generate testcloud config file
  template: src={{ item }} dest=/etc/testcloud/settings.py owner=root group=root mode=0644
  with_first_found:
    - settings.py.testcloud.j2.{{ deployment_type }}
    - settings.py.testcloud.j2

- name: upload yumrepoinfo.conf
  copy: src={{ item }} dest=/etc/taskotron/yumrepoinfo.conf owner=root group=root mode=0644
  with_first_found:
    - yumrepoinfo.conf.{{ deployment_type }}
    - yumrepoinfo.conf

- name: check that baseurl= line exists in yumrepoinfo.conf
  command: grep -q '^baseurl =' /etc/taskotron/yumrepoinfo.conf
  changed_when: False

- name: check that baseurl_altarch= line exists in yumrepoinfo.conf
  command: grep -q '^baseurl_altarch =' /etc/taskotron/yumrepoinfo.conf
  changed_when: False

- name: set infra repo for baseurl in yumrepoinfo.conf
  lineinfile:
    path: /etc/taskotron/yumrepoinfo.conf
    regexp: '^baseurl ='
    line: 'baseurl = http://infrastructure.fedoraproject.org/pub/fedora/linux'

- name: set infra repo for baseurl_altarch in yumrepoinfo.conf
  lineinfile:
    path: /etc/taskotron/yumrepoinfo.conf
    regexp: '^baseurl_altarch ='
    line: 'baseurl_altarch = http://infrastructure.fedoraproject.org/pub/fedora-secondary'

- name: make sure there's no download.fp.o leftover in yumrepoinfo.conf
  command: grep -qE '^[^#].*/download.fedoraproject.org/' /etc/taskotron/yumrepoinfo.conf
  changed_when: False
  register: result
  failed_when: result.rc != 1

- name: create /var/log/taskotron for task logfiles
  file: path=/var/log/taskotron state=directory owner={{ slave_user }} group={{ slave_user }} mode=1755
  when: deployment_type == 'local'

- name: ensure buildslaves group exists
  group: name={{ slaves_group }}
  when: deployment_type in ['dev', 'stg', 'prod']

- name: create directory for disposable clients images
  file: path={{ imagesdir }} state=directory owner=root group={{ slaves_group }} mode=1755
  when: deployment_type in ['dev', 'stg', 'prod']

- name: create /var/log/taskotron for task logfiles
  file: path=/var/log/taskotron state=directory owner=root group={{ slaves_group }} mode=1775
  when: deployment_type in ['dev', 'stg', 'prod']

- name: upload custom selinux policy to allow buildslaves to use qemu-kvm
  copy: src=service-virt-transition.te dest=/root/service-virt-transition.te owner=root group=root mode=0644
  when: deployment_type in ['dev', 'stg', 'prod']

- name: compile selinux policy
  command: chdir=/root/ creates=/root/service-virt-transition.pp make -f /usr/share/selinux/devel/Makefile
  when: deployment_type in ['dev', 'stg', 'prod']

- name: load custom selinux policy for qemu-kvm from buildslaves
  command: chdir=/root/ semodule -i /root/service-virt-transition.pp
  when: deployment_type in ['dev', 'stg', 'prod']

- name: start and enable libvirt services
  service: name={{ item }} state=started enabled=yes
  with_items:
    - libvirtd
    - virtlogd

- name: copy custom libvirt network config
  copy: src=default.xml dest=/etc/libvirt/qemu/networks/default.xml owner=root group=root mode=0600
  when: deployment_type in ['dev', 'stg', 'prod']

- name: hotfix libtaskotron to set longer timeout for testcloud, this should be temporary fix though :/
  replace: dest=/usr/lib/python2.7/site-packages/libtaskotron/ext/disposable/vm.py regexp=', timeout=60\):' replace=', timeout=120):' backup=yes
  when: deployment_type in ['dev', 'stg', 'prod']

- name: setup nested virt on virthosts with nested=true variable
  copy: src=kvm_intel.conf dest=/etc/modprobe.d/kvm_intel.conf
  when: nested == true
