-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprocess_cmd.py
More file actions
83 lines (65 loc) · 2.44 KB
/
Copy pathprocess_cmd.py
File metadata and controls
83 lines (65 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""``ar sandbox process`` — manage sandbox processes."""
import click
from agentrun_cli._utils.error import handle_errors
from agentrun_cli._utils.output import format_output
from ._helpers import _build_cfg
@click.group("process", help="Manage sandbox processes.")
def process_group():
pass
@process_group.command("list")
@click.argument("sandbox_id")
@click.pass_context
@handle_errors
def process_list(ctx, sandbox_id):
"""List processes in the sandbox.
Returns all processes visible to the container, including ones not
started via the Process API. To act on a process via ``get`` / ``kill``,
start it with ``cmd`` first, or fall back to ``cmd --command "kill <pid>"``.
"""
from agentrun.sandbox import Sandbox
cfg = _build_cfg(ctx)
sb = Sandbox.connect(sandbox_id, config=cfg)
result = sb.process.list()
format_output(ctx, result)
@process_group.command("get")
@click.argument("sandbox_id")
@click.argument("pid")
@click.pass_context
@handle_errors
def process_get(ctx, sandbox_id, pid):
"""Get process details. Only PIDs started via the Process API are resolvable."""
from agentrun.sandbox import Sandbox
cfg = _build_cfg(ctx)
sb = Sandbox.connect(sandbox_id, config=cfg)
result = sb.process.get(pid=pid)
format_output(ctx, result)
@process_group.command("kill")
@click.argument("sandbox_id")
@click.argument("pid")
@click.option(
"--force-shell",
is_flag=True,
help="If the Process API does not know this PID, fall back to 'kill -9 <pid>' via the shell.",
)
@click.pass_context
@handle_errors
def process_kill(ctx, sandbox_id, pid, force_shell):
"""Kill a process in the sandbox.
By default this targets processes registered through the Process API.
Container-level PIDs returned by ``process list`` but not registered
through Process API will report ``process with PID ... not found``; pass
``--force-shell`` to fall back to ``kill -9 <pid>`` via the shell.
"""
from agentrun.sandbox import Sandbox
cfg = _build_cfg(ctx)
sb = Sandbox.connect(sandbox_id, config=cfg)
if force_shell:
shell_result = sb.process.cmd(command=f"kill -9 {pid}", cwd="/", timeout=10)
format_output(
ctx,
{"pid": pid, "killed_via": "shell", "result": shell_result},
quiet_field="pid",
)
return
result = sb.process.kill(pid=pid)
format_output(ctx, result if result else {"pid": pid, "killed": True})