diff --git a/CHANGELOG.md b/CHANGELOG.md index b56a94e..039b0b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ Versions follow [Semantic Versioning](https://semver.org/). ### Added +- Optional sandbox expiry through `CreateSandboxRequest.timeout_minutes`, with + `expires_at` response fields and an optional final-minute SIGTERM warning. - Reusable disk and full snapshot create, get, list, and delete operations, plus sandbox restoration through `CreateSandboxRequest.snapshot_id`. - Checkpoint create, list, restore, and delete operations scoped to diff --git a/README.md b/README.md index 4d623ba..ee4cb8b 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,25 @@ sandbox.set_labels({"environment": "production"}) List filters use AND semantics. `set_labels()` merges the supplied keys into the existing label map. +## Limit sandbox lifetime + +```python +sandbox = client.create_sandbox( + CreateSandboxRequest( + shape="s-1vcpu-1gb", + rootfs="devbox:1", + timeout_minutes=10, + sigterm_before_expiry=True, + ) +) +print(sandbox.data.expires_at) +``` + +`timeout_minutes` accepts 2–1440 whole minutes and starts when the sandbox first +reaches `running`. Activity and pause/resume do not extend the deadline. +`sigterm_before_expiry` requests a best-effort warning to managed workloads +during the final minute and requires `timeout_minutes`. + ## Capture and restore snapshots ```python diff --git a/examples/hello_world/main.py b/examples/hello_world/main.py index 973f5e9..0eaed3a 100644 --- a/examples/hello_world/main.py +++ b/examples/hello_world/main.py @@ -13,8 +13,14 @@ def main() -> None: name="hello-python", shape="s-4vcpu-4gb", rootfs="devbox:1", + timeout_minutes=10, + sigterm_before_expiry=True, ) ) + print( + f"expires_at={sandbox.data.expires_at} " + f"sigterm_before_expiry={sandbox.data.sigterm_before_expiry}" + ) try: response = sandbox.run_command( RunCommandRequest(command="sh", arguments=["-c", "uname -a"]) diff --git a/src/createos/client.py b/src/createos/client.py index f768017..3e4b011 100644 --- a/src/createos/client.py +++ b/src/createos/client.py @@ -137,6 +137,8 @@ def create_sandbox( created = _one(CreateSandboxResponse, data) sandbox = Sandbox( snapshot_id=created.snapshot_id, + expires_at=created.expires_at, + sigterm_before_expiry=created.sigterm_before_expiry, id=created.id, status=created.status, ip_address=created.ip_address, diff --git a/src/createos/models.py b/src/createos/models.py index 7c190c0..5e5c99e 100644 --- a/src/createos/models.py +++ b/src/createos/models.py @@ -254,6 +254,12 @@ class CreateSandboxRequest(Model): labels: dict[str, str] = _json( "labels", omit_default=True, default_factory=dict ) + timeout_minutes: int = _json( + "timeout_minutes", omit_default=True, default=0 + ) + sigterm_before_expiry: bool = _json( + "sigterm_before_expiry", omit_default=True, default=False + ) networks: list[NetworkEntry] = _json( "networks", omit_default=True, default_factory=list ) @@ -590,6 +596,8 @@ class CreateSandboxResponse(Model): bandwidth_quota_bytes: int = 0 ingress_url_template: str = "" snapshot_id: str | None = None + expires_at: datetime | None = None + sigterm_before_expiry: bool = False @dataclass(slots=True) @@ -621,6 +629,8 @@ class Sandbox(Model): forked_from: str | None = None auto_pause_after_seconds: int | None = None snapshot_id: str | None = None + expires_at: datetime | None = None + sigterm_before_expiry: bool = False @dataclass(slots=True) diff --git a/tests/test_sdk.py b/tests/test_sdk.py index 073406f..0705a7c 100644 --- a/tests/test_sdk.py +++ b/tests/test_sdk.py @@ -226,6 +226,8 @@ def handler(request): "status": "paused", "name": "demo", "labels": {"team": "backend"}, + "expires_at": "2026-11-23T12:10:00Z", + "sigterm_before_expiry": True, "ip": "10.0.0.2", "shape": "s-4vcpu-4gb", "mem_mib": 4096, @@ -244,14 +246,20 @@ def handler(request): shape="s-4vcpu-4gb", name="demo", labels={"team": "backend"}, + timeout_minutes=10, + sigterm_before_expiry=True, ingress_enabled=True, ) ) assert requests[0].url.path == "/api/v1/sandboxes" assert json.loads(requests[0].content)["labels"] == {"team": "backend"} + assert json.loads(requests[0].content)["timeout_minutes"] == 10 + assert json.loads(requests[0].content)["sigterm_before_expiry"] is True assert sandbox.id == "sb-1" assert sandbox.status is SandboxStatus.PAUSED assert sandbox.data.labels == {"team": "backend"} + assert sandbox.data.expires_at.isoformat() == "2026-11-23T12:10:00+00:00" + assert sandbox.data.sigterm_before_expiry is True assert sandbox.files and sandbox.processes and sandbox.computer.mouse assert sandbox.preview_url(8080) == "https://8080-sb-1.example.test"