# 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
# - openqa_repo
##      string - Repo to enable when updating openQA packages. Set to
##               'updates-testing' to use packages from updates-testing
##      default - 'updates', which is effectively a no-op
# - openqa_nfs_worker
##      bool - whether this worker expects to share the factory dir with
##             the server via NFS. If set, the nfs-client task will be
##             run to set up the share. The worker should also be included
##             in the server's openqa_nfs_workers var so it is granted
##             access to the share
##      default - false
# - openqa_hdds_worker
##      bool - whether this worker creates base disk images for a shared
##             factory dir. There should be only *one* of these per arch
##             per deployment
##      default - false
# - openqa_tap
##      string - tap worker classes this worker should be part of, a comma-
##               separated string, e.g. "tap" or "tap,tap2". If this is
##               empty, the worker will not be configured for tap and
##               swtpm at all; if it's not empty, the worker will be
##               configured for tap and swtpm and the string substituted
##               into the workers.ini config file. Only *one* worker
##               should be in each tap worker class (so there should not
##               be two workers in the "tap" class), but if you are short
##               on workers, one worker can be in *multiple* tap classes,
##               meaning it will pick up more tap jobs. The purpose of
##               having multiple tap classes is to split the load of tap
##               jobs across more than one host, when enough hosts are
##               available
##      default - empty string (disabled)

# Optional vars
# - deployment_type
##      string - Fedora Infrastructure thing; for this role, applies an
##               infra-specific tweak to httpd config. Don't set it outside
##               Fedora infra.
# - openqa_rngd
##      string - if set to any value, rng-tools package will be
##               installed and rngd.service enabled/started
# - openqa_worker_class
##      string - custom WORKER_CLASS value for workers.ini
# - openqa_static_uid
##       int - a static ID for the geekotest group if desired. this is useful
##             for NFS mounting openQA data files. The _openqa_worker user,
##             which os-autoinst runs as, will be added to this group. The
##             idea is that the same group with the same GID exists on the
##             NFS server and is the group of the shared asset directories,
##             so os-autoinst can write to the shared asset directories,
##             which it needs to do when uncompressing compressed disk assets

- name: Remove old scratch repo directory
  file: path=/var/tmp/scratchrepo state=absent

- name: Delete old scratch build repo config
  file: path=/etc/yum.repos.d/scratchrepo.conf state=absent

- name: Write lab side repo config
  copy: src=openqa-lab-repo.repo dest=/etc/yum.repos.d/openqa-lab-repo.repo owner=root group=root mode=0644
  when: "deployment_type is defined and deployment_type == 'stg'"

- name: Install required packages
  package:
    name: ['openqa-worker', 'os-autoinst', 'ffmpeg-free']
    state: latest
    enablerepo: "{{ openqa_repo }}"
  notify:
  - restart openqa workers
  tags:
  - packages

- name: Install packages used by tests
  package:
    name: ['perl-JSON', 'perl-REST-Client']
    state: latest
  tags:
  - packages

- name: Install packages used by ansible plays
  package:
    name: ['python3-libselinux', 'git']
    state: present
  tags:
  - packages

- name: Create geekotest group with static GID
  group: "name=geekotest gid={{ openqa_static_uid }} system=yes"
  when: "openqa_static_uid is defined"

- name: Add _openqa_worker user to geekotest group
  user: name=_openqa-worker groups=geekotest append=yes
  when: "openqa_static_uid is defined"

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

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

- name: Install arm UEFI firmware package (aarch64 only)
  package: 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)
  package: 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)
  systemd:
    daemon_reload: yes
  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"

- include_tasks: nfs-client.yml
  when: openqa_nfs_worker|bool

- include_tasks: tap-setup.yml
  when: openqa_tap

- name: Tell git it's OK for _openqa-worker to run 'git' on the test dir
  copy: src=gitconfig dest=/etc/gitconfig owner=root group=root mode=0644

# this is kinda lazy - we could have a separate openqa_swtpm var so we
# we could potentially separate tap worker hosts from swtpm ones - but
# it makes workers.ini templating annoyingly awkward (we need way more
# conditionals to account for four possibilities rather than two) and
# for now it's fine to just assume the tap host(s) is/are also the
# swtpm host(s)
- name: Install swtpm packages
  package:
    name: ['swtpm', 'swtpm-tools']
    state: latest
    enablerepo: "{{ openqa_repo }}"
  tags:
  - packages
  when: 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
  notify:
  - restart openqa workers
  tags:
  - config

- include_tasks: createhdds.yml
  when: openqa_hdds_worker|bool

- name: override kernel scheduler configuration - rhbz#2009585
  copy: src=60-block-scheduler.rules dest=/etc/udev/rules.d/60-block-scheduler.rules owner=root group=root mode=0644
  when: "deployment_type is defined"

- name: remove wrongly-named kernel scheduler config file
  file: path=/etc/udev/rules.d/60-block-scheduler-override.rules state=absent

- name: Enable and start worker services
  service: name=openqa-worker@{{ item }} enabled=yes state=started
  loop: "{{ range(1, openqa_workers + 1)|list }}"
