Add username to sinfo command

This commit is contained in:
Benoît Seignovert 2024-02-14 13:18:21 +01:00
parent 1f4d8e28fa
commit e2e2318c6c
Signed by: Benoît Seignovert
GPG key ID: F5D8895227D18A0B
2 changed files with 19 additions and 7 deletions

View file

@ -59,13 +59,16 @@ class SlurmNode:
self.gpu = SlurmGpu(*re.findall(r'gpu:(\w+):(\d+)', gres)[0] if 'gpu:' in gres else []) self.gpu = SlurmGpu(*re.findall(r'gpu:(\w+):(\d+)', gres)[0] if 'gpu:' in gres else [])
def _sinfo_run() -> str: def _sinfo_run(username: str = None) -> str:
"""SLURM SINFO run command.""" """SLURM SINFO run command."""
flags = '--federation --noheader --responding --cluster=all' flags = '--federation --noheader --responding'
fmt = 'Cluster,PartitionName,NodeHost,StateLong,CPUsState,Memory,Gres' fmt = 'Cluster,PartitionName,NodeHost,StateLong,CPUsState,Memory,Gres'
cmd = shlex.split(f'sinfo {flags} --Format={fmt}') cmd = f'sinfo {flags} --Format={fmt}'
return subprocess.check_output(cmd).decode('utf-8') if username:
cmd = f'su - {username} -c "{cmd}"'
return subprocess.check_output(shlex.split(cmd, posix=False)).decode('utf-8')
def _sinfo_reader(result) -> list: def _sinfo_reader(result) -> list:
@ -73,13 +76,13 @@ def _sinfo_reader(result) -> list:
return [SlurmNode(*re.findall('.{20}', node)) for node in result.splitlines()] return [SlurmNode(*re.findall('.{20}', node)) for node in result.splitlines()]
def sinfo(with_states=('idle', 'mixed')) -> dict: def sinfo(username: str = None, with_states=('idle', 'mixed')) -> dict:
"""SLURM SINFO resources available with a given state(s). """SLURM SINFO resources available with a given state(s).
Grouped by cluster and partition names. Grouped by cluster and partition names.
""" """
resources = _sinfo_reader(_sinfo_run()) resources = _sinfo_reader(_sinfo_run(username=username))
return { return {
cluster: { cluster: {

View file

@ -53,10 +53,19 @@ def test_slurm_sinfo_run(monkeypatch):
'--federation ' '--federation '
'--noheader ' '--noheader '
'--responding ' '--responding '
'--cluster=all '
'--Format=Cluster,PartitionName,NodeHost,StateLong,CPUsState,Memory,Gres' '--Format=Cluster,PartitionName,NodeHost,StateLong,CPUsState,Memory,Gres'
) )
assert _sinfo_run(username='john-doe') == (
'su - john-doe -c "'
'sinfo '
'--federation '
'--noheader '
'--responding '
'--Format=Cluster,PartitionName,NodeHost,StateLong,CPUsState,Memory,Gres'
'"'
)
def test_slurm_sinfo_reader(): def test_slurm_sinfo_reader():
"""Test SLURM SINFO reader.""" """Test SLURM SINFO reader."""