---
- name: create munge group
  group: name=munge system=yes gid=498
  sudo: true

- name: create munge user
  user: name=munge group=munge system=yes createhome=no uid=498
  sudo: true

- name: create slurm group
  group: name=slurm system=yes gid=497
  sudo: true

- name: create slurm user
  user: name=slurm group=slurm system=yes createhome=no uid=497
  sudo: true

- name: make sure slurm conf dir exists
  file: dest={{ slurm_dir }}/etc state=directory
  sudo: true

- name: make sure slurm lock dir exists
  file: dest=/var/lock/subsys state=directory owner=root group=root mode=755
  sudo: true

- name: create data directory
  file: path={{ slurmdatadir }} state=directory owner=slurm group=slurm mode=755
  sudo: true
  when: slurmdatadir is defined 

- name: create run directory
  file: path={{ slurmpiddir }} state=directory owner=root group=root mode=750
  sudo: true
  when: slurmpiddir is defined 

- name: create state directory
  file: path={{ slurmstatedir }} state=directory owner=slurm group=slurm mode=750

  sudo: true
  when: slurmstatedir is defined 

- name: create log directory
  file: path={{ slurmlogdir }} state=directory owner=slurm group=slurm mode=750
  sudo: true
  when: slurmlogdir is defined 

- name: create greps directory
  file: path={{ slurm_dir }}/etc/gres state=directory owner=slurm group=slurm mode=755
  sudo: true

- name: install deps
  yum: name={{ item }} state=latest
  with_items:
    - perl
    - perl-DBI
    - openssl-devel
    - mysql
    - mysql-devel
    - gcc
    - rpm-build
    - wget
    - openssl-devel
    - readline-devel
    - pam-devel
    - perl-ExtUtils-MakeMaker
    - bzip2-devel
    - hwloc
    - hwloc-devel
    - lua
    - lua-devel
  sudo: true
  when: ansible_os_family == "RedHat"

- name: install deps
  apt: name={{ item }} state=installed update_cache=yes
  sudo: true
  with_items:
    - gcc
    - wget
    - libssl-dev
    - libpam0g-dev
    - libbz2-dev
    - make
    - perl
    - libdbi-perl
    - mysql-server
    - mysql-client
    - python-mysqldb
    - libmysqlclient-dev
    - lua5.2
  when: ansible_os_family == "Debian"

- include: installMungeFromSource.yml

- name: chown mungedir
  file: path={{ munge_dir }} state=directory owner=munge recurse=yes
  sudo: true

- name: make munge logdir
  file: path={{ munge_dir }}/var/log/munge state=directory owner=munge mode=700
  sudo: true

- name: install munge key
  template: src=munge_key.j2 dest={{ munge_dir }}/etc/munge/munge.key owner=munge mode=600
  sudo: true
  notify: restart munge

- name: enable munge on boot
  service: name=munge enabled=yes
  sudo: true

- name: start munge
  service: name=munge state=started
  sudo: true

- include: installSlurmFromSource.yml

- name: check slurm generic resource
  shell: "{{ slurm_gres_check }}"
  register: slurm_generic_resource
  ignore_errors: true
  when: slurm_gres_check is defined

- name: install gres config file 
  template: src=gres.conf.j2 dest={{ slurm_dir }}/etc/gres.conf mode=644
  sudo: true
  when: slurm_generic_resource is defined and slurm_generic_resource.stdout 

- name: install gres sub config file 
  template: src=gres_sub.conf.j2 dest={{ slurm_dir }}/etc/gres/gres.conf mode=644
  sudo: true
  when: slurm_gres_list is defined

- name: install slurm.conf
  template: src=slurm.conf.j2 dest={{ slurm_dir }}/etc/slurm.conf
  sudo: true
  notify: restart slurm
  when: slurm_use_vpn==False

- name: install slurm.conf
  template: src=slurm-vpn.conf.j2 dest={{ slurm_dir }}/etc/slurm.conf
  sudo: true
  when: slurm_use_vpn==True

- name: setup envirnment variables 
  template: src=slurm_setup.sh.j2 dest=/etc/profile.d/slurm_setup.sh
  sudo: true
  when: ansible_os_family == 'RedHat' 

- name: setup plugin
  template: src=job_submit.lua.j2 dest={{ slurm_dir }}/etc/job_submit.lua
  delegate_to: "{{ slurmctrl }}"
  run_once: true
  sudo: true
  when: slurm_lua is defined

- include: installCgroup.yml
- include: installNhc.yml

