Skip to content

Commit 77a9717

Browse files
committed
feat(vmm): show owner username in vmm ls output
Resolve uid from the /run/user/<uid>/dstack-vmm discovery path to a username via pwd.getpwuid() and display it in the USER column.
1 parent 0051ae9 commit 77a9717

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

vmm/src/vmm-cli.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,19 @@ def discover_vmm_instances() -> List[Dict[str, Any]]:
7676
List of VMM instance info dicts, sorted by started_at.
7777
7878
"""
79+
import pwd
7980
instances = []
8081
for discovery_dir in _get_discovery_dirs():
82+
# Extract uid from /run/user/<uid>/dstack-vmm path
83+
parts = discovery_dir.split('/')
84+
uid_str = parts[3] if len(parts) > 3 and parts[1] == 'run' and parts[2] == 'user' else None
85+
username = None
86+
if uid_str and uid_str.isdigit():
87+
try:
88+
username = pwd.getpwuid(int(uid_str)).pw_name
89+
except KeyError:
90+
username = f"uid:{uid_str}"
91+
8192
for fname in os.listdir(discovery_dir):
8293
if not fname.endswith(".json"):
8394
continue
@@ -88,6 +99,8 @@ def discover_vmm_instances() -> List[Dict[str, Any]]:
8899
pid = info.get("pid")
89100
if pid and not os.path.exists(f"/proc/{pid}"):
90101
continue
102+
if username:
103+
info['user'] = username
91104
instances.append(info)
92105
except (json.JSONDecodeError, FileNotFoundError, PermissionError):
93106
continue
@@ -182,18 +195,19 @@ def cmd_ls_vmm(args):
182195

183196
# Table output
184197

185-
fmt = " {active} {id:<12s} {pid:<8s} {node:<12s} {address:<24s} {workdir}"
198+
fmt = " {active} {id:<12s} {pid:<8s} {user:<10s} {node:<12s} {address:<24s} {workdir}"
186199
print(
187200
fmt.format(
188201
active="",
189202
id="ID",
190203
pid="PID",
204+
user="USER",
191205
node="NAME",
192206
address="ADDRESS",
193207
workdir="WORKING DIR",
194208
)
195209
)
196-
print(" " + "-" * 90)
210+
print(" " + "-" * 100)
197211

198212
for inst in instances:
199213
short_id = inst["id"][:8]
@@ -206,6 +220,7 @@ def cmd_ls_vmm(args):
206220
active=is_active,
207221
id=short_id,
208222
pid=str(inst.get("pid", "?")),
223+
user=inst.get("user", "?")[:10],
209224
node=node_name[:12],
210225
address=address[:24],
211226
workdir=inst.get("working_dir", "?"),

0 commit comments

Comments
 (0)