forked from opensandbox-group/OpenSandbox
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
272 lines (224 loc) · 8.86 KB
/
main.py
File metadata and controls
272 lines (224 loc) · 8.86 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# Copyright 2025 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Host Volume Mount Example
=========================
Demonstrates how to mount a host directory into a sandbox container using
the OpenSandbox Volume API. This enables sharing files, datasets, or model
checkpoints between the host machine and sandbox environments.
Three scenarios are demonstrated:
1. **Read-write mount** - Share a working directory for bidirectional file exchange.
2. **Read-only mount** - Provide shared datasets or configs that sandboxes should
not modify.
3. **SubPath mount** - Mount a specific subdirectory from the host path.
Prerequisites:
- OpenSandbox server running with Docker runtime
- Server config includes `[storage]` section with appropriate `allowed_host_paths`
- Host directories created before running this script (see README.md)
"""
import asyncio
import os
import tempfile
from datetime import timedelta
from pathlib import Path
from opensandbox import Sandbox
from opensandbox.config import ConnectionConfig
try:
from opensandbox.models.sandboxes import Host, Volume
except ImportError:
print(
"ERROR: Your installed opensandbox SDK does not include Volume/Host models.\n"
" Volume support requires the latest SDK from source.\n"
" Please install from the local repository:\n"
"\n"
" pip install -e sdks/sandbox/python\n"
"\n"
" See README.md for details."
)
raise SystemExit(1)
async def print_exec(sandbox: Sandbox, command: str) -> str | None:
"""Run a command in the sandbox and print/return stdout."""
result = await sandbox.commands.run(command)
if result.error:
print(f" [error] {result.error.name}: {result.error.value}")
return None
text = "\n".join(msg.text for msg in result.logs.stdout)
if text:
print(f" {text}")
return text
async def demo_readwrite_mount(config: ConnectionConfig, image: str, host_dir: str) -> None:
"""
Scenario 1: Read-write mount.
Mount a host directory into the sandbox at /mnt/shared. Write a file from
inside the sandbox, then verify it appears on the host.
"""
print("\n" + "=" * 60)
print("Scenario 1: Read-Write Host Volume Mount")
print("=" * 60)
print(f" Host path : {host_dir}")
print(f" Mount path: /mnt/shared")
sandbox = await Sandbox.create(
image=image,
connection_config=config,
timeout=timedelta(minutes=2),
volumes=[
Volume(
name="shared-data",
host=Host(path=host_dir),
mountPath="/mnt/shared",
readOnly=False,
),
],
)
async with sandbox:
try:
# Read existing files from host
print("\n [1] Listing files visible from inside the sandbox:")
await print_exec(sandbox, "ls -la /mnt/shared/")
# Write a file from inside the sandbox
print("\n [2] Writing a file from inside the sandbox:")
await print_exec(
sandbox,
"echo 'Hello from sandbox!' > /mnt/shared/sandbox-greeting.txt",
)
print(" -> Written: /mnt/shared/sandbox-greeting.txt")
# Verify the file content
print("\n [3] Reading back the file:")
await print_exec(sandbox, "cat /mnt/shared/sandbox-greeting.txt")
# Check host-side: the file should now exist on the host
host_file = Path(host_dir) / "sandbox-greeting.txt"
if host_file.exists():
print(f"\n [4] Verified on host: {host_file}")
print(f" Content: {host_file.read_text().strip()}")
else:
print(f"\n [4] Note: {host_file} not directly visible (expected on remote Docker)")
finally:
await sandbox.kill()
print("\n Scenario 1 completed.")
async def demo_readonly_mount(config: ConnectionConfig, image: str, host_dir: str) -> None:
"""
Scenario 2: Read-only mount.
Mount the same host directory as read-only. Verify reads work but writes
are rejected by the container runtime.
"""
print("\n" + "=" * 60)
print("Scenario 2: Read-Only Host Volume Mount")
print("=" * 60)
print(f" Host path : {host_dir}")
print(f" Mount path: /mnt/readonly")
sandbox = await Sandbox.create(
image=image,
connection_config=config,
timeout=timedelta(minutes=2),
volumes=[
Volume(
name="readonly-data",
host=Host(path=host_dir),
mountPath="/mnt/readonly",
readOnly=True,
),
],
)
async with sandbox:
try:
# Read existing files
print("\n [1] Reading files from read-only mount:")
await print_exec(sandbox, "ls -la /mnt/readonly/")
# Read the marker file
print("\n [2] Reading marker.txt:")
await print_exec(sandbox, "cat /mnt/readonly/marker.txt")
# Attempt to write (should fail)
print("\n [3] Attempting to write (should fail):")
result = await sandbox.commands.run(
"touch /mnt/readonly/should-fail.txt 2>&1 || echo 'Write denied (expected)'"
)
for msg in result.logs.stdout:
print(f" {msg.text}")
for msg in result.logs.stderr:
print(f" {msg.text}")
finally:
await sandbox.kill()
print("\n Scenario 2 completed.")
async def demo_subpath_mount(config: ConnectionConfig, image: str, host_dir: str) -> None:
"""
Scenario 3: SubPath mount.
Mount only a specific subdirectory from the host path. This is useful when
the host path contains multiple datasets or project directories, and you
want to expose only one of them.
"""
print("\n" + "=" * 60)
print("Scenario 3: SubPath Host Volume Mount")
print("=" * 60)
# Ensure subdirectory exists on host
sub_dir = Path(host_dir) / "datasets" / "train"
sub_dir.mkdir(parents=True, exist_ok=True)
(sub_dir / "data.csv").write_text("id,value\n1,100\n2,200\n3,300\n")
print(f" Host path : {host_dir}")
print(f" SubPath : datasets/train")
print(f" Mount path: /mnt/training-data")
sandbox = await Sandbox.create(
image=image,
connection_config=config,
timeout=timedelta(minutes=2),
volumes=[
Volume(
name="training-data",
host=Host(path=host_dir),
mountPath="/mnt/training-data",
subPath="datasets/train",
readOnly=True,
),
],
)
async with sandbox:
try:
# List the mounted subdirectory
print("\n [1] Listing mounted subpath content:")
await print_exec(sandbox, "ls -la /mnt/training-data/")
# Read the CSV data
print("\n [2] Reading data.csv:")
await print_exec(sandbox, "cat /mnt/training-data/data.csv")
finally:
await sandbox.kill()
print("\n Scenario 3 completed.")
async def main() -> None:
domain = os.getenv("SANDBOX_DOMAIN", "localhost:8080")
api_key = os.getenv("SANDBOX_API_KEY")
image = os.getenv("SANDBOX_IMAGE", "ubuntu")
host_dir = os.getenv("HOST_VOLUME_PATH", "")
# If no host path specified, create a temporary directory with sample data
if not host_dir:
host_dir = tempfile.mkdtemp(prefix="opensandbox-vol-")
print(f"No HOST_VOLUME_PATH set, using temporary directory: {host_dir}")
marker = Path(host_dir) / "marker.txt"
marker.write_text("hello-from-host\n")
print(f"Created marker file: {marker}")
else:
print(f"Using HOST_VOLUME_PATH: {host_dir}")
config = ConnectionConfig(
domain=domain,
api_key=api_key,
request_timeout=timedelta(minutes=3),
)
print(f"\nOpenSandbox server : {config.domain}")
print(f"Sandbox image : {image}")
print(f"Host volume path : {host_dir}")
await demo_readwrite_mount(config, image, host_dir)
await demo_readonly_mount(config, image, host_dir)
await demo_subpath_mount(config, image, host_dir)
print("\n" + "=" * 60)
print("All scenarios completed successfully!")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(main())