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

Thomas Lamprecht t.lamprecht at proxmox.com
Thu Sep 21 13:31:23 CEST 2017


Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
(cherry picked from commit 53922f63700e47eb9efb1d669ccd9eb3c2f5779b)
---
 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 435c915..1ac5666 100644
--- a/data/PVE/Cluster.pm
+++ b/data/PVE/Cluster.pm
@@ -1245,6 +1245,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;
@@ -1290,27 +1293,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