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/users.py
ADDED
|
@@ -0,0 +1,649 @@
|
|
|
1
|
+
"""Utilities for managing users."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import subprocess
|
|
5
|
+
from typing import Any, Dict, List, Optional, Union
|
|
6
|
+
|
|
7
|
+
from rich.box import SIMPLE_HEAVY
|
|
8
|
+
from rich.table import Table
|
|
9
|
+
|
|
10
|
+
from .base_resource import BaseSlurmResource
|
|
11
|
+
from .profiles import get_profile_config, sort_data
|
|
12
|
+
from .utils import console
|
|
13
|
+
|
|
14
|
+
# User configuration options (sacctmgr field names)
|
|
15
|
+
USER_OPTIONS: List[str] = [
|
|
16
|
+
"Account",
|
|
17
|
+
"AdminLevel",
|
|
18
|
+
"Cluster",
|
|
19
|
+
"DefaultAccount",
|
|
20
|
+
"DefaultWCKey",
|
|
21
|
+
"Name",
|
|
22
|
+
"Partition",
|
|
23
|
+
"QOS",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
# Valid options for user update SET clause
|
|
27
|
+
USER_UPDATE_SET_OPTIONS: List[str] = [
|
|
28
|
+
"adminlevel",
|
|
29
|
+
"defaultaccount",
|
|
30
|
+
"defaultwckey",
|
|
31
|
+
"newname",
|
|
32
|
+
"partition",
|
|
33
|
+
"fairshare",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
# Valid options for user update WHERE clause (filters)
|
|
37
|
+
USER_UPDATE_WHERE_OPTIONS: List[str] = [
|
|
38
|
+
"account",
|
|
39
|
+
"adminlevel",
|
|
40
|
+
"cluster",
|
|
41
|
+
"defaultaccount",
|
|
42
|
+
"defaultwckey",
|
|
43
|
+
"name",
|
|
44
|
+
"partition",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
# Valid admin levels
|
|
48
|
+
VALID_ADMIN_LEVELS: List[str] = ["none", "admin", "operator"]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class User(BaseSlurmResource):
|
|
52
|
+
# Default column configuration for users
|
|
53
|
+
DEFAULT_COLUMNS = [
|
|
54
|
+
"name",
|
|
55
|
+
"administrator_level",
|
|
56
|
+
"default",
|
|
57
|
+
"coordinators",
|
|
58
|
+
"flags",
|
|
59
|
+
]
|
|
60
|
+
DEFAULT_STYLES = {
|
|
61
|
+
"name": "cyan",
|
|
62
|
+
"administrator_level": "yellow",
|
|
63
|
+
"default": "green",
|
|
64
|
+
"coordinators": "magenta",
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
def __init__(self, name: str, **kwargs: Any):
|
|
68
|
+
self.name = name
|
|
69
|
+
self.kwargs = kwargs
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def get_profile_fields(cls) -> dict:
|
|
73
|
+
"""Return field names and descriptions for profile templates."""
|
|
74
|
+
return {
|
|
75
|
+
"name": "Username",
|
|
76
|
+
"administrator_level": "Admin level (None, Operator, Admin)",
|
|
77
|
+
"default": "Default associations",
|
|
78
|
+
"coordinators": "Coordinator accounts",
|
|
79
|
+
"flags": "User flags",
|
|
80
|
+
"associations": "User associations",
|
|
81
|
+
"wckeys": "Workload characterization keys",
|
|
82
|
+
"old_name": "Previous username (if renamed)",
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
# All available columns from sacctmgr users JSON output
|
|
86
|
+
ALL_COLUMNS = [
|
|
87
|
+
"name",
|
|
88
|
+
"administrator_level",
|
|
89
|
+
"default",
|
|
90
|
+
"coordinators",
|
|
91
|
+
"flags",
|
|
92
|
+
"associations",
|
|
93
|
+
"wckeys",
|
|
94
|
+
"old_name",
|
|
95
|
+
]
|
|
96
|
+
|
|
97
|
+
@classmethod
|
|
98
|
+
def _get_column_config(
|
|
99
|
+
cls,
|
|
100
|
+
profile: str = "default",
|
|
101
|
+
profile_str: Optional[str] = None,
|
|
102
|
+
) -> tuple:
|
|
103
|
+
"""Get column configuration from profile.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
Tuple of (columns, styles, template, sort_field, sort_asc)
|
|
107
|
+
"""
|
|
108
|
+
(
|
|
109
|
+
columns,
|
|
110
|
+
styles,
|
|
111
|
+
template,
|
|
112
|
+
sort_field,
|
|
113
|
+
sort_asc,
|
|
114
|
+
) = get_profile_config(profile, "users", profile_str)
|
|
115
|
+
|
|
116
|
+
# Use all columns if profile specifies "*"
|
|
117
|
+
if columns == "*":
|
|
118
|
+
columns = cls.ALL_COLUMNS
|
|
119
|
+
# Use default columns if no columns specified
|
|
120
|
+
elif columns is None:
|
|
121
|
+
columns = cls.DEFAULT_COLUMNS
|
|
122
|
+
|
|
123
|
+
# Merge with default styles
|
|
124
|
+
merged_styles = dict(cls.DEFAULT_STYLES)
|
|
125
|
+
merged_styles.update(styles)
|
|
126
|
+
|
|
127
|
+
return columns, merged_styles, template, sort_field, sort_asc
|
|
128
|
+
|
|
129
|
+
# Filter field aliases (user-friendly names -> actual JSON field names)
|
|
130
|
+
FILTER_ALIASES: Dict[str, tuple] = {
|
|
131
|
+
"user": ("name",),
|
|
132
|
+
"username": ("name",),
|
|
133
|
+
"defaultaccount": ("default", "account"),
|
|
134
|
+
"defaultwckey": ("default", "wckey"),
|
|
135
|
+
"account": ("default", "account"),
|
|
136
|
+
"adminlevel": ("administrator_level",),
|
|
137
|
+
"admin": ("administrator_level",),
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
@classmethod
|
|
141
|
+
def _match_filter(
|
|
142
|
+
cls, user: Dict[str, Any], key: str, value: str
|
|
143
|
+
) -> bool:
|
|
144
|
+
"""Check if a user matches the given filter.
|
|
145
|
+
|
|
146
|
+
Handles field aliases and nested fields.
|
|
147
|
+
"""
|
|
148
|
+
key = key.lower()
|
|
149
|
+
value = value.lower()
|
|
150
|
+
|
|
151
|
+
# Check if it's an aliased field
|
|
152
|
+
if key in cls.FILTER_ALIASES:
|
|
153
|
+
path = cls.FILTER_ALIASES[key]
|
|
154
|
+
if len(path) == 1:
|
|
155
|
+
# Simple alias (e.g., adminlevel -> administrator_level)
|
|
156
|
+
actual_value = user.get(path[0], "")
|
|
157
|
+
else:
|
|
158
|
+
# Nested field (e.g., defaultaccount -> default.account)
|
|
159
|
+
obj = user
|
|
160
|
+
for part in path:
|
|
161
|
+
if isinstance(obj, dict):
|
|
162
|
+
obj = obj.get(part, "")
|
|
163
|
+
else:
|
|
164
|
+
obj = ""
|
|
165
|
+
break
|
|
166
|
+
actual_value = obj
|
|
167
|
+
else:
|
|
168
|
+
# Direct field access
|
|
169
|
+
actual_value = user.get(key, "")
|
|
170
|
+
|
|
171
|
+
# Handle nested dict for 'default' field
|
|
172
|
+
if key == "default" and isinstance(actual_value, dict):
|
|
173
|
+
actual_value = actual_value.get("account", "")
|
|
174
|
+
|
|
175
|
+
return str(actual_value).lower() == value
|
|
176
|
+
|
|
177
|
+
@classmethod
|
|
178
|
+
def _format_value(cls, user: Dict[str, Any], column: str) -> str:
|
|
179
|
+
"""Format a value for display."""
|
|
180
|
+
value = user.get(column, "")
|
|
181
|
+
# Handle nested 'default' dict
|
|
182
|
+
if column == "default" and isinstance(value, dict):
|
|
183
|
+
account = value.get("account", "")
|
|
184
|
+
wckey = value.get("wckey", "")
|
|
185
|
+
if account and wckey:
|
|
186
|
+
return f"{account} ({wckey})"
|
|
187
|
+
return account or wckey or "-"
|
|
188
|
+
# Unwrap single-element list fields
|
|
189
|
+
if column == "administrator_level" and isinstance(value, list):
|
|
190
|
+
return value[0] if value else "-"
|
|
191
|
+
# Handle array fields
|
|
192
|
+
if column in (
|
|
193
|
+
"coordinators",
|
|
194
|
+
"flags",
|
|
195
|
+
"associations",
|
|
196
|
+
"wckeys",
|
|
197
|
+
):
|
|
198
|
+
if isinstance(value, list):
|
|
199
|
+
return (
|
|
200
|
+
", ".join(str(v) for v in value) if value else "-"
|
|
201
|
+
)
|
|
202
|
+
if value is None or value == "":
|
|
203
|
+
return "-"
|
|
204
|
+
return str(value)
|
|
205
|
+
|
|
206
|
+
@classmethod
|
|
207
|
+
def generate_autocomplete_options(cls) -> str:
|
|
208
|
+
"""Generate bash autocomplete script for user options."""
|
|
209
|
+
valid_keys = [opt.lower() for opt in USER_OPTIONS]
|
|
210
|
+
where_opts = [opt.lower() for opt in USER_UPDATE_WHERE_OPTIONS]
|
|
211
|
+
set_opts = [opt.lower() for opt in USER_UPDATE_SET_OPTIONS]
|
|
212
|
+
create_opts = [
|
|
213
|
+
"name",
|
|
214
|
+
"account",
|
|
215
|
+
"adminlevel",
|
|
216
|
+
"cluster",
|
|
217
|
+
"defaultaccount",
|
|
218
|
+
"defaultwckey",
|
|
219
|
+
"partition",
|
|
220
|
+
"rawusage",
|
|
221
|
+
]
|
|
222
|
+
filter_opts = " ".join(f"{k}=" for k in valid_keys)
|
|
223
|
+
where_options = " ".join(f"{k}=" for k in where_opts)
|
|
224
|
+
set_options = " ".join(f"{k}=" for k in set_opts)
|
|
225
|
+
create_options = " ".join(f"{k}=" for k in create_opts)
|
|
226
|
+
admin_levels = "None Admin Operator"
|
|
227
|
+
|
|
228
|
+
script = f"""
|
|
229
|
+
_slurm_cli_users_autocomplete() {{
|
|
230
|
+
local cmd="$1"
|
|
231
|
+
local pos="$2"
|
|
232
|
+
|
|
233
|
+
local cur="${{COMP_WORDS[COMP_CWORD]}}"
|
|
234
|
+
local prev="${{COMP_WORDS[COMP_CWORD-1]}}"
|
|
235
|
+
local name="${{COMP_WORDS[$pos]}}"
|
|
236
|
+
|
|
237
|
+
local cached_users="$(_slurm_cache_users)"
|
|
238
|
+
local filter_options="{filter_opts}"
|
|
239
|
+
local where_options="{where_options}"
|
|
240
|
+
local set_options="{set_options}"
|
|
241
|
+
local create_options="{create_options}"
|
|
242
|
+
local update_options="$cached_users $where_options set"
|
|
243
|
+
|
|
244
|
+
# Check if 'set' keyword has been typed
|
|
245
|
+
local found_set=0
|
|
246
|
+
for word in "${{COMP_WORDS[@]}}"; do
|
|
247
|
+
[[ "$word" == "set" ]] && found_set=1 && break
|
|
248
|
+
done
|
|
249
|
+
|
|
250
|
+
# First argument after 'users'
|
|
251
|
+
if [[ $name == users && $prev == users ]]; then
|
|
252
|
+
case "$cmd" in
|
|
253
|
+
show|delete) _slurm_complete "$filter_options $cached_users" "$cur" ;;
|
|
254
|
+
create) _slurm_complete "$create_options" "$cur" ;;
|
|
255
|
+
update) _slurm_complete "$update_options $cached_users" "$cur" ;;
|
|
256
|
+
esac
|
|
257
|
+
return
|
|
258
|
+
fi
|
|
259
|
+
|
|
260
|
+
case "$cmd" in
|
|
261
|
+
delete)
|
|
262
|
+
if _slurm_parse_keyval "$cur" "$prev"; then
|
|
263
|
+
case "$_key" in
|
|
264
|
+
user|name)
|
|
265
|
+
_slurm_complete_value "$cached_users" "$_key" "$_val" "$cur" ;;
|
|
266
|
+
account|defaultaccount)
|
|
267
|
+
_slurm_complete_value "$(_slurm_cache_accounts)" "$_key" "$_val" "$cur" ;;
|
|
268
|
+
partition)
|
|
269
|
+
_slurm_complete_value "$(_slurm_cache_partitions)" "$_key" "$_val" "$cur" ;;
|
|
270
|
+
adminlevel)
|
|
271
|
+
_slurm_complete_value "{admin_levels}" "$_key" "$_val" "$cur" ;;
|
|
272
|
+
esac
|
|
273
|
+
return
|
|
274
|
+
fi
|
|
275
|
+
_slurm_complete "$filter_options" "$cur"
|
|
276
|
+
return
|
|
277
|
+
;;
|
|
278
|
+
update)
|
|
279
|
+
# After 'set' keyword, show SET options
|
|
280
|
+
if [[ $found_set -eq 1 ]]; then
|
|
281
|
+
if _slurm_parse_keyval "$cur" "$prev"; then
|
|
282
|
+
case "$_key" in
|
|
283
|
+
adminlevel)
|
|
284
|
+
_slurm_complete_value "{admin_levels}" "$_key" "$_val" "$cur" ;;
|
|
285
|
+
newname|name)
|
|
286
|
+
_slurm_complete_value "$cached_users" "$_key" "$_val" "$cur" ;;
|
|
287
|
+
defaultaccount)
|
|
288
|
+
_slurm_complete_value "$(_slurm_cache_accounts)" "$_key" "$_val" "$cur" ;;
|
|
289
|
+
partition)
|
|
290
|
+
_slurm_complete_value "$(_slurm_cache_partitions)" "$_key" "$_val" "$cur" ;;
|
|
291
|
+
esac
|
|
292
|
+
return
|
|
293
|
+
fi
|
|
294
|
+
_slurm_complete "$set_options" "$cur"
|
|
295
|
+
return
|
|
296
|
+
fi
|
|
297
|
+
# Before 'set' keyword, show WHERE options and 'set'
|
|
298
|
+
if _slurm_parse_keyval "$cur" "$prev"; then
|
|
299
|
+
case "$_key" in
|
|
300
|
+
user|name)
|
|
301
|
+
_slurm_complete_value "$cached_users" "$_key" "$_val" "$cur" ;;
|
|
302
|
+
account|defaultaccount)
|
|
303
|
+
_slurm_complete_value "$(_slurm_cache_accounts)" "$_key" "$_val" "$cur" ;;
|
|
304
|
+
partition)
|
|
305
|
+
_slurm_complete_value "$(_slurm_cache_partitions)" "$_key" "$_val" "$cur" ;;
|
|
306
|
+
adminlevel)
|
|
307
|
+
_slurm_complete_value "{admin_levels}" "$_key" "$_val" "$cur" ;;
|
|
308
|
+
esac
|
|
309
|
+
return
|
|
310
|
+
fi
|
|
311
|
+
_slurm_complete "$where_options set" "$cur"
|
|
312
|
+
return
|
|
313
|
+
;;
|
|
314
|
+
show)
|
|
315
|
+
if _slurm_parse_keyval "$cur" "$prev"; then
|
|
316
|
+
case "$_key" in
|
|
317
|
+
user|name)
|
|
318
|
+
_slurm_complete_value "$cached_users" "$_key" "$_val" "$cur" ;;
|
|
319
|
+
account|defaultaccount)
|
|
320
|
+
_slurm_complete_value "$(_slurm_cache_accounts)" "$_key" "$_val" "$cur" ;;
|
|
321
|
+
partition)
|
|
322
|
+
_slurm_complete_value "$(_slurm_cache_partitions)" "$_key" "$_val" "$cur" ;;
|
|
323
|
+
qos)
|
|
324
|
+
_slurm_complete_value "$(_slurm_cache_qos)" "$_key" "$_val" "$cur" ;;
|
|
325
|
+
adminlevel)
|
|
326
|
+
_slurm_complete_value "{admin_levels}" "$_key" "$_val" "$cur" ;;
|
|
327
|
+
esac
|
|
328
|
+
return
|
|
329
|
+
fi
|
|
330
|
+
_slurm_complete "$filter_options" "$cur"
|
|
331
|
+
;;
|
|
332
|
+
create)
|
|
333
|
+
if _slurm_parse_keyval "$cur" "$prev"; then
|
|
334
|
+
case "$_key" in
|
|
335
|
+
name)
|
|
336
|
+
_slurm_complete_value "$cached_users" "$_key" "$_val" "$cur" ;;
|
|
337
|
+
account|defaultaccount)
|
|
338
|
+
_slurm_complete_value "$(_slurm_cache_accounts)" "$_key" "$_val" "$cur" ;;
|
|
339
|
+
partition)
|
|
340
|
+
_slurm_complete_value "$(_slurm_cache_partitions)" "$_key" "$_val" "$cur" ;;
|
|
341
|
+
adminlevel)
|
|
342
|
+
_slurm_complete_value "{admin_levels}" "$_key" "$_val" "$cur" ;;
|
|
343
|
+
esac
|
|
344
|
+
return
|
|
345
|
+
fi
|
|
346
|
+
_slurm_complete "$create_options" "$cur"
|
|
347
|
+
;;
|
|
348
|
+
esac
|
|
349
|
+
}}
|
|
350
|
+
"""
|
|
351
|
+
return script
|
|
352
|
+
|
|
353
|
+
@classmethod
|
|
354
|
+
def create(
|
|
355
|
+
cls, name: str, verbose: bool = False, **kwargs: Any
|
|
356
|
+
) -> None:
|
|
357
|
+
"""Create a new user."""
|
|
358
|
+
console.print(f"Creating user: {name}")
|
|
359
|
+
args = ["sacctmgr", "create", "user", name]
|
|
360
|
+
for key, value in kwargs.items():
|
|
361
|
+
args.append(f"{key}={value}")
|
|
362
|
+
|
|
363
|
+
try:
|
|
364
|
+
result = subprocess.run(
|
|
365
|
+
args,
|
|
366
|
+
check=True,
|
|
367
|
+
capture_output=True,
|
|
368
|
+
text=True,
|
|
369
|
+
)
|
|
370
|
+
console.print(
|
|
371
|
+
f"[green]User '{name}' created successfully.[/green]"
|
|
372
|
+
)
|
|
373
|
+
if result.stdout:
|
|
374
|
+
console.print(result.stdout)
|
|
375
|
+
except subprocess.CalledProcessError as e:
|
|
376
|
+
console.print(
|
|
377
|
+
f"[red]Failed to create user '{name}':[/red] {e.stderr or e}"
|
|
378
|
+
)
|
|
379
|
+
|
|
380
|
+
@classmethod
|
|
381
|
+
def update(
|
|
382
|
+
cls,
|
|
383
|
+
name: str,
|
|
384
|
+
verbose: bool = False,
|
|
385
|
+
dry_run: bool = False,
|
|
386
|
+
where_conditions: Optional[List[str]] = None,
|
|
387
|
+
set_values: Optional[List[str]] = None,
|
|
388
|
+
**kwargs: Any,
|
|
389
|
+
) -> None:
|
|
390
|
+
"""Update a user.
|
|
391
|
+
|
|
392
|
+
Two calling modes:
|
|
393
|
+
1. Simple: update(name, key=value, ...) - updates user by name
|
|
394
|
+
2. Where: update("", where_conditions=[...], set_values=[...])
|
|
395
|
+
- uses WHERE/SET syntax for bulk updates
|
|
396
|
+
|
|
397
|
+
Args:
|
|
398
|
+
name: Username to update (for simple mode)
|
|
399
|
+
verbose: Enable verbose output
|
|
400
|
+
dry_run: If True, print command without executing
|
|
401
|
+
where_conditions: List of WHERE conditions (e.g., ["account=test"])
|
|
402
|
+
set_values: List of SET values (e.g., ["adminlevel=admin"])
|
|
403
|
+
**kwargs: SET options as keyword arguments
|
|
404
|
+
"""
|
|
405
|
+
# Build the command
|
|
406
|
+
args = ["sacctmgr", "-i", "modify", "user"]
|
|
407
|
+
|
|
408
|
+
# Determine mode and build WHERE clause
|
|
409
|
+
if where_conditions:
|
|
410
|
+
# WHERE/SET mode
|
|
411
|
+
args.append("where")
|
|
412
|
+
for cond in where_conditions:
|
|
413
|
+
args.append(cond)
|
|
414
|
+
elif name:
|
|
415
|
+
# Simple mode - update by name
|
|
416
|
+
args.extend(["where", f"name={name}"])
|
|
417
|
+
else:
|
|
418
|
+
console.print(
|
|
419
|
+
"[red]No user name or WHERE conditions specified.[/red]"
|
|
420
|
+
)
|
|
421
|
+
return
|
|
422
|
+
|
|
423
|
+
# Build SET clause
|
|
424
|
+
args.append("set")
|
|
425
|
+
|
|
426
|
+
# Use set_values if provided, otherwise use kwargs
|
|
427
|
+
if set_values:
|
|
428
|
+
for val in set_values:
|
|
429
|
+
# Validate adminlevel even in WHERE mode
|
|
430
|
+
if "=" in val:
|
|
431
|
+
k, v = val.split("=", 1)
|
|
432
|
+
if k.lower() == "adminlevel":
|
|
433
|
+
if v.lower() not in VALID_ADMIN_LEVELS:
|
|
434
|
+
console.print(
|
|
435
|
+
f"[red]Invalid adminlevel '{v}'. "
|
|
436
|
+
f"Must be one of: "
|
|
437
|
+
f"{', '.join(VALID_ADMIN_LEVELS)}[/red]"
|
|
438
|
+
)
|
|
439
|
+
return
|
|
440
|
+
args.append(val)
|
|
441
|
+
else:
|
|
442
|
+
for key, value in kwargs.items():
|
|
443
|
+
if value is None:
|
|
444
|
+
continue
|
|
445
|
+
|
|
446
|
+
key_lower = key.lower()
|
|
447
|
+
|
|
448
|
+
# Handle 'name' -> 'newname' conversion
|
|
449
|
+
if key_lower == "name":
|
|
450
|
+
key_lower = "newname"
|
|
451
|
+
|
|
452
|
+
# Validate adminlevel values
|
|
453
|
+
if key_lower == "adminlevel":
|
|
454
|
+
if value.lower() not in VALID_ADMIN_LEVELS:
|
|
455
|
+
console.print(
|
|
456
|
+
f"[red]Invalid adminlevel '{value}'. "
|
|
457
|
+
f"Must be one of: {', '.join(VALID_ADMIN_LEVELS)}"
|
|
458
|
+
"[/red]"
|
|
459
|
+
)
|
|
460
|
+
return
|
|
461
|
+
|
|
462
|
+
# Validate the option is allowed
|
|
463
|
+
if key_lower not in USER_UPDATE_SET_OPTIONS:
|
|
464
|
+
console.print(
|
|
465
|
+
f"[yellow]Warning: '{key}' is not a recognized "
|
|
466
|
+
f"SET option. Valid options: "
|
|
467
|
+
f"{', '.join(USER_UPDATE_SET_OPTIONS)}[/yellow]"
|
|
468
|
+
)
|
|
469
|
+
|
|
470
|
+
args.append(f"{key_lower}={value}")
|
|
471
|
+
|
|
472
|
+
if dry_run:
|
|
473
|
+
console.print(f"[yellow]DRY RUN:[/yellow] {' '.join(args)}")
|
|
474
|
+
return
|
|
475
|
+
|
|
476
|
+
if verbose:
|
|
477
|
+
console.print(f"Running: {' '.join(args)}")
|
|
478
|
+
|
|
479
|
+
try:
|
|
480
|
+
result = subprocess.run(
|
|
481
|
+
args,
|
|
482
|
+
check=True,
|
|
483
|
+
capture_output=True,
|
|
484
|
+
text=True,
|
|
485
|
+
)
|
|
486
|
+
if result.stdout:
|
|
487
|
+
console.print(result.stdout)
|
|
488
|
+
if verbose:
|
|
489
|
+
console.print(
|
|
490
|
+
f"[green]User '{name}' updated successfully.[/green]"
|
|
491
|
+
)
|
|
492
|
+
except subprocess.CalledProcessError as e:
|
|
493
|
+
console.print(
|
|
494
|
+
f"[red]Failed to update user '{name}':[/red] "
|
|
495
|
+
f"{e.stderr or e}"
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
@classmethod
|
|
499
|
+
def delete(cls, name: str, verbose: bool = False) -> None:
|
|
500
|
+
"""Delete a user.
|
|
501
|
+
|
|
502
|
+
Args:
|
|
503
|
+
name: Username to delete
|
|
504
|
+
verbose: Enable verbose output
|
|
505
|
+
"""
|
|
506
|
+
console.print(f"Deleting user: {name}")
|
|
507
|
+
args = ["sacctmgr", "-i", "delete", "user", f"name={name}"]
|
|
508
|
+
|
|
509
|
+
try:
|
|
510
|
+
result = subprocess.run(
|
|
511
|
+
args,
|
|
512
|
+
check=True,
|
|
513
|
+
capture_output=True,
|
|
514
|
+
text=True,
|
|
515
|
+
)
|
|
516
|
+
console.print(
|
|
517
|
+
f"[green]User '{name}' deleted successfully.[/green]"
|
|
518
|
+
)
|
|
519
|
+
if result.stdout:
|
|
520
|
+
console.print(result.stdout)
|
|
521
|
+
except subprocess.CalledProcessError as e:
|
|
522
|
+
console.print(
|
|
523
|
+
f"[red]Failed to delete user '{name}':[/red] {e.stderr or e}"
|
|
524
|
+
)
|
|
525
|
+
|
|
526
|
+
@classmethod
|
|
527
|
+
def show(
|
|
528
|
+
cls,
|
|
529
|
+
name: Union[None, str] = None,
|
|
530
|
+
data: dict = {},
|
|
531
|
+
style: str = "pretty",
|
|
532
|
+
force_cache_update: bool = False,
|
|
533
|
+
delimiter: str = ";",
|
|
534
|
+
zebra: bool = False,
|
|
535
|
+
profile: str = "default",
|
|
536
|
+
profile_str: Optional[str] = None,
|
|
537
|
+
) -> None:
|
|
538
|
+
"""Show user information.
|
|
539
|
+
|
|
540
|
+
Args:
|
|
541
|
+
name: Optional username or filter (e.g., administrator_level=Admin)
|
|
542
|
+
data: Pre-loaded data (unused, for API compatibility)
|
|
543
|
+
style: Output style ("pretty", "json", or "csv")
|
|
544
|
+
force_cache_update: Whether to force cache update (unused)
|
|
545
|
+
delimiter: Delimiter for CSV output (default: ";")
|
|
546
|
+
zebra: Use zebra striping for table rows (default: False)
|
|
547
|
+
profile: Profile name to use for output formatting
|
|
548
|
+
profile_str: Inline profile string (overrides profile)
|
|
549
|
+
"""
|
|
550
|
+
try:
|
|
551
|
+
# Always get JSON output from sacctmgr for processing
|
|
552
|
+
result = subprocess.run(
|
|
553
|
+
["sacctmgr", "show", "users", "--json"],
|
|
554
|
+
check=True,
|
|
555
|
+
capture_output=True,
|
|
556
|
+
text=True,
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
if not result.stdout:
|
|
560
|
+
console.print("[yellow]No users found.[/yellow]")
|
|
561
|
+
return
|
|
562
|
+
|
|
563
|
+
# Parse JSON data
|
|
564
|
+
data = json.loads(result.stdout)
|
|
565
|
+
users = data.get("users", [])
|
|
566
|
+
|
|
567
|
+
# Filter by name if specified
|
|
568
|
+
if name:
|
|
569
|
+
# Check if it's a filter (contains '=')
|
|
570
|
+
if "=" in name:
|
|
571
|
+
key, value = name.split("=", 1)
|
|
572
|
+
key = key.lower()
|
|
573
|
+
users = [
|
|
574
|
+
u
|
|
575
|
+
for u in users
|
|
576
|
+
if cls._match_filter(u, key, value)
|
|
577
|
+
]
|
|
578
|
+
if not users:
|
|
579
|
+
console.print(
|
|
580
|
+
f"[yellow]No users match filter "
|
|
581
|
+
f"'{name}'.[/yellow]"
|
|
582
|
+
)
|
|
583
|
+
return
|
|
584
|
+
else:
|
|
585
|
+
# It's a username
|
|
586
|
+
users = [u for u in users if u.get("name") == name]
|
|
587
|
+
if not users:
|
|
588
|
+
console.print(
|
|
589
|
+
f"[yellow]User '{name}' not found.[/yellow]"
|
|
590
|
+
)
|
|
591
|
+
return
|
|
592
|
+
|
|
593
|
+
# Get column configuration from profile (once)
|
|
594
|
+
(
|
|
595
|
+
columns,
|
|
596
|
+
styles,
|
|
597
|
+
template,
|
|
598
|
+
sort_field,
|
|
599
|
+
sort_asc,
|
|
600
|
+
) = cls._get_column_config(profile, profile_str)
|
|
601
|
+
|
|
602
|
+
# Apply sorting
|
|
603
|
+
if sort_field:
|
|
604
|
+
users = sort_data(users, sort_field, sort_asc)
|
|
605
|
+
|
|
606
|
+
if style == "json":
|
|
607
|
+
# Print filtered JSON
|
|
608
|
+
filtered_data = {"users": users}
|
|
609
|
+
console.print_json(json.dumps(filtered_data, indent=2))
|
|
610
|
+
elif style == "csv":
|
|
611
|
+
# Header
|
|
612
|
+
headers = [
|
|
613
|
+
col.title().replace("_", " ") for col in columns
|
|
614
|
+
]
|
|
615
|
+
print(delimiter.join(headers))
|
|
616
|
+
|
|
617
|
+
# Data rows
|
|
618
|
+
for user in users:
|
|
619
|
+
row = [
|
|
620
|
+
cls._format_value(user, col) for col in columns
|
|
621
|
+
]
|
|
622
|
+
print(delimiter.join(row))
|
|
623
|
+
else: # pretty style
|
|
624
|
+
# Create table
|
|
625
|
+
table = Table(box=SIMPLE_HEAVY, show_header=True)
|
|
626
|
+
|
|
627
|
+
# Add columns with styles
|
|
628
|
+
for col in columns:
|
|
629
|
+
style = styles.get(col, "white")
|
|
630
|
+
table.add_column(
|
|
631
|
+
col.title().replace("_", " "), style=style
|
|
632
|
+
)
|
|
633
|
+
|
|
634
|
+
# Add rows
|
|
635
|
+
for i, user in enumerate(users):
|
|
636
|
+
row = [
|
|
637
|
+
cls._format_value(user, col) for col in columns
|
|
638
|
+
]
|
|
639
|
+
if zebra and i % 2 == 1:
|
|
640
|
+
table.add_row(*row, style="dim")
|
|
641
|
+
else:
|
|
642
|
+
table.add_row(*row)
|
|
643
|
+
|
|
644
|
+
console.print(table)
|
|
645
|
+
|
|
646
|
+
except subprocess.CalledProcessError as e:
|
|
647
|
+
console.print(
|
|
648
|
+
f"[red]Failed to show users:[/red] {e.stderr or e}"
|
|
649
|
+
)
|
slurm_cli/utils/utils.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from rich.console import Console
|
|
2
|
+
from rich.theme import Theme
|
|
3
|
+
|
|
4
|
+
theme = Theme(
|
|
5
|
+
{
|
|
6
|
+
"info": "dim cyan",
|
|
7
|
+
"warning": "dim yellow",
|
|
8
|
+
"danger": "dim red",
|
|
9
|
+
"success": "dim green",
|
|
10
|
+
"object": "b cyan",
|
|
11
|
+
"str": "bright_green",
|
|
12
|
+
"label": "bright_blue",
|
|
13
|
+
"partition": "b cyan",
|
|
14
|
+
"nodes": "b yellow",
|
|
15
|
+
"time": "b green",
|
|
16
|
+
"qos": "b red",
|
|
17
|
+
"allow": "b green",
|
|
18
|
+
"deny": "b red",
|
|
19
|
+
}
|
|
20
|
+
)
|
|
21
|
+
console = Console(theme=theme)
|