12
12
# TODO: Make csync2 usage optional
13
13
# TODO: Configuration file for bootstrap?
14
14
import codecs
15
+ import io
15
16
import os
16
17
import subprocess
17
18
import sys
25
26
26
27
import yaml
27
28
import socket
28
- from tempfile import mktemp
29
29
from string import Template
30
30
from lxml import etree
31
31
32
- import crmsh .parallax
33
32
from . import config , constants
34
33
from . import utils
35
34
from . import xmlutil
36
- from .cibconfig import mkset_obj , cib_factory
35
+ from .cibconfig import cib_factory
37
36
from . import corosync
38
37
from . import tmpfiles
39
38
from . import lock
@@ -855,8 +854,8 @@ def init_ssh_impl(local_user: str, user_node_list: typing.List[typing.Tuple[str,
855
854
public_key_list .append (swap_public_ssh_key (node , local_user , remote_user , local_user , remote_user , add = True ))
856
855
hacluster_public_key_list .append (swap_public_ssh_key (node , 'hacluster' , 'hacluster' , local_user , remote_user , add = True ))
857
856
if len (user_node_list ) > 1 :
858
- shell_script = _merge_authorized_keys ( public_key_list )
859
- hacluster_shell_script = _merge_authorized_keys ( hacluster_public_key_list )
857
+ shell_script = _merge_line_into_file ( '~/.ssh/authorized_keys' , public_key_list ). encode ( 'utf-8' )
858
+ hacluster_shell_script = _merge_line_into_file ( '~/.ssh/authorized_keys' , hacluster_public_key_list ). encode ( 'utf-8' )
860
859
for i , (remote_user , node ) in enumerate (user_node_list ):
861
860
result = utils .su_subprocess_run (
862
861
local_user ,
@@ -885,16 +884,18 @@ def init_ssh_impl(local_user: str, user_node_list: typing.List[typing.Tuple[str,
885
884
change_user_shell ('hacluster' , node )
886
885
887
886
888
- def _merge_authorized_keys (keys : typing .List [str ]) -> bytes :
889
- shell_script = '''for key in "${keys[@]}"; do
890
- grep -F "$key" ~/.ssh/authorized_keys > /dev/null || sed -i "\\ $a $key" ~/.ssh/authorized_keys
891
- done'''
892
- keys_definition = ("keys+=('{}')\n " .format (key ) for key in keys )
893
- buf = bytearray ()
887
+ def _merge_line_into_file (path : str , lines : typing .Iterable [str ]) -> str :
888
+ shell_script = '''[ -e "$path" ] || echo '# created by crmsh' > "$path"
889
+ for key in "${keys[@]}"; do
890
+ grep -F "$key" "$path" > /dev/null || sed -i "\\ $a $key" "$path"
891
+ done'''
892
+ keys_definition = ("keys+=('{}')\n " .format (key ) for key in lines )
893
+ buf = io .StringIO ()
894
+ buf .write (f'path={ path } \n ' )
894
895
for item in keys_definition :
895
- buf .extend (item . encode ( 'utf-8' ) )
896
- buf .extend (shell_script . encode ( 'utf-8' ) )
897
- return buf
896
+ buf .write (item )
897
+ buf .write (shell_script )
898
+ return buf . getvalue ()
898
899
899
900
900
901
def _fetch_core_hosts (local_user , remote_user , remote_host ) -> typing .Tuple [typing .List [str ], typing .List [str ]]:
@@ -1832,7 +1833,7 @@ def join_ssh_merge(cluster_node, remote_user):
1832
1833
rc , _ , _ = utils .get_stdout_stderr_as_local_sudoer ("ssh {} {} true" .format (SSH_OPTION , utils .this_node ()))
1833
1834
assert rc == 0
1834
1835
1835
- known_hosts_new = set ()
1836
+ known_hosts_new : set [ str ] = set ()
1836
1837
1837
1838
cat_cmd = "[ -e ~/.ssh/known_hosts ] && cat ~/.ssh/known_hosts || true"
1838
1839
#logger_utils.log_only_to_file("parallax.call {} : {}".format(hosts, cat_cmd))
@@ -1842,10 +1843,9 @@ def join_ssh_merge(cluster_node, remote_user):
1842
1843
known_hosts_new .update ((utils .to_ascii (known_hosts_content ) or "" ).splitlines ())
1843
1844
1844
1845
if known_hosts_new :
1845
- hoststxt = "\n " .join (sorted (known_hosts_new ))
1846
- #results = parallax.parallax_copy(hosts, tmpf, known_hosts_path, strict=False)
1846
+ script = _merge_line_into_file ('~/.ssh/known_hosts' , known_hosts_new )
1847
1847
for host in hosts :
1848
- utils .write_remote_file ( hoststxt , "~/.ssh/known_hosts" , utils . user_of ( host ) , remote = host )
1848
+ utils .get_stdout_or_raise_error ( script , remote = host )
1849
1849
1850
1850
1851
1851
def update_expected_votes ():
0 commit comments