# Copyright 2022 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: Generate key
  command: dd if=/dev/random of="{{ token_dir }}" bs=32 count=1
  changed_when: false

- name: Copy the key to /etc/slurm
  copy:
    src: "{{ token_dir }}"
    dest: "{{ slurm_dir }}"
    owner: slurm
    group: slurm
    mode: "{{ token_mode }}"
    remote_src: yes

- name: Add authentication alt type entry in slurm.conf
  lineinfile:
    path: "{{ slurm_conf_path }}"
    insertafter: 'AuthType=auth/munge\n'
    line: 'AuthAltTypes=auth/jwt'

- name: Add auth alt parameter entry in slurm.conf
  lineinfile:
    path: "{{ slurm_conf_path }}"
    insertafter: 'AuthAltTypes=auth/jwt\n'
    line: 'AuthAltParameters=jwt_key=/etc/slurm/jwt_hs256.key'

- name: Restart slurmctld
  systemd:
    name: slurmctld
    state: restarted
    daemon_reload: yes
    enabled: yes

- name: Create systemd unit file
  copy:
    src: "{{ role_path }}/files/slurm-restd-custom.service"
    dest: "{{ systemd_path_dest }}"
    remote_src: no
    mode: "{{ fil_mode }}"

- name: Fetch hostname
  command: hostname
  register: current_hostname
  changed_when: false

- name: Set fact for server hostname
  set_fact:
    manager_hostname: "{{ current_hostname.stdout }}"

- name: Update the port in service file
  replace:
    path: "{{ systemd_path_dest }}/slurm-restd-custom.service"
    regexp: "0.0.0.0:6820"
    replace: "{{ manager_hostname }}:{{ slurm_restd_port }}"

- name: Add random token
  shell: set -o pipefail && export SLURM_JWT=nvisjvopaekgvgoawkgvpao84r93fjmdkvmdkvm
  changed_when: false

- name: Start services
  systemd:
    name: slurm-restd-custom.service
    state: restarted
    enabled: yes