Skip to content

Commit

Permalink
Dev: bootstrap: Reuse AuthorizedKeyManager to add key to authorized_keys
Browse files Browse the repository at this point in the history
And remove unused code.
  • Loading branch information
liangxin1300 committed Nov 4, 2024
1 parent a93fd6c commit 1249b0b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 43 deletions.
21 changes: 3 additions & 18 deletions crmsh/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,18 +953,6 @@ def _fetch_core_hosts(shell: sh.ClusterShell, remote_host) -> typing.Tuple[typin
return user_list, host_list


def key_files(user):
"""
Find home directory for user and return key files with abspath
"""
keyfile_dict = {}
home_dir = userdir.gethomedir(user)
keyfile_dict['private'] = "{}/.ssh/id_rsa".format(home_dir)
keyfile_dict['public'] = "{}/.ssh/id_rsa.pub".format(home_dir)
keyfile_dict['authorized'] = "{}/.ssh/authorized_keys".format(home_dir)
return keyfile_dict


def is_nologin(user, remote=None):
"""
Check if user's shell is nologin
Expand Down Expand Up @@ -1077,12 +1065,9 @@ def export_ssh_key_non_interactive(local_user_to_export, remote_user_to_swap, re
def import_ssh_key(local_user, remote_user, local_sudoer, remote_node, remote_sudoer):
"Copy ssh key from remote to local authorized_keys"
remote_key_content = ssh_key.fetch_public_key_list(remote_node, remote_user, with_content=True)[0]
_, _, local_authorized_file = key_files(local_user).values()
if not utils.check_text_included(remote_key_content, local_authorized_file, remote=None):
sh.LocalShell().get_stdout_or_raise_error(
local_user,
"sed -i '$a {}' '{}'".format(remote_key_content, local_authorized_file),
)
in_memory_key = ssh_key.InMemoryPublicKey(remote_key_content)
shell = sh.SSHShell(sh.LocalShell(), local_user)
ssh_key.AuthorizedKeyManager(shell).add(None, local_user, in_memory_key)


def init_csync2():
Expand Down
25 changes: 0 additions & 25 deletions crmsh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2445,16 +2445,6 @@ def get_default_nic_from_route(self) -> str:
return res.group(1) if res else self.nic_list[0]


def check_text_included(text, target_file, remote=None):
"Check whether target_file includes the text"
if not detect_file(target_file, remote=remote):
return False

cmd = "cat {}".format(target_file)
target_data = sh.cluster_shell().get_stdout_or_raise_error(cmd, remote)
return text in target_data


def package_is_installed(pkg, remote_addr=None):
"""
Check if package is installed
Expand Down Expand Up @@ -2933,21 +2923,6 @@ def diff_and_patch(orig_cib_str, current_cib_str):
return True


def detect_file(_file, remote=None):
"""
Detect if file exists, support both local and remote
"""
rc = False
if not remote:
cmd = "test -f {}".format(_file)
else:
# FIXME
cmd = "ssh {} {}@{} 'test -f {}'".format(SSH_OPTION, user_of(remote), remote, _file)
code, _, _ = ShellUtils().get_stdout_stderr(cmd)
rc = code == 0
return rc


def retry_with_timeout(callable, timeout_sec: float, interval_sec=1):
"""Try callable repeatedly until it returns without raising an exception.
Expand Down

0 comments on commit 1249b0b

Please sign in to comment.