sandbox.py 4.3 KB

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