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

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

- name: Enable slurmdbd on manager
  systemd:
    name: slurmdbd
    state: restarted
    enabled: yes
  tags: install

- name: Start slurmctld on manager
  systemd:
    name: slurmctld
    state: restarted
    enabled: yes
  tags: install

- name: check slurmdbd is active
  systemd:
    name: slurmdbd
  register: slurmdbd_status
  until: 'slurmdbd_status.status.ActiveState=="active"'
  retries: 20

- name: Show cluster if exists
  command: sacctmgr -n show cluster {{ cluster_name }}
  register: slurm_clusterlist
  changed_when: false

- name: Create slurm cluster
  command: sacctmgr -i add cluster {{ cluster_name }}
  when: not slurm_clusterlist.stdout

- name: Show account
  command: sacctmgr show account -s
  register: account_added
  changed_when: false

- name: Create default slurm group
  command: sacctmgr -i add account defaultgroup Cluster={{ cluster_name }} Description="Default Account" Organization="Default Org"
  when: account_added.rc != 0

- name: Check if user exists
  command: sacctmgr show user -s
  register: user_added
  changed_when: false

- name: Add root to the default account
  command: sacctmgr -i add user root DefaultAccount=defaultgroup
  when: user_added.rc != 0