# Required vars
# - openqa_workers
##      integer - number of worker instances to create/run

# Required vars with defaults
# - openqa_hostname
##      string - hostname of openQA server to run jobs for
##      default - localhost

# Optional vars
# - openqa_tap
##      bool - whether this is the tap-enabled host or not
##             each deployment should have *one* tap-capable worker host
# - openqa_rngd
##      string - if set to any value, rng-tools package will be
##               installed and rngd.service enabled/started

- name: Install required packages (testing)
  dnf:
    name: ['openqa-worker']
    state: present
    enablerepo: "updates-testing"
  tags:
  - packages

- name: Install packages
  dnf:
    name: ['libselinux-python', 'git']
    state: present
  tags:
  - packages

- name: Install UEFI firmware package (x86_64 only)
  dnf: name=edk2-ovmf state=present
  tags:
  - packages
  when: "ansible_architecture is defined and ansible_architecture == 'x86_64'"

- name: Install UEFI firmware package (aarch64 only)
  dnf: name=edk2-aarch64 state=present
  tags:
  - packages
  when: "ansible_architecture is defined and ansible_architecture == 'aarch64'"

- name: Install arm UEFI firmware package (aarch64 only)
  dnf: name=edk2-arm state=present
  tags:
  - packages
  when: "ansible_architecture is defined and ansible_architecture == 'aarch64'"

- name: Install rng-tools (if specified by openqa_rngd var)
  dnf: name=rng-tools state=present
  tags:
  - packages
  when: "openqa_rngd is defined and openqa_rngd"

- name: Install script to set /dev/kvm perms and disable SMT (ppc64 only)
  copy: src=openqa-ppc64-prep.sh dest=/etc/cron.hourly/openqa-ppc64-prep owner=root group=root mode=0755
  when: "ansible_architecture is defined and ansible_architecture == 'ppc64le'"

- name: Install systemd service to run boot script (ppc64 only)
  copy: src=openqa-ppc64-prep.service dest=/etc/systemd/system/openqa-ppc64-prep.service
  when: "ansible_architecture is defined and ansible_architecture == 'ppc64le'"
  register: prepservice

- name: Do systemctl daemon-reload to register new service (ppc64 only)
  command: "systemctl daemon-reload"
  when: "prepservice is defined and prepservice is changed"

- name: Enable systemd service to run boot script (ppc64 only)
  service: name=openqa-ppc64-prep enabled=yes state=started
  when: "ansible_architecture is defined and ansible_architecture == 'ppc64le'"

- name: Enable rngd.service (if specified by openqa_rngd var)
  service: name=rngd enabled=yes state=started
  when: "openqa_rngd is defined and openqa_rngd"

- import_tasks: nfs-client.yml
  when: openqa_hostname is defined and openqa_hostname != "localhost"

- import_tasks: tap-setup.yml
  when: openqa_tap is defined and openqa_tap

- name: openQA client config
  template: src=client.conf.j2 dest=/etc/openqa/client.conf owner=_openqa-worker group=root mode=0600
  tags:
  - config

- name: openQA worker config
  template: src=workers.ini.j2 dest=/etc/openqa/workers.ini owner=_openqa-worker group=root mode=0644
  tags:
  - config

- import_tasks: createhdds.yml
  when: "inventory_hostname in groups['openqa-hdds-workers']"

- name: Enable and start worker services
  service: name=openqa-worker@{{ item }} enabled=yes state=started
  with_sequence: "count={{ openqa_workers }}"
