Skip to content

Commit

Permalink
Update the Slurm version retrieving in Slurm parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
soline-b committed Nov 7, 2023
1 parent c987d43 commit 737de29
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 20 deletions.
1 change: 1 addition & 0 deletions slurm_state/helpers/clusters_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def _load_clusters_from_config():

clusters_valid.add_field("sacct_path", optional_string)
clusters_valid.add_field("sinfo_path", optional_string)
clusters_valid.add_field("slurm_version", optional_string, default=None)

# Load the clusters from the configuration file, asserting that it uses the
# predefined format
Expand Down
8 changes: 4 additions & 4 deletions slurm_state/mongo_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ def main_read_report_and_update_collection(
# Initialize the time of this operation's beginning
timestamp_start = time.time()

# Retrieve clusters data from the configuration file
clusters = get_all_clusters()
assert cluster_name in clusters

# Check the input parameters
assert entity in ["jobs", "nodes"]

Expand All @@ -164,10 +168,6 @@ def main_read_report_and_update_collection(
f'Incorrect value for entity in main_read_sacct_and_update_collection: "{entity}" when it should be "jobs" or "nodes".'
)

# Retrieve clusters data from the configuration file
clusters = get_all_clusters()
assert cluster_name in clusters

## Retrieve entities ##

# Generate a report file if required
Expand Down
8 changes: 4 additions & 4 deletions slurm_state/parsers/job_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
class JobParser(SlurmParser):
""" """

def __init__(self, cluster_name):
super().__init__("jobs", "sacct", cluster_name)
def __init__(self, cluster_name, slurm_version=None):
super().__init__("jobs", "sacct", cluster_name, slurm_version=slurm_version)

def generate_report(self, file_name):

Expand Down Expand Up @@ -58,9 +58,9 @@ def generate_report(self, file_name):

def parser(self, f):
""" """
if re.search(r"^slurm 22\..*$", self.slurm_version):
if re.search(r"^22\..*$", self.slurm_version):
return self.parser_v22_and_23(f)
elif re.search(r"^slurm 23\..*$", self.slurm_version):
elif re.search(r"^23\..*$", self.slurm_version):
return self.parser_v22_and_23(f)
else:
raise Exception(
Expand Down
6 changes: 3 additions & 3 deletions slurm_state/parsers/node_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
class NodeParser(SlurmParser):
""" """

def __init__(self, cluster_name):
super().__init__("nodes", "sinfo", cluster_name)
def __init__(self, cluster_name, slurm_version=None):
super().__init__("nodes", "sinfo", cluster_name, slurm_version=slurm_version)

def generate_report(self, file_name):
# The command to be launched through SSH is "sinfo --json"
Expand All @@ -27,7 +27,7 @@ def generate_report(self, file_name):

def parser(self, f):
""" """
if re.search("^slurm 22\..*$", self.slurm_version):
if re.search(r"^22\..*$", self.slurm_version):
return self.parser_v22(f)
else:
raise Exception(
Expand Down
36 changes: 27 additions & 9 deletions slurm_state/parsers/slurm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
from slurm_state.helpers.clusters_helper import get_all_clusters

# Common imports
import os
import os, re


class SlurmParser:
"""
A parser for Slurm entities
"""

def __init__(self, entity, slurm_command, cluster_name):
def __init__(self, entity, slurm_command, cluster_name, slurm_version=None):
self.entity = entity
assert entity in ["jobs", "nodes"]

Expand All @@ -31,18 +31,36 @@ def __init__(self, entity, slurm_command, cluster_name):
self.slurm_command
), f"Error. The {self.slurm_command}_path configuration needs to end with '{self.slurm_command}'. It is currently {self.slurm_command_path} ."

# Retrieve the version of Slurm installed on the current cluster
self.slurm_version = self.get_slurm_version()
if slurm_version is not None:
self.slurm_version = slurm_version
else:
# If no Slurm version is provided, retrieve the version of Slurm installed on the current cluster
self.slurm_version = self.get_slurm_version()

def get_slurm_version(self):
"""
Get the Slurm version
"""
# Launch the sacct or sinfo command to get its version
remote_command = f"{self.slurm_command_path} -V"
response = self.launch_slurm_command(remote_command)
assert len(response) == 1
return response[0]
if (
"slurm_version" in self.cluster
and self.cluster["slurm_version"] is not None
):
# If the Slurm version has been added to the configuration file,
# return the value of the configuration
return self.cluster["slurm_version"]
else:
print("3")
# Launch the sacct or sinfo command to get its version
remote_command = f"{self.slurm_command_path} -V"
response = self.launch_slurm_command(remote_command)
assert len(response) == 1
version_regex = re.compile(r"^slurm (\d+\.\d+\.\d+)$")
if m := version_regex.match(response):
return m.group(1)
# If the version has not been identified, raise an error
raise Exception(
f'The version "{response}" has not been recognized as a Slurm version.'
)

def launch_slurm_command(self, remote_command):
""" """
Expand Down
2 changes: 2 additions & 0 deletions test_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ remote_hostname="clockwork-stats"
sacct_enabled=true
sacct_path="/opt/slurm/bin/sacct"
sinfo_path="/opt/slurm/bin/sinfo"
slurm_version="22.05.9"
ssh_key_filename="id_clockwork"
ssh_port=22

Expand Down Expand Up @@ -110,6 +111,7 @@ remote_hostname="cedar.computecanada.ca"
sacct_enabled=false
sacct_path="/opt/software/slurm/bin/sacct"
sinfo_path="/opt/software/slurm/bin/sinfo"
slurm_version="23.02.6"
ssh_key_filename="id_clockwork"
ssh_port=22

Expand Down

0 comments on commit 737de29

Please sign in to comment.