Skip to content

Commit

Permalink
fixup: detect ssh availability when joining
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasyang2022 committed Oct 23, 2023
1 parent d0e0866 commit c05d44e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
9 changes: 8 additions & 1 deletion crmsh/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,14 @@ def join_ssh_with_ssh_agent(
):
# As ssh-agent is used, the local_user does not have any effects
shell = sh.SSHShell(local_shell, 'root')
# FIXME: detect the availability of ssh session
if not shell.can_run_as(seed_host, seed_user):
raise ValueError(f'Failed to login to {seed_user}@{seed_host}')
if seed_user != 'root' and 0 != shell.subprocess_run_without_input(
seed_host, seed_user, 'sudo true',
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
).returncode:
raise ValueError(f'Failed to sudo on {seed_user}@{seed_host}')
authorized_key_manager = ssh_key.AuthorizedKeyManager(shell)
for key in ssh_public_keys:
authorized_key_manager.add(None, local_user, key)
Expand Down
15 changes: 7 additions & 8 deletions crmsh/sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,13 @@ def __init__(self, local_shell: LocalShell, local_user):
self.local_user = local_user

def can_run_as(self, host: typing.Optional[str], user: str) -> bool:
# FIXME: this method seems to return a different result from subprocess_run_without_input
if host is None or host == self.local_shell.hostname():
return self.local_shell.can_run_as(user)
else:
ssh_options = "-o StrictHostKeyChecking=no -o EscapeChar=none -o ConnectTimeout=15"
ssh_cmd = "ssh {} -T -o Batchmode=yes {}@{} true".format(ssh_options, user, host)
rc, output = self.local_shell.get_rc_and_error(self.local_user, ssh_cmd)
return rc == 0
result = self.subprocess_run_without_input(
host, user, 'true',
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
start_new_session=True,
)
return 0 == result.returncode

def get_rc_and_error(
self,
Expand Down

0 comments on commit c05d44e

Please sign in to comment.