sandbox.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import copy
  2. import os
  3. import tarfile
  4. from glob import glob
  5. from e2b import Sandbox as E2BSandbox
  6. from e2b.sandbox.exception import (
  7. TimeoutException,
  8. )
  9. from openhands.core.config import SandboxConfig
  10. from openhands.core.logger import openhands_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 = 'openhands',
  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(self, cmd: str, timeout: int | None = None) -> tuple[int, str]:
  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