# Required vars with defaults
# - openqa_hostname
##      string - hostname of openQA server to run jobs for
##      default - localhost
# - openqa_consumer
##      bool - whether to enable the fedmsg consumer and run whenever
##             a compose completes
##      default - true
# - external_hostname
##       string - The public hostname of the openQA server (used in ResultsDB links)
##       default - ansible_nodename
# - openqa_extraarches
##      bool - when this is set, this instance will be configured to
##             create jobs for 'extra' arches beyond x86_64 (currently
##             ppc64 and aarch64). Only set it if this instance will
##             have worker hosts for those arches
##      default - false
#
# Optional vars
# - wikitcms_token
##      string - a token file to install for unattended reporting to
#                a wiki using openidc auth
# - deployment_type
##      string - Fedora Infrastructure thing; for this role, the
##               fedora_openqa config file will be set appropriately
##               for infra deployments if this is set, so don't set
##               it for private deployments
#
# When all of the above are set, a wikitcms 'token' file will be
# installed and result submission to the wiki will be enabled. If
# deployment_type is set to 'prod', results will be submitted to the
# production wiki in response to openQA production 'job complete'
# fedmsgs; if set to 'stg', results will be submitted to the staging
# wiki in response to openQA staging 'job complete' fedmsgs. Result
# reporting to ResultsDB will also be enabled for the production
# deployment. You probably should NOT set these unless you're
# maintaining the Fedora infrastructure deployments.
#
# NOTE: There are still currently a couple of assumptions that the
# openQA server boxes will always act as their own dispatchers, but
# there is no longer any fundamental reason why this must be the case,
# as we now use the openQA asset downloading capability so that the
# scheduler is no longer responsible for downloading assets.

# just keeping this around for convenience as we often need it
#- name: Install required packages (testing)
#  dnf:
#    name: ['python2-fedfind', 'python2-wikitcms']
#    state: present
#    enablerepo: "updates-testing"
#  tags:
#  - packages

- name: Install required packages
  dnf:
    name: ['python3-fedfind', 'python3-wikitcms', 'python3-fedmsg',
           'python3-openqa_client', 'python3-requests', 'python3-resultsdb_api',
           'python3-resultsdb_conventions-fedora', 'python3-setuptools', 'python3-six']
    state: present
  tags:
  - packages

- name: Install required packages (wiki oidc auth)
  dnf:
    name: python3-openidc-client
    state: present
  when: "wikitcms_token is defined"
  tags:
  - packages

- name: Check tools directory exists with correct ownership
  file: path=/root/fedora_openqa state=directory owner=root group=root

# we don't want to run the checkout if the tests are on a non-standard
# branch, as that usually means we're messing around on staging and
# don't want the checkout reset to HEAD.
- name: Check if tests are checked out and on a non-standard branch
  command: "git status"
  args:
    chdir: /root/fedora_openqa
  register: toolsbranch
  failed_when: "1 != 1"
  changed_when: "1 != 1"
  check_mode: no

- name: Check out fedora_openqa (scheduler / reporter tool)
  git:
    repo: https://pagure.io/fedora-qa/fedora_openqa.git
    dest: /root/fedora_openqa
  register: gittools
  when: "(toolsbranch.stderr.find('Not a git repository') != -1) or (toolsbranch.stdout.find('On branch master') != -1)"

- name: Check if fedora_openqa has ever been installed
  stat: path=/usr/local/bin/fedora-openqa
  register: insttools
  changed_when: "1 != 1"
  failed_when: "1 != 1"
  check_mode: no

- name: Install fedora_openqa
  command: "python3 setup.py install --nodeps"
  args:
    chdir: /root/fedora_openqa
  when: "gittools is changed or not insttools.stat.exists"
  notify:
  - restart fedmsg-hub

# For now, we only have enough ppc64/aarch64 workers for one instance,
# so we have a variant WANTED file that we deploy based on a config
# variable.
- name: Install extra arch WANTED images config if enabled
  copy: src=images.json.extraarches dest=/etc/fedora-openqa/images.json
  when: openqa_extraarches
  notify:
  - restart fedmsg-hub

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

- name: Create fedora_openqa config directory
  file: path=/etc/fedora-openqa state=directory owner=root group=root mode=0755

- name: Write schedule.conf
  template: src=schedule.conf.j2 dest=/etc/fedora-openqa/schedule.conf owner=root group=root mode=0644
  notify:
  - restart fedmsg-hub
  tags:
  - config

- name: Remove old wikitcms credentials file
  file: path=/etc/fedora/credentials state=absent
  tags:
  - config

- name: Create /root/.openidc (token file location for manual runs as root)
  file: path=/root/.openidc state=directory owner=root group=root mode=0700

- name: Create /usr/share/fedmsg/.openidc (token file location for fedmsg consumer runs)
  file: path=/usr/share/fedmsg/.openidc state=directory owner=root group=fedmsg mode=0750

- name: Write wikitcms token file for root
  copy: src={{ wikitcms_token }} dest=/root/.openidc/oidc_wikitcms.json owner=root group=root mode=0600
  when: "wikitcms_token is defined"
  tags:
  - config

#- name: Write wikitcms token file for fedmsg
#  copy: src={{ wikitcms_token }} dest=/usr/share/fedmsg/.openidc/oidc_wikitcms.json owner=root group=fedmsg mode=0660
#  when: "wikitcms_token is defined"
#  tags:
#  - config

- name: Enable fedmsg consumer
  template: src=openqa_consumer.py.j2 dest=/etc/fedmsg.d/openqa_consumer.py owner=root group=root mode=0644
  notify:
  - restart fedmsg-hub
  tags:
  - config

- name: Set up cron job to schedule live-respins jobs
  copy: src=schedule-live-respins.cron dest=/etc/cron.hourly/schedule-live-respins owner=root group=root mode=0755
  tags:
  - config
