#  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: Get hostname
  command: hostname
  register: host_name
  changed_when: true

- name: Add host name in file
  replace:
    dest: "{{ hostname_dest }}"
    regexp: localhost.localdomain
    replace: "{{ host_name.stdout }}"
    backup: yes
    mode: "{{ common_mode }}"

- name: Install packages for slurm
  package:
    name: "{{ common_packages }}"
    state: present
  tags: install

- name: Create munge key
  command: "{{ munge_cmd }}"
  changed_when: true

- name: Copy munge key
  copy:
    src: munge.key
    dest: "{{ munge_dest }}"
    owner: munge
    group: munge
    mode: "{{ munge_mode }}"
  tags: install

- name: Slurm configuration - slurm.conf
  copy:
    src: slurm.conf
    dest: "{{ slurm_dest }}"
    mode: "{{ slurm_mode }}"
  tags: install

- name: Add cluster name
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "ClusterName="
    line: "ClusterName={{ cluster_name }}"

- name: Add slurm user name
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "SlurmUser="
    line: "SlurmUser={{ slurm_user }}"

- name: Add slurmctld port no
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "SlurmctldPort="
    line: "SlurmctldPort={{ slurmctld_port }}"

- name: Add slurmd port no
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "SlurmdPort="
    line: "SlurmdPort={{ slurmd_port }}"

- name: Add srun port range
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "SrunPortRange="
    line: "SrunPortRange={{ srun_port_range }}"

- name: Add spool path
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "SlurmdSpoolDir="
    line: "SlurmdSpoolDir={{ spool_pth }}"

- name: Add slurmctld pid file path
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "SlurmctldPidFile="
    line: "SlurmctldPidFile={{ slurmctld_pid }}"

- name: Add slurmd pid file path
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "SlurmdPidFile="
    line: "SlurmdPidFile={{ slurmd_pid }}"

- name: Add slurmctld log file path
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "SlurmctldLogFile="
    line: "SlurmctldLogFile={{ slurmctld_log }}"

- name: Add slurmd log file path
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "SlurmdLogFile="
    line: "SlurmdLogFile={{ slurmd_log }}"

- name: Add accounting storage port no
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "AccountingStoragePort="
    line: "AccountingStoragePort={{ acct_port }}"

- name: Create slurm group
  group:
    name: slurm
    state: present
  tags: install

- name: Add the user 'slurm' with uid 6001 and a primary group of 'slurm'
  user:
    name: slurm
    comment: Slurm User Account
    uid: "{{ slurm_uid }}"
    group: slurm
  tags: install

- name: Create slurm log directory
  file:
    path: "{{ slurm_logpth }}"
    state: directory
    owner: slurm
    group: slurm
    mode: "{{ gen_mode }}"
    recurse: yes
  tags: install

- name: Give slurm user permission to spool
  file:
    path: "{{ spool_pth }}"
    owner: slurm
    group: slurm
    state: directory
    mode: "{{ gen_mode }}"
    recurse: yes

- name: Give slurm user permission to spool directory
  file:
    path: "{{ spool_dir }}"
    owner: slurm
    group: slurm
    state: directory
    mode: "{{ common_mode }}"
    recurse: yes

- name: Create slurm pid directory
  file:
    path: "{{ slurm_pidpth }}"
    state: directory
    owner: slurm
    group: slurm
    mode: "{{ gen_mode }}"
    recurse: yes
  tags: install

- name: Give slurm user permission to slurmctld
  file:
    path: "{{ slurmctld_pid }}"
    owner: slurm
    group: slurm
    mode: "{{ gen_mode }}"
    state: touch

- name: Give slurm user permission to slurmd
  file:
    path: "{{ slurmd_pid }}"
    owner: slurm
    group: slurm
    mode: "{{ gen_mode }}"
    state: touch

- name: Start munge service
  systemd:
    name: munge
    state: restarted
    enabled: yes
  tags: install
  failed_when: false