]> git.proxmox.com Git - pve-cluster.git/commitdiff
ssh_merge_known_hosts: refactor and simplify
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 6 Jul 2017 11:19:38 +0000 (13:19 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 10 Jul 2017 06:51:46 +0000 (08:51 +0200)
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
data/PVE/Cluster.pm

index 2bda5d90b16ee58a5dd28e601022d6a5bdb61f48..cfcd99a40e54b514d8e7b78147626f6442db97ed 100644 (file)
@@ -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);