scalebox-sdk 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scalebox/__init__.py +80 -0
- scalebox/api/__init__.py +128 -0
- scalebox/api/client/__init__.py +8 -0
- scalebox/api/client/api/__init__.py +1 -0
- scalebox/api/client/api/sandboxes/__init__.py +0 -0
- scalebox/api/client/api/sandboxes/delete_sandboxes_sandbox_id.py +161 -0
- scalebox/api/client/api/sandboxes/get_sandboxes.py +176 -0
- scalebox/api/client/api/sandboxes/get_sandboxes_metrics.py +173 -0
- scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id.py +163 -0
- scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id_logs.py +199 -0
- scalebox/api/client/api/sandboxes/get_sandboxes_sandbox_id_metrics.py +214 -0
- scalebox/api/client/api/sandboxes/get_v2_sandboxes.py +229 -0
- scalebox/api/client/api/sandboxes/post_sandboxes.py +174 -0
- scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_pause.py +165 -0
- scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_refreshes.py +182 -0
- scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_resume.py +190 -0
- scalebox/api/client/api/sandboxes/post_sandboxes_sandbox_id_timeout.py +194 -0
- scalebox/api/client/client.py +288 -0
- scalebox/api/client/errors.py +16 -0
- scalebox/api/client/models/__init__.py +81 -0
- scalebox/api/client/models/build_log_entry.py +79 -0
- scalebox/api/client/models/created_access_token.py +100 -0
- scalebox/api/client/models/created_team_api_key.py +166 -0
- scalebox/api/client/models/error.py +67 -0
- scalebox/api/client/models/identifier_masking_details.py +83 -0
- scalebox/api/client/models/listed_sandbox.py +138 -0
- scalebox/api/client/models/log_level.py +11 -0
- scalebox/api/client/models/new_access_token.py +59 -0
- scalebox/api/client/models/new_sandbox.py +125 -0
- scalebox/api/client/models/new_team_api_key.py +59 -0
- scalebox/api/client/models/node.py +154 -0
- scalebox/api/client/models/node_detail.py +152 -0
- scalebox/api/client/models/node_status.py +11 -0
- scalebox/api/client/models/node_status_change.py +61 -0
- scalebox/api/client/models/post_sandboxes_sandbox_id_refreshes_body.py +59 -0
- scalebox/api/client/models/post_sandboxes_sandbox_id_timeout_body.py +59 -0
- scalebox/api/client/models/resumed_sandbox.py +68 -0
- scalebox/api/client/models/sandbox.py +125 -0
- scalebox/api/client/models/sandbox_detail.py +178 -0
- scalebox/api/client/models/sandbox_log.py +70 -0
- scalebox/api/client/models/sandbox_logs.py +73 -0
- scalebox/api/client/models/sandbox_metric.py +110 -0
- scalebox/api/client/models/sandbox_state.py +9 -0
- scalebox/api/client/models/sandboxes_with_metrics.py +59 -0
- scalebox/api/client/models/team.py +83 -0
- scalebox/api/client/models/team_api_key.py +158 -0
- scalebox/api/client/models/team_user.py +68 -0
- scalebox/api/client/models/template.py +179 -0
- scalebox/api/client/models/template_build.py +117 -0
- scalebox/api/client/models/template_build_file_upload.py +70 -0
- scalebox/api/client/models/template_build_request.py +115 -0
- scalebox/api/client/models/template_build_request_v2.py +88 -0
- scalebox/api/client/models/template_build_start_v2.py +114 -0
- scalebox/api/client/models/template_build_status.py +11 -0
- scalebox/api/client/models/template_step.py +91 -0
- scalebox/api/client/models/template_update_request.py +59 -0
- scalebox/api/client/models/update_team_api_key.py +59 -0
- scalebox/api/client/py.typed +1 -0
- scalebox/api/client/types.py +46 -0
- scalebox/api/metadata.py +19 -0
- scalebox/cli.py +125 -0
- scalebox/client/__init__.py +0 -0
- scalebox/client/aclient.py +57 -0
- scalebox/client/api.proto +460 -0
- scalebox/client/buf.gen.yaml +8 -0
- scalebox/client/client.py +102 -0
- scalebox/client/requirements.txt +5 -0
- scalebox/code_interpreter/__init__.py +12 -0
- scalebox/code_interpreter/charts.py +230 -0
- scalebox/code_interpreter/code_interpreter_async.py +369 -0
- scalebox/code_interpreter/code_interpreter_sync.py +317 -0
- scalebox/code_interpreter/constants.py +3 -0
- scalebox/code_interpreter/exceptions.py +13 -0
- scalebox/code_interpreter/models.py +485 -0
- scalebox/connection_config.py +92 -0
- scalebox/csx_connect/__init__.py +1 -0
- scalebox/csx_connect/client.py +485 -0
- scalebox/csx_desktop/__init__.py +0 -0
- scalebox/csx_desktop/main.py +651 -0
- scalebox/exceptions.py +83 -0
- scalebox/generated/__init__.py +0 -0
- scalebox/generated/api.py +61 -0
- scalebox/generated/api_pb2.py +203 -0
- scalebox/generated/api_pb2.pyi +956 -0
- scalebox/generated/api_pb2_connect.py +1456 -0
- scalebox/generated/rpc.py +50 -0
- scalebox/generated/versions.py +3 -0
- scalebox/requirements.txt +36 -0
- scalebox/sandbox/__init__.py +0 -0
- scalebox/sandbox/commands/__init__.py +0 -0
- scalebox/sandbox/commands/command_handle.py +69 -0
- scalebox/sandbox/commands/main.py +39 -0
- scalebox/sandbox/filesystem/__init__.py +0 -0
- scalebox/sandbox/filesystem/filesystem.py +95 -0
- scalebox/sandbox/filesystem/watch_handle.py +60 -0
- scalebox/sandbox/main.py +139 -0
- scalebox/sandbox/sandbox_api.py +91 -0
- scalebox/sandbox/signature.py +40 -0
- scalebox/sandbox/utils.py +34 -0
- scalebox/sandbox_async/__init__.py +1 -0
- scalebox/sandbox_async/commands/command.py +307 -0
- scalebox/sandbox_async/commands/command_handle.py +187 -0
- scalebox/sandbox_async/commands/pty.py +187 -0
- scalebox/sandbox_async/filesystem/filesystem.py +557 -0
- scalebox/sandbox_async/filesystem/watch_handle.py +61 -0
- scalebox/sandbox_async/main.py +646 -0
- scalebox/sandbox_async/sandbox_api.py +365 -0
- scalebox/sandbox_async/utils.py +7 -0
- scalebox/sandbox_sync/__init__.py +2 -0
- scalebox/sandbox_sync/commands/__init__.py +0 -0
- scalebox/sandbox_sync/commands/command.py +300 -0
- scalebox/sandbox_sync/commands/command_handle.py +150 -0
- scalebox/sandbox_sync/commands/pty.py +181 -0
- scalebox/sandbox_sync/filesystem/__init__.py +0 -0
- scalebox/sandbox_sync/filesystem/filesystem.py +543 -0
- scalebox/sandbox_sync/filesystem/watch_handle.py +66 -0
- scalebox/sandbox_sync/main.py +790 -0
- scalebox/sandbox_sync/sandbox_api.py +356 -0
- scalebox/test/CODE_INTERPRETER_TESTS_READY.md +323 -0
- scalebox/test/README.md +329 -0
- scalebox/test/__init__.py +0 -0
- scalebox/test/aclient.py +72 -0
- scalebox/test/code_interpreter_centext.py +21 -0
- scalebox/test/code_interpreter_centext_sync.py +21 -0
- scalebox/test/code_interpreter_test.py +34 -0
- scalebox/test/code_interpreter_test_sync.py +34 -0
- scalebox/test/run_all_validation_tests.py +334 -0
- scalebox/test/run_code_interpreter_tests.sh +67 -0
- scalebox/test/run_tests.sh +230 -0
- scalebox/test/test_basic.py +78 -0
- scalebox/test/test_code_interpreter_async_comprehensive.py +2653 -0
- scalebox/test/test_code_interpreter_e2basync_comprehensive.py +2655 -0
- scalebox/test/test_code_interpreter_e2bsync_comprehensive.py +3416 -0
- scalebox/test/test_code_interpreter_sync_comprehensive.py +3412 -0
- scalebox/test/test_e2b_first.py +11 -0
- scalebox/test/test_sandbox_async_comprehensive.py +738 -0
- scalebox/test/test_sandbox_stress_and_edge_cases.py +778 -0
- scalebox/test/test_sandbox_sync_comprehensive.py +770 -0
- scalebox/test/test_sandbox_usage_examples.py +987 -0
- scalebox/test/testacreate.py +24 -0
- scalebox/test/testagetinfo.py +18 -0
- scalebox/test/testcodeinterpreter_async.py +508 -0
- scalebox/test/testcodeinterpreter_sync.py +239 -0
- scalebox/test/testcomputeuse.py +243 -0
- scalebox/test/testnovnc.py +12 -0
- scalebox/test/testsandbox_async.py +118 -0
- scalebox/test/testsandbox_sync.py +38 -0
- scalebox/utils/__init__.py +0 -0
- scalebox/utils/httpcoreclient.py +297 -0
- scalebox/utils/httpxclient.py +403 -0
- scalebox/version.py +16 -0
- scalebox_sdk-0.1.0.dist-info/METADATA +292 -0
- scalebox_sdk-0.1.0.dist-info/RECORD +157 -0
- scalebox_sdk-0.1.0.dist-info/WHEEL +5 -0
- scalebox_sdk-0.1.0.dist-info/entry_points.txt +2 -0
- scalebox_sdk-0.1.0.dist-info/licenses/LICENSE +21 -0
- scalebox_sdk-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,646 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
import logging
|
|
3
|
+
import time
|
|
4
|
+
from typing import Dict, List, Optional, TypedDict, overload
|
|
5
|
+
|
|
6
|
+
import aiohttp
|
|
7
|
+
import httpx
|
|
8
|
+
from aiohttp import TCPConnector
|
|
9
|
+
from typing_extensions import Unpack
|
|
10
|
+
|
|
11
|
+
from ..api.client.types import Unset
|
|
12
|
+
from ..connection_config import ConnectionConfig, ProxyTypes
|
|
13
|
+
from ..exceptions import SandboxException, request_timeout_error
|
|
14
|
+
from ..generated.api import ENVD_API_HEALTH_ROUTE, ahandle_envd_api_exception
|
|
15
|
+
from ..sandbox.main import SandboxSetup
|
|
16
|
+
from ..sandbox.sandbox_api import SandboxMetrics
|
|
17
|
+
from ..sandbox.utils import class_method_variant
|
|
18
|
+
from ..sandbox_async.commands.command import Commands
|
|
19
|
+
from ..sandbox_async.commands.pty import Pty
|
|
20
|
+
from ..sandbox_async.filesystem.filesystem import Filesystem
|
|
21
|
+
from ..sandbox_async.sandbox_api import SandboxApi, SandboxInfo
|
|
22
|
+
|
|
23
|
+
logger = logging.getLogger(__name__)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class AsyncTransportWithLogger(httpx.AsyncHTTPTransport):
|
|
27
|
+
async def handle_async_request(self, request):
|
|
28
|
+
url = f"{request.url.scheme}://{request.url.host}{request.url.path}"
|
|
29
|
+
logger.info(f"Request: {request.method} {url}")
|
|
30
|
+
response = await super().handle_async_request(request)
|
|
31
|
+
|
|
32
|
+
# data = connect.GzipCompressor.decompress(response.read()).decode()
|
|
33
|
+
logger.info(f"Response: {response.status_code} {url}")
|
|
34
|
+
|
|
35
|
+
return response
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class AsyncSandboxOpts(TypedDict):
|
|
39
|
+
sandbox_id: str
|
|
40
|
+
sandbox_domain: Optional[str]
|
|
41
|
+
envd_version: Optional[str]
|
|
42
|
+
envd_access_token: Optional[str]
|
|
43
|
+
connection_config: ConnectionConfig
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class AsyncSandbox(SandboxSetup, SandboxApi):
|
|
47
|
+
"""
|
|
48
|
+
E2B cloud sandbox is a secure and isolated cloud environment.
|
|
49
|
+
|
|
50
|
+
The sandbox allows you to:
|
|
51
|
+
- Access Linux OS
|
|
52
|
+
- Create, list, and delete files and directories
|
|
53
|
+
- Run commands
|
|
54
|
+
- Run isolated code
|
|
55
|
+
- Access the internet
|
|
56
|
+
|
|
57
|
+
Check docs [here](https://dev/docs).
|
|
58
|
+
|
|
59
|
+
Use the `AsyncSandbox.create()` to create a new sandbox.
|
|
60
|
+
|
|
61
|
+
Example:
|
|
62
|
+
```python
|
|
63
|
+
from e2b import AsyncSandbox
|
|
64
|
+
|
|
65
|
+
sandbox = await AsyncSandbox.create()
|
|
66
|
+
```
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def files(self) -> Filesystem:
|
|
71
|
+
"""
|
|
72
|
+
Module for interacting with the sandbox filesystem.
|
|
73
|
+
"""
|
|
74
|
+
return self._filesystem
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def commands(self) -> Commands:
|
|
78
|
+
"""
|
|
79
|
+
Module for running commands in the sandbox.
|
|
80
|
+
"""
|
|
81
|
+
return self._commands
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def pty(self) -> Pty:
|
|
85
|
+
"""
|
|
86
|
+
Module for interacting with the sandbox pseudo-terminal.
|
|
87
|
+
"""
|
|
88
|
+
return self._pty
|
|
89
|
+
|
|
90
|
+
@property
|
|
91
|
+
def sandbox_id(self) -> str:
|
|
92
|
+
"""
|
|
93
|
+
Unique identifier of the sandbox.
|
|
94
|
+
"""
|
|
95
|
+
return self._sandbox_id
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def sandbox_domain(self) -> str:
|
|
99
|
+
"""
|
|
100
|
+
Unique identifier of the sandbox.
|
|
101
|
+
"""
|
|
102
|
+
return self._sandbox_domain
|
|
103
|
+
|
|
104
|
+
@property
|
|
105
|
+
def envd_api_url(self) -> str:
|
|
106
|
+
return self._envd_api_url
|
|
107
|
+
|
|
108
|
+
@property
|
|
109
|
+
def _envd_access_token(self) -> str:
|
|
110
|
+
"""Private property to access the envd token"""
|
|
111
|
+
return self.__envd_access_token
|
|
112
|
+
|
|
113
|
+
@_envd_access_token.setter
|
|
114
|
+
def _envd_access_token(self, value: str):
|
|
115
|
+
"""Private setter for envd token"""
|
|
116
|
+
self.__envd_access_token = value
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
def connection_config(self) -> ConnectionConfig:
|
|
120
|
+
return self._connection_config
|
|
121
|
+
|
|
122
|
+
def __init__(self, **opts: Unpack[AsyncSandboxOpts]):
|
|
123
|
+
"""
|
|
124
|
+
Use `AsyncSandbox.create()` to create a new sandbox instead.
|
|
125
|
+
"""
|
|
126
|
+
super().__init__()
|
|
127
|
+
|
|
128
|
+
self._connection_config = opts["connection_config"]
|
|
129
|
+
|
|
130
|
+
self._sandbox_id = opts["sandbox_id"]
|
|
131
|
+
self._sandbox_domain = opts["sandbox_domain"] or self.connection_config.domain
|
|
132
|
+
debug=self._connection_config.debug
|
|
133
|
+
if debug:
|
|
134
|
+
self._envd_api_url = f"http://{self.get_host(8888)}"
|
|
135
|
+
else:
|
|
136
|
+
self._envd_api_url = f"https://{self.get_host(self.envd_port)}"
|
|
137
|
+
self._envd_version = opts["envd_version"]
|
|
138
|
+
self._envd_access_token = opts["envd_access_token"]
|
|
139
|
+
# connection_headers = {"Authorization": "Bearer root", }
|
|
140
|
+
|
|
141
|
+
# self._envd_api_url =f"http://localhost:32119"
|
|
142
|
+
self._envd_version =f"v1.0"
|
|
143
|
+
# self._connection_config = ConnectionConfig(
|
|
144
|
+
# headers=connection_headers,
|
|
145
|
+
# )
|
|
146
|
+
|
|
147
|
+
self._transport = AsyncTransportWithLogger(
|
|
148
|
+
limits=self._limits, proxy=self._connection_config.proxy
|
|
149
|
+
)
|
|
150
|
+
self._envd_api = httpx.AsyncClient(
|
|
151
|
+
base_url=self.envd_api_url,
|
|
152
|
+
transport=self._transport,
|
|
153
|
+
headers=self._connection_config.headers,
|
|
154
|
+
)
|
|
155
|
+
connector = TCPConnector(
|
|
156
|
+
limit=100, # 最大连接数
|
|
157
|
+
limit_per_host=20, # 每主机最大连接数
|
|
158
|
+
keepalive_timeout=30, # 保持连接超时(秒)
|
|
159
|
+
enable_cleanup_closed=True, # 清理已关闭的连接
|
|
160
|
+
ssl=True # 是否使用 SSL
|
|
161
|
+
)
|
|
162
|
+
self._session = aiohttp.ClientSession(connector=connector,
|
|
163
|
+
timeout=aiohttp.ClientTimeout(total=None))
|
|
164
|
+
self._filesystem = Filesystem(
|
|
165
|
+
self.envd_api_url,
|
|
166
|
+
self._envd_version,
|
|
167
|
+
self.connection_config,
|
|
168
|
+
self._session,
|
|
169
|
+
self._envd_api,
|
|
170
|
+
)
|
|
171
|
+
self._commands = Commands(
|
|
172
|
+
self.envd_api_url,
|
|
173
|
+
self.connection_config,
|
|
174
|
+
self._session,
|
|
175
|
+
)
|
|
176
|
+
self._pty = Pty(
|
|
177
|
+
self.envd_api_url,
|
|
178
|
+
self.connection_config,
|
|
179
|
+
self._session,
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
async def is_running(self, request_timeout: Optional[float] = None) -> bool:
|
|
183
|
+
"""
|
|
184
|
+
Check if the sandbox is running.
|
|
185
|
+
|
|
186
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
187
|
+
|
|
188
|
+
:return: `True` if the sandbox is running, `False` otherwise
|
|
189
|
+
|
|
190
|
+
Example
|
|
191
|
+
```python
|
|
192
|
+
sandbox = await AsyncSandbox.create()
|
|
193
|
+
await sandbox.is_running() # Returns True
|
|
194
|
+
|
|
195
|
+
await sandbox.kill()
|
|
196
|
+
await sandbox.is_running() # Returns False
|
|
197
|
+
```
|
|
198
|
+
"""
|
|
199
|
+
try:
|
|
200
|
+
r = await self._envd_api.get(
|
|
201
|
+
ENVD_API_HEALTH_ROUTE,
|
|
202
|
+
timeout=self.connection_config.get_request_timeout(request_timeout),
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
if r.status_code == 502:
|
|
206
|
+
return False
|
|
207
|
+
|
|
208
|
+
err = await ahandle_envd_api_exception(r)
|
|
209
|
+
|
|
210
|
+
if err:
|
|
211
|
+
raise err
|
|
212
|
+
|
|
213
|
+
except httpx.TimeoutException:
|
|
214
|
+
raise request_timeout_error()
|
|
215
|
+
|
|
216
|
+
return True
|
|
217
|
+
|
|
218
|
+
@classmethod
|
|
219
|
+
async def create(
|
|
220
|
+
cls,
|
|
221
|
+
template: Optional[str] = None,
|
|
222
|
+
timeout: Optional[int] = None,
|
|
223
|
+
metadata: Optional[Dict[str, str]] = None,
|
|
224
|
+
envs: Optional[Dict[str, str]] = None,
|
|
225
|
+
api_key: Optional[str] = None,
|
|
226
|
+
domain: Optional[str] = None,
|
|
227
|
+
debug: Optional[bool] = None,
|
|
228
|
+
request_timeout: Optional[float] = None,
|
|
229
|
+
proxy: Optional[ProxyTypes] = None,
|
|
230
|
+
secure: Optional[bool] = None,
|
|
231
|
+
allow_internet_access: Optional[bool] = True,
|
|
232
|
+
):
|
|
233
|
+
"""
|
|
234
|
+
Create a new sandbox.
|
|
235
|
+
|
|
236
|
+
By default, the sandbox is created from the default `base` sandbox template.
|
|
237
|
+
|
|
238
|
+
:param template: Sandbox template name or ID
|
|
239
|
+
:param timeout: Timeout for the sandbox in **seconds**, default to 300 seconds. Maximum time a sandbox can be kept alive is 24 hours (86_400 seconds) for Pro users and 1 hour (3_600 seconds) for Hobby users.
|
|
240
|
+
:param metadata: Custom metadata for the sandbox
|
|
241
|
+
:param envs: Custom environment variables for the sandbox
|
|
242
|
+
:param api_key: E2B API Key to use for authentication, defaults to `E2B_API_KEY` environment variable
|
|
243
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
244
|
+
:param proxy: Proxy to use for the request and for the **requests made to the returned sandbox**
|
|
245
|
+
:param secure: Envd is secured with access token and cannot be used without it
|
|
246
|
+
:param allow_internet_access: Allow sandbox to access the internet, defaults to `True`.
|
|
247
|
+
|
|
248
|
+
:return: sandbox instance for the new sandbox
|
|
249
|
+
|
|
250
|
+
Use this method instead of using the constructor to create a new sandbox.
|
|
251
|
+
"""
|
|
252
|
+
|
|
253
|
+
connection_headers = {"Authorization": "Bearer root", }
|
|
254
|
+
if debug:
|
|
255
|
+
sandbox_id = "debug_sandbox_id"
|
|
256
|
+
sandbox_domain = None
|
|
257
|
+
envd_version = None
|
|
258
|
+
envd_access_token = None
|
|
259
|
+
else:
|
|
260
|
+
response = await SandboxApi._create_sandbox(
|
|
261
|
+
template=template or cls.default_template,
|
|
262
|
+
api_key=api_key,
|
|
263
|
+
timeout=timeout or cls.default_sandbox_timeout,
|
|
264
|
+
metadata=metadata,
|
|
265
|
+
domain=domain,
|
|
266
|
+
debug=debug,
|
|
267
|
+
request_timeout=request_timeout,
|
|
268
|
+
env_vars=envs,
|
|
269
|
+
secure=secure,
|
|
270
|
+
proxy=proxy,
|
|
271
|
+
allow_internet_access=allow_internet_access,
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
sandbox_id = response.sandbox_id
|
|
275
|
+
sandbox_domain = response.sandbox_domain
|
|
276
|
+
envd_version = response.envd_version
|
|
277
|
+
envd_access_token = response.envd_access_token
|
|
278
|
+
|
|
279
|
+
if envd_access_token is not None and not isinstance(
|
|
280
|
+
envd_access_token, Unset
|
|
281
|
+
):
|
|
282
|
+
connection_headers["X-Access-Token"] = envd_access_token
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
connection_config = ConnectionConfig(
|
|
286
|
+
api_key=api_key,
|
|
287
|
+
domain=domain,
|
|
288
|
+
debug=debug,
|
|
289
|
+
request_timeout=request_timeout,
|
|
290
|
+
headers=connection_headers,
|
|
291
|
+
proxy=proxy,
|
|
292
|
+
)
|
|
293
|
+
print("connection_config"+str(connection_config.__dict__))
|
|
294
|
+
sanbox = cls(
|
|
295
|
+
sandbox_id=sandbox_id,
|
|
296
|
+
sandbox_domain=sandbox_domain,
|
|
297
|
+
envd_version=envd_version,
|
|
298
|
+
envd_access_token=envd_access_token,
|
|
299
|
+
connection_config=connection_config,
|
|
300
|
+
)
|
|
301
|
+
timeout = 5.0
|
|
302
|
+
interval = 0.1
|
|
303
|
+
elapsed = 0.0
|
|
304
|
+
|
|
305
|
+
while elapsed <= timeout:
|
|
306
|
+
try:
|
|
307
|
+
isRunning = await sanbox.is_running(request_timeout=1)
|
|
308
|
+
if isRunning:
|
|
309
|
+
break
|
|
310
|
+
except Exception:
|
|
311
|
+
pass
|
|
312
|
+
time.sleep(interval)
|
|
313
|
+
elapsed += interval
|
|
314
|
+
else:
|
|
315
|
+
print("connect "+sandbox_domain+ENVD_API_HEALTH_ROUTE +" timeout 5s")
|
|
316
|
+
return sanbox
|
|
317
|
+
|
|
318
|
+
@classmethod
|
|
319
|
+
async def connect(
|
|
320
|
+
cls,
|
|
321
|
+
sandbox_id: str,
|
|
322
|
+
api_key: Optional[str] = None,
|
|
323
|
+
domain: Optional[str] = None,
|
|
324
|
+
debug: Optional[bool] = None,
|
|
325
|
+
proxy: Optional[ProxyTypes] = None,
|
|
326
|
+
):
|
|
327
|
+
"""
|
|
328
|
+
Connect to an existing sandbox.
|
|
329
|
+
With sandbox ID you can connect to the same sandbox from different places or environments (serverless functions, etc).
|
|
330
|
+
|
|
331
|
+
:param sandbox_id: Sandbox ID
|
|
332
|
+
:param api_key: E2B API Key to use for authentication, defaults to `E2B_API_KEY` environment variable
|
|
333
|
+
:param proxy: Proxy to use for the request and for the **requests made to the returned sandbox**
|
|
334
|
+
|
|
335
|
+
:return: sandbox instance for the existing sandbox
|
|
336
|
+
|
|
337
|
+
@example
|
|
338
|
+
```python
|
|
339
|
+
sandbox = await AsyncSandbox.create()
|
|
340
|
+
sandbox_id = sandbox.sandbox_id
|
|
341
|
+
|
|
342
|
+
# Another code block
|
|
343
|
+
same_sandbox = await AsyncSandbox.connect(sandbox_id)
|
|
344
|
+
"""
|
|
345
|
+
|
|
346
|
+
connection_headers = {"Authorization": "Bearer root"}
|
|
347
|
+
|
|
348
|
+
response = await SandboxApi._cls_get_info(
|
|
349
|
+
sandbox_id,
|
|
350
|
+
api_key=api_key,
|
|
351
|
+
domain=domain,
|
|
352
|
+
debug=debug,
|
|
353
|
+
proxy=proxy,
|
|
354
|
+
)
|
|
355
|
+
|
|
356
|
+
if response._envd_access_token is not None and not isinstance(
|
|
357
|
+
response._envd_access_token, Unset
|
|
358
|
+
):
|
|
359
|
+
connection_headers["X-Access-Token"] = response._envd_access_token
|
|
360
|
+
|
|
361
|
+
connection_config = ConnectionConfig(
|
|
362
|
+
api_key=api_key,
|
|
363
|
+
domain=domain,
|
|
364
|
+
debug=debug,
|
|
365
|
+
headers=connection_headers,
|
|
366
|
+
proxy=proxy,
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
return cls(
|
|
370
|
+
sandbox_id=sandbox_id,
|
|
371
|
+
sandbox_domain=response.sandbox_domain,
|
|
372
|
+
connection_config=connection_config,
|
|
373
|
+
envd_version=response.envd_version,
|
|
374
|
+
envd_access_token=response._envd_access_token,
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
async def __aenter__(self):
|
|
378
|
+
if self._session.closed:
|
|
379
|
+
connector = TCPConnector(
|
|
380
|
+
limit=100, # 最大连接数
|
|
381
|
+
limit_per_host=20, # 每主机最大连接数
|
|
382
|
+
keepalive_timeout=30, # 保持连接超时(秒)
|
|
383
|
+
enable_cleanup_closed=True, # 清理已关闭的连接
|
|
384
|
+
ssl=False # 是否使用 SSL
|
|
385
|
+
)
|
|
386
|
+
self._session = aiohttp.ClientSession(connector=connector,
|
|
387
|
+
timeout=aiohttp.ClientTimeout(total=None))
|
|
388
|
+
return self
|
|
389
|
+
|
|
390
|
+
async def __aexit__(self, exc_type, exc_value, traceback):
|
|
391
|
+
await self.kill()
|
|
392
|
+
await self._session.close()
|
|
393
|
+
|
|
394
|
+
@overload
|
|
395
|
+
async def kill(self, request_timeout: Optional[float] = None) -> bool:
|
|
396
|
+
"""
|
|
397
|
+
Kill the sandbox.
|
|
398
|
+
|
|
399
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
400
|
+
|
|
401
|
+
:return: `True` if the sandbox was killed, `False` if the sandbox was not found
|
|
402
|
+
"""
|
|
403
|
+
...
|
|
404
|
+
|
|
405
|
+
@overload
|
|
406
|
+
@staticmethod
|
|
407
|
+
async def kill(
|
|
408
|
+
sandbox_id: str,
|
|
409
|
+
api_key: Optional[str] = None,
|
|
410
|
+
domain: Optional[str] = None,
|
|
411
|
+
debug: Optional[bool] = None,
|
|
412
|
+
request_timeout: Optional[float] = None,
|
|
413
|
+
proxy: Optional[ProxyTypes] = None,
|
|
414
|
+
) -> bool:
|
|
415
|
+
"""
|
|
416
|
+
Kill the sandbox specified by sandbox ID.
|
|
417
|
+
|
|
418
|
+
:param sandbox_id: Sandbox ID
|
|
419
|
+
:param api_key: E2B API Key to use for authentication, defaults to `E2B_API_KEY` environment variable
|
|
420
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
421
|
+
:param proxy: Proxy to use for the request
|
|
422
|
+
|
|
423
|
+
:return: `True` if the sandbox was killed, `False` if the sandbox was not found
|
|
424
|
+
"""
|
|
425
|
+
...
|
|
426
|
+
|
|
427
|
+
@class_method_variant("_cls_kill")
|
|
428
|
+
async def kill(
|
|
429
|
+
self,
|
|
430
|
+
request_timeout: Optional[float] = None,
|
|
431
|
+
) -> bool: # type: ignore
|
|
432
|
+
config_dict = self.connection_config.__dict__
|
|
433
|
+
config_dict.pop("access_token", None)
|
|
434
|
+
config_dict.pop("api_url", None)
|
|
435
|
+
|
|
436
|
+
if request_timeout:
|
|
437
|
+
config_dict["request_timeout"] = request_timeout
|
|
438
|
+
|
|
439
|
+
await SandboxApi._cls_kill(
|
|
440
|
+
sandbox_id=self.sandbox_id,
|
|
441
|
+
**config_dict,
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
@overload
|
|
445
|
+
async def set_timeout(
|
|
446
|
+
self,
|
|
447
|
+
timeout: int,
|
|
448
|
+
request_timeout: Optional[float] = None,
|
|
449
|
+
) -> None:
|
|
450
|
+
"""
|
|
451
|
+
Set the timeout of the sandbox.
|
|
452
|
+
After the timeout expires the sandbox will be automatically killed.
|
|
453
|
+
This method can extend or reduce the sandbox timeout set when creating the sandbox or from the last call to `.set_timeout`.
|
|
454
|
+
|
|
455
|
+
Maximum time a sandbox can be kept alive is 24 hours (86_400 seconds) for Pro users and 1 hour (3_600 seconds) for Hobby users.
|
|
456
|
+
|
|
457
|
+
:param timeout: Timeout for the sandbox in **seconds**
|
|
458
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
459
|
+
"""
|
|
460
|
+
...
|
|
461
|
+
|
|
462
|
+
@overload
|
|
463
|
+
@staticmethod
|
|
464
|
+
async def set_timeout(
|
|
465
|
+
sandbox_id: str,
|
|
466
|
+
timeout: int,
|
|
467
|
+
api_key: Optional[str] = None,
|
|
468
|
+
domain: Optional[str] = None,
|
|
469
|
+
debug: Optional[bool] = None,
|
|
470
|
+
request_timeout: Optional[float] = None,
|
|
471
|
+
proxy: Optional[ProxyTypes] = None,
|
|
472
|
+
) -> None:
|
|
473
|
+
"""
|
|
474
|
+
Set the timeout of the specified sandbox.
|
|
475
|
+
After the timeout expires the sandbox will be automatically killed.
|
|
476
|
+
This method can extend or reduce the sandbox timeout set when creating the sandbox or from the last call to `.set_timeout`.
|
|
477
|
+
|
|
478
|
+
Maximum time a sandbox can be kept alive is 24 hours (86_400 seconds) for Pro users and 1 hour (3_600 seconds) for Hobby users.
|
|
479
|
+
|
|
480
|
+
:param sandbox_id: Sandbox ID
|
|
481
|
+
:param timeout: Timeout for the sandbox in **seconds**
|
|
482
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
483
|
+
:param proxy: Proxy to use for the request
|
|
484
|
+
"""
|
|
485
|
+
...
|
|
486
|
+
|
|
487
|
+
@class_method_variant("_cls_set_timeout")
|
|
488
|
+
async def set_timeout( # type: ignore
|
|
489
|
+
self,
|
|
490
|
+
timeout: int,
|
|
491
|
+
request_timeout: Optional[float] = None,
|
|
492
|
+
) -> None:
|
|
493
|
+
config_dict = self.connection_config.__dict__
|
|
494
|
+
config_dict.pop("access_token", None)
|
|
495
|
+
config_dict.pop("api_url", None)
|
|
496
|
+
|
|
497
|
+
if request_timeout:
|
|
498
|
+
config_dict["request_timeout"] = request_timeout
|
|
499
|
+
|
|
500
|
+
await SandboxApi._cls_set_timeout(
|
|
501
|
+
sandbox_id=self.sandbox_id,
|
|
502
|
+
timeout=timeout,
|
|
503
|
+
**config_dict,
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
@overload
|
|
507
|
+
async def get_info(
|
|
508
|
+
self,
|
|
509
|
+
request_timeout: Optional[float] = None,
|
|
510
|
+
) -> SandboxInfo:
|
|
511
|
+
"""
|
|
512
|
+
Get sandbox information like sandbox ID, template, metadata, started at/end at date.
|
|
513
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
514
|
+
:return: Sandbox info
|
|
515
|
+
"""
|
|
516
|
+
...
|
|
517
|
+
|
|
518
|
+
@overload
|
|
519
|
+
@staticmethod
|
|
520
|
+
async def get_info(
|
|
521
|
+
sandbox_id: str,
|
|
522
|
+
api_key: Optional[str] = None,
|
|
523
|
+
domain: Optional[str] = None,
|
|
524
|
+
debug: Optional[bool] = None,
|
|
525
|
+
request_timeout: Optional[float] = None,
|
|
526
|
+
headers: Optional[Dict[str, str]] = None,
|
|
527
|
+
proxy: Optional[ProxyTypes] = None,
|
|
528
|
+
) -> SandboxInfo:
|
|
529
|
+
"""
|
|
530
|
+
Get sandbox information like sandbox ID, template, metadata, started at/end at date.
|
|
531
|
+
:param sandbox_id: Sandbox ID
|
|
532
|
+
:param api_key: E2B API Key to use for authentication, defaults to `E2B_API_KEY` environment variable
|
|
533
|
+
:param domain: E2B domain to use for authentication, defaults to `E2B_DOMAIN` environment variable
|
|
534
|
+
:param debug: Whether to use debug mode, defaults to `E2B_DEBUG` environment variable
|
|
535
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
536
|
+
:param headers: Custom headers to use for the request
|
|
537
|
+
:param proxy: Proxy to use for the request
|
|
538
|
+
:return: Sandbox info
|
|
539
|
+
"""
|
|
540
|
+
...
|
|
541
|
+
|
|
542
|
+
@class_method_variant("_cls_get_info")
|
|
543
|
+
async def get_info( # type: ignore
|
|
544
|
+
self,
|
|
545
|
+
request_timeout: Optional[float] = None,
|
|
546
|
+
) -> SandboxInfo:
|
|
547
|
+
"""
|
|
548
|
+
Get sandbox information like sandbox ID, template, metadata, started at/end at date.
|
|
549
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
550
|
+
:return: Sandbox info
|
|
551
|
+
"""
|
|
552
|
+
|
|
553
|
+
config_dict = self.connection_config.__dict__
|
|
554
|
+
config_dict.pop("access_token", None)
|
|
555
|
+
config_dict.pop("api_url", None)
|
|
556
|
+
|
|
557
|
+
if request_timeout:
|
|
558
|
+
config_dict["request_timeout"] = request_timeout
|
|
559
|
+
|
|
560
|
+
return await SandboxApi._cls_get_info(
|
|
561
|
+
sandbox_id=self.sandbox_id,
|
|
562
|
+
**config_dict,
|
|
563
|
+
)
|
|
564
|
+
|
|
565
|
+
@overload
|
|
566
|
+
async def get_metrics( # type: ignore
|
|
567
|
+
self,
|
|
568
|
+
start: Optional[datetime.datetime] = None,
|
|
569
|
+
end: Optional[datetime.datetime] = None,
|
|
570
|
+
request_timeout: Optional[float] = None,
|
|
571
|
+
) -> List[SandboxMetrics]:
|
|
572
|
+
"""
|
|
573
|
+
Get the metrics of the current sandbox.
|
|
574
|
+
|
|
575
|
+
:param start: Start time for the metrics, defaults to the start of the sandbox
|
|
576
|
+
:param end: End time for the metrics, defaults to current time
|
|
577
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
578
|
+
|
|
579
|
+
:return: List of sandbox metrics containing CPU, memory and disk usage information
|
|
580
|
+
"""
|
|
581
|
+
...
|
|
582
|
+
|
|
583
|
+
@overload
|
|
584
|
+
@staticmethod
|
|
585
|
+
async def get_metrics(
|
|
586
|
+
sandbox_id: str,
|
|
587
|
+
start: Optional[datetime.datetime] = None,
|
|
588
|
+
end: Optional[datetime.datetime] = None,
|
|
589
|
+
api_key: Optional[str] = None,
|
|
590
|
+
domain: Optional[str] = None,
|
|
591
|
+
debug: Optional[bool] = None,
|
|
592
|
+
request_timeout: Optional[float] = None,
|
|
593
|
+
) -> List[SandboxMetrics]:
|
|
594
|
+
"""
|
|
595
|
+
Get the metrics of the sandbox specified by sandbox ID.
|
|
596
|
+
|
|
597
|
+
:param sandbox_id: Sandbox ID
|
|
598
|
+
:param start: Start time for the metrics, defaults to the start of the sandbox
|
|
599
|
+
:param end: End time for the metrics, defaults to current time
|
|
600
|
+
:param api_key: E2B API Key to use for authentication, defaults to `E2B_API_KEY` environment variable
|
|
601
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
602
|
+
|
|
603
|
+
:return: List of sandbox metrics containing CPU, memory and disk usage information
|
|
604
|
+
"""
|
|
605
|
+
...
|
|
606
|
+
|
|
607
|
+
@class_method_variant("_cls_get_metrics")
|
|
608
|
+
async def get_metrics( # type: ignore
|
|
609
|
+
self,
|
|
610
|
+
start: Optional[datetime.datetime] = None,
|
|
611
|
+
end: Optional[datetime.datetime] = None,
|
|
612
|
+
request_timeout: Optional[float] = None,
|
|
613
|
+
) -> List[SandboxMetrics]:
|
|
614
|
+
"""
|
|
615
|
+
Get the metrics of the current sandbox.
|
|
616
|
+
|
|
617
|
+
:param start: Start time for the metrics, defaults to the start of the sandbox
|
|
618
|
+
:param end: End time for the metrics, defaults to current time
|
|
619
|
+
:param request_timeout: Timeout for the request in **seconds**
|
|
620
|
+
|
|
621
|
+
:return: List of sandbox metrics containing CPU, memory and disk usage information
|
|
622
|
+
"""
|
|
623
|
+
# if self._envd_version:
|
|
624
|
+
# if Version(self._envd_version) < Version("0.1.5"):
|
|
625
|
+
# raise SandboxException(
|
|
626
|
+
# "Metrics are not supported in this version of the sandbox, please rebuild your template."
|
|
627
|
+
# )
|
|
628
|
+
#
|
|
629
|
+
# if Version(self._envd_version) < Version("0.2.4"):
|
|
630
|
+
# logger.warning(
|
|
631
|
+
# "Disk metrics are not supported in this version of the sandbox, please rebuild the template to get disk metrics."
|
|
632
|
+
# )
|
|
633
|
+
|
|
634
|
+
config_dict = self.connection_config.__dict__
|
|
635
|
+
print(str(config_dict))
|
|
636
|
+
config_dict.pop("access_token", None)
|
|
637
|
+
config_dict.pop("api_url", None)
|
|
638
|
+
if request_timeout:
|
|
639
|
+
config_dict["request_timeout"] = request_timeout
|
|
640
|
+
|
|
641
|
+
return await self._cls_get_metrics(
|
|
642
|
+
sandbox_id=self.sandbox_id,
|
|
643
|
+
start=start,
|
|
644
|
+
end=end,
|
|
645
|
+
**config_dict,
|
|
646
|
+
)
|