- name: hesperia
  hosts: hesperia.fi.muni.cz
  vars:
    postgres_server: true
    dhcpcd: false
    postfix: true
    backports: false
    nftables:
      nfs: false
      snmp: true
      pds_local: false
    snmpd:
      load: "7 6.5 6"
      disks:
        - path: /
          free: 1574400
        - path: /tmp
          free: 1048576
    expr_test_ids:
      - name: ib015
        id: 1000
#     - name: ib102
#        id: 1003
#      - name: ib002
#        id: 1004
      - name: ib113
        id: 1005
#      - name: pv264
#        id: 1006
      - name: ib111
        id: 1007
      - name: ib016
        id: 1008
      - name: ib005
        id: 1009
      - name: pb006
        id: 1015
    rc_with_homes:
      - ib015
      - ib016
      - pb006

  tasks:
    - import_role:
        name: common_deb

    - name: "Setup fstab"
      mount:
        path: '{{item.path}}'
        src: '{{item.src}}'
        fstype: '{{item.fstype}}'
        opts: '{{item.opts|default("defaults")}}'
        dump: "0"
        passno: '{{item.passno|default("0")}}'
        state: '{{item.state|default("mounted")}}'
      with_items:
        - path: "/"
          src: /dev/hsexprtest-vg/root
          fstype: ext4
          opts: errors=remount-ro
          passno: 1

        - path: "/boot"
          src: "UUID=e25766ad-2d27-4109-86f6-723091bd15f2"
          fstype: ext2
          passno: 2

        - path: none
          src: /dev/hsexprtest-vg/swap
          fstype: swap
          state: present

        - path: /tmp
          src: /dev/hsexprtest-vg/tmp
          fstype: ext4
          opts: defaults,relatime


    - name: exprTest groups
      group:
        name: '{{item.name}}'
        gid: '{{item.id}}'
      loop: "{{expr_test_ids}}"

    - name: exprTest users
      user:
        name: '{{"" if item.1 else "rc-"}}{{item.0.name}}'
        uid: '{{item.0.id if item.1 else 10000 + item.0.id - 1000}}'
        home: '{{"/home/" + item.0.name if item.1 else ("/srv/exprtest/rc-" + item.0.name if item.0.name in rc_with_homes else "/var/empty")}}'
        group: '{{item.0.name if item.1 else "runcheck"}}'
        shell: /bin/bash
      loop: "{{expr_test_ids | product([True, False]) | list}}"

    - import_role:
        name: postgres_deb

    - name: "MS repository"
      apt_repository:
        repo: deb [arch=amd64,arm64,armhf] https://packages.microsoft.com/debian/11/prod bullseye main
        filename: microsoft-prod
        state: present

    - name: install .NET Core
      apt:
        update_cache: yes
        pkg:
          - dotnet-sdk-3.1

    - name: install Mono mcs
      apt:
        pkg:
          - mono-mcs

    - name: Create /srv
      file:
        path: /srv
        state: directory
        mode: '0755'

    - name: exprtest dependencies
      apt:
        pkg:
          - python3-aiohttp
          - python3-aiohttp-jinja2
          - python3-asyncpg

    - name: fjaweb dependencies
      apt:
        pkg:
          - antlr4
          - python3-antlr4
        state: absent

    - name: extra fjaweb dependencies
      pip:
        name:
          - Flask-Markdown
        executable: pip3
        state: absent

    - name: disable fjaweb
      systemd:
        name: 'fjaweb.{{item}}'
        enabled: false
        state: stopped
      loop:
        - socket
        - service

    - name: Misc eval dependencies
      apt:
        pkg:
          - python3-sympy

    - name: Create postgreSQL role for hsExprTest
      postgresql_user:
        login_unix_socket: "/var/run/postgresql/"
        name: checker

    - name: Create postgreSQL DB for hsExprTest
      postgresql_db:
        login_unix_socket: "/var/run/postgresql/"
        name: exprtest
        owner: checker

    - name: Install SNMP
      apt:
        pkg:
          - snmp

    - name: Setup SNMP
      template:
        src: snmpd.conf.j2
        dest: /etc/snmp/snmpd.conf
      register: snmp_setup

    - name: Restart SNMP
      service:
        name: snmpd
        state: '{{"restarted" if snmp_setup.changed else "started"}}'
        masked: false
        enabled: true

    - name: Setup GHC
      import_role:
        name: ghc_udeb
      vars:
        versions:
          # the latest has higher priority
          - ghc: 9.0.1
          - ghc: 9.2.1
            cabal: 3.6.2.0

    - name: hsExprTest
      import_role:
        name: hsExprTest
      vars:
        pkgs: [QuickCheck, lens, HUnit, graph-wrapper]
        ghc_version: 9.2.1
        configs:
          - user: rc-ib015
          - user: rc-ib016
            extra_pkgs: [mtl, exceptions, base-unicode-symbols, HUnit]

    - name: Auxiliary course helpers
      block:
        - name: Auxiliary course group
          group:
            name: '{{item}}'
            system: true
          loop:
            - ib015-adm
            - fja-adm

        - name: Auxiliary course user
          user:
            name: '{{item}}'
            home: '/home/{{item}}'
            create_home: true
            move_home: true
            system: true
            group: '{{item}}'
          loop:
            - ib015-adm
            - fja-adm

    - name: FCGI handlers
      systemd:
        name: 'fcgiwrap@{{item}}.socket'
        state: started
        enabled: true
      loop:
        - ib015-adm
        - fja-adm
        - www-data

    - name: Extra utilities for course
      apt:
        pkg:
          - python3-tabulate
