forked from agentscope-ai/agentscope-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboxlite_client.py
More file actions
454 lines (384 loc) · 14.7 KB
/
boxlite_client.py
File metadata and controls
454 lines (384 loc) · 14.7 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# -*- coding: utf-8 -*-
# pylint: disable=too-many-branches
import atexit
import logging
import socket
import traceback
import boxlite
from boxlite import SyncBoxlite
from .base_client import BaseClient
from ..collections import (
RedisSetCollection,
InMemorySetCollection,
RedisMapping,
InMemoryMapping,
)
logger = logging.getLogger(__name__)
class BoxliteClient(BaseClient):
"""
BoxLite client implementation that provides a Docker-like interface
for managing boxes using the BoxLite SDK.
"""
def __init__(self, config=None):
"""
Initialize the BoxLite client.
Args:
config: Configuration object with optional attributes:
- port_range: Tuple of (start, end) for port range
(default: (8000, 9000))
- redis_enabled: Whether to use Redis for port management
(default: False)
- redis_server: Redis server host (default: 'localhost')
- redis_port: Redis server port (default: 6379)
- redis_db: Redis database number (default: 0)
- redis_user: Redis username (optional)
- redis_password: Redis password (optional)
- redis_port_key: Redis key prefix for ports
(default: 'boxlite:ports')
"""
self.config = config
self.port_range = range(*self.config.port_range)
# Initialize port management
if hasattr(self.config, "redis_enabled") and self.config.redis_enabled:
import redis
redis_client = redis.Redis(
host=getattr(self.config, "redis_server", "localhost"),
port=getattr(self.config, "redis_port", 6379),
db=getattr(self.config, "redis_db", 0),
username=getattr(self.config, "redis_user", None),
password=getattr(self.config, "redis_password", None),
decode_responses=True,
)
try:
redis_client.ping()
except ConnectionError as e:
raise RuntimeError(
"Unable to connect to the Redis server.",
) from e
self.port_set = RedisSetCollection(
redis_client,
set_name=getattr(
self.config,
"redis_port_key",
"boxlite:ports",
),
)
self.ports_cache = RedisMapping(
redis_client,
prefix=getattr(self.config, "redis_port_key", "boxlite:ports"),
)
else:
# Use in-memory collections
self.port_set = InMemorySetCollection()
self.ports_cache = InMemoryMapping()
# Initialize BoxLite runtime
try:
from ...sandbox.constant import REGISTRY
if REGISTRY:
image_registries = [REGISTRY]
else:
image_registries = ["ghcr.io", "docker.io"]
options = boxlite.Options(
image_registries=image_registries,
)
self.runtime = SyncBoxlite(options=options)
self.runtime.start()
except Exception as e:
raise RuntimeError(
f"BoxLite client initialization failed: {str(e)}\n"
"Solutions:\n"
"• Switch to the sync SDK (async is not supported yet)\n"
"• Ensure BoxLite is properly installed\n"
"• Check BoxLite runtime configuration",
) from e
atexit.register(self._cleanup_runtime)
def _cleanup_runtime(self):
try:
if hasattr(self, "runtime") and self.runtime:
if hasattr(self.runtime, "__exit__"):
self.runtime.__exit__(None, None, None)
elif hasattr(self.runtime, "close"):
self.runtime.stop()
logger.info("BoxLite runtime cleaned up via atexit.")
except Exception as e:
logger.warning(f"An error occurred during BoxLite cleanup: {e}")
logger.debug(traceback.format_exc())
def create(
self,
image,
name=None,
ports=None,
volumes=None,
environment=None,
runtime_config=None,
):
"""
Create a new BoxLite box.
Args:
image: Container image to use
name: Optional name for the box
ports: List of container ports to expose (e.g., [8080, 3000])
volumes: List of volume mounts as
(host_path, guest_path, mode) tuples
environment: Dict of environment variables
runtime_config: Additional runtime configuration
(cpus, memory_mib, etc.)
Returns:
Tuple of (container_id, host_ports, host) or (None, None,
None) on failure
"""
if runtime_config is None:
runtime_config = {}
port_mapping = {}
if ports:
free_ports = self._find_free_ports(len(ports))
for container_port, host_port in zip(ports, free_ports):
port_mapping[container_port] = host_port
try:
# Convert environment dict to list of tuples
env_list = []
if environment:
env_list = [
(str(k), "" if v is None else str(v))
for k, v in environment.items()
]
# Convert volumes to BoxLite format
volume_list = []
if volumes:
if isinstance(volumes, dict):
for host_path, spec in volumes.items():
guest_path = spec.get("bind")
mode = (spec.get("mode") or "rw").lower()
read_only = mode in ("ro", "readonly")
volume_list.append(
(host_path, guest_path, read_only),
)
elif isinstance(volumes, (list, tuple)):
for vol in volumes:
if isinstance(vol, (list, tuple)) and len(vol) >= 2:
host_path, guest_path = vol[0], vol[1]
third = vol[2] if len(vol) > 2 else "rw"
read_only = third is True or str(
third,
).lower() in (
"ro",
"readonly",
"true",
"1",
)
volume_list.append(
(host_path, guest_path, read_only),
)
# Convert ports to BoxLite format
port_list = []
for container_port, host_port in port_mapping.items():
if isinstance(container_port, str):
if "/" in container_port:
container_port = container_port.split("/")[0]
port_list.append((int(host_port), int(container_port), "tcp"))
# Create BoxOptions
box_options = boxlite.BoxOptions(
image=image,
env=env_list,
volumes=volume_list,
ports=port_list,
auto_remove=False, # We'll manage removal ourselves
detach=True,
**runtime_config,
)
# Create the box
box = self.runtime.create(box_options, name=name)
box_id = box.id
logger.debug(f"✓ Box created: {box.id}")
# Execute command (mirrors: await box.exec())
execution = box.exec("echo", ["Hello from default runtime"])
stdout = execution.stdout()
logger.debug("Output:")
for line in stdout: # Regular for loop, not async for
logger.debug(f" {line.strip()}")
exec_result = execution.wait() # No await
logger.debug(f"✓ Exit code: {exec_result.exit_code}")
# Store port mapping
if port_mapping:
self.ports_cache.set(box_id, list(port_mapping.values()))
return box_id, list(port_mapping.values()), "localhost"
except Exception as e:
logger.warning(f"An error occurred: {e}")
logger.debug(f"{traceback.format_exc()}")
return None, None, None
def start(self, container_id):
"""
Start a BoxLite box.
Args:
container_id: Box ID or name
Returns:
bool: True if successful, False otherwise
"""
try:
box = self.runtime.get(container_id)
if box is None:
logger.warning(f"Box '{container_id}' not found")
return False
box.start()
return True
except Exception as e:
logger.warning(f"An error occurred: {e}")
logger.debug(f"{traceback.format_exc()}")
return False
def stop(self, container_id, timeout=None):
"""
Stop a BoxLite box.
Args:
container_id: Box ID or name
timeout: Optional timeout in seconds (not used in BoxLite)
Returns:
bool: True if successful, False otherwise
"""
try:
box = self.runtime.get(container_id)
if box is None:
logger.warning(f"Box '{container_id}' not found")
return False
# Stop the box
box.stop()
return True
except Exception as e:
logger.warning(f"An error occurred: {e}")
logger.debug(f"{traceback.format_exc()}")
return False
def remove(self, container_id, force=False):
"""
Remove a BoxLite box.
Args:
container_id: Box ID or name
force: If True, stop the box first if running
Returns:
bool: True if successful, False otherwise
"""
try:
# Get ports before removal
ports = self.ports_cache.get(container_id)
# Remove the box
self.runtime.remove(container_id, force=force)
# Clean up port cache
self.ports_cache.delete(container_id)
# Remove ports from port set
if ports:
for host_port in ports:
self.port_set.remove(host_port)
return True
except Exception as e:
logger.warning(f"An error occurred: {e}")
logger.debug(f"{traceback.format_exc()}")
return False
def inspect(self, container_id):
"""
Inspect a BoxLite box.
Args:
container_id: Box ID or name
Returns:
Dict with box information or None if not found
"""
try:
box = self.runtime.get(container_id)
if box is None:
return None
info = box.info()
ports = self.ports_cache.get(container_id) or []
# Convert BoxInfo to dict format similar to Docker
return {
"Id": info.id,
"Name": info.name or "",
"State": {
"Status": info.state.status,
"Running": info.state.running,
"Paused": False,
"Restarting": False,
"OOMKilled": False,
"Dead": not info.state.running,
"Pid": info.state.pid or 0,
"ExitCode": 0 if info.state.running else 1,
"Error": "",
"StartedAt": info.created_at,
"FinishedAt": ""
if info.state.running
else info.created_at,
},
"Created": info.created_at,
"Image": info.image,
"Config": {
"Env": [], # BoxInfo doesn't expose env directly
},
"NetworkSettings": {
"Ports": self._format_ports(ports),
},
"HostConfig": {
"CpuCount": info.cpus,
"Memory": info.memory_mib * 1024 * 1024,
# Convert MiB to bytes
},
}
except Exception as e:
logger.warning(f"An error occurred: {e}")
logger.debug(f"{traceback.format_exc()}")
return None
def get_status(self, container_id):
"""
Get the current status of the specified box.
Args:
container_id: Box ID or name
Returns:
str: Status string ('running', 'stopped', etc.) or None if not
found
"""
box_attrs = self.inspect(container_id=container_id)
if box_attrs:
return box_attrs["State"]["Status"]
return None
def _find_free_ports(self, n):
"""
Find n free ports in the configured port range.
Args:
n: Number of ports to find
Returns:
List of free port numbers
Raises:
RuntimeError: If not enough free ports are available
"""
free_ports = []
for port in self.port_range:
if len(free_ports) >= n:
break # We have found enough ports
if not self.port_set.add(port):
continue # Port already in set
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind(("", port))
free_ports.append(port) # Port is available
except OSError:
# Bind failed, port is in use
self.port_set.remove(port)
# Try the next one
continue
if len(free_ports) < n:
raise RuntimeError(
"Not enough free ports available in the specified range.",
)
return free_ports
def _format_ports(self, host_ports):
"""
Format port list for Docker-like inspect output.
Args:
host_ports: List of host port numbers
Returns:
Dict formatted like Docker's NetworkSettings.Ports
"""
if not host_ports:
return {}
ports = {}
for host_port in host_ports:
# We don't have the container port info here, so we'll use the
# host port as both host and container port
key = f"{host_port}/tcp"
ports[key] = [{"HostIp": "0.0.0.0", "HostPort": str(host_port)}]
return ports