Skip to content

πŸ™Œ OpenHands: AI-Driven Development

refractor ssh login into one function (#2066)

Xingyao Wanga6b3ce866d98
diff --git a/opendevin/runtime/docker/ssh_box.py b/opendevin/runtime/docker/ssh_box.py
index 22cf81857a..02310b993b 100644
--- a/opendevin/runtime/docker/ssh_box.py
+++ b/opendevin/runtime/docker/ssh_box.py
@@ -358,28 +358,24 @@ class DockerSSHBox(Sandbox):
environment=self._env,
)
- @retry(stop=stop_after_attempt(5), wait=wait_fixed(5))
- def __create_ssh(self):
- try:
- return pxssh.pxssh(
- echo=False,
- timeout=self.timeout,
- encoding='utf-8',
- codec_errors='replace',
- )
- except pxssh.ExceptionPxssh as e:
- logger.exception(
- 'Failed to create SSH session, retrying...', exc_info=False
- )
- raise e
-
# Use the retry decorator, with a maximum of 5 attempts and a fixed wait time of 5 seconds between attempts
@retry(stop=stop_after_attempt(5), wait=wait_fixed(5))
- def __ssh_login(self, hostname, username, ssh_password, ssh_port):
+ def __ssh_login(self):
try:
- self.ssh.login(
- hostname, username, ssh_password, port=ssh_port
+ self.ssh = pxssh.pxssh(
+ echo=False,
+ timeout=self.timeout,
+ encoding='utf-8',
+ codec_errors='replace',
+ )
+ hostname = self.ssh_hostname
+ username = 'opendevin' if self.run_as_devin else 'root'
+ logger.info(
+ f'Connecting to {username}@{hostname} via ssh. '
+ f"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. "
+ f"If you started OpenDevin with `docker run`, you should try `ssh -v -p {self._ssh_port} {username}@localhost` with the password '{self._ssh_password} on the host machine (where you started the container)."
)
+ self.ssh.login(hostname, username, self._ssh_password, port=self._ssh_port)
except pxssh.ExceptionPxssh as e:
logger.exception(
'Failed to login to SSH session, retrying...', exc_info=False
@@ -387,15 +383,7 @@ class DockerSSHBox(Sandbox):
raise e
def start_ssh_session(self):
- self.ssh = self.__create_ssh()
- hostname = self.ssh_hostname
- username = 'opendevin' if self.run_as_devin else 'root'
- logger.info(
- f'Connecting to {username}@{hostname} via ssh. '
- f"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. "
- f"If you started OpenDevin with `docker run`, you should try `ssh -v -p {self._ssh_port} {username}@localhost` with the password '{self._ssh_password} on the host machine (where you started the container)."
- )
- self.__ssh_login(hostname, username, self._ssh_password, self._ssh_port)
+ self.__ssh_login()
# Fix: https://github.com/pexpect/pexpect/issues/669
self.ssh.sendline("bind 'set enable-bracketed-paste off'")
@@ -417,10 +405,10 @@ class DockerSSHBox(Sandbox):
return bg_cmd.read_logs()
def _send_interrupt(
- self,
- cmd: str,
- prev_output: str = '',
- ignore_last_output: bool = False,
+ self,
+ cmd: str,
+ prev_output: str = '',
+ ignore_last_output: bool = False,
) -> tuple[int, str]:
logger.exception('Command timed out, killing process...', exc_info=False)
# send a SIGINT to the process
@@ -435,7 +423,7 @@ class DockerSSHBox(Sandbox):
)
def execute(
- self, cmd: str, stream: bool = False, timeout: int | None = None
+ self, cmd: str, stream: bool = False, timeout: int | None = None
) -> tuple[int, str | CancellableStream]:
timeout = timeout or self.timeout
commands = split_bash_commands(cmd)