---
# gsm tester would only need the libraries, not the dev packages.
# But the name of the -dev packages are more stable over multiple release.

- name: update apt
  apt:
    cache_valid_time: 3600
    update_cache: yes

- name: install osmocom runtime dependencies
  apt:
    name:
      - build-essential
      - git
      - automake
      - libdbi-dev
      - libdbd-sqlite3
      - sqlite3
      - libsctp-dev
      - libortp-dev
      - libpcap-dev
      - libc-ares-dev
      - libtool
      - shtool
      - pkg-config
      - libtalloc-dev
      - libpcsclite-dev
      - libgnutls28-dev
      - libmnl-dev
      - libssl-dev
      - libczmq-dev
      - libsofia-sip-ua-glib-dev
      - libsqlite3-dev
      - libasound2-dev
      - liblua5.3-dev
      - lua-socket
  tags:
    - osmocom

# mdbus2 is not available in debian10. TODO: compile it for >9
- name: install mdbus2
  apt:
    name:
      - mdbus2
  when: ansible_distribution == 'Debian' and ansible_distribution_version <= '9'
  tags:
    - ofono
    - modem

- name: install srsLTE build-only dependencies
  apt:
    name:
      - cmake
      - libfftw3-dev
      - libmbedtls-dev
      - libboost-program-options-dev
      - libconfig++-dev
      - libsctp-dev
      - libpcsclite-dev
      - libuhd-dev
      - libczmq-dev
      - libsoapysdr-dev
  tags:
    - srs

- name: install srsLTE runtime extra dependencies (Debian)
  apt:
    name:
      - soapysdr-module-lms7
  when: ansible_distribution == 'Debian'
  tags:
    - srs

- name: install srsLTE runtime extra dependencies (Ubuntu)
  apt:
    name:
      - soapysdr0.6-module-lms7
  when: ansible_distribution == 'Ubuntu'
  tags:
    - srs

- name: install ofono build dependencies
  apt:
    name: ofono
    state: build-dep
  tags:
    - ofono
    - modem

- name: install libqmi-glib a dependency of sysmocom ofono
  apt:
    name: libqmi-glib-dev
  tags:
    - ofono
    - modem

- name: install uhub dependencies
  apt:
    name:
      - libusb-1.0-0-dev
  tags:
    - modem

- name: setup uhubctl repository
  git:
    repo: "https://github.com/mvp/uhubctl.git"
    dest: /root/uhubctl
    version: master
    clone: yes
    update: no
  tags:
    - modem

- name: build uhubctl
  shell: |
      make && \
      make install
  args:
    chdir: /root/uhubctl
    creates: /usr/sbin/uhubctl
  tags:
    - modem

# TODO: move this into restart-modems.d directory
- name: copy quad_modem_power_cycle.sh
  template:
    src: quad_modem_power_cycle.sh
    dest: /usr/local/bin/quad_modem_power_cycle.sh
    mode: 0755
  tags:
    - modem

- name: setup ofono repository
  git:
    repo: 'https://gitea.sysmocom.de/sysmocom/ofono'
    dest: /root/ofono
    version: osmo-gsm-tester
    clone: yes
    update: no
  tags:
    - ofono
    - modem

- name: build ofono
  shell: |
      ./bootstrap && \
      CFLAGS="-g" ./configure --disable-bluez4 && \
      CFLAGS="-g" make -j3 && \
      make install && \
      systemctl daemon-reload
  args:
    chdir: /root/ofono
    creates: /usr/local/sbin/ofonod
  tags:
    - ofono
    - modem

- name: ensure ofono is started
  service:
    name: ofono
    state: started
    enabled: yes
  tags:
    - ofono
    - modem

- name: install patchelf build dependencies
  apt:
    name:
      - autoconf
  tags:
    - patchelf
    - osmo-gsm-tester-proc

# patchelf 0.9 available in debian has bugs with certain binaries. Version 0.10
# fails on other binaries, so we need version 0.11 installed in /usr/local/bin
# (takes preference in PATH)
- name: setup patchelf v0.11 repository
  git:
    repo: 'https://github.com/NixOS/patchelf.git'
    dest: /root/patchelf
    version: "0.11"
    clone: yes
    update: no
  tags:
    - patchelf
    - osmo-gsm-tester-proc

- name: build patchelf v0.11
  shell: |
      autoreconf -fi && \
      ./configure --prefix=/usr/local && \
      make && \
      make install
  args:
    chdir: /root/patchelf
    creates: /usr/local/bin/patchelf
  tags:
    - patchelf
    - osmo-gsm-tester-proc

- name: install gsm tester dependencies (main)
  apt:
    name:
      - sqlite3
      - python3
      - python3-setuptools
      - python3-yaml
      - python3-mako
      - python3-gi
      - python3-watchdog
      - python3-numpy
      - python3-websocket
      - ofono
      - patchelf
      - libcap2-bin
      - python3-pip
      - udhcpc
  tags:
    - osmo-gsm-tester-main

- name: install gsm tester dependencies (proc)
  apt:
    name:
      - tcpdump
      - patchelf
      - libcap2-bin
      - iperf3
      - sudo
  tags:
    - osmo-gsm-tester-proc

- name: install gsm tester pip dependencies
  pip:
    name: "{{ item }}"
    executable: pip3
    editable: no
  with_items:
    - "git+https://github.com/podshumok/python-smpplib.git@master#egg=smpplib"
    - pydbus
    - pyusb
    - pysispm
  tags:
    - pip
    - osmo-gsm-tester-main

- name: create group osmo-gsm-tester
  group:
    name: osmo-gsm-tester
  tags:
    - osmo-gsm-tester-main
    - osmo-gsm-tester-proc

