[pve-devel] [PATCH cluster 1/1] add Cluster: node_address and node_address_to_ssh

Wolfgang Bumiller w.bumiller at proxmox.com
Mon May 22 10:29:58 CEST 2017


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 | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/data/PVE/Cluster.pm b/data/PVE/Cluster.pm
index bb49491..188e4ca 100644
--- a/data/PVE/Cluster.pm
+++ b/data/PVE/Cluster.pm
@@ -1835,4 +1835,55 @@ sub complete_migration_target {
     return $res;
 }
 
+sub node_address {
+    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 node_address() with
+	# $network_cidr undefined.
+	# FIXME: Use the REST API client for this after creating an API entry
+	# for get_migration_ip.
+	my $default_remote = node_address($node, undef);
+	my $default_ssh = node_address_to_ssh($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 node_address_to_ssh {
+    my ($info, @extra_options) = @_;
+    return [
+	'/usr/bin/ssh',
+	'-o', 'BatchMode=yes',
+	'-o', 'HostKeyAlias='.$info->{name},
+	@extra_options,
+	"root\@$info->{ip}"
+    ];
+}
+
 1;
-- 
2.11.0





More information about the pve-devel mailing list