[pmg-devel] applied: [PATCH api] replace lookup_node_ip with get_ip_from_hostname from pve-common

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Feb 21 14:33:49 CET 2020


besides some irrelevant implementation details 'lookup_node_ip' is
identical to pve-common's get_ip_from_hostname, as they both rely on
'PVE::Tools::getaddrinfo_all' to get the addresses. So just reuse
the one from common instead of shipping a copy here.

The pve-common's one was recently improved by checking not only the
first IP it gets from getaddrinfo_all, but all and only complain if
none of those is a "real WAN (non-local)" IP. This will help
container installations of PMG with DHCP as their network
configuration option, as those often also have a hosts entry from
hostname to loopback addresses. Also, static setups often have both,
WAN and loopback addresses in /etc/hosts - as getaddrinfo_all gives
use them all do not just check the first.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---

With this plus the pve-common patch[0] it gets much easier to use DCHP for a
PMG container hosted on a PVE, albeit not yet fully out of the box.
But without this one had to effectively tell PVE to not touch /etc/hosts, as it
always added the "127.0.0.1 <hostname>" entry, which seems to be always the
first one to get returned by getaddrinfo_all.

 src/PMG/Cluster.pm       |  5 +++--
 src/PMG/ClusterConfig.pm |  3 ++-
 src/PMG/Utils.pm         | 26 --------------------------
 3 files changed, 5 insertions(+), 29 deletions(-)

diff --git a/src/PMG/Cluster.pm b/src/PMG/Cluster.pm
index 0486621..e890924 100644
--- a/src/PMG/Cluster.pm
+++ b/src/PMG/Cluster.pm
@@ -11,6 +11,7 @@ use PVE::SafeSyslog;
 use PVE::Tools;
 use PVE::INotify;
 use PVE::APIClient::LWP;
+use PVE::Network;
 
 use PMG::Utils;
 use PMG::Config;
@@ -35,7 +36,7 @@ sub remote_node_ip {
     }
 
     # fallback: try to get IP by other means
-    return PMG::Utils::lookup_node_ip($nodename, $noerr);
+    return PVE::Network::get_ip_from_hostname($nodename, $noerr);
 }
 
 sub get_master_node {
@@ -99,7 +100,7 @@ sub read_local_cluster_info {
 
     $res->{name} = $nodename;
 
-    $res->{ip} = PMG::Utils::lookup_node_ip($nodename);
+    $res->{ip} = PVE::Network::get_ip_from_hostname($nodename);
 
     $res->{hostrsapubkey} = $hostrsapubkey;
 
diff --git a/src/PMG/ClusterConfig.pm b/src/PMG/ClusterConfig.pm
index af58d76..166e927 100644
--- a/src/PMG/ClusterConfig.pm
+++ b/src/PMG/ClusterConfig.pm
@@ -6,6 +6,7 @@ use Data::Dumper;
 
 use PVE::Tools;
 use PVE::JSONSchema qw(get_standard_option);
+use PVE::Network;
 use PVE::SectionConfig;
 
 use base qw(PVE::SectionConfig);
@@ -172,7 +173,7 @@ sub read_cluster_conf {
     my $cinfo = PMG::ClusterConfig::Base->parse_config($filename, $raw);
 
     my $localname = PVE::INotify::nodename();
-    my $localip = PMG::Utils::lookup_node_ip($localname);
+    my $localip = PVE::Network::get_ip_from_hostname($localname);
 
     $cinfo->{remnodes} = [];
 
diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index 5aad482..d0654e1 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -790,32 +790,6 @@ sub service_cmd {
     PVE::Tools::run_command(['systemctl', $cmd, $service]);
 };
 
-# this is also used to get the IP of the local node
-sub lookup_node_ip {
-    my ($nodename, $noerr) = @_;
-
-    my ($family, $packed_ip);
-
-    eval {
-	my @res = PVE::Tools::getaddrinfo_all($nodename);
-	$family = $res[0]->{family};
-	$packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2];
-    };
-
-    if ($@) {
-	die "hostname lookup failed:\n$@" if !$noerr;
-	return undef;
-    }
-
-    my $ip = Socket::inet_ntop($family, $packed_ip);
-    if ($ip =~ m/^127\.|^::1$/) {
-	die "hostname lookup failed - got local IP address ($nodename = $ip)\n" if !$noerr;
-	return undef;
-    }
-
-    return wantarray ? ($ip, $family) : $ip;
-}
-
 sub run_postmap {
     my ($filename) = @_;
 
-- 
2.20.1




More information about the pmg-devel mailing list