27
27
from string import Template
28
28
from lxml import etree
29
29
30
- from . import config , constants , ssh_key , sh
30
+ from . import config , constants , ssh_key , sh , cibquery , user_of_host
31
31
from . import utils
32
32
from . import xmlutil
33
33
from .cibconfig import cib_factory
@@ -1046,7 +1046,9 @@ def ssh_copy_id_no_raise(local_user, remote_user, remote_node, shell: sh.LocalSh
1046
1046
if utils .check_ssh_passwd_need (local_user , remote_user , remote_node , shell ):
1047
1047
public_key = None
1048
1048
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 ]
1050
1052
except ssh_key .Error :
1051
1053
logger .debug ('No public key in ssh-agent.' , exc_info = True )
1052
1054
if public_key is not None :
@@ -1868,27 +1870,14 @@ def setup_passwordless_with_other_nodes(init_node, remote_user):
1868
1870
"""
1869
1871
# Fetch cluster nodes list
1870
1872
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 )
1873
1878
if rc != 0 :
1874
1879
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 ))]
1892
1881
user_by_host = utils .HostUserConfig ()
1893
1882
user_by_host .add (local_user , utils .this_node ())
1894
1883
try :
@@ -1904,22 +1893,22 @@ def setup_passwordless_with_other_nodes(init_node, remote_user):
1904
1893
rc , out , err = shell .get_rc_stdout_stderr_without_input (init_node , 'hostname' )
1905
1894
if rc != 0 :
1906
1895
utils .fatal ("Can't fetch hostname of {}: {}" .format (init_node , err ))
1896
+ init_node_hostname = out
1907
1897
# 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 )
1917
1905
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 )
1921
1910
1922
- user_by_host .save_remote (cluster_nodes_list )
1911
+ user_by_host .save_remote (cluster_node_list )
1923
1912
1924
1913
1925
1914
def swap_key_for_hacluster (other_node_list ):
0 commit comments