# SPDX-License-Identifier: BSD-3-Clause
---
- name: Install packages required to set up mock wifi network
  package:
    name:
      - NetworkManager
      - wpa_supplicant
    state: present
    use: "{{ (__network_is_ostree | d(false)) |
      ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
  register: __install_status
  until: __install_status is success
  retries: 6
  delay: 10

- name: Configure CentOS system for testing, if possible
  when: ansible_distribution == 'CentOS'
  block:
    # It is currently too difficult to install the required kernel
    # if using rpm ostree - so just skip this test
    - name: >-
        This is an rpm-ostree system. This test cannot be run on such a system.
        The test wants to install the hostapd package from copr, and install
        the mac80211_hwsim kernel module. These operations are currently not
        supported on rpm-ostree systems.
      meta: end_host
      when: __network_is_ostree | d(false)

    # yamllint disable rule:line-length
    # Even though hostapd can be installed via EPEL, Simultaneous Authentication
    # of Equals (SAE) has not been enabled by default. To warrant the test support
    # on CentOS, we setup hostapd copr repo to enable SAE option.
    - name: Install hostapd and mac80211_hwsim kernel module in CentOS
      shell: |
        dnf -y copr enable liangwen12year/hostapd-owe
        dnf -y install hostapd
        release=$(uname -r)
        tmp="${release/-//}"
        tmp="${tmp/.x//x}"
        dnf -y install https://kojihub.stream.centos.org/kojifiles/packages/kernel/$tmp/kernel-core-$release.rpm
        dnf -y install https://kojihub.stream.centos.org/kojifiles/packages/kernel/$tmp/kernel-modules-$release.rpm
        dnf -y install https://kojihub.stream.centos.org/kojifiles/packages/kernel/$tmp/kernel-modules-internal-$release.rpm
      changed_when: false
      # yamllint enable rule:line-length

- name: Install hostapd in Fedora
  package:
    name:
      - hostapd
    state: present
  when:
    - ansible_distribution == 'Fedora'
  register: __install_status
  until: __install_status is success
  retries: 6
  delay: 10

# Since DNF package manager can not warrant installing the kernel module with
# the same version as the kernel in target machine, install the kernel module
# from koji instead
- name: Install mac80211_hwsim kernel module in Fedora
  shell: |
    dnf -y install koji
    koji download-build --arch=$(uname -m) kernel-modules-internal-$(uname -r)
    dnf -y install kernel-modules*.rpm
  when:
    - ansible_distribution == 'Fedora'
  changed_when: false

- name: Create hostapd config
  copy:
    content: |
      interface=wlan1
      ssid=hostapd-sae
      hw_mode=g
      channel=6
      wpa=2
      wpa_passphrase=p@55w0rD
      wpa_key_mgmt=SAE
      rsn_pairwise=CCMP
      ieee80211w=2
    dest: /etc/hostapd/wireless.conf
    mode: "0644"

- name: Include the task 'start_mock_wifi.yml'
  include_tasks: tasks/start_mock_wifi.yml
