]> git.proxmox.com Git - pve-cluster.git/commitdiff
Cluster.pm: add get_ssh_info and ssh_info_to_command
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Mon, 22 May 2017 08:29:58 +0000 (10:29 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 22 May 2017 10:11:28 +0000 (12:11 +0200)
To get a node's address info optionally inside a specified
network (eg. migration_network), and a standardized way to
create an SSH command from this info.

data/PVE/Cluster.pm

index 1504d66931327b9e6fe427e0a3f0aa3366c4df43..7504410d710a71e5f20346ffa1aba4bd7bfd0667 100644 (file)
@@ -1836,4 +1836,55 @@ sub complete_migration_target {
     return $res;
 }
 
+sub get_ssh_info {
+    my ($node, $network_cidr) = @_;
+
+    my $ip;
+    if (defined($network_cidr)) {
+       # Use mtunnel via to get the remote node's ip inside $network_cidr.
+       # This goes over the regular network (iow. uses get_ssh_info() with
+       # $network_cidr undefined.
+       # FIXME: Use the REST API client for this after creating an API entry
+       # for get_migration_ip.
+       my $default_remote = get_ssh_info($node, undef);
+       my $default_ssh = ssh_info_to_command($default_remote);
+       my $cmd =[@$default_ssh, 'pvecm', 'mtunnel',
+           '-migration_network', $network_cidr,
+           '-get_migration_ip'
+       ];
+       PVE::Tools::run_command($cmd, outfunc => sub {
+           my ($line) = @_;
+           chomp $line;
+           die "internal error: unexpected output from mtunnel\n"
+               if defined($ip);
+           if ($line =~ /^ip: '(.*)'$/) {
+               $ip = $1;
+           } else {
+               die "internal error: bad output from mtunnel\n"
+                   if defined($ip);
+           }
+       });
+       die "failed to get ip for node '$node' in network '$network_cidr'\n"
+           if !defined($ip);
+    } else {
+       $ip = remote_node_ip($node);
+    }
+    return {
+       ip => $ip,
+       name => $node
+    };
+}
+
+sub ssh_info_to_command {
+    my ($info, @extra_options) = @_;
+    return [
+       '/usr/bin/ssh',
+       '-o', 'BatchMode=yes',
+       '-o', 'HostKeyAlias='.$info->{name},
+       @extra_options,
+       "root\@$info->{ip}"
+    ];
+}
+
 1;