Skip to content

Commit 48ff8f7

Browse files
Dev: bootstrap init -N: do not raise an error when no ssh key is available (#1663)
1 parent 172216a commit 48ff8f7

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

crmsh/bootstrap.py

+29-26
Original file line numberDiff line numberDiff line change
@@ -873,14 +873,7 @@ def init_ssh_impl(local_user: str, ssh_public_keys: typing.List[ssh_key.Key], us
873873
user_by_host.set_no_generating_ssh_key(bool(ssh_public_keys))
874874
user_by_host.save_local()
875875
if user_node_list:
876-
print()
877-
if ssh_public_keys:
878-
for user, node in user_node_list:
879-
logger.info("Adding public keys to authorized_keys on %s@%s", user, node)
880-
for key in ssh_public_keys:
881-
authorized_key_manager.add(node, local_user, key)
882-
else:
883-
_init_ssh_on_remote_nodes(local_user, user_node_list)
876+
_init_ssh_on_remote_nodes(local_shell, local_user, user_node_list)
884877
for user, node in user_node_list:
885878
if user != 'root' and 0 != shell.subprocess_run_without_input(
886879
node, user, 'sudo true',
@@ -906,28 +899,38 @@ def init_ssh_impl(local_user: str, ssh_public_keys: typing.List[ssh_key.Key], us
906899

907900

908901
def _init_ssh_on_remote_nodes(
902+
local_shell: sh.LocalShell,
909903
local_user: str,
910904
user_node_list: typing.List[typing.Tuple[str, str]],
911905
):
912906
# Swap public ssh key between remote node and local
907+
ssh_shell = sh.SSHShell(local_shell, local_user)
908+
authorized_key_manager = ssh_key.AuthorizedKeyManager(ssh_shell)
913909
public_key_list = list()
910+
for user, node in user_node_list:
911+
logger.info("Adding public keys to authorized_keys on %s@%s", user, node)
912+
result = ssh_copy_id_no_raise(local_user, user, node, local_shell)
913+
if result.returncode != 0:
914+
utils.fatal("Failed to login to remote host {}@{}".format(user, node))
915+
elif isinstance(result.public_key, ssh_key.KeyFile):
916+
public_key = ssh_key.InMemoryPublicKey(generate_ssh_key_pair_on_remote(local_user, node, user, user))
917+
public_key_list.append(public_key)
918+
authorized_key_manager.add(node, user, public_key)
919+
authorized_key_manager.add(None, local_user, public_key)
920+
shell_script = _merge_line_into_file(
921+
'~/.ssh/authorized_keys',
922+
(key.public_key() for key in public_key_list),
923+
).encode('utf-8')
914924
for i, (remote_user, node) in enumerate(user_node_list):
915-
ssh_copy_id(local_user, remote_user, node)
916-
# After this, login to remote_node is passwordless
917-
public_key_list.append(swap_public_ssh_key(node, local_user, remote_user, local_user, remote_user))
918-
if len(user_node_list) > 1:
919-
shell = sh.LocalShell()
920-
shell_script = _merge_line_into_file('~/.ssh/authorized_keys', public_key_list).encode('utf-8')
921-
for i, (remote_user, node) in enumerate(user_node_list):
922-
result = shell.su_subprocess_run(
923-
local_user,
924-
'ssh {} {}@{} /bin/sh'.format(constants.SSH_OPTION, remote_user, node),
925-
input=shell_script,
926-
stdout=subprocess.PIPE,
927-
stderr=subprocess.STDOUT,
928-
)
929-
if result.returncode != 0:
930-
utils.fatal('Failed to add public keys to {}@{}: {}'.format(remote_user, node, result.stdout))
925+
result = local_shell.su_subprocess_run(
926+
local_user,
927+
'ssh {} {}@{} /bin/sh'.format(constants.SSH_OPTION, remote_user, node),
928+
input=shell_script,
929+
stdout=subprocess.PIPE,
930+
stderr=subprocess.STDOUT,
931+
)
932+
if result.returncode != 0:
933+
utils.fatal('Failed to add public keys to {}@{}: {}'.format(remote_user, node, result.stdout))
931934

932935

933936
def _init_ssh_for_secondary_user_on_remote_nodes(
@@ -2311,8 +2314,8 @@ def bootstrap_add(context):
23112314
options += '-i {} '.format(nic)
23122315
options = " {}".format(options.strip()) if options else ""
23132316

2314-
if context.use_ssh_agent:
2315-
options += ' --use-ssh-agent'
2317+
if not context.use_ssh_agent:
2318+
options += ' --no-use-ssh-agent'
23162319

23172320
shell = sh.ClusterShell(sh.LocalShell(), UserOfHost.instance(), _context.use_ssh_agent)
23182321
for (user, node) in (_parse_user_at_host(x, _context.current_user) for x in _context.user_at_node_list):

0 commit comments

Comments
 (0)