Skip to content

Commit b9ff708

Browse files
Dev: bootstrap join: allow to fallback to keyfile when login with ssh-agent fails (#1663)
1 parent 48ff8f7 commit b9ff708

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

crmsh/bootstrap.py

+23-34
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from string import Template
2828
from lxml import etree
2929

30-
from . import config, constants, ssh_key, sh
30+
from . import config, constants, ssh_key, sh, cibquery, user_of_host
3131
from . import utils
3232
from . import xmlutil
3333
from .cibconfig import cib_factory
@@ -1046,7 +1046,9 @@ def ssh_copy_id_no_raise(local_user, remote_user, remote_node, shell: sh.LocalSh
10461046
if utils.check_ssh_passwd_need(local_user, remote_user, remote_node, shell):
10471047
public_key = None
10481048
try:
1049-
public_key = ssh_key.AgentClient().list()[0]
1049+
public_key = ssh_key.AgentClient(
1050+
shell.additional_environ.get('SSH_AUTH_SOCK') if shell.additional_environ is not None else None
1051+
).list()[0]
10501052
except ssh_key.Error:
10511053
logger.debug('No public key in ssh-agent.', exc_info=True)
10521054
if public_key is not None:
@@ -1868,27 +1870,14 @@ def setup_passwordless_with_other_nodes(init_node, remote_user):
18681870
"""
18691871
# Fetch cluster nodes list
18701872
local_user = _context.current_user
1871-
shell = sh.cluster_shell()
1872-
rc, out, err = shell.get_rc_stdout_stderr_without_input(init_node, 'crm_node -l')
1873+
local_shell = sh.LocalShell(
1874+
additional_environ={'SSH_AUTH_SOCK': os.environ.get('SSH_AUTH_SOCK', '') if _context.use_ssh_agent else ''},
1875+
)
1876+
shell = sh.ClusterShell(local_shell, user_of_host.UserOfHost.instance(), _context.use_ssh_agent, True)
1877+
rc, out, err = shell.get_rc_stdout_stderr_without_input(init_node, constants.CIB_QUERY)
18731878
if rc != 0:
18741879
utils.fatal("Can't fetch cluster nodes list from {}: {}".format(init_node, err))
1875-
cluster_nodes_list = []
1876-
for line in out.splitlines():
1877-
# Parse line in format: <id> <nodename> <state>, and collect the
1878-
# nodename.
1879-
tokens = line.split()
1880-
if len(tokens) == 0:
1881-
pass # Skip any spurious empty line.
1882-
elif len(tokens) < 3:
1883-
logger.warning("Unable to configure passwordless ssh with nodeid {}. The "
1884-
"node has no known name and/or state information".format(
1885-
tokens[0]))
1886-
elif tokens[2] != "member":
1887-
logger.warning("Skipping configuration of passwordless ssh with node {} in "
1888-
"state '{}'. The node is not a current member".format(
1889-
tokens[1], tokens[2]))
1890-
else:
1891-
cluster_nodes_list.append(tokens[1])
1880+
cluster_node_list = [x.uname for x in cibquery.get_cluster_nodes(etree.fromstring(out))]
18921881
user_by_host = utils.HostUserConfig()
18931882
user_by_host.add(local_user, utils.this_node())
18941883
try:
@@ -1904,22 +1893,22 @@ def setup_passwordless_with_other_nodes(init_node, remote_user):
19041893
rc, out, err = shell.get_rc_stdout_stderr_without_input(init_node, 'hostname')
19051894
if rc != 0:
19061895
utils.fatal("Can't fetch hostname of {}: {}".format(init_node, err))
1896+
init_node_hostname = out
19071897
# Swap ssh public key between join node and other cluster nodes
1908-
if not _context.use_ssh_agent:
1909-
for node in (node for node in cluster_nodes_list if node != out):
1910-
remote_user_to_swap = utils.user_of(node)
1911-
remote_privileged_user = remote_user_to_swap
1912-
ssh_copy_id(local_user, remote_privileged_user, node)
1913-
swap_public_ssh_key(node, local_user, remote_user_to_swap, local_user, remote_privileged_user)
1914-
if local_user != 'hacluster':
1915-
change_user_shell('hacluster', node)
1916-
swap_public_ssh_key(node, 'hacluster', 'hacluster', local_user, remote_privileged_user)
1898+
for node in (node for node in cluster_node_list if node != init_node_hostname):
1899+
remote_user_to_swap = utils.user_of(node)
1900+
remote_privileged_user = remote_user_to_swap
1901+
result = ssh_copy_id_no_raise(local_user, remote_privileged_user, node, local_shell)
1902+
if result.returncode != 0:
1903+
utils.fatal("Failed to login to remote host {}@{}".format(remote_user_to_swap, node))
1904+
swap_public_ssh_key(node, local_user, remote_user_to_swap, local_user, remote_privileged_user)
19171905
if local_user != 'hacluster':
1918-
swap_key_for_hacluster(cluster_nodes_list)
1919-
else:
1920-
swap_key_for_hacluster(cluster_nodes_list)
1906+
change_user_shell('hacluster', node)
1907+
swap_public_ssh_key(node, 'hacluster', 'hacluster', local_user, remote_privileged_user)
1908+
if local_user != 'hacluster':
1909+
swap_key_for_hacluster(cluster_node_list)
19211910

1922-
user_by_host.save_remote(cluster_nodes_list)
1911+
user_by_host.save_remote(cluster_node_list)
19231912

19241913

19251914
def swap_key_for_hacluster(other_node_list):

0 commit comments

Comments
 (0)