- name: Surveyor prerequisites
  apt:
    pkg:
      - python3-flask
      - python3-flask-sqlalchemy
      - python3-flask-migrate
      - python3-psycopg2

- name: Surveyor benchmarking user
  group:
    name: surveyor
    gid: 753

- name: Surveyor benchmarking user
  user:
    name: surveyor
    group: surveyor
    uid: 753
    shell: /bin/bash
    home: /var/obj/surveyor

- name: Surveyor web user
  user:
    name: surveyor-web
    group: surveyor
    uid: 754
    shell: /bin/bash
    home: /srv/surveyor

- name: Extra permissions for xmrazek7
  copy:
    dest: /etc/sudoers.d/xmrazek7
    content: |
      xmrazek7 ALL=NOPASSWD: /bin/systemctl restart quickpoll,/bin/systemctl start quickpoll,/bin/systemctl stop quickpoll,/bin/systemctl restart surveyor,/bin/systemctl start surveyor,/bin/systemctl stop surveyor,/bin/systemctl restart surveyor-web,/bin/systemctl start surveyor-web,/bin/systemctl stop surveyor-web
      xmrazek7 ALL=(quickpoll) NOPASSWD: ALL
      xmrazek7 ALL=(surveyor) NOPASSWD: ALL
      xmrazek7 ALL=(surveyor-web) NOPASSWD: ALL
    validate: /usr/sbin/visudo -csf %s

- name: Surveyor unit
  copy:
    src: surveyor.service
    dest: /etc/systemd/system/
  register: surveyor_unit

- systemd:
    name: surveyor
    daemon_reload: '{{surveyor_unit.changed}}'
    state: '{{("restarted" if surveyor_unit.changed else "started") if run_surveyor else "stopped"}}'
    enabled: '{{run_surveyor}}'
    masked: false
