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,580 @@
|
|
|
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
|
+
"""Sandbox lifecycle commands: create, list, get, kill, pause, resume, renew, endpoint, health, metrics."""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import json
|
|
20
|
+
from datetime import datetime, timedelta, timezone
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
import click
|
|
24
|
+
from opensandbox.models.sandboxes import (
|
|
25
|
+
NetworkPolicy,
|
|
26
|
+
SandboxFilter,
|
|
27
|
+
SandboxImageAuth,
|
|
28
|
+
SandboxImageSpec,
|
|
29
|
+
SandboxMetrics,
|
|
30
|
+
SandboxState,
|
|
31
|
+
Volume,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
from opensandbox_cli.client import ClientContext
|
|
35
|
+
from opensandbox_cli.utils import (
|
|
36
|
+
DURATION,
|
|
37
|
+
KEY_VALUE,
|
|
38
|
+
handle_errors,
|
|
39
|
+
output_option,
|
|
40
|
+
parse_nullable_duration,
|
|
41
|
+
prepare_output,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@click.group("sandbox", invoke_without_command=True)
|
|
46
|
+
@click.pass_context
|
|
47
|
+
def sandbox_group(ctx: click.Context) -> None:
|
|
48
|
+
"""📦 Manage sandbox lifecycle."""
|
|
49
|
+
if ctx.invoked_subcommand is None:
|
|
50
|
+
click.echo(ctx.get_help())
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
# Alias: osb sb ...
|
|
54
|
+
sandbox_group.name = "sandbox"
|
|
55
|
+
|
|
56
|
+
_SANDBOX_STATE_CANONICAL = {
|
|
57
|
+
state.lower(): state for state in SandboxState.values()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _normalize_sandbox_states(states: tuple[str, ...]) -> list[str] | None:
|
|
62
|
+
"""Normalize case-insensitive CLI state filters to SDK canonical values."""
|
|
63
|
+
if not states:
|
|
64
|
+
return None
|
|
65
|
+
|
|
66
|
+
normalized: list[str] = []
|
|
67
|
+
for state in states:
|
|
68
|
+
canonical = _SANDBOX_STATE_CANONICAL.get(state.strip().lower())
|
|
69
|
+
if canonical is None:
|
|
70
|
+
choices = ", ".join(sorted(SandboxState.values()))
|
|
71
|
+
raise click.ClickException(
|
|
72
|
+
f"Invalid sandbox state '{state}'. Valid values: {choices}"
|
|
73
|
+
)
|
|
74
|
+
normalized.append(canonical)
|
|
75
|
+
return normalized
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# ---- create ---------------------------------------------------------------
|
|
79
|
+
|
|
80
|
+
@sandbox_group.command("create")
|
|
81
|
+
@click.option("--image", "-i", required=False, help="Container image (e.g. python:3.11). Defaults to config value if set.")
|
|
82
|
+
@click.option(
|
|
83
|
+
"--image-auth-username",
|
|
84
|
+
default=None,
|
|
85
|
+
help="Registry username for pulling a private image.",
|
|
86
|
+
)
|
|
87
|
+
@click.option(
|
|
88
|
+
"--image-auth-password",
|
|
89
|
+
default=None,
|
|
90
|
+
help="Registry password or token for pulling a private image.",
|
|
91
|
+
)
|
|
92
|
+
@click.option(
|
|
93
|
+
"--timeout",
|
|
94
|
+
"-t",
|
|
95
|
+
"timeout_raw",
|
|
96
|
+
default=None,
|
|
97
|
+
help="Sandbox lifetime (e.g. 10m, 1h), 'none' for manual cleanup, or omit to use defaults.timeout / SDK default TTL.",
|
|
98
|
+
)
|
|
99
|
+
@click.option("--env", "-e", "envs", multiple=True, type=KEY_VALUE, help="Environment variable (KEY=VALUE). Repeatable.")
|
|
100
|
+
@click.option("--metadata", "-m", "metadata_kv", multiple=True, type=KEY_VALUE, help="Metadata (KEY=VALUE). Repeatable.")
|
|
101
|
+
@click.option("--extension", "extensions_kv", multiple=True, type=KEY_VALUE, help="Extension parameter (KEY=VALUE). Repeatable.")
|
|
102
|
+
@click.option("--resource", "resources_kv", multiple=True, type=KEY_VALUE, help="Resource limit (e.g. cpu=1 memory=2Gi). Repeatable.")
|
|
103
|
+
@click.option(
|
|
104
|
+
"--entrypoint",
|
|
105
|
+
"entrypoint",
|
|
106
|
+
multiple=True,
|
|
107
|
+
help="Entrypoint argv item. Repeat to build the full entrypoint.",
|
|
108
|
+
)
|
|
109
|
+
@click.option("--network-policy-file", type=click.Path(exists=True), default=None, help="Network policy JSON file.")
|
|
110
|
+
@click.option("--volumes-file", type=click.Path(exists=True), default=None, help="Volumes JSON file (list of volume objects).")
|
|
111
|
+
@click.option("--skip-health-check", is_flag=True, default=False, help="Skip waiting for sandbox readiness.")
|
|
112
|
+
@click.option("--ready-timeout", type=DURATION, default=None, help="Max wait time for sandbox readiness (e.g. 30s).")
|
|
113
|
+
@output_option("table", "json", "yaml")
|
|
114
|
+
@click.pass_obj
|
|
115
|
+
@handle_errors
|
|
116
|
+
def sandbox_create(
|
|
117
|
+
obj: ClientContext,
|
|
118
|
+
image: str | None,
|
|
119
|
+
image_auth_username: str | None,
|
|
120
|
+
image_auth_password: str | None,
|
|
121
|
+
timeout_raw: str | None,
|
|
122
|
+
envs: tuple[tuple[str, str], ...],
|
|
123
|
+
metadata_kv: tuple[tuple[str, str], ...],
|
|
124
|
+
extensions_kv: tuple[tuple[str, str], ...],
|
|
125
|
+
resources_kv: tuple[tuple[str, str], ...],
|
|
126
|
+
entrypoint: tuple[str, ...],
|
|
127
|
+
network_policy_file: str | None,
|
|
128
|
+
volumes_file: str | None,
|
|
129
|
+
skip_health_check: bool,
|
|
130
|
+
ready_timeout: timedelta | None,
|
|
131
|
+
output_format: str | None,
|
|
132
|
+
) -> None:
|
|
133
|
+
"""Create a new sandbox."""
|
|
134
|
+
from opensandbox.sync.sandbox import SandboxSync
|
|
135
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
136
|
+
|
|
137
|
+
if image is None:
|
|
138
|
+
image = obj.resolved_config.get("default_image")
|
|
139
|
+
if not image:
|
|
140
|
+
raise click.ClickException(
|
|
141
|
+
"Sandbox image is required. Pass --image or set defaults.image in the CLI config."
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
if bool(image_auth_username) != bool(image_auth_password):
|
|
145
|
+
raise click.ClickException(
|
|
146
|
+
"Pass both --image-auth-username and --image-auth-password together."
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
timeout: timedelta | None
|
|
150
|
+
timeout_is_set = False
|
|
151
|
+
if timeout_raw is not None:
|
|
152
|
+
timeout = parse_nullable_duration(timeout_raw)
|
|
153
|
+
timeout_is_set = True
|
|
154
|
+
else:
|
|
155
|
+
timeout = None
|
|
156
|
+
|
|
157
|
+
if timeout_raw is None:
|
|
158
|
+
default_timeout = obj.resolved_config.get("default_timeout")
|
|
159
|
+
if default_timeout:
|
|
160
|
+
timeout = parse_nullable_duration(default_timeout)
|
|
161
|
+
timeout_is_set = True
|
|
162
|
+
|
|
163
|
+
image_spec: SandboxImageSpec | str = image
|
|
164
|
+
if image_auth_username and image_auth_password:
|
|
165
|
+
image_spec = SandboxImageSpec(
|
|
166
|
+
image=image,
|
|
167
|
+
auth=SandboxImageAuth(
|
|
168
|
+
username=image_auth_username,
|
|
169
|
+
password=image_auth_password,
|
|
170
|
+
),
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
kwargs: dict = {
|
|
174
|
+
"connection_config": obj.connection_config,
|
|
175
|
+
"skip_health_check": skip_health_check,
|
|
176
|
+
}
|
|
177
|
+
if timeout_is_set:
|
|
178
|
+
kwargs["timeout"] = timeout
|
|
179
|
+
if ready_timeout is not None:
|
|
180
|
+
kwargs["ready_timeout"] = ready_timeout
|
|
181
|
+
if envs:
|
|
182
|
+
kwargs["env"] = dict(envs)
|
|
183
|
+
if metadata_kv:
|
|
184
|
+
kwargs["metadata"] = dict(metadata_kv)
|
|
185
|
+
if extensions_kv:
|
|
186
|
+
kwargs["extensions"] = dict(extensions_kv)
|
|
187
|
+
if resources_kv:
|
|
188
|
+
kwargs["resource"] = dict(resources_kv)
|
|
189
|
+
if entrypoint:
|
|
190
|
+
kwargs["entrypoint"] = list(entrypoint)
|
|
191
|
+
if network_policy_file:
|
|
192
|
+
with open(network_policy_file) as f:
|
|
193
|
+
kwargs["network_policy"] = NetworkPolicy(**json.load(f))
|
|
194
|
+
if volumes_file:
|
|
195
|
+
with open(volumes_file) as f:
|
|
196
|
+
raw_volumes = json.load(f)
|
|
197
|
+
if not isinstance(raw_volumes, list):
|
|
198
|
+
raise click.ClickException(
|
|
199
|
+
f"Volumes file must contain a JSON array, got {type(raw_volumes).__name__}."
|
|
200
|
+
)
|
|
201
|
+
kwargs["volumes"] = [Volume(**item) for item in raw_volumes]
|
|
202
|
+
|
|
203
|
+
with obj.output.spinner("Creating sandbox..."):
|
|
204
|
+
sandbox = SandboxSync.create(image_spec, **kwargs)
|
|
205
|
+
obj.output.success_panel(
|
|
206
|
+
{
|
|
207
|
+
"id": sandbox.id,
|
|
208
|
+
"image": image,
|
|
209
|
+
"status": "created",
|
|
210
|
+
"timeout": _describe_create_timeout(timeout_is_set, timeout),
|
|
211
|
+
},
|
|
212
|
+
title="Sandbox Created",
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
# ---- list -----------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
@sandbox_group.command("list")
|
|
219
|
+
@click.option("--state", "-s", "states", multiple=True, help="Filter by state (Pending, Running, Paused, ...). Repeatable.")
|
|
220
|
+
@click.option("--metadata", "-m", "metadata_kv", multiple=True, type=KEY_VALUE, help="Metadata filter (KEY=VALUE). Repeatable.")
|
|
221
|
+
@click.option("--page", type=click.IntRange(min=1), default=None, help="Page number (1-indexed).")
|
|
222
|
+
@click.option("--page-size", type=click.IntRange(min=1), default=None, help="Items per page.")
|
|
223
|
+
@output_option("table", "json", "yaml")
|
|
224
|
+
@click.pass_obj
|
|
225
|
+
@handle_errors
|
|
226
|
+
def sandbox_list(
|
|
227
|
+
obj: ClientContext,
|
|
228
|
+
states: tuple[str, ...],
|
|
229
|
+
metadata_kv: tuple[tuple[str, str], ...],
|
|
230
|
+
page: int | None,
|
|
231
|
+
page_size: int | None,
|
|
232
|
+
output_format: str | None,
|
|
233
|
+
) -> None:
|
|
234
|
+
"""List sandboxes."""
|
|
235
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
236
|
+
mgr = obj.get_manager()
|
|
237
|
+
filt = SandboxFilter(
|
|
238
|
+
states=_normalize_sandbox_states(states),
|
|
239
|
+
metadata=dict(metadata_kv) if metadata_kv else None,
|
|
240
|
+
page=page,
|
|
241
|
+
page_size=page_size,
|
|
242
|
+
)
|
|
243
|
+
with obj.output.spinner("Fetching sandboxes..."):
|
|
244
|
+
result = mgr.list_sandbox_infos(filt)
|
|
245
|
+
if not result.sandbox_infos:
|
|
246
|
+
if obj.output.fmt in ("json", "yaml"):
|
|
247
|
+
obj.output.print_dict(
|
|
248
|
+
{
|
|
249
|
+
"items": [],
|
|
250
|
+
"pagination": result.pagination.model_dump(mode="json"),
|
|
251
|
+
},
|
|
252
|
+
title="Sandboxes",
|
|
253
|
+
)
|
|
254
|
+
else:
|
|
255
|
+
obj.output.info("No sandboxes found.")
|
|
256
|
+
return
|
|
257
|
+
|
|
258
|
+
raw_rows = [info.model_dump(mode="json") for info in result.sandbox_infos]
|
|
259
|
+
|
|
260
|
+
# For machine-readable formats, preserve the original structure
|
|
261
|
+
if obj.output.fmt in ("json", "yaml"):
|
|
262
|
+
obj.output.print_dict(
|
|
263
|
+
{
|
|
264
|
+
"items": raw_rows,
|
|
265
|
+
"pagination": result.pagination.model_dump(mode="json"),
|
|
266
|
+
},
|
|
267
|
+
title="Sandboxes",
|
|
268
|
+
)
|
|
269
|
+
return
|
|
270
|
+
|
|
271
|
+
# Flatten nested status/image objects for clean table display
|
|
272
|
+
rows = []
|
|
273
|
+
for d in raw_rows:
|
|
274
|
+
flat = dict(d)
|
|
275
|
+
status_val = flat.get("status")
|
|
276
|
+
if isinstance(status_val, dict):
|
|
277
|
+
flat["status"] = status_val.get("state", str(status_val))
|
|
278
|
+
image_val = flat.get("image")
|
|
279
|
+
if isinstance(image_val, dict):
|
|
280
|
+
flat["image"] = image_val.get("image", str(image_val))
|
|
281
|
+
rows.append(flat)
|
|
282
|
+
|
|
283
|
+
obj.output.print_rows(
|
|
284
|
+
rows,
|
|
285
|
+
columns=["id", "status", "image", "created_at", "expires_at"],
|
|
286
|
+
title="Sandboxes",
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
# ---- get ------------------------------------------------------------------
|
|
291
|
+
|
|
292
|
+
@sandbox_group.command("get")
|
|
293
|
+
@click.argument("sandbox_id")
|
|
294
|
+
@output_option("table", "json", "yaml")
|
|
295
|
+
@click.pass_obj
|
|
296
|
+
@handle_errors
|
|
297
|
+
def sandbox_get(obj: ClientContext, sandbox_id: str, output_format: str | None) -> None:
|
|
298
|
+
"""Get sandbox details."""
|
|
299
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
300
|
+
sandbox_id = obj.resolve_sandbox_id(sandbox_id)
|
|
301
|
+
mgr = obj.get_manager()
|
|
302
|
+
info = mgr.get_sandbox_info(sandbox_id)
|
|
303
|
+
d = info.model_dump(mode="json")
|
|
304
|
+
|
|
305
|
+
# For machine-readable formats, preserve the original structure
|
|
306
|
+
if obj.output.fmt in ("json", "yaml"):
|
|
307
|
+
obj.output.print_dict(d, title="Sandbox Info")
|
|
308
|
+
return
|
|
309
|
+
|
|
310
|
+
# Flatten nested objects for clean table display
|
|
311
|
+
status_val = d.get("status")
|
|
312
|
+
if isinstance(status_val, dict):
|
|
313
|
+
d["status"] = status_val.get("state", str(status_val))
|
|
314
|
+
if status_val.get("reason"):
|
|
315
|
+
d["status_reason"] = status_val["reason"]
|
|
316
|
+
if status_val.get("message"):
|
|
317
|
+
d["status_message"] = status_val["message"]
|
|
318
|
+
image_val = d.get("image")
|
|
319
|
+
if isinstance(image_val, dict):
|
|
320
|
+
d["image"] = image_val.get("image", str(image_val))
|
|
321
|
+
obj.output.print_dict(d, title="Sandbox Info")
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
# ---- kill -----------------------------------------------------------------
|
|
325
|
+
|
|
326
|
+
@sandbox_group.command("kill")
|
|
327
|
+
@click.argument("sandbox_ids", nargs=-1, required=True)
|
|
328
|
+
@output_option("table", "json", "yaml")
|
|
329
|
+
@click.pass_obj
|
|
330
|
+
@handle_errors
|
|
331
|
+
def sandbox_kill(
|
|
332
|
+
obj: ClientContext, sandbox_ids: tuple[str, ...], output_format: str | None
|
|
333
|
+
) -> None:
|
|
334
|
+
"""Terminate one or more sandboxes."""
|
|
335
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
336
|
+
mgr = obj.get_manager()
|
|
337
|
+
rows: list[dict[str, str]] = []
|
|
338
|
+
for sid in sandbox_ids:
|
|
339
|
+
resolved = obj.resolve_sandbox_id(sid)
|
|
340
|
+
with obj.output.spinner(f"Killing sandbox {resolved}..."):
|
|
341
|
+
mgr.kill_sandbox(resolved)
|
|
342
|
+
rows.append({"sandbox_id": resolved, "status": "terminated"})
|
|
343
|
+
obj.output.print_rows(rows, columns=["sandbox_id", "status"], title="Sandboxes")
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
# ---- pause ----------------------------------------------------------------
|
|
347
|
+
|
|
348
|
+
@sandbox_group.command("pause")
|
|
349
|
+
@click.argument("sandbox_id")
|
|
350
|
+
@output_option("table", "json", "yaml")
|
|
351
|
+
@click.pass_obj
|
|
352
|
+
@handle_errors
|
|
353
|
+
def sandbox_pause(obj: ClientContext, sandbox_id: str, output_format: str | None) -> None:
|
|
354
|
+
"""Pause a running sandbox."""
|
|
355
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
356
|
+
sandbox_id = obj.resolve_sandbox_id(sandbox_id)
|
|
357
|
+
mgr = obj.get_manager()
|
|
358
|
+
with obj.output.spinner("Pausing sandbox..."):
|
|
359
|
+
mgr.pause_sandbox(sandbox_id)
|
|
360
|
+
obj.output.success(f"Sandbox paused: {sandbox_id}")
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
# ---- resume ---------------------------------------------------------------
|
|
364
|
+
|
|
365
|
+
@sandbox_group.command("resume")
|
|
366
|
+
@click.argument("sandbox_id")
|
|
367
|
+
@click.option("--skip-health-check", is_flag=True, default=False, help="Skip waiting for sandbox readiness after resume.")
|
|
368
|
+
@click.option("--resume-timeout", type=DURATION, default=None, help="Max wait time for sandbox readiness after resume (e.g. 30s).")
|
|
369
|
+
@output_option("table", "json", "yaml")
|
|
370
|
+
@click.pass_obj
|
|
371
|
+
@handle_errors
|
|
372
|
+
def sandbox_resume(
|
|
373
|
+
obj: ClientContext,
|
|
374
|
+
sandbox_id: str,
|
|
375
|
+
skip_health_check: bool,
|
|
376
|
+
resume_timeout: timedelta | None,
|
|
377
|
+
output_format: str | None,
|
|
378
|
+
) -> None:
|
|
379
|
+
"""Resume a paused sandbox."""
|
|
380
|
+
from opensandbox.sync.sandbox import SandboxSync
|
|
381
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
382
|
+
|
|
383
|
+
sandbox_id = obj.resolve_sandbox_id(sandbox_id)
|
|
384
|
+
sandbox = None
|
|
385
|
+
try:
|
|
386
|
+
kwargs = {
|
|
387
|
+
"connection_config": obj.connection_config,
|
|
388
|
+
"skip_health_check": skip_health_check,
|
|
389
|
+
}
|
|
390
|
+
if resume_timeout is not None:
|
|
391
|
+
kwargs["resume_timeout"] = resume_timeout
|
|
392
|
+
|
|
393
|
+
with obj.output.spinner("Resuming sandbox..."):
|
|
394
|
+
sandbox = SandboxSync.resume(sandbox_id, **kwargs)
|
|
395
|
+
obj.output.success(f"Sandbox resumed: {sandbox_id}")
|
|
396
|
+
finally:
|
|
397
|
+
if sandbox is not None:
|
|
398
|
+
sandbox.close()
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
# ---- renew ----------------------------------------------------------------
|
|
402
|
+
|
|
403
|
+
@sandbox_group.command("renew")
|
|
404
|
+
@click.argument("sandbox_id")
|
|
405
|
+
@click.option("--timeout", "-t", required=True, type=DURATION, help="New TTL duration (e.g. 30m, 2h).")
|
|
406
|
+
@output_option("table", "json", "yaml")
|
|
407
|
+
@click.pass_obj
|
|
408
|
+
@handle_errors
|
|
409
|
+
def sandbox_renew(
|
|
410
|
+
obj: ClientContext,
|
|
411
|
+
sandbox_id: str,
|
|
412
|
+
timeout: timedelta,
|
|
413
|
+
output_format: str | None,
|
|
414
|
+
) -> None:
|
|
415
|
+
"""Renew sandbox expiration."""
|
|
416
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
417
|
+
sandbox_id = obj.resolve_sandbox_id(sandbox_id)
|
|
418
|
+
mgr = obj.get_manager()
|
|
419
|
+
with obj.output.spinner("Renewing sandbox..."):
|
|
420
|
+
resp = mgr.renew_sandbox(sandbox_id, timeout)
|
|
421
|
+
obj.output.success_panel(
|
|
422
|
+
{"sandbox_id": sandbox_id, "expires_at": str(resp.expires_at)},
|
|
423
|
+
title="Sandbox Renewed",
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
# ---- endpoint -------------------------------------------------------------
|
|
428
|
+
|
|
429
|
+
@sandbox_group.command("endpoint")
|
|
430
|
+
@click.argument("sandbox_id")
|
|
431
|
+
@click.option("--port", "-p", required=True, type=click.IntRange(min=1, max=65535), help="Port number.")
|
|
432
|
+
@output_option("table", "json", "yaml")
|
|
433
|
+
@click.pass_obj
|
|
434
|
+
@handle_errors
|
|
435
|
+
def sandbox_endpoint(
|
|
436
|
+
obj: ClientContext, sandbox_id: str, port: int, output_format: str | None
|
|
437
|
+
) -> None:
|
|
438
|
+
"""Get the public endpoint for a sandbox port."""
|
|
439
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
440
|
+
sandbox = obj.connect_sandbox(sandbox_id)
|
|
441
|
+
try:
|
|
442
|
+
ep = sandbox.get_endpoint(port)
|
|
443
|
+
obj.output.print_model(ep, title="Sandbox Endpoint")
|
|
444
|
+
finally:
|
|
445
|
+
sandbox.close()
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
# ---- health ---------------------------------------------------------------
|
|
449
|
+
|
|
450
|
+
@sandbox_group.command("health")
|
|
451
|
+
@click.argument("sandbox_id")
|
|
452
|
+
@output_option("table", "json", "yaml")
|
|
453
|
+
@click.pass_obj
|
|
454
|
+
@handle_errors
|
|
455
|
+
def sandbox_health(
|
|
456
|
+
obj: ClientContext, sandbox_id: str, output_format: str | None
|
|
457
|
+
) -> None:
|
|
458
|
+
"""Check sandbox health."""
|
|
459
|
+
prepare_output(obj, output_format, allowed=("table", "json", "yaml"), fallback="table")
|
|
460
|
+
sandbox = obj.connect_sandbox(sandbox_id)
|
|
461
|
+
try:
|
|
462
|
+
healthy = sandbox.is_healthy()
|
|
463
|
+
if obj.output.fmt == "table":
|
|
464
|
+
if healthy:
|
|
465
|
+
obj.output.success(f"Sandbox {sandbox_id} is healthy")
|
|
466
|
+
else:
|
|
467
|
+
obj.output.error(f"Sandbox {sandbox_id} is unhealthy")
|
|
468
|
+
else:
|
|
469
|
+
obj.output.print_dict(
|
|
470
|
+
{"sandbox_id": sandbox_id, "healthy": healthy},
|
|
471
|
+
title="Health Check",
|
|
472
|
+
)
|
|
473
|
+
finally:
|
|
474
|
+
sandbox.close()
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
# ---- metrics --------------------------------------------------------------
|
|
478
|
+
|
|
479
|
+
@sandbox_group.command("metrics")
|
|
480
|
+
@click.argument("sandbox_id")
|
|
481
|
+
@click.option("--watch", is_flag=True, default=False, help="Stream metrics updates in real time.")
|
|
482
|
+
@output_option("table", "json", "yaml", "raw")
|
|
483
|
+
@click.pass_obj
|
|
484
|
+
@handle_errors
|
|
485
|
+
def sandbox_metrics(
|
|
486
|
+
obj: ClientContext,
|
|
487
|
+
sandbox_id: str,
|
|
488
|
+
watch: bool,
|
|
489
|
+
output_format: str | None,
|
|
490
|
+
) -> None:
|
|
491
|
+
"""Get sandbox resource metrics."""
|
|
492
|
+
fallback = "raw" if watch else "table"
|
|
493
|
+
prepare_output(
|
|
494
|
+
obj, output_format, allowed=("table", "json", "yaml", "raw"), fallback=fallback
|
|
495
|
+
)
|
|
496
|
+
sandbox = obj.connect_sandbox(sandbox_id)
|
|
497
|
+
try:
|
|
498
|
+
if watch:
|
|
499
|
+
_watch_sandbox_metrics(obj, sandbox)
|
|
500
|
+
return
|
|
501
|
+
|
|
502
|
+
m = sandbox.get_metrics()
|
|
503
|
+
obj.output.print_model(m, title="Sandbox Metrics")
|
|
504
|
+
finally:
|
|
505
|
+
sandbox.close()
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
def _watch_sandbox_metrics(obj: ClientContext, sandbox) -> None: # type: ignore[no-untyped-def]
|
|
509
|
+
"""Stream sandbox metrics from the execd SSE endpoint."""
|
|
510
|
+
client = getattr(sandbox.metrics, "_httpx_client", None)
|
|
511
|
+
if client is None:
|
|
512
|
+
raise click.ClickException("Streaming metrics are unavailable for this sandbox connection.")
|
|
513
|
+
|
|
514
|
+
headers = {"Accept": "text/event-stream"}
|
|
515
|
+
|
|
516
|
+
try:
|
|
517
|
+
with client.stream("GET", "/metrics/watch", headers=headers) as response:
|
|
518
|
+
response.raise_for_status()
|
|
519
|
+
for line in response.iter_lines():
|
|
520
|
+
metric, warning = _parse_metric_stream_line(line)
|
|
521
|
+
if warning:
|
|
522
|
+
obj.output.warning(warning)
|
|
523
|
+
continue
|
|
524
|
+
if metric is None:
|
|
525
|
+
continue
|
|
526
|
+
_render_stream_metric(obj, metric)
|
|
527
|
+
except KeyboardInterrupt:
|
|
528
|
+
return
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
def _parse_metric_stream_line(line: str) -> tuple[SandboxMetrics | None, str | None]:
|
|
532
|
+
"""Parse one line from the metrics SSE stream."""
|
|
533
|
+
stripped = line.strip()
|
|
534
|
+
if not stripped or stripped.startswith((":","event:", "id:", "retry:")):
|
|
535
|
+
return None, None
|
|
536
|
+
|
|
537
|
+
payload = stripped[5:].strip() if stripped.startswith("data:") else stripped
|
|
538
|
+
if not payload:
|
|
539
|
+
return None, None
|
|
540
|
+
|
|
541
|
+
decoded: Any = json.loads(payload)
|
|
542
|
+
if isinstance(decoded, dict) and decoded.get("error"):
|
|
543
|
+
return None, f"Metrics stream error: {decoded['error']}"
|
|
544
|
+
return SandboxMetrics.model_validate(decoded), None
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
def _describe_create_timeout(
|
|
548
|
+
timeout_is_set: bool, timeout: timedelta | None
|
|
549
|
+
) -> str:
|
|
550
|
+
"""Describe the sandbox timeout mode shown in create output."""
|
|
551
|
+
if not timeout_is_set:
|
|
552
|
+
return "sdk-default"
|
|
553
|
+
if timeout is None:
|
|
554
|
+
return "manual-cleanup"
|
|
555
|
+
return str(timeout)
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
def _render_stream_metric(obj: ClientContext, metric: SandboxMetrics) -> None:
|
|
559
|
+
"""Render one streaming metrics sample."""
|
|
560
|
+
if obj.output.fmt in ("table", "raw"):
|
|
561
|
+
timestamp = datetime.fromtimestamp(metric.timestamp / 1000, tz=timezone.utc).isoformat()
|
|
562
|
+
click.echo(
|
|
563
|
+
" ".join(
|
|
564
|
+
[
|
|
565
|
+
f"[{timestamp}]",
|
|
566
|
+
f"cpu={metric.cpu_used_percentage:.2f}%",
|
|
567
|
+
f"cores={metric.cpu_count:g}",
|
|
568
|
+
f"mem={metric.memory_used_in_mib:.2f}/{metric.memory_total_in_mib:.2f}MiB",
|
|
569
|
+
]
|
|
570
|
+
)
|
|
571
|
+
)
|
|
572
|
+
return
|
|
573
|
+
|
|
574
|
+
if obj.output.fmt == "json":
|
|
575
|
+
click.echo(json.dumps(metric.model_dump(mode="json"), default=str))
|
|
576
|
+
return
|
|
577
|
+
|
|
578
|
+
if obj.output.fmt == "yaml":
|
|
579
|
+
obj.output.print_model(metric)
|
|
580
|
+
return
|