sandbox.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import os
  2. import tarfile
  3. import copy
  4. from glob import glob
  5. from e2b import Sandbox as E2BSandbox
  6. from e2b.sandbox.exception import (
  7. TimeoutException,
  8. )
  9. from opendevin.core.config import SandboxConfig
  10. from opendevin.core.logger import opendevin_logger as logger
  11. class E2BBox:
  12. closed = False
  13. _cwd: str = '/home/user'
  14. _env: dict[str, str] = {}
  15. is_initial_session: bool = True
  16. def __init__(
  17. self,
  18. config: SandboxConfig,
  19. e2b_api_key: str,
  20. template: str = 'open-devin',
  21. ):
  22. self.config = copy.deepcopy(config)
  23. self.initialize_plugins: bool = config.initialize_plugins
  24. self.sandbox = E2BSandbox(
  25. api_key=e2b_api_key,
  26. template=template,
  27. # It's possible to stream stdout and stderr from sandbox and from each process
  28. on_stderr=lambda x: logger.info(f'E2B sandbox stderr: {x}'),
  29. on_stdout=lambda x: logger.info(f'E2B sandbox stdout: {x}'),
  30. cwd=self._cwd, # Default workdir inside sandbox
  31. )
  32. logger.info(f'Started E2B sandbox with ID "{self.sandbox.id}"')
  33. @property
  34. def filesystem(self):
  35. return self.sandbox.filesystem
  36. def _archive(self, host_src: str, recursive: bool = False):
  37. if recursive:
  38. assert os.path.isdir(
  39. host_src
  40. ), 'Source must be a directory when recursive is True'
  41. files = glob(host_src + '/**/*', recursive=True)
  42. srcname = os.path.basename(host_src)
  43. tar_filename = os.path.join(os.path.dirname(host_src), srcname + '.tar')
  44. with tarfile.open(tar_filename, mode='w') as tar:
  45. for file in files:
  46. tar.add(
  47. file, arcname=os.path.relpath(file, os.path.dirname(host_src))
  48. )
  49. else:
  50. assert os.path.isfile(
  51. host_src
  52. ), 'Source must be a file when recursive is False'
  53. srcname = os.path.basename(host_src)
  54. tar_filename = os.path.join(os.path.dirname(host_src), srcname + '.tar')
  55. with tarfile.open(tar_filename, mode='w') as tar:
  56. tar.add(host_src, arcname=srcname)
  57. return tar_filename
  58. def execute(
  59. self, cmd: str, timeout: int | None = None
  60. ) -> tuple[int, str]:
  61. timeout = timeout if timeout is not None else self.config.timeout
  62. process = self.sandbox.process.start(cmd, env_vars=self._env)
  63. try:
  64. process_output = process.wait(timeout=timeout)
  65. except TimeoutException:
  66. logger.info('Command timed out, killing process...')
  67. process.kill()
  68. return -1, f'Command: "{cmd}" timed out'
  69. logs = [m.line for m in process_output.messages]
  70. logs_str = '\n'.join(logs)
  71. if process.exit_code is None:
  72. return -1, logs_str
  73. assert process_output.exit_code is not None
  74. return process_output.exit_code, logs_str
  75. def copy_to(self, host_src: str, sandbox_dest: str, recursive: bool = False):
  76. """Copies a local file or directory to the sandbox."""
  77. tar_filename = self._archive(host_src, recursive)
  78. # Prepend the sandbox destination with our sandbox cwd
  79. sandbox_dest = os.path.join(self._cwd, sandbox_dest.removeprefix('/'))
  80. with open(tar_filename, 'rb') as tar_file:
  81. # Upload the archive to /home/user (default destination that always exists)
  82. uploaded_path = self.sandbox.upload_file(tar_file)
  83. # Check if sandbox_dest exists. If not, create it.
  84. process = self.sandbox.process.start_and_wait(f'test -d {sandbox_dest}')
  85. if process.exit_code != 0:
  86. self.sandbox.filesystem.make_dir(sandbox_dest)
  87. # Extract the archive into the destination and delete the archive
  88. process = self.sandbox.process.start_and_wait(
  89. f'sudo tar -xf {uploaded_path} -C {sandbox_dest} && sudo rm {uploaded_path}'
  90. )
  91. if process.exit_code != 0:
  92. raise Exception(
  93. f'Failed to extract {uploaded_path} to {sandbox_dest}: {process.stderr}'
  94. )
  95. # Delete the local archive
  96. os.remove(tar_filename)
  97. def close(self):
  98. self.sandbox.close()
  99. def get_working_directory(self):
  100. return self.sandbox.cwd