19
19
# ------------------------------------------------------------------------------
20
20
"""Source code to run and stop deployments created."""
21
21
import json
22
+ import multiprocessing
22
23
import os
23
24
import platform
24
25
import shutil # nosec
@@ -95,12 +96,33 @@ class BaseDeploymentRunner(AbstractDeploymentRunner, metaclass=ABCMeta):
95
96
TM_CONTROL_URL = constants .TM_CONTROL_URL
96
97
SLEEP_BEFORE_TM_KILL = 2 # seconds
97
98
98
- def _run_aea (self , * args : str , cwd : Path ) -> Any :
99
+ def _run_aea_command (self , * args : str , cwd : Path ) -> Any :
99
100
"""Run aea command."""
100
- # TODO: Patch for Windows failing hash (add -s). Revert once it's fixed on OpenAutonomy / OpenAEA
101
- # The fix is also implemented in PyInstallerHostDeploymentRunner._start_agent and
102
- # on HostPythonHostDeploymentRunner._start_agent
103
- return self ._run_cmd (args = [self ._aea_bin , "-s" , * args ], cwd = cwd )
101
+ cmd = " " .join (args )
102
+ print ("Runnin aea command: " , cmd , " at " , str (cwd ))
103
+ p = multiprocessing .Process (
104
+ target = self .__class__ ._call_aea_command ,
105
+ args = (cwd , args ),
106
+ )
107
+ p .start ()
108
+ p .join ()
109
+ if p .exitcode != 0 :
110
+ raise RuntimeError (
111
+ f"aea command `{ cmd } `execution failed with exit code: { p .exitcode } "
112
+ )
113
+
114
+ @staticmethod
115
+ def _call_aea_command (cwd , args ):
116
+ try :
117
+ import os
118
+
119
+ os .chdir (cwd )
120
+ from aea .cli .core import cli as call_aea
121
+
122
+ call_aea (args , standalone_mode = False )
123
+ except :
124
+ print_exc ()
125
+ raise
104
126
105
127
@staticmethod
106
128
def _run_cmd (args : t .List [str ], cwd : t .Optional [Path ] = None ) -> None :
@@ -159,7 +181,7 @@ def _setup_agent(self) -> None:
159
181
working_dir = self ._work_directory
160
182
env = self ._prepare_agent_env ()
161
183
162
- self ._run_aea (
184
+ self ._run_aea_command (
163
185
"init" ,
164
186
"--reset" ,
165
187
"--author" ,
@@ -171,17 +193,19 @@ def _setup_agent(self) -> None:
171
193
cwd = working_dir ,
172
194
)
173
195
174
- self ._run_aea ("fetch" , env ["AEA_AGENT" ], "--alias" , "agent" , cwd = working_dir )
196
+ self ._run_aea_command (
197
+ "fetch" , env ["AEA_AGENT" ], "--alias" , "agent" , cwd = working_dir
198
+ )
175
199
176
200
# Add keys
177
201
shutil .copy (
178
202
working_dir / "ethereum_private_key.txt" ,
179
203
working_dir / "agent" / "ethereum_private_key.txt" ,
180
204
)
181
205
182
- self ._run_aea ("add-key" , "ethereum" , cwd = working_dir / "agent" )
206
+ self ._run_aea_command ("add-key" , "ethereum" , cwd = working_dir / "agent" )
183
207
184
- self ._run_aea ("issue-certificates" , cwd = working_dir / "agent" )
208
+ self ._run_aea_command ("issue-certificates" , cwd = working_dir / "agent" )
185
209
186
210
def start (self ) -> None :
187
211
"""Start the deployment."""
@@ -229,7 +253,7 @@ def _start_agent(self) -> None:
229
253
230
254
@property
231
255
@abstractmethod
232
- def _aea_bin (self ) -> str :
256
+ def _agent_runner_bin (self ) -> str :
233
257
"""Return aea_bin path."""
234
258
raise NotImplementedError
235
259
@@ -238,7 +262,7 @@ class PyInstallerHostDeploymentRunner(BaseDeploymentRunner):
238
262
"""Deployment runner within pyinstaller env."""
239
263
240
264
@property
241
- def _aea_bin (self ) -> str :
265
+ def _agent_runner_bin (self ) -> str :
242
266
"""Return aea_bin path."""
243
267
abin = str (Path (os .path .dirname (sys .executable )) / "aea_bin" ) # type: ignore # pylint: disable=protected-access
244
268
return abin
@@ -256,7 +280,11 @@ def _start_agent(self) -> None:
256
280
env ["PYTHONIOENCODING" ] = "utf8"
257
281
env = {** os .environ , ** env }
258
282
process = subprocess .Popen ( # pylint: disable=consider-using-with # nosec
259
- args = [self ._aea_bin , "-s" , "run" ], # TODO: Patch for Windows failing hash
283
+ args = [
284
+ self ._agent_runner_bin ,
285
+ "-s" ,
286
+ "run" ,
287
+ ], # TODO: Patch for Windows failing hash
260
288
cwd = working_dir / "agent" ,
261
289
stdout = subprocess .DEVNULL ,
262
290
stderr = subprocess .DEVNULL ,
@@ -313,7 +341,7 @@ class PyInstallerHostDeploymentRunnerWindows(PyInstallerHostDeploymentRunner):
313
341
"""Windows deployment runner."""
314
342
315
343
@property
316
- def _aea_bin (self ) -> str :
344
+ def _agent_runner_bin (self ) -> str :
317
345
"""Return aea_bin path."""
318
346
abin = str (Path (os .path .dirname (sys .executable )) / "aea_win.exe" ) # type: ignore # pylint: disable=protected-access
319
347
return abin
@@ -328,7 +356,7 @@ class HostPythonHostDeploymentRunner(BaseDeploymentRunner):
328
356
"""Deployment runner for host installed python."""
329
357
330
358
@property
331
- def _aea_bin (self ) -> str :
359
+ def _agent_runner_bin (self ) -> str :
332
360
"""Return aea_bin path."""
333
361
return str (self ._venv_dir / "bin" / "aea" )
334
362
@@ -340,7 +368,11 @@ def _start_agent(self) -> None:
340
368
env ["PYTHONIOENCODING" ] = "utf8"
341
369
342
370
process = subprocess .Popen ( # pylint: disable=consider-using-with # nosec
343
- args = [self ._aea_bin , "-s" , "run" ], # TODO: Patch for Windows failing hash
371
+ args = [
372
+ self ._agent_runner_bin ,
373
+ "-s" ,
374
+ "run" ,
375
+ ], # TODO: Patch for Windows failing hash
344
376
cwd = str (working_dir / "agent" ),
345
377
env = {** os .environ , ** env },
346
378
creationflags = (
@@ -413,7 +445,7 @@ def _setup_agent(self) -> None:
413
445
self ._setup_venv ()
414
446
super ()._setup_agent ()
415
447
# Install agent dependencies
416
- self ._run_aea (
448
+ self ._run_aea_command (
417
449
"-v" ,
418
450
"debug" ,
419
451
"install" ,
0 commit comments