-
Notifications
You must be signed in to change notification settings - Fork 860
Expand file tree
/
Copy pathcommand.py
More file actions
336 lines (303 loc) · 11.8 KB
/
command.py
File metadata and controls
336 lines (303 loc) · 11.8 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
from typing import Callable, Dict, List, Literal, Optional, Union, overload
import e2b_connect
import httpcore
from packaging.version import Version
from e2b.connection_config import (
ConnectionConfig,
Username,
KEEPALIVE_PING_HEADER,
KEEPALIVE_PING_INTERVAL_SEC,
)
from e2b.envd.process import process_connect, process_pb2
from e2b.envd.rpc import authentication_header, handle_rpc_exception
from e2b.envd.versions import ENVD_COMMANDS_STDIN
from e2b.exceptions import SandboxException
from e2b.sandbox.commands.main import ProcessInfo
from e2b.sandbox.commands.command_handle import CommandResult
from e2b.sandbox_sync.commands.command_handle import CommandHandle
class Commands:
"""
Module for executing commands in the sandbox.
"""
def __init__(
self,
envd_api_url: str,
connection_config: ConnectionConfig,
pool: httpcore.ConnectionPool,
envd_version: Version,
) -> None:
self._connection_config = connection_config
self._envd_version = envd_version
self._rpc = process_connect.ProcessClient(
envd_api_url,
# TODO: Fix and enable compression again — the headers compression is not solved for streaming.
# compressor=e2b_connect.GzipCompressor,
pool=pool,
json=True,
headers=connection_config.sandbox_headers,
)
def list(
self,
request_timeout: Optional[float] = None,
) -> List[ProcessInfo]:
"""
Lists all running commands and PTY sessions.
:param request_timeout: Timeout for the request in **seconds**
:return: List of running commands and PTY sessions
"""
try:
res = self._rpc.list(
process_pb2.ListRequest(),
request_timeout=self._connection_config.get_request_timeout(
request_timeout
),
)
return [
ProcessInfo(
pid=p.pid,
tag=p.tag,
cmd=p.config.cmd,
args=list(p.config.args),
envs=dict(p.config.envs),
cwd=p.config.cwd,
)
for p in res.processes
]
except Exception as e:
raise handle_rpc_exception(e)
def kill(
self,
pid: int,
request_timeout: Optional[float] = None,
) -> bool:
"""
Kills a running command specified by its process ID.
It uses `SIGKILL` signal to kill the command.
:param pid: Process ID of the command. You can get the list of processes using `sandbox.commands.list()`
:param request_timeout: Timeout for the request in **seconds**
:return: `True` if the command was killed, `False` if the command was not found
"""
try:
self._rpc.send_signal(
process_pb2.SendSignalRequest(
process=process_pb2.ProcessSelector(pid=pid),
signal=process_pb2.Signal.SIGNAL_SIGKILL,
),
request_timeout=self._connection_config.get_request_timeout(
request_timeout
),
)
return True
except Exception as e:
if isinstance(e, e2b_connect.ConnectException):
if e.status == e2b_connect.Code.not_found:
return False
raise handle_rpc_exception(e)
def send_stdin(
self,
pid: int,
data: str,
request_timeout: Optional[float] = None,
):
"""
Send data to command stdin.
:param pid Process ID of the command. You can get the list of processes using `sandbox.commands.list()`.
:param data: Data to send to the command
:param request_timeout: Timeout for the request in **seconds**
"""
try:
self._rpc.send_input(
process_pb2.SendInputRequest(
process=process_pb2.ProcessSelector(pid=pid),
input=process_pb2.ProcessInput(
stdin=data.encode(),
),
),
request_timeout=self._connection_config.get_request_timeout(
request_timeout
),
)
except Exception as e:
raise handle_rpc_exception(e)
@overload
def run(
self,
cmd: str,
background: Union[Literal[False], None] = None,
envs: Optional[Dict[str, str]] = None,
user: Optional[Username] = None,
cwd: Optional[str] = None,
on_stdout: Optional[Callable[[str], None]] = None,
on_stderr: Optional[Callable[[str], None]] = None,
stdin: Optional[bool] = None,
timeout: Optional[float] = None,
request_timeout: Optional[float] = None,
) -> CommandResult:
"""
Start a new command and wait until it finishes executing.
:param cmd: Command to execute
:param background: **`False` if the command should be executed in the foreground**, `True` if the command should be executed in the background
:param envs: Environment variables used for the command
:param user: User to run the command as
:param cwd: Working directory to run the command
:param on_stdout: Callback for command stdout output
:param on_stderr: Callback for command stderr output
:param stdin: If `True`, the command will have a stdin stream that you can send data to using `sandbox.commands.send_stdin()`
:param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time. Default is `60` seconds for foreground commands, `0` (no limit) for background commands
:param request_timeout: Timeout for the request in **seconds**
:return: `CommandResult` result of the command execution
"""
...
@overload
def run(
self,
cmd: str,
background: Literal[True],
envs: Optional[Dict[str, str]] = None,
user: Optional[Username] = None,
cwd: Optional[str] = None,
on_stdout: None = None,
on_stderr: None = None,
stdin: Optional[bool] = None,
timeout: Optional[float] = None,
request_timeout: Optional[float] = None,
) -> CommandHandle:
"""
Start a new command and return a handle to interact with it.
:param cmd: Command to execute
:param background: `False` if the command should be executed in the foreground, **`True` if the command should be executed in the background**
:param envs: Environment variables used for the command
:param user: User to run the command as
:param cwd: Working directory to run the command
:param stdin: If `True`, the command will have a stdin stream that you can send data to using `sandbox.commands.send_stdin()`
:param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time. Default is `0` (no limit) for background commands
:param request_timeout: Timeout for the request in **seconds**
:return: `CommandHandle` handle to interact with the running command
"""
...
def run(
self,
cmd: str,
background: Union[bool, None] = None,
envs: Optional[Dict[str, str]] = None,
user: Optional[Username] = None,
cwd: Optional[str] = None,
on_stdout: Optional[Callable[[str], None]] = None,
on_stderr: Optional[Callable[[str], None]] = None,
stdin: Optional[bool] = None,
timeout: Optional[float] = None,
request_timeout: Optional[float] = None,
):
# Check version for stdin support
if stdin is False and self._envd_version < ENVD_COMMANDS_STDIN:
raise SandboxException(
f"Sandbox envd version {self._envd_version} can't specify stdin, it's always turned on. "
f"Please rebuild your template if you need this feature."
)
# Default to `False`
stdin = stdin or False
# When timeout is not explicitly provided, default to 60s for foreground
# commands, or 0 (unlimited) for background commands so the process
# remains reachable for connect(pid)
if timeout is None:
effective_timeout = 0 if background else 60
else:
effective_timeout = timeout
proc = self._start(
cmd,
envs,
user,
cwd,
stdin,
effective_timeout,
request_timeout,
)
return (
proc
if background
else proc.wait(
on_stdout=on_stdout,
on_stderr=on_stderr,
)
)
def _start(
self,
cmd: str,
envs: Optional[Dict[str, str]],
user: Optional[Username],
cwd: Optional[str],
stdin: bool,
timeout: Optional[float],
request_timeout: Optional[float],
):
events = self._rpc.start(
process_pb2.StartRequest(
process=process_pb2.ProcessConfig(
cmd="/bin/bash",
envs=envs,
args=["-l", "-c", cmd],
cwd=cwd,
),
stdin=stdin,
),
headers={
**authentication_header(self._envd_version, user),
KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
},
timeout=timeout,
request_timeout=self._connection_config.get_request_timeout(
request_timeout
),
)
try:
start_event = events.__next__()
if not start_event.HasField("event"):
raise SandboxException(
f"Failed to start process: expected start event, got {start_event}"
)
return CommandHandle(
pid=start_event.event.start.pid,
handle_kill=lambda: self.kill(start_event.event.start.pid),
events=events,
)
except Exception as e:
raise handle_rpc_exception(e)
def connect(
self,
pid: int,
timeout: Optional[float] = 60,
request_timeout: Optional[float] = None,
):
"""
Connects to a running command.
You can use `CommandHandle.wait()` to wait for the command to finish and get execution results.
:param pid: Process ID of the command to connect to. You can get the list of processes using `sandbox.commands.list()`
:param timeout: Timeout for the connection in **seconds**. Using `0` will not limit the connection time
:param request_timeout: Timeout for the request in **seconds**
:return: `CommandHandle` handle to interact with the running command
"""
events = self._rpc.connect(
process_pb2.ConnectRequest(
process=process_pb2.ProcessSelector(pid=pid),
),
headers={
KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
},
timeout=timeout,
request_timeout=self._connection_config.get_request_timeout(
request_timeout
),
)
try:
start_event = events.__next__()
if not start_event.HasField("event"):
raise SandboxException(
f"Failed to connect to process: expected start event, got {start_event}"
)
return CommandHandle(
pid=start_event.event.start.pid,
handle_kill=lambda: self.kill(start_event.event.start.pid),
events=events,
)
except Exception as e:
raise handle_rpc_exception(e)