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
slurm_cli/utils/nodes.py
ADDED
|
@@ -0,0 +1,774 @@
|
|
|
1
|
+
"""Utilities for managing nodes."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import subprocess
|
|
5
|
+
from typing import Any, Dict, Optional, Union, List
|
|
6
|
+
|
|
7
|
+
from rich.box import SIMPLE_HEAVY
|
|
8
|
+
from rich.markup import escape
|
|
9
|
+
from rich.table import Table
|
|
10
|
+
|
|
11
|
+
from .base_resource import BaseSlurmResource
|
|
12
|
+
from .profiles import (
|
|
13
|
+
format_with_template,
|
|
14
|
+
get_profile_config,
|
|
15
|
+
sort_data,
|
|
16
|
+
)
|
|
17
|
+
from .utils import console
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Node(BaseSlurmResource):
|
|
21
|
+
_WIDTH = None
|
|
22
|
+
|
|
23
|
+
# Node options for autocomplete
|
|
24
|
+
NODE_SHOW_OPTIONS = [
|
|
25
|
+
"name",
|
|
26
|
+
"state",
|
|
27
|
+
"partition",
|
|
28
|
+
"features",
|
|
29
|
+
"gres",
|
|
30
|
+
]
|
|
31
|
+
NODE_UPDATE_OPTIONS = [
|
|
32
|
+
"nodename", # Sets NEW node name (for renaming)
|
|
33
|
+
"activefeatures",
|
|
34
|
+
"availablefeatures",
|
|
35
|
+
"comment",
|
|
36
|
+
"cpubind",
|
|
37
|
+
"extra",
|
|
38
|
+
"gres",
|
|
39
|
+
"instanceid",
|
|
40
|
+
"instancetype",
|
|
41
|
+
"nodeaddr",
|
|
42
|
+
"nodehostname",
|
|
43
|
+
"reason",
|
|
44
|
+
"resumeafter",
|
|
45
|
+
"state",
|
|
46
|
+
"weight",
|
|
47
|
+
]
|
|
48
|
+
# Valid states for node update
|
|
49
|
+
NODE_UPDATE_STATES = [
|
|
50
|
+
"cancel_reboot",
|
|
51
|
+
"down",
|
|
52
|
+
"drain",
|
|
53
|
+
"fail",
|
|
54
|
+
"future",
|
|
55
|
+
"idle",
|
|
56
|
+
"noresp",
|
|
57
|
+
"resume",
|
|
58
|
+
"undrain",
|
|
59
|
+
]
|
|
60
|
+
# Valid values for CpuBind option
|
|
61
|
+
NODE_CPUBIND_VALUES = [
|
|
62
|
+
"none",
|
|
63
|
+
"socket",
|
|
64
|
+
"ldom",
|
|
65
|
+
"core",
|
|
66
|
+
"thread",
|
|
67
|
+
"off",
|
|
68
|
+
]
|
|
69
|
+
# States shown in show command (informational)
|
|
70
|
+
NODE_STATES = [
|
|
71
|
+
"idle",
|
|
72
|
+
"alloc",
|
|
73
|
+
"drain",
|
|
74
|
+
"down",
|
|
75
|
+
"resume",
|
|
76
|
+
"undrain",
|
|
77
|
+
"fail",
|
|
78
|
+
"power_down",
|
|
79
|
+
"power_up",
|
|
80
|
+
]
|
|
81
|
+
# Only future and cloud states allowed for node creation
|
|
82
|
+
NODE_CREATE_STATES = ["future", "cloud"]
|
|
83
|
+
NODE_CREATE_OPTIONS = [
|
|
84
|
+
"state",
|
|
85
|
+
"cpus",
|
|
86
|
+
"features",
|
|
87
|
+
"gres",
|
|
88
|
+
"reason",
|
|
89
|
+
"weight",
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
# Default columns for table output
|
|
93
|
+
DEFAULT_COLUMNS = [
|
|
94
|
+
"name",
|
|
95
|
+
"state",
|
|
96
|
+
"cpus",
|
|
97
|
+
"alloc_cpus",
|
|
98
|
+
"real_memory",
|
|
99
|
+
"alloc_memory",
|
|
100
|
+
"gres",
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
# Default styles for table columns
|
|
104
|
+
DEFAULT_STYLES = {
|
|
105
|
+
"name": "cyan",
|
|
106
|
+
"state": "green",
|
|
107
|
+
"cpus": "yellow",
|
|
108
|
+
"alloc_cpus": "yellow",
|
|
109
|
+
"real_memory": "blue",
|
|
110
|
+
"alloc_memory": "blue",
|
|
111
|
+
"gres": "magenta",
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
def __init__(self, name: str, **kwargs: Any):
|
|
115
|
+
self.name = name
|
|
116
|
+
self.kwargs = kwargs
|
|
117
|
+
|
|
118
|
+
# Node filter options for selecting nodes (first argument)
|
|
119
|
+
NODE_FILTER_OPTIONS = [
|
|
120
|
+
"ALL",
|
|
121
|
+
"partition",
|
|
122
|
+
"state",
|
|
123
|
+
"user",
|
|
124
|
+
"reservation",
|
|
125
|
+
"drainreason",
|
|
126
|
+
]
|
|
127
|
+
|
|
128
|
+
@classmethod
|
|
129
|
+
def generate_autocomplete_options(cls) -> str:
|
|
130
|
+
"""Generate bash autocomplete script for node options."""
|
|
131
|
+
show_opts = " ".join(f"{opt}=" for opt in cls.NODE_SHOW_OPTIONS)
|
|
132
|
+
update_opts = " ".join(
|
|
133
|
+
f"{opt}=" for opt in cls.NODE_UPDATE_OPTIONS
|
|
134
|
+
)
|
|
135
|
+
# Add node filter options for selecting nodes
|
|
136
|
+
filter_opts = " ".join(
|
|
137
|
+
f"{opt}=" if opt != "ALL" else opt
|
|
138
|
+
for opt in cls.NODE_FILTER_OPTIONS
|
|
139
|
+
)
|
|
140
|
+
# Add negation filter options (not:filter=)
|
|
141
|
+
neg_filter_opts = " ".join(
|
|
142
|
+
f"not:{opt}="
|
|
143
|
+
for opt in cls.NODE_FILTER_OPTIONS
|
|
144
|
+
if opt != "ALL"
|
|
145
|
+
)
|
|
146
|
+
create_opts = " ".join(
|
|
147
|
+
f"{opt}=" for opt in cls.NODE_CREATE_OPTIONS
|
|
148
|
+
)
|
|
149
|
+
states = " ".join(cls.NODE_STATES)
|
|
150
|
+
create_states = " ".join(cls.NODE_CREATE_STATES)
|
|
151
|
+
update_states = " ".join(cls.NODE_UPDATE_STATES)
|
|
152
|
+
cpubind_values = " ".join(cls.NODE_CPUBIND_VALUES)
|
|
153
|
+
|
|
154
|
+
script = f"""
|
|
155
|
+
_slurm_cli_nodes_autocomplete() {{
|
|
156
|
+
local cmd="$1"
|
|
157
|
+
local pos="$2"
|
|
158
|
+
|
|
159
|
+
local cur="${{COMP_WORDS[COMP_CWORD]}}"
|
|
160
|
+
local prev="${{COMP_WORDS[COMP_CWORD-1]}}"
|
|
161
|
+
|
|
162
|
+
local cached_nodes="$(_slurm_cache_nodes)"
|
|
163
|
+
local cached_partitions="$(_slurm_cache_partitions)"
|
|
164
|
+
local show_options="{show_opts}"
|
|
165
|
+
local update_options="{update_opts}"
|
|
166
|
+
local filter_options="{filter_opts}"
|
|
167
|
+
local neg_filter_options="{neg_filter_opts}"
|
|
168
|
+
local create_options="{create_opts}"
|
|
169
|
+
|
|
170
|
+
# Handle key=value completion
|
|
171
|
+
if _slurm_parse_keyval "$cur" "$prev"; then
|
|
172
|
+
case "$_key" in
|
|
173
|
+
state)
|
|
174
|
+
# Different states for create vs update
|
|
175
|
+
if [[ "$cmd" == "create" ]]; then
|
|
176
|
+
_slurm_complete_value "{create_states}" "$_key" "$_val" "$cur"
|
|
177
|
+
elif [[ "$cmd" == "update" ]]; then
|
|
178
|
+
_slurm_complete_value "{update_states}" "$_key" "$_val" "$cur"
|
|
179
|
+
else
|
|
180
|
+
_slurm_complete_value "{states}" "$_key" "$_val" "$cur"
|
|
181
|
+
fi
|
|
182
|
+
;;
|
|
183
|
+
cpubind)
|
|
184
|
+
_slurm_complete_value "{cpubind_values}" "$_key" "$_val" "$cur" ;;
|
|
185
|
+
partition)
|
|
186
|
+
_slurm_complete_value "$cached_partitions" "$_key" "$_val" "$cur" ;;
|
|
187
|
+
name|nodename)
|
|
188
|
+
_slurm_complete_value "$cached_nodes" "$_key" "$_val" "$cur" ;;
|
|
189
|
+
user)
|
|
190
|
+
# Node filter: show users for selecting nodes by user's jobs
|
|
191
|
+
_slurm_complete_value "$(_slurm_cache_users)" "$_key" "$_val" "$cur" ;;
|
|
192
|
+
reservation)
|
|
193
|
+
# Node filter: show reservations for selecting nodes
|
|
194
|
+
_slurm_complete_value "$(_slurm_cache_reservations)" "$_key" "$_val" "$cur" ;;
|
|
195
|
+
esac
|
|
196
|
+
[[ ${{#COMPREPLY[@]}} -gt 0 ]] && return
|
|
197
|
+
fi
|
|
198
|
+
|
|
199
|
+
# For show command: filter options, show options, then node names
|
|
200
|
+
if [[ "$cmd" == "show" ]]; then
|
|
201
|
+
_slurm_complete "$filter_options $neg_filter_options $show_options $cached_nodes" "$cur"
|
|
202
|
+
return
|
|
203
|
+
fi
|
|
204
|
+
|
|
205
|
+
# For create command: show create options (name is positional)
|
|
206
|
+
if [[ "$cmd" == "create" ]]; then
|
|
207
|
+
_slurm_complete "$create_options" "$cur"
|
|
208
|
+
return
|
|
209
|
+
fi
|
|
210
|
+
|
|
211
|
+
# For update command: filter options, update options, then node names
|
|
212
|
+
if [[ "$cmd" == "update" ]]; then
|
|
213
|
+
_slurm_complete "$filter_options $update_options $cached_nodes" "$cur"
|
|
214
|
+
return
|
|
215
|
+
fi
|
|
216
|
+
|
|
217
|
+
# Default: show node names
|
|
218
|
+
_slurm_complete "$cached_nodes" "$cur"
|
|
219
|
+
}}
|
|
220
|
+
""" # noqa: E501
|
|
221
|
+
return script
|
|
222
|
+
|
|
223
|
+
@classmethod
|
|
224
|
+
def get_profile_fields(cls) -> dict:
|
|
225
|
+
"""Return field names and descriptions for profile templates."""
|
|
226
|
+
return {
|
|
227
|
+
"name": "Node name",
|
|
228
|
+
"state": "Node state",
|
|
229
|
+
"cpus": "Number of CPUs",
|
|
230
|
+
"real_memory": "Real memory (MB)",
|
|
231
|
+
"tmp_disk": "Tmp disk space",
|
|
232
|
+
"features": "Node features",
|
|
233
|
+
"gres": "Generic resources",
|
|
234
|
+
"partitions": "Partitions this node belongs to",
|
|
235
|
+
"reason": "State reason (if down/drained)",
|
|
236
|
+
"comment": "Node comment",
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
@classmethod
|
|
240
|
+
def max_width(cls) -> int:
|
|
241
|
+
"""Get the maximum width of the console."""
|
|
242
|
+
if cls._WIDTH is None:
|
|
243
|
+
cls._WIDTH = console.width
|
|
244
|
+
return cls._WIDTH
|
|
245
|
+
|
|
246
|
+
@classmethod
|
|
247
|
+
def create(
|
|
248
|
+
cls, name: str, verbose: bool = False, **kwargs: Any
|
|
249
|
+
) -> None:
|
|
250
|
+
"""Create a new node.
|
|
251
|
+
|
|
252
|
+
Both NodeName and state are required.
|
|
253
|
+
Only 'future' and 'cloud' states are allowed for node creation.
|
|
254
|
+
Command format: scontrol create NodeName=NAME state=STATE [OPTIONS]
|
|
255
|
+
"""
|
|
256
|
+
# State is required
|
|
257
|
+
state = kwargs.get("state", "").lower()
|
|
258
|
+
if not state:
|
|
259
|
+
console.print(
|
|
260
|
+
"[red]Node creation requires state= parameter.[/red]"
|
|
261
|
+
)
|
|
262
|
+
console.print(
|
|
263
|
+
f"Allowed states: {', '.join(cls.NODE_CREATE_STATES)}"
|
|
264
|
+
)
|
|
265
|
+
console.print(
|
|
266
|
+
"Usage: slurm-cli create nodes NODENAME state=future|cloud"
|
|
267
|
+
)
|
|
268
|
+
return
|
|
269
|
+
|
|
270
|
+
# Validate state value
|
|
271
|
+
if state not in cls.NODE_CREATE_STATES:
|
|
272
|
+
console.print(
|
|
273
|
+
f"[red]Invalid state '{state}' for node creation.[/red]"
|
|
274
|
+
)
|
|
275
|
+
console.print(
|
|
276
|
+
f"Allowed states: {', '.join(cls.NODE_CREATE_STATES)}"
|
|
277
|
+
)
|
|
278
|
+
return
|
|
279
|
+
|
|
280
|
+
# Build scontrol command
|
|
281
|
+
args = ["scontrol", "create", f"NodeName={name}"]
|
|
282
|
+
for key, value in kwargs.items():
|
|
283
|
+
if value is not None:
|
|
284
|
+
args.append(f"{key}={value}")
|
|
285
|
+
|
|
286
|
+
if verbose:
|
|
287
|
+
console.print(f"[dim]Running: {' '.join(args)}[/dim]")
|
|
288
|
+
|
|
289
|
+
try:
|
|
290
|
+
result = subprocess.run(
|
|
291
|
+
args,
|
|
292
|
+
check=True,
|
|
293
|
+
capture_output=True,
|
|
294
|
+
text=True,
|
|
295
|
+
)
|
|
296
|
+
console.print(
|
|
297
|
+
f"[green]Node '{name}' created successfully.[/green]"
|
|
298
|
+
)
|
|
299
|
+
if result.stdout:
|
|
300
|
+
console.print(result.stdout)
|
|
301
|
+
except subprocess.CalledProcessError as e:
|
|
302
|
+
console.print(
|
|
303
|
+
f"[red]Failed to create node '{name}':[/red] {e.stderr or e}"
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
@classmethod
|
|
307
|
+
def update(
|
|
308
|
+
cls,
|
|
309
|
+
name: str,
|
|
310
|
+
verbose: bool = False,
|
|
311
|
+
dry_run: bool = False,
|
|
312
|
+
**kwargs: Any,
|
|
313
|
+
) -> None:
|
|
314
|
+
"""Update a node.
|
|
315
|
+
|
|
316
|
+
Command format: scontrol update NodeName=NAME [OPTIONS]
|
|
317
|
+
|
|
318
|
+
Valid options:
|
|
319
|
+
ActiveFeatures, AvailableFeatures, Comment, CpuBind, Extra,
|
|
320
|
+
Gres, InstanceId, InstanceType, NodeAddr, NodeHostname,
|
|
321
|
+
Reason, ResumeAfter, State, Weight
|
|
322
|
+
|
|
323
|
+
Valid states: CANCEL_REBOOT, DOWN, DRAIN, FAIL, FUTURE, IDLE,
|
|
324
|
+
NoResp, RESUME, UNDRAIN
|
|
325
|
+
Valid CpuBind: none, socket, ldom, core, thread, off
|
|
326
|
+
"""
|
|
327
|
+
if not name:
|
|
328
|
+
console.print(
|
|
329
|
+
"[red]Node name is required for update.[/red]"
|
|
330
|
+
)
|
|
331
|
+
console.print(
|
|
332
|
+
"Usage: slurm-cli update nodes NODENAME KEY=VALUE..."
|
|
333
|
+
)
|
|
334
|
+
return
|
|
335
|
+
|
|
336
|
+
# Validate state if provided
|
|
337
|
+
state = kwargs.get("state", "")
|
|
338
|
+
if state and state.lower() not in cls.NODE_UPDATE_STATES:
|
|
339
|
+
console.print(
|
|
340
|
+
f"[red]Invalid state '{state}' for node update.[/red]"
|
|
341
|
+
)
|
|
342
|
+
console.print(
|
|
343
|
+
f"Valid states: {', '.join(cls.NODE_UPDATE_STATES)}"
|
|
344
|
+
)
|
|
345
|
+
return
|
|
346
|
+
|
|
347
|
+
# Validate cpubind if provided
|
|
348
|
+
cpubind = kwargs.get("cpubind", "")
|
|
349
|
+
if cpubind and cpubind.lower() not in cls.NODE_CPUBIND_VALUES:
|
|
350
|
+
console.print(f"[red]Invalid cpubind '{cpubind}'.[/red]")
|
|
351
|
+
console.print(
|
|
352
|
+
f"Valid values: {', '.join(cls.NODE_CPUBIND_VALUES)}"
|
|
353
|
+
)
|
|
354
|
+
return
|
|
355
|
+
|
|
356
|
+
# Build scontrol command
|
|
357
|
+
args = ["scontrol", "update", f"NodeName={name}"]
|
|
358
|
+
for key, value in kwargs.items():
|
|
359
|
+
if value is not None:
|
|
360
|
+
args.append(f"{key}={value}")
|
|
361
|
+
|
|
362
|
+
if dry_run:
|
|
363
|
+
console.print(f"[yellow]DRY RUN:[/yellow] {' '.join(args)}")
|
|
364
|
+
return
|
|
365
|
+
|
|
366
|
+
if verbose:
|
|
367
|
+
console.print(f"[dim]Running: {' '.join(args)}[/dim]")
|
|
368
|
+
|
|
369
|
+
try:
|
|
370
|
+
result = subprocess.run(
|
|
371
|
+
args,
|
|
372
|
+
check=True,
|
|
373
|
+
capture_output=True,
|
|
374
|
+
text=True,
|
|
375
|
+
)
|
|
376
|
+
console.print(
|
|
377
|
+
f"[green]Node '{name}' updated successfully.[/green]"
|
|
378
|
+
)
|
|
379
|
+
if result.stdout:
|
|
380
|
+
console.print(result.stdout)
|
|
381
|
+
except subprocess.CalledProcessError as e:
|
|
382
|
+
console.print(
|
|
383
|
+
f"[red]Failed to update node '{name}':[/red] {e.stderr or e}"
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
@classmethod
|
|
387
|
+
def delete(cls, name: str, verbose: bool = False) -> None:
|
|
388
|
+
"""Delete a node.
|
|
389
|
+
|
|
390
|
+
Command format: scontrol delete nodename=NODES
|
|
391
|
+
"""
|
|
392
|
+
if not name:
|
|
393
|
+
console.print(
|
|
394
|
+
"[red]Node name is required for deletion.[/red]"
|
|
395
|
+
)
|
|
396
|
+
console.print("Usage: slurm-cli delete nodes NODENAME")
|
|
397
|
+
return
|
|
398
|
+
|
|
399
|
+
args = ["scontrol", "delete", f"nodename={name}"]
|
|
400
|
+
|
|
401
|
+
if verbose:
|
|
402
|
+
console.print(f"[dim]Running: {' '.join(args)}[/dim]")
|
|
403
|
+
|
|
404
|
+
try:
|
|
405
|
+
result = subprocess.run(
|
|
406
|
+
args,
|
|
407
|
+
check=True,
|
|
408
|
+
capture_output=True,
|
|
409
|
+
text=True,
|
|
410
|
+
)
|
|
411
|
+
console.print(
|
|
412
|
+
f"[green]Node '{name}' deleted successfully.[/green]"
|
|
413
|
+
)
|
|
414
|
+
if result.stdout:
|
|
415
|
+
console.print(result.stdout)
|
|
416
|
+
except subprocess.CalledProcessError as e:
|
|
417
|
+
console.print(
|
|
418
|
+
f"[red]Failed to delete node '{name}':[/red] {e.stderr or e}"
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
@classmethod
|
|
422
|
+
def show(
|
|
423
|
+
cls,
|
|
424
|
+
name: Optional[str] = None,
|
|
425
|
+
data: Optional[Dict[str, Any]] = None,
|
|
426
|
+
style: str = "pretty",
|
|
427
|
+
verbose: bool = False,
|
|
428
|
+
force_cache_update: bool = False,
|
|
429
|
+
delimiter: str = ";",
|
|
430
|
+
zebra: bool = False,
|
|
431
|
+
profile: str = "default",
|
|
432
|
+
profile_str: Optional[str] = None,
|
|
433
|
+
) -> None:
|
|
434
|
+
"""Show node information."""
|
|
435
|
+
# Get profile configuration
|
|
436
|
+
(
|
|
437
|
+
columns_cfg,
|
|
438
|
+
styles_cfg,
|
|
439
|
+
template_cfg,
|
|
440
|
+
sort_field,
|
|
441
|
+
sort_asc,
|
|
442
|
+
) = get_profile_config(profile, "nodes", profile_str)
|
|
443
|
+
if not data:
|
|
444
|
+
console.print("[red]No data available.[/red]")
|
|
445
|
+
return
|
|
446
|
+
|
|
447
|
+
# Convert dict to sorted list if sorting is needed
|
|
448
|
+
if sort_field and data and not name:
|
|
449
|
+
items = [{"name": k, **v} for k, v in data.items()]
|
|
450
|
+
items = sort_data(items, sort_field, sort_asc)
|
|
451
|
+
data = {item["name"]: item for item in items}
|
|
452
|
+
|
|
453
|
+
try:
|
|
454
|
+
if style == "json":
|
|
455
|
+
if name:
|
|
456
|
+
console.print_json(json.dumps(data[name], indent=4))
|
|
457
|
+
else:
|
|
458
|
+
console.print_json(json.dumps(data, indent=4))
|
|
459
|
+
elif style == "csv":
|
|
460
|
+
cls.show_csv(
|
|
461
|
+
name, data, delimiter, verbose, columns_cfg # type: ignore
|
|
462
|
+
)
|
|
463
|
+
elif template_cfg and style == "pretty":
|
|
464
|
+
# Use template-based output (e.g., oneline profile)
|
|
465
|
+
nodes_to_show = (
|
|
466
|
+
{name: data[name]}
|
|
467
|
+
if name and name in data
|
|
468
|
+
else data
|
|
469
|
+
)
|
|
470
|
+
for node_name, node_data in sorted(
|
|
471
|
+
nodes_to_show.items()
|
|
472
|
+
):
|
|
473
|
+
# Prepare data with name included
|
|
474
|
+
prepared = {"name": node_name, **node_data}
|
|
475
|
+
output = format_with_template(
|
|
476
|
+
template_cfg, prepared, resource="nodes"
|
|
477
|
+
)
|
|
478
|
+
console.print(output)
|
|
479
|
+
elif (
|
|
480
|
+
columns_cfg and columns_cfg != "*" and style == "pretty"
|
|
481
|
+
):
|
|
482
|
+
# Use column-based table output (e.g., compact, minimal)
|
|
483
|
+
cls.show_columns(
|
|
484
|
+
name, data, columns_cfg, styles_cfg, zebra # type: ignore
|
|
485
|
+
)
|
|
486
|
+
else: # Default pretty style
|
|
487
|
+
if name:
|
|
488
|
+
cls.show_one_pretty(name, data[name], verbose)
|
|
489
|
+
else:
|
|
490
|
+
for node in data.keys():
|
|
491
|
+
cls.show_one_pretty(node, data[node], verbose)
|
|
492
|
+
except subprocess.CalledProcessError as e:
|
|
493
|
+
console.print(
|
|
494
|
+
f"[red]Failed to show node(s):[/red] {e.stderr or e}"
|
|
495
|
+
)
|
|
496
|
+
|
|
497
|
+
@classmethod
|
|
498
|
+
def show_columns(
|
|
499
|
+
cls,
|
|
500
|
+
name: Optional[str] = None,
|
|
501
|
+
nodes: Optional[Dict[str, Any]] = None,
|
|
502
|
+
columns: Optional[List[str]] = None,
|
|
503
|
+
styles: Optional[Dict[str, str]] = None,
|
|
504
|
+
zebra: bool = False,
|
|
505
|
+
) -> None:
|
|
506
|
+
"""Show nodes in a column-based table."""
|
|
507
|
+
if not nodes:
|
|
508
|
+
console.print("[red]No nodes found.[/red]")
|
|
509
|
+
return
|
|
510
|
+
|
|
511
|
+
nodes_to_show = (
|
|
512
|
+
{name: nodes[name]} if name and name in nodes else nodes
|
|
513
|
+
)
|
|
514
|
+
|
|
515
|
+
# Create table
|
|
516
|
+
row_styles = ["", "dim"] if zebra else None
|
|
517
|
+
table = Table(
|
|
518
|
+
title="Nodes",
|
|
519
|
+
box=SIMPLE_HEAVY,
|
|
520
|
+
pad_edge=False,
|
|
521
|
+
row_styles=row_styles,
|
|
522
|
+
)
|
|
523
|
+
|
|
524
|
+
# Use default columns if not specified
|
|
525
|
+
if not columns:
|
|
526
|
+
columns = cls.DEFAULT_COLUMNS
|
|
527
|
+
|
|
528
|
+
# Merge with default styles
|
|
529
|
+
merged_styles = dict(cls.DEFAULT_STYLES)
|
|
530
|
+
if styles:
|
|
531
|
+
merged_styles.update(styles)
|
|
532
|
+
|
|
533
|
+
# Add columns to table
|
|
534
|
+
for col in columns:
|
|
535
|
+
style = merged_styles.get(col.lower(), "white")
|
|
536
|
+
table.add_column(col.title(), style=style)
|
|
537
|
+
|
|
538
|
+
# Add rows
|
|
539
|
+
for node_name in sorted(nodes_to_show.keys()):
|
|
540
|
+
node_data = nodes_to_show[node_name]
|
|
541
|
+
row = []
|
|
542
|
+
for col in columns:
|
|
543
|
+
col_lower = col.lower()
|
|
544
|
+
if col_lower == "name":
|
|
545
|
+
row.append(node_name)
|
|
546
|
+
elif col_lower == "state":
|
|
547
|
+
# State can be a list
|
|
548
|
+
state = node_data.get("state", [])
|
|
549
|
+
if isinstance(state, list):
|
|
550
|
+
row.append(",".join(state))
|
|
551
|
+
else:
|
|
552
|
+
row.append(str(state))
|
|
553
|
+
else:
|
|
554
|
+
value = node_data.get(
|
|
555
|
+
col_lower, node_data.get(col, "")
|
|
556
|
+
)
|
|
557
|
+
if isinstance(value, list):
|
|
558
|
+
row.append(",".join(str(v) for v in value))
|
|
559
|
+
else:
|
|
560
|
+
row.append(str(value) if value else "")
|
|
561
|
+
table.add_row(*row)
|
|
562
|
+
|
|
563
|
+
console.print(table)
|
|
564
|
+
|
|
565
|
+
@classmethod
|
|
566
|
+
def show_csv(
|
|
567
|
+
cls,
|
|
568
|
+
name: Optional[str] = None,
|
|
569
|
+
nodes: Optional[Dict[str, Any]] = None,
|
|
570
|
+
delimiter: str = ";",
|
|
571
|
+
verbose: bool = False,
|
|
572
|
+
columns: Optional[List[str]] = None,
|
|
573
|
+
) -> None:
|
|
574
|
+
"""Show node information in CSV format."""
|
|
575
|
+
if not nodes:
|
|
576
|
+
console.print("[red]No nodes found.[/red]")
|
|
577
|
+
return
|
|
578
|
+
|
|
579
|
+
# Helper to flatten complex values
|
|
580
|
+
def flatten_value(value):
|
|
581
|
+
"""Convert complex values to strings."""
|
|
582
|
+
if isinstance(value, dict):
|
|
583
|
+
# Handle set/number structures
|
|
584
|
+
if "set" in value and "number" in value:
|
|
585
|
+
return (
|
|
586
|
+
str(value.get("number", ""))
|
|
587
|
+
if value.get("set")
|
|
588
|
+
else ""
|
|
589
|
+
)
|
|
590
|
+
# Handle other dicts as JSON
|
|
591
|
+
return json.dumps(value)
|
|
592
|
+
elif isinstance(value, list):
|
|
593
|
+
# Join list items
|
|
594
|
+
return ",".join(str(v) for v in value)
|
|
595
|
+
else:
|
|
596
|
+
return str(value) if value is not None else ""
|
|
597
|
+
|
|
598
|
+
# Determine nodes to show
|
|
599
|
+
nodes_to_show = (
|
|
600
|
+
{name: nodes[name]} if name and name in nodes else nodes
|
|
601
|
+
)
|
|
602
|
+
|
|
603
|
+
# If specific columns specified in profile, use those
|
|
604
|
+
if columns and columns != "*":
|
|
605
|
+
headers = ["NodeName"] + [c for c in columns if c != "name"]
|
|
606
|
+
print(delimiter.join(headers))
|
|
607
|
+
for node_name in sorted(nodes_to_show.keys()):
|
|
608
|
+
node_data = nodes_to_show[node_name]
|
|
609
|
+
row = [node_name]
|
|
610
|
+
for col in columns:
|
|
611
|
+
if col.lower() == "name":
|
|
612
|
+
continue
|
|
613
|
+
value = node_data.get(
|
|
614
|
+
col.lower(), node_data.get(col, "")
|
|
615
|
+
)
|
|
616
|
+
row.append(flatten_value(value))
|
|
617
|
+
print(delimiter.join(row))
|
|
618
|
+
return
|
|
619
|
+
|
|
620
|
+
# Find all unique fields across all nodes
|
|
621
|
+
all_fields = set()
|
|
622
|
+
for node_data in nodes_to_show.values():
|
|
623
|
+
all_fields.update(node_data.keys())
|
|
624
|
+
|
|
625
|
+
# Priority fields (most important ones first)
|
|
626
|
+
priority_fields = [
|
|
627
|
+
"state",
|
|
628
|
+
"cpus",
|
|
629
|
+
"alloc_cpus",
|
|
630
|
+
"real_memory",
|
|
631
|
+
"alloc_memory",
|
|
632
|
+
"gres",
|
|
633
|
+
"gres_used",
|
|
634
|
+
"tres",
|
|
635
|
+
"tres_used",
|
|
636
|
+
]
|
|
637
|
+
|
|
638
|
+
# Fields to skip in non-verbose mode
|
|
639
|
+
skip_fields = set()
|
|
640
|
+
if not verbose:
|
|
641
|
+
skip_fields = {
|
|
642
|
+
"cpu_load",
|
|
643
|
+
"architecture",
|
|
644
|
+
"boards",
|
|
645
|
+
"boot_time",
|
|
646
|
+
"cluster_name",
|
|
647
|
+
"cores",
|
|
648
|
+
"specialized_cores",
|
|
649
|
+
"cpu_binding",
|
|
650
|
+
"free_mem",
|
|
651
|
+
"effective_cpus",
|
|
652
|
+
"specialized_cpus",
|
|
653
|
+
"energy",
|
|
654
|
+
"external_sensors",
|
|
655
|
+
"power",
|
|
656
|
+
"gres_drained",
|
|
657
|
+
"next_state_after_reboot",
|
|
658
|
+
"address",
|
|
659
|
+
"operating_system",
|
|
660
|
+
"owner",
|
|
661
|
+
"port",
|
|
662
|
+
"reason_changed_at",
|
|
663
|
+
"resume_after",
|
|
664
|
+
"specialized_memory",
|
|
665
|
+
"last_busy",
|
|
666
|
+
"alloc_idle_cpus",
|
|
667
|
+
"tres_weighted",
|
|
668
|
+
"slurmd_start_time",
|
|
669
|
+
"sockets",
|
|
670
|
+
"threads",
|
|
671
|
+
"temporary_disk",
|
|
672
|
+
"weight",
|
|
673
|
+
"version",
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
# Filter fields based on verbose mode
|
|
677
|
+
if not verbose:
|
|
678
|
+
all_fields = all_fields - skip_fields
|
|
679
|
+
|
|
680
|
+
# Sort fields: priority first, then alphabetically
|
|
681
|
+
sorted_fields = [
|
|
682
|
+
f for f in priority_fields if f in all_fields
|
|
683
|
+
] + sorted([f for f in all_fields if f not in priority_fields])
|
|
684
|
+
|
|
685
|
+
# Print CSV header
|
|
686
|
+
headers = ["NodeName"] + sorted_fields
|
|
687
|
+
print(delimiter.join(headers))
|
|
688
|
+
|
|
689
|
+
# Print data rows
|
|
690
|
+
for node_name in sorted(nodes_to_show.keys()):
|
|
691
|
+
node_data = nodes_to_show[node_name]
|
|
692
|
+
row = [node_name] + [
|
|
693
|
+
flatten_value(node_data.get(field, ""))
|
|
694
|
+
for field in sorted_fields
|
|
695
|
+
]
|
|
696
|
+
print(delimiter.join(row))
|
|
697
|
+
|
|
698
|
+
@classmethod
|
|
699
|
+
def show_one_pretty(
|
|
700
|
+
cls,
|
|
701
|
+
name: str,
|
|
702
|
+
data: dict,
|
|
703
|
+
verbose: bool = False,
|
|
704
|
+
) -> None:
|
|
705
|
+
"""Show one node information."""
|
|
706
|
+
console.print("=============================================")
|
|
707
|
+
# console.print_json(json.dumps(data, indent=4))
|
|
708
|
+
console.print(
|
|
709
|
+
f"[label]Node[/]: [object]{escape(name)}[/] "
|
|
710
|
+
f"{','.join(data['state'])}"
|
|
711
|
+
)
|
|
712
|
+
console.print(
|
|
713
|
+
f"[label]CPUs[/]: [b]{data['cpus']}/"
|
|
714
|
+
f"{data['alloc_cpus']}[/] "
|
|
715
|
+
f"[label]Mem[/]: [b]{data['real_memory']}/"
|
|
716
|
+
f"{data['alloc_memory']}[/]"
|
|
717
|
+
)
|
|
718
|
+
console.print(
|
|
719
|
+
f"[label]GRES[/]: [b]{data['gres']} "
|
|
720
|
+
f"[label]Used[/]: {data['gres_used']}[/]"
|
|
721
|
+
)
|
|
722
|
+
console.print(
|
|
723
|
+
f"[label]TRES[/]: [b]{data['tres']} "
|
|
724
|
+
f"[label]Used[/]: {data['tres_used']}[/]"
|
|
725
|
+
)
|
|
726
|
+
for k in [
|
|
727
|
+
"state",
|
|
728
|
+
"cpus",
|
|
729
|
+
"alloc_cpus",
|
|
730
|
+
"real_memory",
|
|
731
|
+
"alloc_memory",
|
|
732
|
+
"gres",
|
|
733
|
+
"gres_used",
|
|
734
|
+
"tres",
|
|
735
|
+
"tres_used",
|
|
736
|
+
]:
|
|
737
|
+
data.pop(k)
|
|
738
|
+
if not verbose:
|
|
739
|
+
for k in [
|
|
740
|
+
"cpu_load",
|
|
741
|
+
"architecture",
|
|
742
|
+
"boards",
|
|
743
|
+
"boot_time",
|
|
744
|
+
"cluster_name",
|
|
745
|
+
"cores",
|
|
746
|
+
"specialized_cores",
|
|
747
|
+
"cpu_binding",
|
|
748
|
+
"free_mem",
|
|
749
|
+
"effective_cpus",
|
|
750
|
+
"specialized_cpus",
|
|
751
|
+
"energy",
|
|
752
|
+
"external_sensors",
|
|
753
|
+
"power",
|
|
754
|
+
"gres_drained",
|
|
755
|
+
"next_state_after_reboot",
|
|
756
|
+
"address",
|
|
757
|
+
"operating_system",
|
|
758
|
+
"owner",
|
|
759
|
+
"port",
|
|
760
|
+
"reason_changed_at",
|
|
761
|
+
"resume_after",
|
|
762
|
+
"specialized_memory",
|
|
763
|
+
"last_busy",
|
|
764
|
+
"alloc_idle_cpus",
|
|
765
|
+
"tres_weighted",
|
|
766
|
+
"slurmd_start_time",
|
|
767
|
+
"sockets",
|
|
768
|
+
"threads",
|
|
769
|
+
"temporary_disk",
|
|
770
|
+
"weight",
|
|
771
|
+
"version",
|
|
772
|
+
]:
|
|
773
|
+
data.pop(k)
|
|
774
|
+
cls.print_dict_pretty(data)
|