]> git.proxmox.com Git - pve-cluster.git/blob - data/PVE/SSHInfo.pm
fix #4234: vzdump: add cluster-wide configuration
[pve-cluster.git] / data / PVE / SSHInfo.pm
1 package PVE::SSHInfo;
2
3 use strict;
4 use warnings;
5
6 use PVE::Cluster;
7 use PVE::Tools;
8
9 sub get_ssh_info {
10 my ($node, $network_cidr) = @_;
11
12 my $ip;
13 if (defined($network_cidr)) {
14 # Use mtunnel via to get the remote node's ip inside $network_cidr.
15 # This goes over the regular network (iow. uses get_ssh_info() with
16 # $network_cidr undefined.
17 # FIXME: Use the REST API client for this after creating an API entry
18 # for get_migration_ip.
19 my $default_remote = get_ssh_info($node, undef);
20 my $default_ssh = ssh_info_to_command($default_remote);
21 my $cmd =[@$default_ssh, 'pvecm', 'mtunnel',
22 '-migration_network', $network_cidr,
23 '-get_migration_ip'
24 ];
25 PVE::Tools::run_command($cmd, outfunc => sub {
26 my ($line) = @_;
27 chomp $line;
28 die "internal error: unexpected output from mtunnel\n"
29 if defined($ip);
30 if ($line =~ /^ip: '(.*)'$/) {
31 $ip = $1;
32 } else {
33 die "internal error: bad output from mtunnel\n"
34 if defined($ip);
35 }
36 });
37 die "failed to get ip for node '$node' in network '$network_cidr'\n"
38 if !defined($ip);
39 } else {
40 $ip = PVE::Cluster::remote_node_ip($node);
41 }
42
43 return {
44 ip => $ip,
45 name => $node,
46 network => $network_cidr,
47 };
48 }
49
50 sub ssh_info_to_command_base {
51 my ($info, @extra_options) = @_;
52 return [
53 '/usr/bin/ssh',
54 '-e', 'none',
55 '-o', 'BatchMode=yes',
56 '-o', 'HostKeyAlias='.$info->{name},
57 @extra_options
58 ];
59 }
60
61 sub ssh_info_to_command {
62 my ($info, @extra_options) = @_;
63 my $cmd = ssh_info_to_command_base($info, @extra_options);
64 push @$cmd, "root\@$info->{ip}";
65 return $cmd;
66 }
67
68 1;