#  Copyright 2020 Dell Inc. or its subsidiaries. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

---
- name: create download folder
  file:
    path: /root/Downloads
    state: directory
    mode: '0755'
- name: Download Slurm source
  get_url:
    url: "{{ slurm_url }}"
    dest: /root/Downloads/
    checksum: "{{ slurm_md5 }}"
    validate_certs: no    
  tags: install

- name: Build SLURM RPMs
  command: rpmbuild -ta /root/Downloads/slurm-20.02.0.tar.bz2
  tags: install

- name: Copy RPMs to NFS share
  copy:
    src: "{{ item }}"
    dest: /home/rpms/
  with_fileglob:
    - /root/rpmbuild/RPMS/x86_64/slurm*20*.rpm
  tags: install

- name: Install SLURM RPMs on Manager
  yum:
    name: "{{ item }}"
    #name: "{{ query('fileglob', ['/home/rpms/slurm*20*.rpm']) }}" <-- how it should work to avoid loop
  with_fileglob:
    - /home/rpms/slurm*20*.rpm
  tags: install

- name: Firewall Rule slurm allow 6817/tcp
  command: firewall-cmd  --zone=internal --add-port=6817/tcp --permanent
  tags: install

- name: Firewall Rule slurm allow 6818/tcp
  command: firewall-cmd  --zone=internal --add-port=6818/tcp --permanent
  tags: install

- name: Firewall Rule slurm allow 6819/tcp
  command: firewall-cmd  --zone=internal --add-port=6819/tcp --permanent
  tags: install

- name: Firewall Rule slurm allow all incoming traffic on internal network
  command: firewall-cmd --permanent --zone=internal --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" accept'
  tags: install

- name: Firewall Reload
  command: firewall-cmd  --reload
  tags: install


- name: Start MariaDB
  service:
    name: mariadb
    state: restarted
    enabled: yes
  tags: install

- name: Grant Permissions for SLURM DB
  command: mysql -u root -e "GRANT ALL ON slurm_acct_db.* TO 'slurm'@'localhost' identified by 'password' with grant option;"
  tags: install

- name: Create slurmdbd.conf file
  copy:
    src: /etc/slurm/slurmdbd.conf.example
    dest: /etc/slurm/slurmdbd.conf
    mode: 0600
  tags: install

- name: Populate Accounting Database
  command: slurmdbd
  tags: install

- name: Create Slurm Cluster
  command: sacctmgr -i add cluster {{inventory_hostname}}
  tags: install

- name: Create Default Slurm Group
  command: sacctmgr -i add account defaultgroup Cluster={{inventory_hostname}} Description="Default Account" Organization="Default Org"
  tags: install

- name: Add root to the Default Account
  command: sacctmgr -i add user root DefaultAccount=defaultgroup
  tags: install

- name: Start slurmctld on Manager
  service:
    name: slurmctld
    state: restarted
    enabled: yes
  tags: install

- name: Enable Slurmdbd on Manager
  service:
    name: slurmdbd
    state: restarted
    enabled: yes
  tags: install