forked from opensandbox-group/OpenSandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
353 lines (296 loc) · 11.6 KB
/
Copy pathmain.py
File metadata and controls
353 lines (296 loc) · 11.6 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# 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.
"""
Docker PVC (Named Volume) Mount Example
========================================
Demonstrates how to mount Docker named volumes into sandbox containers using
the OpenSandbox ``pvc`` backend. In Docker runtime the ``pvc`` backend maps
``claimName`` to a Docker named volume -- providing a more convenient and
secure alternative to host-path bind mounts for sharing data across sandboxes.
Four scenarios are demonstrated:
1. **Read-write mount** - Mount a named volume for bidirectional file I/O.
2. **Read-only mount** - Mount a named volume as read-only.
3. **Cross-sandbox sharing** - Two sandboxes share data through the same named
volume without exposing any host path.
4. **SubPath mount** - Mount only a subdirectory of a named volume,
keeping the same API as Kubernetes PVC subPath.
Prerequisites:
- OpenSandbox server running with Docker runtime
- Docker named volume created before running this script (see README.md)
"""
import asyncio
import os
import subprocess
from datetime import timedelta
from opensandbox import Sandbox
from opensandbox.config import ConnectionConfig
try:
from opensandbox.models.sandboxes import PVC, Volume
except ImportError:
print(
"ERROR: Your installed opensandbox SDK does not include Volume/PVC 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)
VOLUME_NAME = "opensandbox-pvc-demo"
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
def ensure_named_volume() -> None:
"""Create the Docker named volume and seed it with test data."""
print(f" Ensuring Docker named volume '{VOLUME_NAME}' exists...")
subprocess.run(
["docker", "volume", "rm", VOLUME_NAME],
capture_output=True,
)
subprocess.run(
["docker", "volume", "create", VOLUME_NAME],
check=True,
capture_output=True,
)
# Seed the volume with a marker file and subpath test data
subprocess.run(
[
"docker", "run", "--rm",
"-v", f"{VOLUME_NAME}:/data",
"alpine",
"sh", "-c",
"echo 'hello-from-named-volume' > /data/marker.txt && "
"mkdir -p /data/datasets/train && "
"echo 'id,value' > /data/datasets/train/data.csv && "
"echo '1,100' >> /data/datasets/train/data.csv && "
"echo '2,200' >> /data/datasets/train/data.csv",
],
check=True,
capture_output=True,
)
print(f" Created volume '{VOLUME_NAME}' with marker.txt and datasets/train/")
async def demo_readwrite_mount(config: ConnectionConfig, image: str) -> None:
"""
Scenario 1: Read-write named volume mount.
Mount a Docker named volume into the sandbox at /mnt/data.
Write a file inside the sandbox, then read it back to verify.
"""
print("\n" + "=" * 60)
print("Scenario 1: Read-Write PVC (Named Volume) Mount")
print("=" * 60)
print(f" Volume name: {VOLUME_NAME}")
print(f" Mount path : /mnt/data")
sandbox = await Sandbox.create(
image=image,
connection_config=config,
timeout=timedelta(minutes=2),
volumes=[
Volume(
name="demo-data",
pvc=PVC(claimName=VOLUME_NAME),
mountPath="/mnt/data",
readOnly=False,
),
],
)
async with sandbox:
try:
# Read the seeded marker file
print("\n [1] Reading marker file from named volume:")
await print_exec(sandbox, "cat /mnt/data/marker.txt")
# Write a new file
print("\n [2] Writing a file from inside the sandbox:")
await print_exec(
sandbox,
"echo 'written-by-sandbox' > /mnt/data/sandbox-output.txt",
)
print(" -> Written: /mnt/data/sandbox-output.txt")
# Read it back
print("\n [3] Reading back the written file:")
await print_exec(sandbox, "cat /mnt/data/sandbox-output.txt")
# List all files
print("\n [4] Listing volume contents:")
await print_exec(sandbox, "ls -la /mnt/data/")
finally:
await sandbox.kill()
print("\n Scenario 1 completed.")
async def demo_readonly_mount(config: ConnectionConfig, image: str) -> None:
"""
Scenario 2: Read-only named volume mount.
Mount the same named volume as read-only. Verify reads succeed but
writes are rejected by the container runtime.
"""
print("\n" + "=" * 60)
print("Scenario 2: Read-Only PVC (Named Volume) Mount")
print("=" * 60)
print(f" Volume name: {VOLUME_NAME}")
print(f" Mount path : /mnt/readonly")
sandbox = await Sandbox.create(
image=image,
connection_config=config,
timeout=timedelta(minutes=2),
volumes=[
Volume(
name="readonly-vol",
pvc=PVC(claimName=VOLUME_NAME),
mountPath="/mnt/readonly",
readOnly=True,
),
],
)
async with sandbox:
try:
# Read the marker file
print("\n [1] Reading marker.txt from read-only mount:")
await print_exec(sandbox, "cat /mnt/readonly/marker.txt")
# Attempt to write (should fail)
print("\n [2] 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_cross_sandbox_sharing(config: ConnectionConfig, image: str) -> None:
"""
Scenario 3: Cross-sandbox data sharing via named volume.
Two sandboxes mount the same named volume. Sandbox A writes a file,
then Sandbox B reads it -- demonstrating data sharing without any host
path exposure.
"""
print("\n" + "=" * 60)
print("Scenario 3: Cross-Sandbox Sharing via PVC (Named Volume)")
print("=" * 60)
print(f" Volume name: {VOLUME_NAME}")
volume_spec = Volume(
name="shared-vol",
pvc=PVC(claimName=VOLUME_NAME),
mountPath="/mnt/shared",
readOnly=False,
)
# --- Sandbox A: write ---
print("\n [Sandbox A] Creating sandbox and writing data...")
sandbox_a = await Sandbox.create(
image=image,
connection_config=config,
timeout=timedelta(minutes=2),
volumes=[volume_spec],
)
async with sandbox_a:
try:
await print_exec(
sandbox_a,
"echo 'message-from-sandbox-a' > /mnt/shared/cross-sandbox.txt",
)
print(" [Sandbox A] Wrote /mnt/shared/cross-sandbox.txt")
finally:
await sandbox_a.kill()
# --- Sandbox B: read ---
print("\n [Sandbox B] Creating sandbox and reading data...")
sandbox_b = await Sandbox.create(
image=image,
connection_config=config,
timeout=timedelta(minutes=2),
volumes=[volume_spec],
)
async with sandbox_b:
try:
print(" [Sandbox B] Reading file written by Sandbox A:")
text = await print_exec(sandbox_b, "cat /mnt/shared/cross-sandbox.txt")
if text and "message-from-sandbox-a" in text:
print("\n Cross-sandbox data sharing verified!")
finally:
await sandbox_b.kill()
print("\n Scenario 3 completed.")
async def demo_subpath_mount(config: ConnectionConfig, image: str) -> None:
"""
Scenario 4: SubPath mount on a named volume.
Mount only a subdirectory (datasets/train) of the named volume. The server
resolves the volume's host-side Mountpoint via ``docker volume inspect`` and
appends the subPath, producing a standard bind mount. This keeps the API
consistent with Kubernetes PVC subPath semantics.
"""
print("\n" + "=" * 60)
print("Scenario 4: SubPath PVC (Named Volume) Mount")
print("=" * 60)
print(f" Volume name: {VOLUME_NAME}")
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="train-data",
pvc=PVC(claimName=VOLUME_NAME),
mountPath="/mnt/training-data",
readOnly=True,
subPath="datasets/train",
),
],
)
async with sandbox:
try:
# List contents -- should only show the subpath
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")
# Verify the root marker.txt is NOT visible (we're inside datasets/train)
print("\n [3] Verifying volume root is NOT visible:")
result = await sandbox.commands.run("test -f /mnt/training-data/marker.txt && echo FOUND || echo NOT-FOUND")
text = "\n".join(msg.text for msg in result.logs.stdout)
print(f" marker.txt at mount root: {text}")
if "NOT-FOUND" in text:
print(" -> Confirmed: subPath isolation is working correctly")
finally:
await sandbox.kill()
print("\n Scenario 4 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")
config = ConnectionConfig(
domain=domain,
api_key=api_key,
request_timeout=timedelta(minutes=3),
)
print(f"OpenSandbox server : {config.domain}")
print(f"Sandbox image : {image}")
print(f"Docker volume : {VOLUME_NAME}")
# Ensure the named volume exists with seed data
ensure_named_volume()
await demo_readwrite_mount(config, image)
await demo_readonly_mount(config, image)
await demo_cross_sandbox_sharing(config, image)
await demo_subpath_mount(config, image)
print("\n" + "=" * 60)
print("All scenarios completed successfully!")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(main())