exec_box.py 12 KB

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