#  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: Give slurm user permission to slurmctld spool
  file:
    path: "{{ spool_slurmctld_pth }}"
    owner: slurm
    group: slurm
    mode: "{{ tmp_mode }}"
    state: touch

- name: Give slurm ownership to cluster state
  file:
    path: "{{ cluster_state_path }}"
    owner: slurm
    group: slurm
    mode: "{{ tmp_mode }}"
    state: touch

- name: Create slurmctld log file on manager
  file:
    path: "{{ slurm_logpth }}"
    owner: slurm
    mode: "{{ tmp_mode }}"
    state: touch
  with_items:
    - slurmctld.log

- name: Create log files on manager
  file:
    path: "{{ slurm_logpth }}"
    owner: slurm
    mode: "{{ tmp_mode }}"
    state: touch
  with_items:
    - "{{ log_files_manager }}"

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

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

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

- name: Add control machine name
  lineinfile:
    path: "{{ slurm_confpth }}"
    regexp: "ControlMachine="
    line: "ControlMachine={{ machine_name.stdout }}"

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

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

- name: Start and enable firewalld
  service:
    name: firewalld
    state: started
    enabled: yes
  tags: firewalld

- name: Firewall rule for slurm - tcp/udp ports
  firewalld:
    zone: public
    port: "{{ item }}"
    permanent: true
    state: enabled
  with_items:
    - "{{ tcp_port1 }}"
    - "{{ tcp_port2 }}"
    - "{{ tcp_port3 }}"
    - "{{ tcp_port4 }}"
    - "{{ tcp_port5 }}"
    - "{{ udp_port3 }}"
    - "{{ udp_port1 }}"
    - "{{ udp_port2 }}"
    - "{{ udp_port4 }}"
  when: "'manager' in group_names"
  tags: firewalld

- name: Get network address/subnet mask
  set_fact:
    network_address: "{{ (ansible_default_ipv4.network + '/' + ansible_default_ipv4.netmask) | ipaddr('network/prefix') }}"

- name: Firewall rule slurm - allow all incoming traffic on internal network
  firewalld:
    zone: public
    rich_rule: 'rule family="{{ family }}" source address="{{ network_address }}" accept'
    permanent: true
    state: enabled
  tags: firewalld

- name: Reload firewalld
  command: firewall-cmd --reload
  changed_when: true
  tags: firewalld

- name: Start mariadb
  systemd:
    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 '{{ db_user }}'@'{{
    db_host }}' identified by '{{ hostvars['127.0.0.1']['db_password'] }}'with
    grant option;"
  tags: install
  changed_when: true

- name: Create slurmdbd.conf file
  copy:
    src: slurmdbd.conf
    dest: "{{ slurmdbd_path }}"
    mode: "{{ slurmdbd_mode }}"
    owner: slurm
  tags: install

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

- name: Add db address
  lineinfile:
    path: "{{ slurmdbd_path }}"
    regexp: "DbdAddr="
    line: "DbdAddr={{ dbd_addr }}"

- name: Add db host
  lineinfile:
    path: "{{ slurmdbd_path }}"
    regexp: "DbdHost="
    line: "DbdHost={{ dbd_host }}"

- name: Add storage password
  lineinfile:
    path: "{{ slurmdbd_path }}"
    regexp: "StoragePass="
    line: "StoragePass={{ hostvars['127.0.0.1']['db_password'] }}"

- name: Add storage user
  lineinfile:
    path: "{{ slurmdbd_path }}"
    regexp: "StorageUser="
    line: "StorageUser={{ slurm_user }}"

- name: Add log file path
  lineinfile:
    path: "{{ slurmdbd_path }}"
    regexp: "LogFile="
    line: "LogFile={{ logfile }}"

- name: Add pid file path
  lineinfile:
    path: "{{ slurmdbd_path }}"
    regexp: "PidFile="
    line: "PidFile={{ pidfile }}"

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