sandbox.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 config
  9. from opendevin.core.logger import opendevin_logger as logger
  10. from opendevin.core.schema import CancellableStream
  11. from opendevin.runtime.e2b.process import E2BProcess
  12. from opendevin.runtime.process import Process
  13. from opendevin.runtime.sandbox import Sandbox
  14. class E2BBox(Sandbox):
  15. closed = False
  16. cur_background_id = 0
  17. background_commands: dict[int, Process] = {}
  18. _cwd: str = '/home/user'
  19. def __init__(
  20. self,
  21. template: str = 'open-devin',
  22. timeout: int = config.sandbox_timeout,
  23. ):
  24. self.sandbox = E2BSandbox(
  25. api_key=config.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. self.timeout = timeout
  33. logger.info(f'Started E2B sandbox with ID "{self.sandbox.id}"')
  34. super().__init__()
  35. @property
  36. def filesystem(self):
  37. return self.sandbox.filesystem
  38. def _archive(self, host_src: str, recursive: bool = False):
  39. if recursive:
  40. assert os.path.isdir(
  41. host_src
  42. ), 'Source must be a directory when recursive is True'
  43. files = glob(host_src + '/**/*', recursive=True)
  44. srcname = os.path.basename(host_src)
  45. tar_filename = os.path.join(os.path.dirname(host_src), srcname + '.tar')
  46. with tarfile.open(tar_filename, mode='w') as tar:
  47. for file in files:
  48. tar.add(
  49. file, arcname=os.path.relpath(file, os.path.dirname(host_src))
  50. )
  51. else:
  52. assert os.path.isfile(
  53. host_src
  54. ), 'Source must be a file when recursive is False'
  55. srcname = os.path.basename(host_src)
  56. tar_filename = os.path.join(os.path.dirname(host_src), srcname + '.tar')
  57. with tarfile.open(tar_filename, mode='w') as tar:
  58. tar.add(host_src, arcname=srcname)
  59. return tar_filename
  60. # TODO: This won't work if we didn't wait for the background process to finish
  61. def read_logs(self, process_id: int) -> str:
  62. proc = self.background_commands.get(process_id)
  63. if proc is None:
  64. raise ValueError(f'Process {process_id} not found')
  65. assert isinstance(proc, E2BProcess)
  66. return '\n'.join([m.line for m in proc.output_messages])
  67. def execute(
  68. self, cmd: str, stream: bool = False, timeout: int | None = None
  69. ) -> tuple[int, str | CancellableStream]:
  70. timeout = timeout if timeout is not None else self.timeout
  71. process = self.sandbox.process.start(cmd, env_vars=self._env)
  72. try:
  73. process_output = process.wait(timeout=timeout)
  74. except TimeoutException:
  75. logger.info('Command timed out, killing process...')
  76. process.kill()
  77. return -1, f'Command: "{cmd}" timed out'
  78. logs = [m.line for m in process_output.messages]
  79. logs_str = '\n'.join(logs)
  80. if process.exit_code is None:
  81. return -1, logs_str
  82. assert process_output.exit_code is not None
  83. return process_output.exit_code, logs_str
  84. def copy_to(self, host_src: str, sandbox_dest: str, recursive: bool = False):
  85. """Copies a local file or directory to the sandbox."""
  86. tar_filename = self._archive(host_src, recursive)
  87. # Prepend the sandbox destination with our sandbox cwd
  88. sandbox_dest = os.path.join(self._cwd, sandbox_dest.removeprefix('/'))
  89. with open(tar_filename, 'rb') as tar_file:
  90. # Upload the archive to /home/user (default destination that always exists)
  91. uploaded_path = self.sandbox.upload_file(tar_file)
  92. # Check if sandbox_dest exists. If not, create it.
  93. process = self.sandbox.process.start_and_wait(f'test -d {sandbox_dest}')
  94. if process.exit_code != 0:
  95. self.sandbox.filesystem.make_dir(sandbox_dest)
  96. # Extract the archive into the destination and delete the archive
  97. process = self.sandbox.process.start_and_wait(
  98. f'sudo tar -xf {uploaded_path} -C {sandbox_dest} && sudo rm {uploaded_path}'
  99. )
  100. if process.exit_code != 0:
  101. raise Exception(
  102. f'Failed to extract {uploaded_path} to {sandbox_dest}: {process.stderr}'
  103. )
  104. # Delete the local archive
  105. os.remove(tar_filename)
  106. def execute_in_background(self, cmd: str) -> Process:
  107. process = self.sandbox.process.start(cmd)
  108. e2b_process = E2BProcess(process, cmd)
  109. self.cur_background_id += 1
  110. self.background_commands[self.cur_background_id] = e2b_process
  111. return e2b_process
  112. def kill_background(self, process_id: int):
  113. process = self.background_commands.get(process_id)
  114. if process is None:
  115. raise ValueError(f'Process {process_id} not found')
  116. assert isinstance(process, E2BProcess)
  117. process.kill()
  118. return process
  119. def close(self):
  120. self.sandbox.close()
  121. def get_working_directory(self):
  122. return self.sandbox.cwd