-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathconfigure_boxes.py
More file actions
96 lines (77 loc) · 2.48 KB
/
Copy pathconfigure_boxes.py
File metadata and controls
96 lines (77 loc) · 2.48 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
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
"""
Custom box configuration via the REST API.
Demonstrates:
- BoxOptions with custom CPU and memory
- Environment variables and working directory
- auto_remove=False to keep box after stop
- Inspecting box configuration via info()
Prerequisites:
make dev:python
boxlite serve --port 8100
"""
import asyncio
from boxlite import ApiKeyCredential, Boxlite, BoxOptions, BoxliteRestOptions
SERVER_URL = "http://localhost:8100"
def connect() -> Boxlite:
return Boxlite.rest(BoxliteRestOptions(
url=SERVER_URL, credential=ApiKeyCredential("local-dev-key"),
))
async def main():
print("=" * 50)
print("REST API: Box Configuration")
print("=" * 50)
rt = connect()
# --- Custom resources ---
print("\n=== Custom CPU and Memory ===")
opts = BoxOptions(
image="alpine:latest",
cpus=2,
memory_mib=1024,
auto_remove=False,
)
box = await rt.create(opts, name="config-demo")
info = await box.info()
print(f" Created box: {box.id}")
print(f" CPUs: {info.cpus}")
print(f" Memory MiB: {info.memory_mib}")
print(f" Image: {info.image}")
# --- Environment variables ---
print("\n=== Environment Variables ===")
await box.start()
run = await box.exec(
"sh", args=["-c", "echo APP=$APP_NAME ENV=$APP_ENV"],
env=[("APP_NAME", "boxlite-demo"), ("APP_ENV", "staging")],
)
stdout = run.stdout()
async for line in stdout:
print(f" {line}")
await run.wait()
# --- Working directory ---
print("\n=== Working Directory ===")
opts_wd = BoxOptions(
image="alpine:latest",
working_dir="/tmp",
auto_remove=False,
)
box_wd = await rt.create(opts_wd, name="workdir-demo")
await box_wd.start()
run = await box_wd.exec("pwd")
stdout = run.stdout()
async for line in stdout:
print(f" Working dir: {line}")
await run.wait()
# --- Stop and verify box persists (auto_remove=False) ---
print("\n=== Stop (auto_remove=False) ===")
await box.stop()
info_after_stop = await rt.get_info(box.id)
print(f" Box still exists after stop: {info_after_stop is not None}")
if info_after_stop:
print(f" Status: {info_after_stop.state.status}")
# --- Cleanup ---
await rt.remove(box.id, force=True)
await rt.remove(box_wd.id, force=True)
print(f"\n Cleaned up boxes")
print("\n Done")
if __name__ == "__main__":
asyncio.run(main())