# Variables that should be set in group_vars (and their default values)
#
# slurm_db: slurm_acct_db
# port: 6819
# cluster: slurm_cluster


# Because for EC2 prebuilt AMIs would have hostname which we need to update.
- name: Update EC2 Hostname
  hostname:
    name: "{{ ec2_private_dns_name.split('.')[0] }}"
  when: ec2_private_dns_name is defined
  become: yes
  tags: slurm-reconfigure

- block:
  - name: Generate slurm configuration
    local_action: template src="{{item.src}}" dest="{{item.dest}}"
    with_items:
      - { src: 'slurm.conf.template', dest: /tmp/slurm.conf }
      - { src: 'gres.conf.template', dest: /tmp/gres.conf }
    tags: slurm-reconfigure
  run_once: true

- become: yes
  include: sudo.yml

# This step must happen after slurm.conf & gres.conf has been updated.
# If EC2 instance was started from stopped state Slurm thinks now "node unexpectedly rebooted"
# Assumption here Slurm binaries were already installed on AMI.
# Moreover, this step is only relevant for slurm-reconfigure
- block:
  - name: check the current slurm state of the node
    command: "/usr/local/bin/sinfo -h -o '%T' -n {{ec2_private_dns_name.split('.')[0]}}"
    register: slurm_status

  - name: Resume EC2 node in Slurm
    command: scontrol update state=resume nodename="{{ ec2_private_dns_name.split('.')[0] }}" reason=slurm-reconfigure
    when: '"down" in slurm_status.stdout or "drain" in slurm_status.stdout'

  become: yes
  ignore_errors: yes
  when: ec2_id is defined and ("slurm-reconfigure" in ansible_run_tags)
  tags: slurm-reconfigure
