slurm-cli 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- slurm_cli/__init__.py +17 -0
- slurm_cli/cli.py +5113 -0
- slurm_cli/py.typed +0 -0
- slurm_cli/utils/__init__.py +1 -0
- slurm_cli/utils/accounts.py +544 -0
- slurm_cli/utils/associations.py +871 -0
- slurm_cli/utils/autocomplete_helpers.py +266 -0
- slurm_cli/utils/base_resource.py +349 -0
- slurm_cli/utils/config.py +88 -0
- slurm_cli/utils/coordinators.py +426 -0
- slurm_cli/utils/events.py +695 -0
- slurm_cli/utils/job_filter.py +551 -0
- slurm_cli/utils/jobs.py +854 -0
- slurm_cli/utils/node_filter.py +778 -0
- slurm_cli/utils/nodes.py +774 -0
- slurm_cli/utils/partitions.py +837 -0
- slurm_cli/utils/prefix_utils.py +1050 -0
- slurm_cli/utils/profiles.py +1360 -0
- slurm_cli/utils/qos.py +883 -0
- slurm_cli/utils/reservations.py +665 -0
- slurm_cli/utils/resources.py +341 -0
- slurm_cli/utils/slurm_config.py +73 -0
- slurm_cli/utils/users.py +649 -0
- slurm_cli/utils/utils.py +21 -0
- slurm_cli-1.0.0.dist-info/METADATA +216 -0
- slurm_cli-1.0.0.dist-info/RECORD +29 -0
- slurm_cli-1.0.0.dist-info/WHEEL +4 -0
- slurm_cli-1.0.0.dist-info/entry_points.txt +3 -0
- slurm_cli-1.0.0.dist-info/licenses/LICENSE +22 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import re
|
|
4
|
+
import subprocess
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
# from enum import Enum
|
|
8
|
+
from typing import Any, Dict, List, Tuple, Union
|
|
9
|
+
|
|
10
|
+
from .utils import console
|
|
11
|
+
|
|
12
|
+
SPINNER_STYLE = "dots2"
|
|
13
|
+
# moon, dots, simpleDots, dots2, dots12
|
|
14
|
+
# noise, line
|
|
15
|
+
SPINNER_SPEED = 1.0
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Resource:
|
|
19
|
+
CACHE_TIMEOUT = 600
|
|
20
|
+
CACHE_DIR = "/tmp/"
|
|
21
|
+
CACHE_FILES = {
|
|
22
|
+
"nodes": f"{CACHE_DIR}slurm_cli_nodes.json",
|
|
23
|
+
"partitions": f"{CACHE_DIR}slurm_cli_partitions.json",
|
|
24
|
+
"jobs": f"{CACHE_DIR}slurm_cli_jobs.json",
|
|
25
|
+
"users": f"{CACHE_DIR}slurm_cli_users.json",
|
|
26
|
+
"qos": f"{CACHE_DIR}slurm_cli_qos.json",
|
|
27
|
+
"accounts": f"{CACHE_DIR}slurm_cli_accounts.json",
|
|
28
|
+
"associations": f"{CACHE_DIR}slurm_cli_associations.json",
|
|
29
|
+
"reservations": f"{CACHE_DIR}slurm_cli_reservations.json",
|
|
30
|
+
"coordinators": f"{CACHE_DIR}slurm_cli_coordinators.json",
|
|
31
|
+
"config": f"{CACHE_DIR}slurm_cli_config.json",
|
|
32
|
+
}
|
|
33
|
+
CACHE_LIST_FILES = {
|
|
34
|
+
"nodes": f"{CACHE_DIR}slurm_cli_nodes.list",
|
|
35
|
+
"partitions": f"{CACHE_DIR}slurm_cli_partitions.list",
|
|
36
|
+
"jobs": f"{CACHE_DIR}slurm_cli_jobs.list",
|
|
37
|
+
"users": f"{CACHE_DIR}slurm_cli_users.list",
|
|
38
|
+
"qos": f"{CACHE_DIR}slurm_cli_qos.list",
|
|
39
|
+
"accounts": f"{CACHE_DIR}slurm_cli_accounts.list",
|
|
40
|
+
"associations": f"{CACHE_DIR}slurm_cli_associations.list",
|
|
41
|
+
"reservations": f"{CACHE_DIR}slurm_cli_reservations.list",
|
|
42
|
+
"coordinators": f"{CACHE_DIR}slurm_cli_coordinators.list",
|
|
43
|
+
"config": f"{CACHE_DIR}slurm_cli_config.list",
|
|
44
|
+
}
|
|
45
|
+
CACHE_CMD = {
|
|
46
|
+
"nodes": ["scontrol", "show", "nodes", "--json"],
|
|
47
|
+
"partitions": ["scontrol", "show", "partitions", "--details"],
|
|
48
|
+
"jobs": ["scontrol", "show", "job", "--json"],
|
|
49
|
+
"users": ["sacctmgr", "show", "users", "--json"],
|
|
50
|
+
"qos": ["sacctmgr", "show", "qos", "--json"],
|
|
51
|
+
"accounts": ["sacctmgr", "show", "accounts", "--json"],
|
|
52
|
+
"associations": ["sacctmgr", "show", "associations", "--json"],
|
|
53
|
+
"reservations": ["scontrol", "show", "reservations", "--json"],
|
|
54
|
+
"coordinators": ["sacctmgr", "show", "coordinators", "--json"],
|
|
55
|
+
"config": ["scontrol", "show", "config", "--json"],
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@classmethod
|
|
59
|
+
def set_cache_timeout(cls, timeout: int) -> None:
|
|
60
|
+
"""Set the cache timeout."""
|
|
61
|
+
cls.CACHE_TIMEOUT = timeout
|
|
62
|
+
|
|
63
|
+
@classmethod
|
|
64
|
+
def guess_resource_type(
|
|
65
|
+
cls, name: str, force_update: bool = False
|
|
66
|
+
) -> Tuple[str, Union[Dict[str, Any], None]]:
|
|
67
|
+
"""Guess the resource type from the resource name."""
|
|
68
|
+
if name[:1] == "j" or re.match(r"^[0-9_]+$", name):
|
|
69
|
+
return "jobs", None
|
|
70
|
+
part = cls.cached_resource_list("partitions")
|
|
71
|
+
if name[:4] == "part" or (part and name in part):
|
|
72
|
+
return "partitions", Resource.cached_resource(
|
|
73
|
+
"partitions", force_update
|
|
74
|
+
)
|
|
75
|
+
node = cls.cached_resource_list("nodes")
|
|
76
|
+
if name[:4] == "node" or (node and name in node):
|
|
77
|
+
return "nodes", Resource.cached_resource(
|
|
78
|
+
"nodes", force_update
|
|
79
|
+
)
|
|
80
|
+
user = cls.cached_resource_list("users")
|
|
81
|
+
if name[:4] == "user" or (user and name in user):
|
|
82
|
+
return "users", Resource.cached_resource(
|
|
83
|
+
"users", force_update
|
|
84
|
+
)
|
|
85
|
+
qos = cls.cached_resource_list("qos")
|
|
86
|
+
if name[:3] == "qos" or (qos and name in qos):
|
|
87
|
+
return "qos", Resource.cached_resource("qos", force_update)
|
|
88
|
+
account = cls.cached_resource_list("accounts")
|
|
89
|
+
if name[:3] == "acc" or (account and name in account):
|
|
90
|
+
return "accounts", Resource.cached_resource(
|
|
91
|
+
"accounts", force_update
|
|
92
|
+
)
|
|
93
|
+
reservation = cls.cached_resource_list("reservations")
|
|
94
|
+
if name[:3] == "res" or (reservation and name in reservation):
|
|
95
|
+
return "reservations", Resource.cached_resource(
|
|
96
|
+
"reservations", force_update
|
|
97
|
+
)
|
|
98
|
+
# Check for coordinator prefix (don't fetch list yet to avoid errors)
|
|
99
|
+
if name[:5] == "coord":
|
|
100
|
+
return "coordinators", Resource.cached_resource(
|
|
101
|
+
"coordinators", force_update
|
|
102
|
+
)
|
|
103
|
+
if name[:4] == "prob":
|
|
104
|
+
return "problems", {}
|
|
105
|
+
elif name[:4] == "stat":
|
|
106
|
+
return "stats", {}
|
|
107
|
+
elif name[:4] == "assoc":
|
|
108
|
+
return "associations", {}
|
|
109
|
+
elif name[:4] == "dump":
|
|
110
|
+
return "dump", {}
|
|
111
|
+
elif name[:2] == "ev":
|
|
112
|
+
return "events", {}
|
|
113
|
+
elif name[:3] == "lic" or name[:4] == "reso":
|
|
114
|
+
return "licenses", {}
|
|
115
|
+
elif name[:3] == "bad" or name[:3] == "runa":
|
|
116
|
+
return "runawayjobs", {}
|
|
117
|
+
elif name[:3] == "tra":
|
|
118
|
+
return "transactions", {}
|
|
119
|
+
elif name[:2] == "tr":
|
|
120
|
+
return "tres", {}
|
|
121
|
+
elif name[:2] == "ar":
|
|
122
|
+
return "archive", {}
|
|
123
|
+
|
|
124
|
+
# If name looks like a username (alphanumeric with
|
|
125
|
+
# underscores/hyphens), assume it's a user - this handles cases
|
|
126
|
+
# where user isn't in cached list
|
|
127
|
+
if re.match(r"^[a-zA-Z][a-zA-Z0-9_-]*$", name):
|
|
128
|
+
return "users", Resource.cached_resource(
|
|
129
|
+
"users", force_update
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
return "unknown", None
|
|
133
|
+
|
|
134
|
+
@classmethod
|
|
135
|
+
def update_cache(cls, name: str) -> Dict[str, Any]:
|
|
136
|
+
list_data = None # For the list file (names/IDs)
|
|
137
|
+
if name == "partitions":
|
|
138
|
+
raw_data = cls.run_cmd(cls.CACHE_CMD[name])
|
|
139
|
+
if raw_data:
|
|
140
|
+
raw_data = cls.partitions2json(raw_data)
|
|
141
|
+
else:
|
|
142
|
+
raw_data = {}
|
|
143
|
+
console.print(f"[red]Failed to fetch partitions data.[/red]")
|
|
144
|
+
elif name == "reservations":
|
|
145
|
+
raw_data = cls.run_cmd_json(cls.CACHE_CMD[name])
|
|
146
|
+
if raw_data:
|
|
147
|
+
raw_data = {
|
|
148
|
+
hash.pop("name"): hash
|
|
149
|
+
for hash in raw_data["reservations"]
|
|
150
|
+
}
|
|
151
|
+
else:
|
|
152
|
+
raw_data = {}
|
|
153
|
+
console.print(f"[red]Failed to fetch reservations data.[/red]")
|
|
154
|
+
elif name == "nodes":
|
|
155
|
+
raw_data = cls.run_cmd_json(cls.CACHE_CMD[name])
|
|
156
|
+
if raw_data:
|
|
157
|
+
raw_data = {
|
|
158
|
+
hash.pop("name"): hash for hash in raw_data["nodes"]
|
|
159
|
+
}
|
|
160
|
+
else:
|
|
161
|
+
raw_data = {}
|
|
162
|
+
console.print(f"[red]Failed to fetch nodes data.[/red]")
|
|
163
|
+
elif name == "jobs":
|
|
164
|
+
raw_data = cls.run_cmd_json(cls.CACHE_CMD[name])
|
|
165
|
+
if not raw_data:
|
|
166
|
+
raw_data = {}
|
|
167
|
+
console.print(f"[red]Failed to fetch jobs data.[/red]")
|
|
168
|
+
# Jobs are stored as {"jobs": [...]} for jq compatibility
|
|
169
|
+
# List file needs job IDs, not dict keys
|
|
170
|
+
if raw_data and "jobs" in raw_data:
|
|
171
|
+
list_data = [
|
|
172
|
+
str(j.get("job_id", ""))
|
|
173
|
+
for j in raw_data["jobs"]
|
|
174
|
+
if j.get("job_id")
|
|
175
|
+
]
|
|
176
|
+
else:
|
|
177
|
+
raw_data = cls.run_cmd_json(cls.CACHE_CMD[name])
|
|
178
|
+
if raw_data:
|
|
179
|
+
with open(cls.CACHE_FILES[name], "w") as f:
|
|
180
|
+
json.dump(raw_data, f)
|
|
181
|
+
os.chmod(cls.CACHE_FILES[name], 0o600)
|
|
182
|
+
with open(cls.CACHE_LIST_FILES[name], "w") as f:
|
|
183
|
+
if list_data is not None:
|
|
184
|
+
json.dump(list_data, f)
|
|
185
|
+
else:
|
|
186
|
+
json.dump(list(raw_data.keys()), f)
|
|
187
|
+
os.chmod(cls.CACHE_LIST_FILES[name], 0o600)
|
|
188
|
+
else:
|
|
189
|
+
console.print(f"[red]Failed to fetch {name} data.[/red]")
|
|
190
|
+
raw_data = {}
|
|
191
|
+
return raw_data
|
|
192
|
+
|
|
193
|
+
@classmethod
|
|
194
|
+
def cached_resource(
|
|
195
|
+
cls,
|
|
196
|
+
name: str,
|
|
197
|
+
force_update: bool = False,
|
|
198
|
+
) -> Union[Dict[str, Any], None]:
|
|
199
|
+
"""Check if the resource is a cached resource."""
|
|
200
|
+
file = cls.CACHE_FILES.get(name)
|
|
201
|
+
if not file:
|
|
202
|
+
return None
|
|
203
|
+
if not force_update and os.path.exists(file):
|
|
204
|
+
file_mtime = os.path.getmtime(file)
|
|
205
|
+
if time.time() - file_mtime < cls.CACHE_TIMEOUT:
|
|
206
|
+
with open(cls.CACHE_FILES[name], "r") as f:
|
|
207
|
+
raw_data = json.load(f)
|
|
208
|
+
# The cache file already has the correct
|
|
209
|
+
# structure (name -> data)
|
|
210
|
+
# so we can use it directly
|
|
211
|
+
return raw_data
|
|
212
|
+
|
|
213
|
+
# Update cache and check again
|
|
214
|
+
with console.status(
|
|
215
|
+
"[bold blue]Calling slurm...",
|
|
216
|
+
spinner=SPINNER_STYLE,
|
|
217
|
+
spinner_style="yellow",
|
|
218
|
+
speed=SPINNER_SPEED,
|
|
219
|
+
):
|
|
220
|
+
data = cls.update_cache(name)
|
|
221
|
+
return data
|
|
222
|
+
|
|
223
|
+
@classmethod
|
|
224
|
+
def cached_resource_list(
|
|
225
|
+
cls,
|
|
226
|
+
name: str,
|
|
227
|
+
) -> List[str]:
|
|
228
|
+
"""Check if the resource is a cached resource."""
|
|
229
|
+
file = cls.CACHE_LIST_FILES.get(name)
|
|
230
|
+
if not file:
|
|
231
|
+
return []
|
|
232
|
+
if not os.path.exists(file):
|
|
233
|
+
# just update!
|
|
234
|
+
cls.cached_resource(name)
|
|
235
|
+
try:
|
|
236
|
+
with open(file, "r") as f:
|
|
237
|
+
raw_data = json.load(f)
|
|
238
|
+
return raw_data
|
|
239
|
+
except Exception:
|
|
240
|
+
return []
|
|
241
|
+
return []
|
|
242
|
+
|
|
243
|
+
@classmethod
|
|
244
|
+
def run_cmd_json(cls, cmd: List[str]) -> Dict[str, Any] | None:
|
|
245
|
+
"""Run a command and return the JSON output."""
|
|
246
|
+
result = None
|
|
247
|
+
try:
|
|
248
|
+
result = subprocess.run(
|
|
249
|
+
cmd,
|
|
250
|
+
check=True,
|
|
251
|
+
capture_output=True,
|
|
252
|
+
text=True,
|
|
253
|
+
)
|
|
254
|
+
if result.stdout:
|
|
255
|
+
json_text = json.loads(result.stdout)
|
|
256
|
+
return json_text
|
|
257
|
+
except (
|
|
258
|
+
subprocess.CalledProcessError,
|
|
259
|
+
FileNotFoundError,
|
|
260
|
+
ValueError,
|
|
261
|
+
) as e:
|
|
262
|
+
console.print(
|
|
263
|
+
f"[red]Failed to run command '{cmd}':[/red]" f" {e}"
|
|
264
|
+
)
|
|
265
|
+
if result and result.stderr:
|
|
266
|
+
console.print(result.stderr)
|
|
267
|
+
if result and result.stdout:
|
|
268
|
+
console.print(result.stdout)
|
|
269
|
+
exit(1)
|
|
270
|
+
return None
|
|
271
|
+
|
|
272
|
+
@classmethod
|
|
273
|
+
def run_cmd(cls, cmd: Union[str, List[str]]) -> Union[str, None]:
|
|
274
|
+
"""Run a command and return the output."""
|
|
275
|
+
result = None
|
|
276
|
+
if not isinstance(cmd, List):
|
|
277
|
+
# cmd = " ".join(cmd)
|
|
278
|
+
cmd = [cmd]
|
|
279
|
+
try:
|
|
280
|
+
result = subprocess.run(
|
|
281
|
+
cmd,
|
|
282
|
+
check=True,
|
|
283
|
+
capture_output=True,
|
|
284
|
+
text=True,
|
|
285
|
+
)
|
|
286
|
+
return result.stdout
|
|
287
|
+
except subprocess.CalledProcessError as e:
|
|
288
|
+
console.print(
|
|
289
|
+
f"[red]Failed to run command '{cmd}':[/red]" f" {e}"
|
|
290
|
+
)
|
|
291
|
+
if result and result.stderr:
|
|
292
|
+
console.print(result.stderr)
|
|
293
|
+
if result and result.stdout:
|
|
294
|
+
console.print(result.stdout)
|
|
295
|
+
return None
|
|
296
|
+
|
|
297
|
+
@classmethod
|
|
298
|
+
def partitions2json(cls, output: str) -> dict:
|
|
299
|
+
"""Convert partitions to JSON.
|
|
300
|
+
|
|
301
|
+
Partition data is multi-line format:
|
|
302
|
+
PartitionName=xxx
|
|
303
|
+
Key1=Value1 Key2=Value2
|
|
304
|
+
Key3=Value3
|
|
305
|
+
(empty line)
|
|
306
|
+
"""
|
|
307
|
+
partitions = {}
|
|
308
|
+
current_partition = None
|
|
309
|
+
current_data = {}
|
|
310
|
+
|
|
311
|
+
for line in output.splitlines():
|
|
312
|
+
line = line.strip()
|
|
313
|
+
|
|
314
|
+
# Empty line marks end of a partition block
|
|
315
|
+
if not line:
|
|
316
|
+
if current_partition:
|
|
317
|
+
partitions[current_partition] = current_data
|
|
318
|
+
current_partition = None
|
|
319
|
+
current_data = {}
|
|
320
|
+
continue
|
|
321
|
+
|
|
322
|
+
# Parse key=value pairs from the line
|
|
323
|
+
for pair in line.split():
|
|
324
|
+
if "=" in pair:
|
|
325
|
+
key, value = pair.split("=", 1)
|
|
326
|
+
|
|
327
|
+
# Check if this starts a new partition
|
|
328
|
+
if key == "PartitionName":
|
|
329
|
+
# Save previous partition if any
|
|
330
|
+
if current_partition:
|
|
331
|
+
partitions[current_partition] = current_data
|
|
332
|
+
current_partition = value
|
|
333
|
+
current_data = {}
|
|
334
|
+
else:
|
|
335
|
+
current_data[key] = value
|
|
336
|
+
|
|
337
|
+
# Don't forget the last partition
|
|
338
|
+
if current_partition:
|
|
339
|
+
partitions[current_partition] = current_data
|
|
340
|
+
|
|
341
|
+
return partitions
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""SLURM config utilities."""
|
|
2
|
+
|
|
3
|
+
import subprocess
|
|
4
|
+
from typing import Any, Optional, Union
|
|
5
|
+
|
|
6
|
+
from .base_resource import BaseSlurmResource
|
|
7
|
+
from .profiles import get_profile_config
|
|
8
|
+
from .utils import console
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Config(BaseSlurmResource):
|
|
12
|
+
def __init__(self, name: str, **kwargs: Any):
|
|
13
|
+
self.name = name
|
|
14
|
+
self.kwargs = kwargs
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def update(cls, name: str, verbose: bool, **kwargs: Any) -> None:
|
|
18
|
+
"""Update a config."""
|
|
19
|
+
args = ["scontrol", "reconfigure"]
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
result = subprocess.run(
|
|
23
|
+
["echo", *args],
|
|
24
|
+
check=True,
|
|
25
|
+
capture_output=True,
|
|
26
|
+
text=True,
|
|
27
|
+
)
|
|
28
|
+
console.print("[green]Config updated successfully.[/green]")
|
|
29
|
+
if result.stdout:
|
|
30
|
+
console.print(result.stdout)
|
|
31
|
+
except subprocess.CalledProcessError as e:
|
|
32
|
+
console.print(
|
|
33
|
+
f"[red]Failed to update config:[/red] {e.stderr or e}"
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def show(
|
|
38
|
+
cls,
|
|
39
|
+
data: Union[None, dict] = None,
|
|
40
|
+
style: str = "pretty",
|
|
41
|
+
force_cache_update: bool = False,
|
|
42
|
+
delimiter: str = ";",
|
|
43
|
+
profile: str = "default",
|
|
44
|
+
profile_str: Optional[str] = None,
|
|
45
|
+
) -> None:
|
|
46
|
+
"""Show a config."""
|
|
47
|
+
# Get profile configuration (for future enhancement)
|
|
48
|
+
_, _, _, _, _ = get_profile_config(
|
|
49
|
+
profile, "config", profile_str
|
|
50
|
+
)
|
|
51
|
+
try:
|
|
52
|
+
if style == "json":
|
|
53
|
+
result = subprocess.run(
|
|
54
|
+
["scontrol", "show", "config", "--json"],
|
|
55
|
+
check=True,
|
|
56
|
+
capture_output=True,
|
|
57
|
+
text=True,
|
|
58
|
+
)
|
|
59
|
+
if result.stdout:
|
|
60
|
+
console.print_json(result.stdout)
|
|
61
|
+
else: # pretty style
|
|
62
|
+
result = subprocess.run(
|
|
63
|
+
["scontrol", "show", "config"],
|
|
64
|
+
check=True,
|
|
65
|
+
capture_output=True,
|
|
66
|
+
text=True,
|
|
67
|
+
)
|
|
68
|
+
if result.stdout:
|
|
69
|
+
console.print(result.stdout)
|
|
70
|
+
except subprocess.CalledProcessError as e:
|
|
71
|
+
console.print(
|
|
72
|
+
f"[red]Failed to show config:[/red] {e.stderr or e}"
|
|
73
|
+
)
|