@@ -873,14 +873,7 @@ def init_ssh_impl(local_user: str, ssh_public_keys: typing.List[ssh_key.Key], us
873
873
user_by_host .set_no_generating_ssh_key (bool (ssh_public_keys ))
874
874
user_by_host .save_local ()
875
875
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 )
884
877
for user , node in user_node_list :
885
878
if user != 'root' and 0 != shell .subprocess_run_without_input (
886
879
node , user , 'sudo true' ,
@@ -906,28 +899,38 @@ def init_ssh_impl(local_user: str, ssh_public_keys: typing.List[ssh_key.Key], us
906
899
907
900
908
901
def _init_ssh_on_remote_nodes (
902
+ local_shell : sh .LocalShell ,
909
903
local_user : str ,
910
904
user_node_list : typing .List [typing .Tuple [str , str ]],
911
905
):
912
906
# 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 )
913
909
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' )
914
924
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 ))
931
934
932
935
933
936
def _init_ssh_for_secondary_user_on_remote_nodes (
@@ -2311,8 +2314,8 @@ def bootstrap_add(context):
2311
2314
options += '-i {} ' .format (nic )
2312
2315
options = " {}" .format (options .strip ()) if options else ""
2313
2316
2314
- if context .use_ssh_agent :
2315
- options += ' --use-ssh-agent'
2317
+ if not context .use_ssh_agent :
2318
+ options += ' --no- use-ssh-agent'
2316
2319
2317
2320
shell = sh .ClusterShell (sh .LocalShell (), UserOfHost .instance (), _context .use_ssh_agent )
2318
2321
for (user , node ) in (_parse_user_at_host (x , _context .current_user ) for x in _context .user_at_node_list ):
0 commit comments