---
- name: install bench-server packages
  ansible.builtin.apt:
    force_apt_get: true
    install_recommends: false
    update_cache: true
    name: "{{ bench_server_packages }}"
    state: latest
  register: apt_status
  retries: 60
  until: apt_status is success or ('Failed to lock apt for exclusive operation' not in apt_status.msg and '/var/lib/dpkg/lock' not in apt_status.msg)

- name: ensure configuration directory exists
  ansible.builtin.file:
    path: /etc/mediaserver
    state: directory
    mode: "755"

- name: benchmark configuration settings
  ansible.builtin.copy:
    dest: /etc/mediaserver/bench-conf.json
    content: |
      {
      "LOCUST_HOST":"{{ bench_server }}",
      "MS_HOST":"{{ bench_host }}",
      "MS_USERNAME":"{{ bench_user }}",
      "MS_PASSWORD":"{{ bench_password }}",
      "MEDIA_OID":"{{ bench_oid }}",
      "DL_STREAMS":{{ bench_dl_streams }},
      "TIME_STATS":{{ bench_time_stat }}
      }
    mode: "644"

- name: reload systemd daemon
  ansible.builtin.systemd:
    daemon_reload: true

- name: restart bench-server
  ansible.builtin.systemd:
    name: bench-server
    state: restarted

- name: streaming configuration settings
  ansible.builtin.template:
    src: bench-streaming.conf.j2
    dest: /etc/mediaserver/bench-streaming.conf
    mode: "644"

- name: clone ms-testing-suite repository
  ansible.builtin.git:
    repo: "{{ bench_stream_repo }}"
    version: stable
    dest: /usr/share/ms-testing-suite
    update: true
    force: true

- name: copy configuration for testing tools
  ansible.builtin.copy:
    src: /etc/mediaserver/bench-streaming.conf
    dest: /usr/share/ms-testing-suite/config.json
    remote_src: true
    mode: "644"

- name: add docker key
  when:
    - not offline_mode | d(false)
    - not in_docker | d(false)
  ansible.builtin.apt_key:
    url: https://download.docker.com/linux/debian/gpg
    state: present

- name: add docker debian repository
  when:
    - not offline_mode | d(false)
    - not in_docker | d(false)
  ansible.builtin.apt_repository:
    repo: deb https://download.docker.com/linux/debian buster stable
    state: present
    update_cache: true

- name: install docker
  when:
    - not offline_mode | d(false)
    - not in_docker | d(false)
  ansible.builtin.apt:
    force_apt_get: true
    install_recommends: false
    update_cache: true
    name: docker-ce
    state: latest
  register: apt_status
  retries: 60
  until: apt_status is success or ('Failed to lock apt for exclusive operation' not in apt_status.msg and '/var/lib/dpkg/lock' not in apt_status.msg)

- name: generate docker image
  when:
    - not offline_mode | d(false)
    - not in_docker | d(false)
  ansible.builtin.command:
    cmd: make build_docker_img
    chdir: /usr/share/ms-testing-suite
  changed_when: false
  run_once: true
