ssh_box.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import atexit
  2. import os
  3. import platform
  4. import sys
  5. import time
  6. import uuid
  7. from collections import namedtuple
  8. from typing import Dict, List, Tuple, Union
  9. import docker
  10. from pexpect import pxssh
  11. from opendevin import config
  12. from opendevin.logger import opendevin_logger as logger
  13. from opendevin.sandbox.sandbox import Sandbox, BackgroundCommand
  14. from opendevin.schema import ConfigType
  15. from opendevin.utils import find_available_tcp_port
  16. from opendevin.exceptions import SandboxInvalidBackgroundCommandError
  17. InputType = namedtuple('InputType', ['content'])
  18. OutputType = namedtuple('OutputType', ['content'])
  19. SANDBOX_WORKSPACE_DIR = '/workspace'
  20. CONTAINER_IMAGE = config.get(ConfigType.SANDBOX_CONTAINER_IMAGE)
  21. SSH_HOSTNAME = config.get(ConfigType.SSH_HOSTNAME)
  22. USE_HOST_NETWORK = platform.system() == 'Linux'
  23. if config.get(ConfigType.USE_HOST_NETWORK) is not None:
  24. USE_HOST_NETWORK = config.get(
  25. ConfigType.USE_HOST_NETWORK).lower() != 'false'
  26. # FIXME: On some containers, the devin user doesn't have enough permission, e.g. to install packages
  27. # How do we make this more flexible?
  28. RUN_AS_DEVIN = config.get('RUN_AS_DEVIN').lower() != 'false'
  29. USER_ID = 1000
  30. if SANDBOX_USER_ID := config.get('SANDBOX_USER_ID'):
  31. USER_ID = int(SANDBOX_USER_ID)
  32. elif hasattr(os, 'getuid'):
  33. USER_ID = os.getuid()
  34. class DockerSSHBox(Sandbox):
  35. instance_id: str
  36. container_image: str
  37. container_name_prefix = 'opendevin-sandbox-'
  38. container_name: str
  39. container: docker.models.containers.Container
  40. docker_client: docker.DockerClient
  41. _ssh_password: str
  42. _ssh_port: int
  43. cur_background_id = 0
  44. background_commands: Dict[int, BackgroundCommand] = {}
  45. def __init__(
  46. self,
  47. container_image: str | None = None,
  48. timeout: int = 120,
  49. sid: str | None = None,
  50. ):
  51. # Initialize docker client. Throws an exception if Docker is not reachable.
  52. try:
  53. self.docker_client = docker.from_env()
  54. except Exception as ex:
  55. logger.exception(
  56. 'Please check Docker is running using `docker ps`.', exc_info=False)
  57. raise ex
  58. self.instance_id = sid if sid is not None else str(uuid.uuid4())
  59. # TODO: this timeout is actually essential - need a better way to set it
  60. # if it is too short, the container may still waiting for previous
  61. # command to finish (e.g. apt-get update)
  62. # if it is too long, the user may have to wait for a unnecessary long time
  63. self.timeout = timeout
  64. self.container_image = CONTAINER_IMAGE if container_image is None else container_image
  65. self.container_name = self.container_name_prefix + self.instance_id
  66. # set up random user password
  67. self._ssh_password = str(uuid.uuid4())
  68. self._ssh_port = find_available_tcp_port()
  69. # always restart the container, cuz the initial be regarded as a new session
  70. self.restart_docker_container()
  71. self.setup_user()
  72. self.start_ssh_session()
  73. atexit.register(self.close)
  74. def setup_user(self):
  75. # Make users sudoers passwordless
  76. # TODO(sandbox): add this line in the Dockerfile for next minor version of docker image
  77. exit_code, logs = self.container.exec_run(
  78. ['/bin/bash', '-c',
  79. r"echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers"],
  80. workdir=SANDBOX_WORKSPACE_DIR,
  81. )
  82. if exit_code != 0:
  83. raise Exception(
  84. f'Failed to make all users passwordless sudoers in sandbox: {logs}')
  85. # Check if the opendevin user exists
  86. exit_code, logs = self.container.exec_run(
  87. ['/bin/bash', '-c', 'id -u opendevin'],
  88. workdir=SANDBOX_WORKSPACE_DIR,
  89. )
  90. if exit_code == 0:
  91. # User exists, delete it
  92. exit_code, logs = self.container.exec_run(
  93. ['/bin/bash', '-c', 'userdel -r opendevin'],
  94. workdir=SANDBOX_WORKSPACE_DIR,
  95. )
  96. if exit_code != 0:
  97. raise Exception(
  98. f'Failed to remove opendevin user in sandbox: {logs}')
  99. if RUN_AS_DEVIN:
  100. # Create the opendevin user
  101. exit_code, logs = self.container.exec_run(
  102. ['/bin/bash', '-c',
  103. f'useradd -rm -d /home/opendevin -s /bin/bash -g root -G sudo -u {USER_ID} opendevin'],
  104. workdir=SANDBOX_WORKSPACE_DIR,
  105. )
  106. if exit_code != 0:
  107. raise Exception(
  108. f'Failed to create opendevin user in sandbox: {logs}')
  109. exit_code, logs = self.container.exec_run(
  110. ['/bin/bash', '-c',
  111. f"echo 'opendevin:{self._ssh_password}' | chpasswd"],
  112. workdir=SANDBOX_WORKSPACE_DIR,
  113. )
  114. if exit_code != 0:
  115. raise Exception(f'Failed to set password in sandbox: {logs}')
  116. else:
  117. exit_code, logs = self.container.exec_run(
  118. # change password for root
  119. ['/bin/bash', '-c',
  120. f"echo 'root:{self._ssh_password}' | chpasswd"],
  121. workdir=SANDBOX_WORKSPACE_DIR,
  122. )
  123. if exit_code != 0:
  124. raise Exception(
  125. f'Failed to set password for root in sandbox: {logs}')
  126. exit_code, logs = self.container.exec_run(
  127. ['/bin/bash', '-c', "echo 'opendevin-sandbox' > /etc/hostname"],
  128. workdir=SANDBOX_WORKSPACE_DIR,
  129. )
  130. def start_ssh_session(self):
  131. # start ssh session at the background
  132. self.ssh = pxssh.pxssh()
  133. hostname = SSH_HOSTNAME
  134. if RUN_AS_DEVIN:
  135. username = 'opendevin'
  136. else:
  137. username = 'root'
  138. logger.info(
  139. f"Connecting to {username}@{hostname} via ssh. If you encounter any issues, you can try `ssh -v -p {self._ssh_port} {username}@{hostname}` with the password '{self._ssh_password}' and report the issue on GitHub."
  140. )
  141. self.ssh.login(hostname, username, self._ssh_password,
  142. port=self._ssh_port)
  143. # Fix: https://github.com/pexpect/pexpect/issues/669
  144. self.ssh.sendline("bind 'set enable-bracketed-paste off'")
  145. self.ssh.prompt()
  146. # cd to workspace
  147. self.ssh.sendline('cd /workspace')
  148. self.ssh.prompt()
  149. def get_exec_cmd(self, cmd: str) -> List[str]:
  150. if RUN_AS_DEVIN:
  151. return ['su', 'opendevin', '-c', cmd]
  152. else:
  153. return ['/bin/bash', '-c', cmd]
  154. def read_logs(self, id) -> str:
  155. if id not in self.background_commands:
  156. raise SandboxInvalidBackgroundCommandError()
  157. bg_cmd = self.background_commands[id]
  158. return bg_cmd.read_logs()
  159. def execute(self, cmd: str) -> Tuple[int, str]:
  160. # use self.ssh
  161. self.ssh.sendline(cmd)
  162. success = self.ssh.prompt(timeout=self.timeout)
  163. if not success:
  164. logger.exception(
  165. 'Command timed out, killing process...', exc_info=False)
  166. # send a SIGINT to the process
  167. self.ssh.sendintr()
  168. self.ssh.prompt()
  169. command_output = self.ssh.before.decode(
  170. 'utf-8').lstrip(cmd).strip()
  171. return -1, f'Command: "{cmd}" timed out. Sending SIGINT to the process: {command_output}'
  172. command_output = self.ssh.before.decode('utf-8').lstrip(cmd).strip()
  173. # get the exit code
  174. self.ssh.sendline('echo $?')
  175. self.ssh.prompt()
  176. exit_code = self.ssh.before.decode('utf-8')
  177. # remove the echo $? itself
  178. exit_code = int(exit_code.lstrip('echo $?').strip())
  179. return exit_code, command_output
  180. def execute_in_background(self, cmd: str) -> BackgroundCommand:
  181. result = self.container.exec_run(
  182. self.get_exec_cmd(cmd), socket=True, workdir=SANDBOX_WORKSPACE_DIR
  183. )
  184. result.output._sock.setblocking(0)
  185. pid = self.get_pid(cmd)
  186. bg_cmd = BackgroundCommand(self.cur_background_id, cmd, result, pid)
  187. self.background_commands[bg_cmd.id] = bg_cmd
  188. self.cur_background_id += 1
  189. return bg_cmd
  190. def get_pid(self, cmd):
  191. exec_result = self.container.exec_run('ps aux')
  192. processes = exec_result.output.decode('utf-8').splitlines()
  193. cmd = ' '.join(self.get_exec_cmd(cmd))
  194. for process in processes:
  195. if cmd in process:
  196. pid = process.split()[1] # second column is the pid
  197. return pid
  198. return None
  199. def kill_background(self, id: int) -> BackgroundCommand:
  200. if id not in self.background_commands:
  201. raise SandboxInvalidBackgroundCommandError()
  202. bg_cmd = self.background_commands[id]
  203. if bg_cmd.pid is not None:
  204. self.container.exec_run(
  205. f'kill -9 {bg_cmd.pid}', workdir=SANDBOX_WORKSPACE_DIR)
  206. bg_cmd.result.output.close()
  207. self.background_commands.pop(id)
  208. return bg_cmd
  209. def stop_docker_container(self):
  210. try:
  211. container = self.docker_client.containers.get(self.container_name)
  212. container.stop()
  213. container.remove()
  214. elapsed = 0
  215. while container.status != 'exited':
  216. time.sleep(1)
  217. elapsed += 1
  218. if elapsed > self.timeout:
  219. break
  220. container = self.docker_client.containers.get(
  221. self.container_name)
  222. except docker.errors.NotFound:
  223. pass
  224. def is_container_running(self):
  225. try:
  226. container = self.docker_client.containers.get(self.container_name)
  227. if container.status == 'running':
  228. self.container = container
  229. return True
  230. return False
  231. except docker.errors.NotFound:
  232. return False
  233. def restart_docker_container(self):
  234. try:
  235. self.stop_docker_container()
  236. logger.info('Container stopped')
  237. except docker.errors.DockerException as ex:
  238. logger.exception('Failed to stop container', exc_info=False)
  239. raise ex
  240. try:
  241. network_kwargs: Dict[str, Union[str, Dict[str, int]]] = {}
  242. if USE_HOST_NETWORK:
  243. network_kwargs['network_mode'] = 'host'
  244. else:
  245. # FIXME: This is a temporary workaround for Mac OS
  246. network_kwargs['ports'] = {'2222/tcp': self._ssh_port}
  247. logger.warning(
  248. ('Using port forwarding for Mac OS. '
  249. 'Server started by OpenDevin will not be accessible from the host machine at the moment. '
  250. 'See https://github.com/OpenDevin/OpenDevin/issues/897 for more information.'
  251. )
  252. )
  253. mount_dir = config.get('WORKSPACE_MOUNT_PATH')
  254. print('Mounting workspace directory: ', mount_dir)
  255. # start the container
  256. self.container = self.docker_client.containers.run(
  257. self.container_image,
  258. # allow root login
  259. command="/usr/sbin/sshd -D -p 2222 -o 'PermitRootLogin=yes'",
  260. **network_kwargs,
  261. working_dir=SANDBOX_WORKSPACE_DIR,
  262. name=self.container_name,
  263. hostname='opendevin_sandbox',
  264. detach=True,
  265. volumes={
  266. mount_dir: {
  267. 'bind': SANDBOX_WORKSPACE_DIR,
  268. 'mode': 'rw'
  269. },
  270. },
  271. )
  272. logger.info('Container started')
  273. except Exception as ex:
  274. logger.exception('Failed to start container', exc_info=False)
  275. raise ex
  276. # wait for container to be ready
  277. elapsed = 0
  278. while self.container.status != 'running':
  279. if self.container.status == 'exited':
  280. logger.info('container exited')
  281. logger.info('container logs:')
  282. logger.info(self.container.logs())
  283. break
  284. time.sleep(1)
  285. elapsed += 1
  286. self.container = self.docker_client.containers.get(
  287. self.container_name)
  288. logger.info(
  289. f'waiting for container to start: {elapsed}, container status: {self.container.status}')
  290. if elapsed > self.timeout:
  291. break
  292. if self.container.status != 'running':
  293. raise Exception('Failed to start container')
  294. # clean up the container, cannot do it in __del__ because the python interpreter is already shutting down
  295. def close(self):
  296. containers = self.docker_client.containers.list(all=True)
  297. for container in containers:
  298. try:
  299. if container.name.startswith(self.container_name_prefix):
  300. container.remove(force=True)
  301. except docker.errors.NotFound:
  302. pass
  303. if __name__ == '__main__':
  304. try:
  305. ssh_box = DockerSSHBox()
  306. except Exception as e:
  307. logger.exception('Failed to start Docker container: %s', e)
  308. sys.exit(1)
  309. logger.info(
  310. "Interactive Docker container started. Type 'exit' or use Ctrl+C to exit.")
  311. bg_cmd = ssh_box.execute_in_background(
  312. "while true; do echo 'dot ' && sleep 1; done"
  313. )
  314. sys.stdout.flush()
  315. try:
  316. while True:
  317. try:
  318. user_input = input('>>> ')
  319. except EOFError:
  320. logger.info('Exiting...')
  321. break
  322. if user_input.lower() == 'exit':
  323. logger.info('Exiting...')
  324. break
  325. if user_input.lower() == 'kill':
  326. ssh_box.kill_background(bg_cmd.id)
  327. logger.info('Background process killed')
  328. continue
  329. exit_code, output = ssh_box.execute(user_input)
  330. logger.info('exit code: %d', exit_code)
  331. logger.info(output)
  332. if bg_cmd.id in ssh_box.background_commands:
  333. logs = ssh_box.read_logs(bg_cmd.id)
  334. logger.info('background logs: %s', logs)
  335. sys.stdout.flush()
  336. except KeyboardInterrupt:
  337. logger.info('Exiting...')
  338. ssh_box.close()