]> git.proxmox.com Git - pve-cluster.git/commitdiff
ssh_merge_known_hosts: refactor and simplify
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 21 Sep 2017 11:31:23 +0000 (13:31 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Thu, 21 Sep 2017 12:28:13 +0000 (14:28 +0200)
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
(cherry picked from commit 53922f63700e47eb9efb1d669ccd9eb3c2f5779b)

data/PVE/Cluster.pm

index 435c915f14f175690797b4e6a09b3d23ea0f08f2..1ac5666de623fe8a73334e048bfe8e0d0c19783c 100644 (file)
@@ -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);