#  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: Include common variables
  include_vars: ../../slurm_manager/vars/main.yml

- name: Give slurm user permission to slurmd spool
  file:
    path: "{{ spool_slurmd_pth }}"
    owner: slurm
    group: slurm
    mode: "{{ tmp_mode }}"
    state: touch

- name: Create log files on compute nodes
  file:
    path: "{{ slurm_logpth }}"
    owner: slurm
    group: slurm
    mode: "{{ tmp_mode }}"
    state: touch
  with_items:
    - slurmd.log

- name: Install firewalld
  package:
    name: firewalld
    state: present
  tags: firewalld

- name: Stop and disable firewalld
  service:
    name: firewalld
    state: stopped
    enabled: no
  tags: firewalld

- name: Copy slurm conf from buffer
  copy:
    src: "{{ buffer_path }}"
    dest: "{{ slurm_confpth }}"
    mode: "{{ slurm_mode }}"

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

- name: Install development tools
  package:
    name: "{{ item }}"
    state: present
  with_items:
    - "{{ dev_tools }}"
  tags: install

- name: Create temporary download folder for slurm
  file:
    path: "{{ tmp_path }}"
    owner: slurm
    group: slurm
    mode: "{{ tmp_mode }}"
    state: directory

- name: Download slurm source
  get_url:
    url: "{{ slurm_url }}"
    dest: "{{ tmp_path }}"
    checksum: "{{ slurm_md5 }}"
    validate_certs: no
  tags: install

- name: Build slurm rpms
  command: rpmbuild -ta "{{ rpmbuild_path }}" --with mysql
  changed_when: false
  args:
    warn: no

- name: Verify package md5
  command: rpm -qa
  ignore_errors: true
  register: verify_result
  changed_when: no
  failed_when: no
  args:
    warn: no

- name: Install rpms
  command: rpm -Uvh ~"{{ rpm_loop }}"
  args:
    chdir: "{{ rpm_path }}"
    warn: no
  changed_when: true

- name: Get the hostname
  command: hostname -s
  register: machine_name
  changed_when: true

- name: Add socket and core info
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "NodeName= Sockets= CoresPerSocket="
    line: "NodeName={{ machine_name.stdout }} Sockets={{ hostvars[inventory_hostname]['ansible_facts']['processor_count'] }}
      CoresPerSocket={{ hostvars[inventory_hostname]['ansible_facts']['processor_cores'] }}"


- name: Save slurm conf in buffer
  fetch:
    src: "{{ slurm_confpth }}"
    dest: "{{ buffer_path }}"
    flat: true

- name: Start slurmd on compute nodes
  systemd:
    name: slurmd.service
    state: started
  tags: install