# TODO: delete /etc/one-context.d/loc-05-grow-rootfs

- name: eval-pv264
  hosts: eval-pv264.fi.muni.cz
  vars:
    dhcpcd: false
    postfix: true
    frag_workers:
        pv264: 2
        pb173llvm: 2
    frag_eval_tools:
      - auto
      - cc
      - group
      - sandbox
      - sub
    llvm11_state: present

  tasks:
    - import_role:
        name: runner_install
      tags:
        - install

    - block:
      - import_role:
          name: common_deb

      - import_role:
          name: llvm_deb

      - name: Install frag requisites
        apt:
          pkg:
            - darcs
            - clang
            - libcurl4-gnutls-dev
            - libexpat1-dev
            - libldap2-dev
            - libacl1-dev
            - libcap-dev

      - name: Postgres key
        apt_key:
          data: |
            -----BEGIN PGP PUBLIC KEY BLOCK-----

[encoded data, 5544 characters]
            Gtz3cydIohvNO9d90+29h0eGEDYti7j7maHkBKUAwlcPvMg5m3Y=
            =DA1T
            -----END PGP PUBLIC KEY BLOCK-----


      - name: Postgres repo
        apt_repository:
          repo: deb http://apt.postgresql.org/pub/repos/apt bullseye-pgdg main
          filename: postgres

      - name: Postgres
        apt:
          pkg:
            - postgresql-client-12
            - libpq-dev
          default_release: bullseye-pgdg

      - name: Frag prereqs
        apt:
          pkg:
            - libunistring-dev


      - name: Frag dir
        file:
          state: directory
          path: /var/frag
          owner: root
          group: systemd-journal
          mode: 02755

      - name: frag checkout exits?
        stat:
           path: /var/frag/frag/_darcs
        register: frag_chekout

      - name: Checkout frag
        command:
          chdir: /var/frag
          cmd: darcs get https://paradise.fi.muni.cz/~xstill/src/frag/
        when: not frag_chekout.stat.exists

      - name: Update frag
        command:
          chdir: /var/frag/frag
          cmd: '{{item}}'
        loop:
          - darcs pull -a
          - env CXX=clang++-12 PWD=/var/frag/frag make
#          - /var/frag/frag/frag-teacher init --subject ib015 --daemon eval --eval-count 8

      - name: Builder user
        user:
          name: builder
          create_home: True
          shell: /bin/bash
          system: True

#       - name: Frag eval users
#         user:
#           name: 'ib015_eval_{{item}}'
#           create_home: True
#           home: '/var/frag/ib015_eval_{{item}}/'
#           shell: /bin/bash
#           system: True
#         loop: '{{range(frag_workers)}}'
# 
#       - name: IB015 evals' env
#         copy:
#           content: |
#             FRAG_SUBJECT=ib015
#             FRAG_HOST=frag-db.fi.muni.cz
#           dest: '/var/frag/ib015_eval_{{item}}/env'
#         loop: '{{range(ib015_workers)}}'
# 
#       - name: IB015 evals' progs
#         copy:
#           src: '/var/frag/frag/eval-{{item[1]}}'
#           dest: '/var/frag/ib015_eval_{{item[0]}}/{{item[1]}}'
#           remote_src: True
#           mode: 0555
#         loop: '{{range(workers_workers) | product(frag_eval_tools) | list}}'
# 
#       - name: IB015 eval deamons
#         copy:
#           src: '/var/frag/frag/frag-eval'
#           dest: '/var/frag/ib015_eval_{{item}}/'
#           remote_src: True
#           mode: 0555
#         loop: '{{range(ib015_workers)}}'
# 
#       - name: Eval service
#         get_url:
#           url: https://gitlab.fi.muni.cz/xstill/frag-utils/-/raw/master/systemd/frag-eval@.service
#           dest: /etc/systemd/system/frag-eval@.service
#         register: eval_service
# 
#       - name: Enable evals
#         systemd:
#           name: 'frag-eval@ib015_eval_{{item}}'
#           enabled: True
#           daemon_reload: '{{eval_service.changed}}'
#         loop: '{{range(ib015_workers)}}'


      - name: Enable user namespaces
        sysctl:
          name: kernel.unprivileged_userns_clone
          value: "1"
          state: present
          reload: yes

      tags:
        - deploy

# vim: shiftwidth=2 tabstop=2 softtabstop=2 ft=yaml.ansible
