1
- """ Remote code execution contexts
1
+ """Remote code execution contexts
2
2
3
3
Implements the interface for local docker containers, remote modal sandboxes,
4
4
and HTTP servers.
5
5
"""
6
6
7
-
8
7
from abc import ABC
9
8
import docker
10
9
import logging
15
14
from commit0 .harness .spec import Spec
16
15
from commit0 .harness .docker_build import (
17
16
close_logger ,
18
- setup_logger ,
19
17
)
20
18
from commit0 .harness .docker_utils import (
21
19
cleanup_container ,
26
24
delete_file_from_container ,
27
25
exec_run_with_timeout ,
28
26
)
29
- from commit0 .harness .utils import (
30
- EvaluationError ,
31
- extract_test_output ,
32
- get_hash_string ,
33
- get_ip ,
34
- get_user ,
35
- )
36
27
37
28
29
+ def read_stream (stream : modal .io_streams .StreamReader ) -> str :
30
+ strings = []
31
+ for line in stream :
32
+ strings .append (line )
33
+ return "\n " .join (strings )
34
+
38
35
class ExecutionContext (ABC ):
39
36
def __init__ (
40
37
self ,
@@ -44,29 +41,29 @@ def __init__(
44
41
timeout : int ,
45
42
log_dir : Path ,
46
43
):
47
- """ Create the remote execution context
44
+ """Create the remote execution context
48
45
49
46
The execution context will persist for the lifetime of this object.
50
47
The execution context can be a Docker container or Modal sandbox.
51
48
"""
52
49
raise NotImplementedError
53
50
54
- def copy_ssh_pubkey_from_remote (self ):
51
+ def copy_ssh_pubkey_from_remote (self ) -> None :
55
52
raise NotImplementedError
56
53
57
- def copy_to_remote (self , local_path , remote_path ) :
54
+ def copy_to_remote (self , local_path : Path , remote_path : Path ) -> None :
58
55
raise NotImplementedError
59
56
60
- def exec_run_with_timeout (self , command , timeout ) :
57
+ def exec_run_with_timeout (self , command : str , timeout : int ) -> None :
61
58
raise NotImplementedError
62
59
63
- def exec_run (self , command ) :
60
+ def exec_run (self , command : str ) -> None :
64
61
raise NotImplementedError
65
62
66
- def copy_from_remote (self , remote_path , local_path ):
63
+ def copy_from_remote (self , remote_path , local_path ) -> None :
67
64
raise NotImplementedError
68
65
69
- def delete_file_from_remote (self , remote_path ):
66
+ def delete_file_from_remote (self , remote_path ) -> None :
70
67
raise NotImplementedError
71
68
72
69
def __enter__ (self ):
@@ -85,10 +82,10 @@ def __init__(
85
82
timeout : int ,
86
83
log_dir : Path ,
87
84
):
88
- client = docker .from_env ()
85
+ self . client = docker .from_env ()
89
86
self .logger = logger
90
87
self .container = create_container (
91
- client = client ,
88
+ client = self . client ,
92
89
image_name = spec .repo_image_key ,
93
90
container_name = spec .get_container_name (),
94
91
logger = logger ,
@@ -103,9 +100,7 @@ def copy_to_remote(self, local_file: Path, remote_path: Path) -> None:
103
100
copy_to_container (self .container , local_file , remote_path )
104
101
105
102
def exec_run_with_timeout (self , command : str , timeout : int ) -> ():
106
- return exec_run_with_timeout (
107
- self .container , command , timeout
108
- )
103
+ return exec_run_with_timeout (self .container , command , timeout )
109
104
110
105
def exec_run (self , command : str ) -> None :
111
106
return self .container .exec_run (command , demux = True )
@@ -147,6 +142,8 @@ def __init__(
147
142
network_file_systems = {
148
143
"/vol" : self .nfs ,
149
144
},
145
+ cpu = 8.0 ,
146
+ timeout = 30 ,
150
147
)
151
148
152
149
self .copy_ssh_pubkey_from_remote ()
@@ -174,32 +171,30 @@ def copy_ssh_pubkey_from_remote(self):
174
171
authorized_keys_file .write (public_key + "\n " )
175
172
176
173
def copy_to_remote (self , local_path : Path , remote_path : Path ) -> None :
174
+ tempname = "tmpfile"
177
175
with local_path .open ("rb" ) as f :
178
- self .nfs .write_file (str ( local_path ) , f )
179
- self .sandbox .exec ("bash" , "-c" , f"cp /vol/{ str ( local_path ) } { str (remote_path )} " )
176
+ self .nfs .write_file (tempname , f )
177
+ self .sandbox .exec ("bash" , "-c" , f"cp /vol/{ tempname } { str (remote_path )} " )
180
178
181
179
def exec_run_with_timeout (self , command : str , timeout : int ) -> None :
182
180
"""Execute command on modal sandbox"""
181
+ print ("Executing:" , command )
183
182
process = self .sandbox .exec ("bash" , "-c" , command )
184
- stdout = []
185
- for line in process .stdout :
186
- stdout .append (line )
187
- stderr = []
188
- for line in process .stderr :
189
- stderr .append (line )
190
- return "\n " .join (stdout ), False , 1
191
- return "\n " .join (stdout ), "\n " .join (stderr )
183
+ print ("stdout" )
184
+ stdout = read_stream (process .stdout )
185
+ print ("stderr" )
186
+ stderr = read_stream (process .stderr )
187
+ print (stderr )
188
+ return stdout , False , 1
189
+ return stdout , stderr
192
190
193
191
def exec_run (self , command : str ) -> None :
194
192
"""Execute command on modal sandbox"""
195
193
process = self .sandbox .exec ("bash" , "-c" , command )
196
- stdout = []
197
- for line in process .stdout :
198
- stdout .append (line )
199
- stderr = []
200
- for line in process .stderr :
201
- stderr .append (line )
202
- return 1 , "\n " .join (stdout )
194
+ stdout = read_stream (process .stdout )
195
+ stderr = read_stream (process .stderr )
196
+ print (stderr )
197
+ return 1 , stdout
203
198
204
199
def copy_from_remote (self , remote_path : Path , local_path : Path ) -> None :
205
200
"""Copy file from modal sandbox"""
@@ -215,4 +210,5 @@ def __enter__(self):
215
210
return self
216
211
217
212
def __exit__ (self , exc_type , exc_value , exc_traceback ):
218
- self .nfs .__exit__ ()
213
+ # self.nfs.__exit__()
214
+ pass
0 commit comments