opensandbox-cli 0.1.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.
- opensandbox_cli/__init__.py +20 -0
- opensandbox_cli/__main__.py +20 -0
- opensandbox_cli/client.py +127 -0
- opensandbox_cli/commands/__init__.py +13 -0
- opensandbox_cli/commands/command.py +359 -0
- opensandbox_cli/commands/config_cmd.py +183 -0
- opensandbox_cli/commands/devops.py +123 -0
- opensandbox_cli/commands/egress.py +98 -0
- opensandbox_cli/commands/file.py +442 -0
- opensandbox_cli/commands/sandbox.py +580 -0
- opensandbox_cli/commands/skills.py +775 -0
- opensandbox_cli/config.py +160 -0
- opensandbox_cli/main.py +138 -0
- opensandbox_cli/output.py +363 -0
- opensandbox_cli/py.typed +1 -0
- opensandbox_cli/skill_registry.py +184 -0
- opensandbox_cli/skills/opensandbox-command-execution.md +215 -0
- opensandbox_cli/skills/opensandbox-file-operations.md +244 -0
- opensandbox_cli/skills/opensandbox-network-egress.md +179 -0
- opensandbox_cli/skills/opensandbox-sandbox-lifecycle.md +305 -0
- opensandbox_cli/skills/opensandbox-sandbox-troubleshooting.md +177 -0
- opensandbox_cli/utils.py +212 -0
- opensandbox_cli-0.1.0.dist-info/METADATA +597 -0
- opensandbox_cli-0.1.0.dist-info/RECORD +27 -0
- opensandbox_cli-0.1.0.dist-info/WHEEL +4 -0
- opensandbox_cli-0.1.0.dist-info/entry_points.txt +3 -0
- opensandbox_cli-0.1.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
from importlib.metadata import version
|
|
17
|
+
|
|
18
|
+
__version__ = version("opensandbox-cli")
|
|
19
|
+
except Exception:
|
|
20
|
+
__version__ = "0.0.0-dev"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Allow running as ``python -m opensandbox_cli``."""
|
|
16
|
+
|
|
17
|
+
from opensandbox_cli.main import cli
|
|
18
|
+
|
|
19
|
+
if __name__ == "__main__":
|
|
20
|
+
cli()
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""SDK client factory stored in Click context."""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from dataclasses import dataclass, field
|
|
20
|
+
from datetime import timedelta
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
import httpx
|
|
25
|
+
from opensandbox.config.connection_sync import ConnectionConfigSync
|
|
26
|
+
from opensandbox.sync.manager import SandboxManagerSync
|
|
27
|
+
from opensandbox.sync.sandbox import SandboxSync
|
|
28
|
+
|
|
29
|
+
from opensandbox_cli.output import OutputFormatter
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass
|
|
33
|
+
class ClientContext:
|
|
34
|
+
"""Shared context passed via ``ctx.obj`` to all Click commands."""
|
|
35
|
+
|
|
36
|
+
resolved_config: dict[str, Any]
|
|
37
|
+
config_path: Path
|
|
38
|
+
cli_overrides: dict[str, Any]
|
|
39
|
+
output: OutputFormatter = field(init=False)
|
|
40
|
+
_connection_config: ConnectionConfigSync | None = field(
|
|
41
|
+
default=None, init=False, repr=False
|
|
42
|
+
)
|
|
43
|
+
_manager: SandboxManagerSync | None = field(
|
|
44
|
+
default=None, init=False, repr=False
|
|
45
|
+
)
|
|
46
|
+
_devops_client: httpx.Client | None = field(
|
|
47
|
+
default=None, init=False, repr=False
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
def __post_init__(self) -> None:
|
|
51
|
+
"""Initialize a default human-readable formatter for the command context."""
|
|
52
|
+
self.output = OutputFormatter(
|
|
53
|
+
"table",
|
|
54
|
+
color=self.resolved_config.get("color", True),
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def connection_config(self) -> ConnectionConfigSync:
|
|
59
|
+
if self._connection_config is None:
|
|
60
|
+
cfg = self.resolved_config
|
|
61
|
+
self._connection_config = ConnectionConfigSync(
|
|
62
|
+
api_key=cfg.get("api_key"),
|
|
63
|
+
domain=cfg.get("domain"),
|
|
64
|
+
protocol=cfg.get("protocol", "http"),
|
|
65
|
+
request_timeout=timedelta(seconds=cfg.get("request_timeout", 30)),
|
|
66
|
+
use_server_proxy=cfg.get("use_server_proxy", False),
|
|
67
|
+
)
|
|
68
|
+
return self._connection_config
|
|
69
|
+
|
|
70
|
+
def get_devops_client(self) -> httpx.Client:
|
|
71
|
+
"""Return a cached HTTP client for experimental diagnostics endpoints."""
|
|
72
|
+
if self._devops_client is None:
|
|
73
|
+
config = self.connection_config
|
|
74
|
+
headers = dict(config.headers)
|
|
75
|
+
headers.setdefault("Accept", "text/plain")
|
|
76
|
+
headers.setdefault("User-Agent", config.user_agent)
|
|
77
|
+
if config.api_key:
|
|
78
|
+
headers["OPEN-SANDBOX-API-KEY"] = config.api_key
|
|
79
|
+
|
|
80
|
+
self._devops_client = httpx.Client(
|
|
81
|
+
base_url=config.get_base_url(),
|
|
82
|
+
headers=headers,
|
|
83
|
+
timeout=config.request_timeout.total_seconds(),
|
|
84
|
+
)
|
|
85
|
+
return self._devops_client
|
|
86
|
+
|
|
87
|
+
def get_manager(self) -> SandboxManagerSync:
|
|
88
|
+
"""Return a lazily-created ``SandboxManagerSync``."""
|
|
89
|
+
if self._manager is None:
|
|
90
|
+
self._manager = SandboxManagerSync.create(self.connection_config)
|
|
91
|
+
return self._manager
|
|
92
|
+
|
|
93
|
+
def resolve_sandbox_id(self, sandbox_id: str) -> str:
|
|
94
|
+
"""Return the sandbox ID exactly as provided by the user."""
|
|
95
|
+
return sandbox_id
|
|
96
|
+
|
|
97
|
+
def connect_sandbox(
|
|
98
|
+
self, sandbox_id: str, *, skip_health_check: bool = True
|
|
99
|
+
) -> SandboxSync:
|
|
100
|
+
"""Connect to an existing sandbox by ID."""
|
|
101
|
+
sandbox_id = self.resolve_sandbox_id(sandbox_id)
|
|
102
|
+
return SandboxSync.connect(
|
|
103
|
+
sandbox_id,
|
|
104
|
+
connection_config=self.connection_config,
|
|
105
|
+
skip_health_check=skip_health_check,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
def make_output(self, fmt: str) -> OutputFormatter:
|
|
109
|
+
"""Create and cache the current command-scoped output formatter."""
|
|
110
|
+
formatter = OutputFormatter(
|
|
111
|
+
fmt,
|
|
112
|
+
color=self.resolved_config.get("color", True),
|
|
113
|
+
)
|
|
114
|
+
self.output = formatter
|
|
115
|
+
return formatter
|
|
116
|
+
|
|
117
|
+
def close(self) -> None:
|
|
118
|
+
"""Release resources."""
|
|
119
|
+
if self._manager is not None:
|
|
120
|
+
self._manager.close()
|
|
121
|
+
self._manager = None
|
|
122
|
+
if self._devops_client is not None:
|
|
123
|
+
self._devops_client.close()
|
|
124
|
+
self._devops_client = None
|
|
125
|
+
if self._connection_config is not None:
|
|
126
|
+
self._connection_config.close_transport_if_owned()
|
|
127
|
+
self._connection_config = None
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
# Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Command execution commands: one interface, foreground stream or background tracking."""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import shlex
|
|
20
|
+
import sys
|
|
21
|
+
from datetime import timedelta
|
|
22
|
+
|
|
23
|
+
import click
|
|
24
|
+
from opensandbox.models.execd import OutputMessage, RunCommandOpts
|
|
25
|
+
from opensandbox.models.execd_sync import ExecutionHandlersSync
|
|
26
|
+
|
|
27
|
+
from opensandbox_cli.client import ClientContext
|
|
28
|
+
from opensandbox_cli.utils import (
|
|
29
|
+
DURATION,
|
|
30
|
+
handle_errors,
|
|
31
|
+
output_option,
|
|
32
|
+
prepare_output,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@click.group("command", invoke_without_command=True)
|
|
37
|
+
@click.pass_context
|
|
38
|
+
def command_group(ctx: click.Context) -> None:
|
|
39
|
+
"""⚡ Execute commands in a sandbox."""
|
|
40
|
+
if ctx.invoked_subcommand is None:
|
|
41
|
+
click.echo(ctx.get_help())
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# ---- run ------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
def _run_command(
|
|
47
|
+
obj: ClientContext,
|
|
48
|
+
sandbox_id: str,
|
|
49
|
+
command: tuple[str, ...],
|
|
50
|
+
background: bool,
|
|
51
|
+
workdir: str | None,
|
|
52
|
+
timeout: timedelta | None,
|
|
53
|
+
output_format: str | None,
|
|
54
|
+
) -> None:
|
|
55
|
+
"""Shared implementation for ``command run``.
|
|
56
|
+
|
|
57
|
+
Mode contract:
|
|
58
|
+
- foreground (default): stream output directly, allow only ``-o raw``
|
|
59
|
+
- background (``--background``): return a tracked execution object, allow structured output
|
|
60
|
+
"""
|
|
61
|
+
allowed = ("table", "json", "yaml") if background else ("raw",)
|
|
62
|
+
fallback = "table" if background else "raw"
|
|
63
|
+
prepare_output(obj, output_format, allowed=allowed, fallback=fallback)
|
|
64
|
+
cmd_str = " ".join(shlex.quote(arg) for arg in command)
|
|
65
|
+
sandbox = obj.connect_sandbox(sandbox_id)
|
|
66
|
+
|
|
67
|
+
try:
|
|
68
|
+
opts = RunCommandOpts(
|
|
69
|
+
background=background,
|
|
70
|
+
working_directory=workdir,
|
|
71
|
+
timeout=timeout,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
if background:
|
|
75
|
+
execution = sandbox.commands.run(cmd_str, opts=opts)
|
|
76
|
+
obj.output.success_panel(
|
|
77
|
+
{
|
|
78
|
+
"execution_id": execution.id,
|
|
79
|
+
"sandbox_id": sandbox_id,
|
|
80
|
+
"mode": "background",
|
|
81
|
+
},
|
|
82
|
+
title="Background Command Started",
|
|
83
|
+
)
|
|
84
|
+
return
|
|
85
|
+
|
|
86
|
+
# Foreground: stream stdout/stderr to terminal
|
|
87
|
+
last_text = ""
|
|
88
|
+
|
|
89
|
+
def on_stdout(msg: OutputMessage) -> None:
|
|
90
|
+
nonlocal last_text
|
|
91
|
+
last_text = msg.text
|
|
92
|
+
sys.stdout.write(msg.text)
|
|
93
|
+
sys.stdout.flush()
|
|
94
|
+
|
|
95
|
+
def on_stderr(msg: OutputMessage) -> None:
|
|
96
|
+
nonlocal last_text
|
|
97
|
+
last_text = msg.text
|
|
98
|
+
sys.stderr.write(msg.text)
|
|
99
|
+
sys.stderr.flush()
|
|
100
|
+
|
|
101
|
+
handlers = ExecutionHandlersSync(on_stdout=on_stdout, on_stderr=on_stderr)
|
|
102
|
+
execution = sandbox.commands.run(cmd_str, opts=opts, handlers=handlers)
|
|
103
|
+
|
|
104
|
+
# Ensure terminal prompt starts on a new line
|
|
105
|
+
if last_text and not last_text.endswith("\n"):
|
|
106
|
+
sys.stdout.write("\n")
|
|
107
|
+
sys.stdout.flush()
|
|
108
|
+
|
|
109
|
+
_handle_execution_error(obj, execution)
|
|
110
|
+
finally:
|
|
111
|
+
sandbox.close()
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _handle_execution_error(obj: ClientContext, execution) -> None:
|
|
115
|
+
"""Exit non-zero if the execution finished with an error."""
|
|
116
|
+
if execution.error:
|
|
117
|
+
obj.output.error_panel(
|
|
118
|
+
f"{execution.error.name}: {execution.error.value}",
|
|
119
|
+
title="Execution Error",
|
|
120
|
+
)
|
|
121
|
+
sys.exit(1)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@command_group.command(
|
|
125
|
+
"run",
|
|
126
|
+
help="Run a command in a sandbox. Use `--` before the sandbox command payload.",
|
|
127
|
+
epilog="Separator rule: use `--` before the sandbox command payload.",
|
|
128
|
+
)
|
|
129
|
+
@click.argument("sandbox_id")
|
|
130
|
+
@click.argument("command", nargs=-1, required=True)
|
|
131
|
+
@click.option("-d", "--background", is_flag=True, default=False, help="Run in background.")
|
|
132
|
+
@click.option("-w", "--workdir", default=None, help="Working directory.")
|
|
133
|
+
@click.option("-t", "--timeout", type=DURATION, default=None, help="Command timeout (e.g. 30s, 5m).")
|
|
134
|
+
@output_option("table", "json", "yaml", "raw")
|
|
135
|
+
@click.pass_obj
|
|
136
|
+
@handle_errors
|
|
137
|
+
def command_run(
|
|
138
|
+
obj: ClientContext,
|
|
139
|
+
sandbox_id: str,
|
|
140
|
+
command: tuple[str, ...],
|
|
141
|
+
background: bool,
|
|
142
|
+
workdir: str | None,
|
|
143
|
+
timeout: timedelta | None,
|
|
144
|
+
output_format: str | None,
|
|
145
|
+
) -> None:
|
|
146
|
+
"""Run a command in a sandbox.
|
|
147
|
+
|
|
148
|
+
Default mode streams output directly. Add ``--background`` to return a
|
|
149
|
+
tracked execution object instead. Use ``--`` before the sandbox command payload.
|
|
150
|
+
"""
|
|
151
|
+
_run_command(
|
|
152
|
+
obj,
|
|
153
|
+
sandbox_id,
|
|
154
|
+
command,
|
|
155
|
+
background,
|
|
156
|
+
workdir,
|
|
157
|
+
timeout,
|
|
158
|
+
output_format,
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
# ---- status ---------------------------------------------------------------
|
|
163
|
+
|
|
164
|
+
@command_group.command("status")
|
|
165
|
+
@click.argument("sandbox_id")
|
|
166
|
+
@click.argument("execution_id")
|
|
167
|
+
@output_option("table", "json", "yaml")
|
|
168
|
+
@click.pass_obj
|
|
169
|
+
@handle_errors
|
|
170
|
+
def command_status(
|
|
171
|
+
obj: ClientContext,
|
|
172
|
+
sandbox_id: str,
|
|
173
|
+
execution_id: str,
|
|
174
|
+
output_format: str | None,
|
|
175
|
+
) -> None:
|
|
176
|
+
"""Get command execution status."""
|
|
177
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
178
|
+
sandbox = obj.connect_sandbox(sandbox_id)
|
|
179
|
+
try:
|
|
180
|
+
status = sandbox.commands.get_command_status(execution_id)
|
|
181
|
+
obj.output.print_model(status, title="Command Status")
|
|
182
|
+
finally:
|
|
183
|
+
sandbox.close()
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
# ---- logs -----------------------------------------------------------------
|
|
187
|
+
|
|
188
|
+
@command_group.command("logs")
|
|
189
|
+
@click.argument("sandbox_id")
|
|
190
|
+
@click.argument("execution_id")
|
|
191
|
+
@click.option("--cursor", type=int, default=None, help="Cursor for incremental reads.")
|
|
192
|
+
@output_option("table", "json", "yaml", "raw")
|
|
193
|
+
@click.pass_obj
|
|
194
|
+
@handle_errors
|
|
195
|
+
def command_logs(
|
|
196
|
+
obj: ClientContext,
|
|
197
|
+
sandbox_id: str,
|
|
198
|
+
execution_id: str,
|
|
199
|
+
cursor: int | None,
|
|
200
|
+
output_format: str | None,
|
|
201
|
+
) -> None:
|
|
202
|
+
"""Get background command logs."""
|
|
203
|
+
prepare_output(
|
|
204
|
+
obj, output_format, allowed=("table", "json", "yaml", "raw"), fallback="table"
|
|
205
|
+
)
|
|
206
|
+
sandbox = obj.connect_sandbox(sandbox_id)
|
|
207
|
+
try:
|
|
208
|
+
logs = sandbox.commands.get_background_command_logs(execution_id, cursor=cursor)
|
|
209
|
+
if obj.output.fmt in ("json", "yaml"):
|
|
210
|
+
obj.output.print_model(logs, title="Command Logs")
|
|
211
|
+
elif obj.output.fmt == "raw":
|
|
212
|
+
click.echo(logs.content)
|
|
213
|
+
else:
|
|
214
|
+
obj.output.panel(logs.content, title="Command Logs")
|
|
215
|
+
finally:
|
|
216
|
+
sandbox.close()
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
# ---- interrupt ------------------------------------------------------------
|
|
220
|
+
|
|
221
|
+
@command_group.command("interrupt")
|
|
222
|
+
@click.argument("sandbox_id")
|
|
223
|
+
@click.argument("execution_id")
|
|
224
|
+
@output_option("table", "json", "yaml")
|
|
225
|
+
@click.pass_obj
|
|
226
|
+
@handle_errors
|
|
227
|
+
def command_interrupt(
|
|
228
|
+
obj: ClientContext,
|
|
229
|
+
sandbox_id: str,
|
|
230
|
+
execution_id: str,
|
|
231
|
+
output_format: str | None,
|
|
232
|
+
) -> None:
|
|
233
|
+
"""Interrupt a running command."""
|
|
234
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
235
|
+
sandbox = obj.connect_sandbox(sandbox_id)
|
|
236
|
+
try:
|
|
237
|
+
sandbox.commands.interrupt(execution_id)
|
|
238
|
+
obj.output.success(f"Interrupted: {execution_id}")
|
|
239
|
+
finally:
|
|
240
|
+
sandbox.close()
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
@command_group.group("session", invoke_without_command=True)
|
|
244
|
+
@click.pass_context
|
|
245
|
+
def session_group(ctx: click.Context) -> None:
|
|
246
|
+
"""Manage persistent bash sessions."""
|
|
247
|
+
if ctx.invoked_subcommand is None:
|
|
248
|
+
click.echo(ctx.get_help())
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
@session_group.command("create")
|
|
252
|
+
@click.argument("sandbox_id")
|
|
253
|
+
@click.option("-w", "--workdir", default=None, help="Initial working directory.")
|
|
254
|
+
@output_option("table", "json", "yaml")
|
|
255
|
+
@click.pass_obj
|
|
256
|
+
@handle_errors
|
|
257
|
+
def session_create(
|
|
258
|
+
obj: ClientContext,
|
|
259
|
+
sandbox_id: str,
|
|
260
|
+
workdir: str | None,
|
|
261
|
+
output_format: str | None,
|
|
262
|
+
) -> None:
|
|
263
|
+
"""Create a persistent bash session."""
|
|
264
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
265
|
+
sandbox = obj.connect_sandbox(sandbox_id)
|
|
266
|
+
try:
|
|
267
|
+
session_id = sandbox.commands.create_session(working_directory=workdir)
|
|
268
|
+
obj.output.success_panel(
|
|
269
|
+
{
|
|
270
|
+
"sandbox_id": sandbox.id,
|
|
271
|
+
"session_id": session_id,
|
|
272
|
+
"working_directory": workdir,
|
|
273
|
+
},
|
|
274
|
+
title="Session Created",
|
|
275
|
+
)
|
|
276
|
+
finally:
|
|
277
|
+
sandbox.close()
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
@session_group.command(
|
|
281
|
+
"run",
|
|
282
|
+
help="Run a command in an existing bash session. Use `--` before the sandbox command payload.",
|
|
283
|
+
epilog="Separator rule: use `--` before the sandbox command payload.",
|
|
284
|
+
)
|
|
285
|
+
@click.argument("sandbox_id")
|
|
286
|
+
@click.argument("session_id")
|
|
287
|
+
@click.argument("command", nargs=-1, required=True)
|
|
288
|
+
@click.option("-w", "--workdir", default=None, help="Working directory override for this run.")
|
|
289
|
+
@click.option("-t", "--timeout", type=DURATION, default=None, help="Command timeout (e.g. 30s, 5m).")
|
|
290
|
+
@output_option("raw", help_text="Output format: raw.")
|
|
291
|
+
@click.pass_obj
|
|
292
|
+
@handle_errors
|
|
293
|
+
def session_run(
|
|
294
|
+
obj: ClientContext,
|
|
295
|
+
sandbox_id: str,
|
|
296
|
+
session_id: str,
|
|
297
|
+
command: tuple[str, ...],
|
|
298
|
+
workdir: str | None,
|
|
299
|
+
timeout: timedelta | None,
|
|
300
|
+
output_format: str | None,
|
|
301
|
+
) -> None:
|
|
302
|
+
"""Run a command in an existing bash session. Use `--` before the sandbox command payload."""
|
|
303
|
+
prepare_output(obj, output_format, allowed=("raw",), fallback="raw")
|
|
304
|
+
cmd_str = " ".join(shlex.quote(arg) for arg in command)
|
|
305
|
+
sandbox = obj.connect_sandbox(sandbox_id)
|
|
306
|
+
try:
|
|
307
|
+
last_text = ""
|
|
308
|
+
|
|
309
|
+
def on_stdout(msg: OutputMessage) -> None:
|
|
310
|
+
nonlocal last_text
|
|
311
|
+
last_text = msg.text
|
|
312
|
+
sys.stdout.write(msg.text)
|
|
313
|
+
sys.stdout.flush()
|
|
314
|
+
|
|
315
|
+
def on_stderr(msg: OutputMessage) -> None:
|
|
316
|
+
nonlocal last_text
|
|
317
|
+
last_text = msg.text
|
|
318
|
+
sys.stderr.write(msg.text)
|
|
319
|
+
sys.stderr.flush()
|
|
320
|
+
|
|
321
|
+
handlers = ExecutionHandlersSync(on_stdout=on_stdout, on_stderr=on_stderr)
|
|
322
|
+
execution = sandbox.commands.run_in_session(
|
|
323
|
+
session_id,
|
|
324
|
+
cmd_str,
|
|
325
|
+
working_directory=workdir,
|
|
326
|
+
timeout=timeout,
|
|
327
|
+
handlers=handlers,
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
if last_text and not last_text.endswith("\n"):
|
|
331
|
+
sys.stdout.write("\n")
|
|
332
|
+
sys.stdout.flush()
|
|
333
|
+
|
|
334
|
+
_handle_execution_error(obj, execution)
|
|
335
|
+
finally:
|
|
336
|
+
sandbox.close()
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
@session_group.command("delete")
|
|
340
|
+
@click.argument("sandbox_id")
|
|
341
|
+
@click.argument("session_id")
|
|
342
|
+
@output_option("table", "json", "yaml")
|
|
343
|
+
@click.pass_obj
|
|
344
|
+
@handle_errors
|
|
345
|
+
def session_delete(
|
|
346
|
+
obj: ClientContext,
|
|
347
|
+
sandbox_id: str,
|
|
348
|
+
session_id: str,
|
|
349
|
+
output_format: str | None,
|
|
350
|
+
) -> None:
|
|
351
|
+
"""Delete a persistent bash session."""
|
|
352
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
353
|
+
sandbox = obj.connect_sandbox(sandbox_id)
|
|
354
|
+
try:
|
|
355
|
+
sandbox.commands.delete_session(session_id)
|
|
356
|
+
obj.output.success(f"Deleted session: {session_id}")
|
|
357
|
+
finally:
|
|
358
|
+
sandbox.close()
|
|
359
|
+
|