exec_box.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import atexit
  2. import concurrent.futures
  3. import os
  4. import sys
  5. import time
  6. import uuid
  7. from collections import namedtuple
  8. from typing import Dict, List, Tuple
  9. import docker
  10. from opendevin import config
  11. from opendevin.logger import opendevin_logger as logger
  12. from opendevin.sandbox.sandbox import Sandbox, BackgroundCommand
  13. from opendevin.schema import ConfigType
  14. from opendevin.exceptions import SandboxInvalidBackgroundCommandError
  15. InputType = namedtuple('InputType', ['content'])
  16. OutputType = namedtuple('OutputType', ['content'])
  17. CONTAINER_IMAGE = config.get(ConfigType.SANDBOX_CONTAINER_IMAGE)
  18. SANDBOX_WORKSPACE_DIR = '/workspace'
  19. # FIXME: On some containers, the devin user doesn't have enough permission, e.g. to install packages
  20. # How do we make this more flexible?
  21. RUN_AS_DEVIN = config.get('RUN_AS_DEVIN').lower() != 'false'
  22. USER_ID = 1000
  23. if SANDBOX_USER_ID := config.get('SANDBOX_USER_ID'):
  24. USER_ID = int(SANDBOX_USER_ID)
  25. elif hasattr(os, 'getuid'):
  26. USER_ID = os.getuid()
  27. class DockerExecBox(Sandbox):
  28. instance_id: str
  29. container_image: str
  30. container_name_prefix = 'opendevin-sandbox-'
  31. container_name: str
  32. container: docker.models.containers.Container
  33. docker_client: docker.DockerClient
  34. cur_background_id = 0
  35. background_commands: Dict[int, BackgroundCommand] = {}
  36. def __init__(
  37. self,
  38. container_image: str | None = None,
  39. timeout: int = 120,
  40. sid: str | None = None,
  41. ):
  42. # Initialize docker client. Throws an exception if Docker is not reachable.
  43. try:
  44. self.docker_client = docker.from_env()
  45. except Exception as ex:
  46. logger.exception(
  47. 'Please check Docker is running using `docker ps`.', exc_info=False)
  48. raise ex
  49. self.instance_id = sid if sid is not None else str(uuid.uuid4())
  50. # TODO: this timeout is actually essential - need a better way to set it
  51. # if it is too short, the container may still waiting for previous
  52. # command to finish (e.g. apt-get update)
  53. # if it is too long, the user may have to wait for a unnecessary long time
  54. self.timeout = timeout
  55. self.container_image = CONTAINER_IMAGE if container_image is None else container_image
  56. self.container_name = self.container_name_prefix + self.instance_id
  57. # always restart the container, cuz the initial be regarded as a new session
  58. self.restart_docker_container()
  59. if RUN_AS_DEVIN:
  60. self.setup_devin_user()
  61. atexit.register(self.close)
  62. def setup_devin_user(self):
  63. cmds = [
  64. f'useradd --shell /bin/bash -u {USER_ID} -o -c "" -m devin',
  65. r"echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers",
  66. 'sudo adduser devin sudo',
  67. ]
  68. for cmd in cmds:
  69. exit_code, logs = self.container.exec_run(
  70. ['/bin/bash', '-c', cmd], workdir=SANDBOX_WORKSPACE_DIR
  71. )
  72. if exit_code != 0:
  73. raise Exception(f'Failed to setup devin user: {logs}')
  74. def get_exec_cmd(self, cmd: str) -> List[str]:
  75. if RUN_AS_DEVIN:
  76. return ['su', 'devin', '-c', cmd]
  77. else:
  78. return ['/bin/bash', '-c', cmd]
  79. def read_logs(self, id) -> str:
  80. if id not in self.background_commands:
  81. raise SandboxInvalidBackgroundCommandError()
  82. bg_cmd = self.background_commands[id]
  83. return bg_cmd.read_logs()
  84. def execute(self, cmd: str) -> Tuple[int, str]:
  85. # TODO: each execute is not stateful! We need to keep track of the current working directory
  86. def run_command(container, command):
  87. return container.exec_run(command, workdir=SANDBOX_WORKSPACE_DIR)
  88. # Use ThreadPoolExecutor to control command and set timeout
  89. with concurrent.futures.ThreadPoolExecutor() as executor:
  90. future = executor.submit(
  91. run_command, self.container, self.get_exec_cmd(cmd)
  92. )
  93. try:
  94. exit_code, logs = future.result(timeout=self.timeout)
  95. except concurrent.futures.TimeoutError:
  96. logger.exception(
  97. 'Command timed out, killing process...', exc_info=False)
  98. pid = self.get_pid(cmd)
  99. if pid is not None:
  100. self.container.exec_run(
  101. f'kill -9 {pid}', workdir=SANDBOX_WORKSPACE_DIR)
  102. return -1, f'Command: "{cmd}" timed out'
  103. return exit_code, logs.decode('utf-8')
  104. def execute_in_background(self, cmd: str) -> BackgroundCommand:
  105. result = self.container.exec_run(
  106. self.get_exec_cmd(cmd), socket=True, workdir=SANDBOX_WORKSPACE_DIR
  107. )
  108. result.output._sock.setblocking(0)
  109. pid = self.get_pid(cmd)
  110. bg_cmd = BackgroundCommand(self.cur_background_id, cmd, result, pid)
  111. self.background_commands[bg_cmd.id] = bg_cmd
  112. self.cur_background_id += 1
  113. return bg_cmd
  114. def get_pid(self, cmd):
  115. exec_result = self.container.exec_run('ps aux')
  116. processes = exec_result.output.decode('utf-8').splitlines()
  117. cmd = ' '.join(self.get_exec_cmd(cmd))
  118. for process in processes:
  119. if cmd in process:
  120. pid = process.split()[1] # second column is the pid
  121. return pid
  122. return None
  123. def kill_background(self, id: int) -> BackgroundCommand:
  124. if id not in self.background_commands:
  125. raise SandboxInvalidBackgroundCommandError()
  126. bg_cmd = self.background_commands[id]
  127. if bg_cmd.pid is not None:
  128. self.container.exec_run(
  129. f'kill -9 {bg_cmd.pid}', workdir=SANDBOX_WORKSPACE_DIR)
  130. bg_cmd.result.output.close()
  131. self.background_commands.pop(id)
  132. return bg_cmd
  133. def stop_docker_container(self):
  134. try:
  135. container = self.docker_client.containers.get(self.container_name)
  136. container.stop()
  137. container.remove()
  138. elapsed = 0
  139. while container.status != 'exited':
  140. time.sleep(1)
  141. elapsed += 1
  142. if elapsed > self.timeout:
  143. break
  144. container = self.docker_client.containers.get(
  145. self.container_name)
  146. except docker.errors.NotFound:
  147. pass
  148. def is_container_running(self):
  149. try:
  150. container = self.docker_client.containers.get(self.container_name)
  151. if container.status == 'running':
  152. self.container = container
  153. return True
  154. return False
  155. except docker.errors.NotFound:
  156. return False
  157. def restart_docker_container(self):
  158. try:
  159. self.stop_docker_container()
  160. logger.info('Container stopped')
  161. except docker.errors.DockerException as e:
  162. logger.exception('Failed to stop container', exc_info=False)
  163. raise e
  164. try:
  165. # start the container
  166. mount_dir = config.get('WORKSPACE_MOUNT_PATH')
  167. self.container = self.docker_client.containers.run(
  168. self.container_image,
  169. command='tail -f /dev/null',
  170. network_mode='host',
  171. working_dir=SANDBOX_WORKSPACE_DIR,
  172. name=self.container_name,
  173. detach=True,
  174. volumes={mount_dir: {
  175. 'bind': SANDBOX_WORKSPACE_DIR, 'mode': 'rw'}},
  176. )
  177. logger.info('Container started')
  178. except Exception as ex:
  179. logger.exception('Failed to start container', exc_info=False)
  180. raise ex
  181. # wait for container to be ready
  182. elapsed = 0
  183. while self.container.status != 'running':
  184. if self.container.status == 'exited':
  185. logger.info('container exited')
  186. logger.info('container logs:')
  187. logger.info(self.container.logs())
  188. break
  189. time.sleep(1)
  190. elapsed += 1
  191. self.container = self.docker_client.containers.get(
  192. self.container_name)
  193. if elapsed > self.timeout:
  194. break
  195. if self.container.status != 'running':
  196. raise Exception('Failed to start container')
  197. # clean up the container, cannot do it in __del__ because the python interpreter is already shutting down
  198. def close(self):
  199. containers = self.docker_client.containers.list(all=True)
  200. for container in containers:
  201. try:
  202. if container.name.startswith(self.container_name_prefix):
  203. container.remove(force=True)
  204. except docker.errors.NotFound:
  205. pass
  206. if __name__ == '__main__':
  207. try:
  208. exec_box = DockerExecBox()
  209. except Exception as e:
  210. logger.exception('Failed to start Docker container: %s', e)
  211. sys.exit(1)
  212. logger.info(
  213. "Interactive Docker container started. Type 'exit' or use Ctrl+C to exit.")
  214. bg_cmd = exec_box.execute_in_background(
  215. "while true; do echo -n '.' && sleep 1; done"
  216. )
  217. sys.stdout.flush()
  218. try:
  219. while True:
  220. try:
  221. user_input = input('>>> ')
  222. except EOFError:
  223. logger.info('Exiting...')
  224. break
  225. if user_input.lower() == 'exit':
  226. logger.info('Exiting...')
  227. break
  228. if user_input.lower() == 'kill':
  229. exec_box.kill_background(bg_cmd.id)
  230. logger.info('Background process killed')
  231. continue
  232. exit_code, output = exec_box.execute(user_input)
  233. logger.info('exit code: %d', exit_code)
  234. logger.info(output)
  235. if bg_cmd.id in exec_box.background_commands:
  236. logs = exec_box.read_logs(bg_cmd.id)
  237. logger.info('background logs: %s', logs)
  238. sys.stdout.flush()
  239. except KeyboardInterrupt:
  240. logger.info('Exiting...')
  241. exec_box.close()