-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdevbox_from_blueprint_lifecycle.py
More file actions
82 lines (67 loc) · 2.44 KB
/
devbox_from_blueprint_lifecycle.py
File metadata and controls
82 lines (67 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
#!/usr/bin/env -S uv run python
"""
---
title: Devbox From Blueprint (Run Command, Shutdown)
slug: devbox-from-blueprint-lifecycle
use_case: Create a devbox from a blueprint, run a command, validate output, and cleanly tear everything down.
workflow:
- Create a blueprint
- Create a devbox from the blueprint
- Execute a command in the devbox
- Validate exit code and stdout
- Shutdown devbox and delete blueprint
tags:
- devbox
- blueprint
- commands
- cleanup
prerequisites:
- RUNLOOP_API_KEY
run: uv run python -m examples.devbox_from_blueprint_lifecycle
test: uv run pytest -m smoketest tests/smoketests/examples/
---
"""
from __future__ import annotations
from runloop_api_client import RunloopSDK
from runloop_api_client.lib.polling import PollingConfig
from ._harness import run_as_cli, unique_name, wrap_recipe
from .example_types import ExampleCheck, RecipeOutput, RecipeContext
BLUEPRINT_POLL_TIMEOUT_S = 10 * 60
def recipe(ctx: RecipeContext) -> RecipeOutput:
"""Create a devbox from a blueprint, run a command, and clean up."""
cleanup = ctx.cleanup
sdk = RunloopSDK()
blueprint = sdk.blueprint.create(
name=unique_name("example-blueprint"),
dockerfile='FROM ubuntu:22.04\nRUN echo "Hello from your blueprint"',
polling_config=PollingConfig(timeout_seconds=BLUEPRINT_POLL_TIMEOUT_S),
)
cleanup.add(f"blueprint:{blueprint.id}", blueprint.delete)
devbox = blueprint.create_devbox(
name=unique_name("example-devbox"),
launch_parameters={
"resource_size_request": "X_SMALL",
"keep_alive_time_seconds": 60 * 5,
},
)
cleanup.add(f"devbox:{devbox.id}", devbox.shutdown)
result = devbox.cmd.exec('echo "Hello from your devbox"')
stdout = result.stdout()
return RecipeOutput(
resources_created=[f"blueprint:{blueprint.id}", f"devbox:{devbox.id}"],
checks=[
ExampleCheck(
name="command exits successfully",
passed=result.exit_code == 0,
details=f"exitCode={result.exit_code}",
),
ExampleCheck(
name="command output contains expected text",
passed="Hello from your devbox" in stdout,
details=stdout.strip(),
),
],
)
run_devbox_from_blueprint_lifecycle_example = wrap_recipe(recipe)
if __name__ == "__main__":
run_as_cli(run_devbox_from_blueprint_lifecycle_example)