[pve-devel] [PATCH cluster 2/2] ssh_merge_known_hosts: refactor and simplify

Thomas Lamprecht t.lamprecht at proxmox.com
Thu Jul 6 13:19:38 CEST 2017


Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 data/PVE/Cluster.pm | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)

diff --git a/data/PVE/Cluster.pm b/data/PVE/Cluster.pm
index 2bda5d9..cfcd99a 100644
--- a/data/PVE/Cluster.pm
+++ b/data/PVE/Cluster.pm
@@ -1227,6 +1227,9 @@ sub ssh_merge_known_hosts {
     my $merge_line = sub {
 	my ($line, $all) = @_;
 
+	return if $line =~ m/^\s*$/; # skip empty lines
+	return if $line =~ m/^#/; # skip comments
+
 	if ($line =~ m/^(\S+)\s(ssh-rsa\s\S+)(\s.*)?$/) {
 	    my $key = $1;
 	    my $rsakey = $2;
@@ -1272,27 +1275,17 @@ sub ssh_merge_known_hosts {
 
     while ($old && $old =~ s/^((.*?)(\n|$))//) {
 	my $line = "$2\n";
-	next if $line =~ m/^\s*$/; # skip empty lines
-	next if $line =~ m/^#/; # skip comments
 	&$merge_line($line, 1);
     }
 
     while ($new && $new =~ s/^((.*?)(\n|$))//) {
 	my $line = "$2\n";
-	next if $line =~ m/^\s*$/; # skip empty lines
-	next if $line =~ m/^#/; # skip comments
 	&$merge_line($line);
     }
 
-    my $add_known_hosts_entry = sub {
-	my ($name, $hostkey) = @_;
-	$data .= "$name $hostkey\n";
-    };
-
-    if (!$found_nodename || !$found_local_ip) {
-	&$add_known_hosts_entry($nodename, $hostkey) if !$found_nodename;
-	&$add_known_hosts_entry($ip_address, $hostkey) if !$found_local_ip;
-    }
+    # add our own key if not already there
+    $data .= "$nodename $hostkey\n" if !$found_nodename;
+    $data .= "$ip_address $hostkey\n" if !$found_local_ip;
 
     PVE::Tools::file_set_contents($sshknownhosts, $data);
 
-- 
2.11.0





More information about the pve-devel mailing list