- name: Add jenkins user to osmo-gsm-tester group
  user:
    name: "{{ jenkins_user }}"
    groups: "{{ item }}"
    append: yes
    shell: /bin/bash
  with_items:
    - osmo-gsm-tester
  tags:
    - osmo-gsm-tester-main
    - osmo-gsm-tester-proc

- name: add jenkins user to systemd-journal and dialout groups
  user:
    name: "{{ jenkins_user }}"
    groups: "{{ item }}"
    append: yes
    shell: /bin/bash
  with_items:
    - systemd-journal
    - dialout
  tags:
    - osmo-gsm-tester-main

- name: setup state directory
  file:
    path: /var/tmp/osmo-gsm-tester/state
    state: directory
    group: osmo-gsm-tester
    mode: g+rwxs
  tags:
    - osmo-gsm-tester-main

- name: install acl
  apt:
    name: acl
  tags:
    - osmo-gsm-tester-main

# Ensure the group always has access to all files
- name: add acl to state directory
  acl:
    path: /var/tmp/osmo-gsm-tester/state
    state: present
    etype: group
    entity: osmo-gsm-tester
    default: yes
    permissions: rwx
  tags:
    - osmo-gsm-tester-main

- name: create trials directory
  file:
    path: /var/tmp/osmo-gsm-tester/trials
    state: directory
    group: osmo-gsm-tester
    mode: g+rwxs
  tags:
    - osmo-gsm-tester-main

- name: ensure tcpdump can be called by osmo-gsm-tester
  file:
    path: /usr/sbin/tcpdump
    group: osmo-gsm-tester
    mode: 0750
  tags:
    - osmo-gsm-tester-proc
    - tcpdump

- name: create a symlink for tcpdump
  file:
    src: /usr/sbin/tcpdump
    dest: /usr/local/bin/tcpdump
    state: link
  tags:
    - osmo-gsm-tester-proc
    - tcpdump

- name: add correct capabilities to tcpdump
  capabilities:
    path: /usr/sbin/tcpdump
    capability: cap_net_raw,cap_net_admin=eip
    state: present
  tags:
    - osmo-gsm-tester-proc
    - tcpdump

- name: allow core files for the group osmo-gsm-tester
  copy:
    content: "@osmo-gsm-tester - core unlimited"
    dest: /etc/security/limits.d/osmo-gsm-tester_allowcore.conf
  tags:
    - osmo-gsm-tester-proc

- name: allow rt priority for the group osmo-gsm-tester
  copy:
    content: "@osmo-gsm-tester - rtprio 99"
    dest: /etc/security/limits.d/osmo-gsm-tester_rtprio.conf
  tags:
    - osmo-gsm-tester-proc

- name: setup osmo-gsm-tester repository
  git:
    repo: 'https://gitea.osmocom.org/cellular-infrastructure/osmo-gsm-tester'
    dest: /root/osmo-gsm-tester
    version: master
    clone: yes
    update: no
  tags:
    - osmo-gsm-tester-proc

- name: Install osmo-gsm-tester script helpers to /usr/local/bin (PATH) (mkdir)
  file: path=/usr/local/bin state=directory

- name: Install osmo-gsm-tester script helpers to /usr/local/bin (PATH)
  shell: |
      cp utils/bin/*.sh /usr/local/bin/
  args:
    chdir: /root/osmo-gsm-tester
  tags:
    - osmo-gsm-tester-proc

- name: Install osmo-gsm-tester sudoers.d files to /etc/sudoers.d/ (PATH (mkdir)
  file: path=/etc/sudoers.d/ state=directory

- name: Install osmo-gsm-tester sudoers.d files to /etc/sudoers.d/ (PATH)
  shell: |
      cp utils/sudoers.d/* /etc/sudoers.d/
  args:
    chdir: /root/osmo-gsm-tester
  tags:
    - osmo-gsm-tester-proc

- name: Install dbus policy file to allow osmo-gsm-tester group to access ofono
  shell: |
      cp utils/dbus-1/* /etc/dbus-1/system.d/
  args:
    chdir: /root/osmo-gsm-tester
  tags:
    - ofono
    - modem

- name: logrotate limit filesizes to 10M
  copy:
    content: "maxsize 10M"
    dest: /etc/logrotate.d/maxsize
  tags:
    - osmo-gsm-tester-main

- name: install sispmctl (usb power socket)
  apt:
    name: sispmctl
  tags:
    - sispm

- name: allow group osmo-gsm-tester to access sispm devices
  copy:
    src: 60-sispmctl.rules
    dest: /etc/udev/rules.d/
  notify: restart udev
  tags:
    - sispm

- name: allow group osmo-gsm-tester to access LMS devices
  copy:
    src: 64-limesuite.rules
    dest: /etc/udev/rules.d/
  notify: restart udev
  tags:
    - lms

- name: use persistent naming for modem network interfaces
  copy:
    src: 70-net-setup-link-modems.rules
    dest: /etc/udev/rules.d/
  notify: restart udev
  tags:
    - modem

- name: create remote rundir directories
  file:
    path: "/osmo-gsm-tester-{{ item }}"
    state: directory
    group: osmo-gsm-tester
    mode: g+rwxs
  with_items:
    - srsenb
    - srsepc
    - srsue
    - trx
    - amarisoftenb
    - amarisoftepc
    - amarisoftue
  tags:
    - osmo-gsm-tester-proc
    - osmocom
    - srs

- name: avoid dhcpcd managing modem interfaces and racing with udev rename
  lineinfile:
    path: /etc/dhcpcd.conf
    regexp: '^denyinterfaces'
    line: 'denyinterfaces ww* r*'
  tags:
  tags:
    - osmo-gsm-tester-main
    - dhcpcd
