[pve-devel] [RFC container 2/2] change update_etc_hosts to use ct_modify_file

Wolfgang Bumiller w.bumiller at proxmox.com
Thu Feb 25 16:26:29 CET 2016


---
 src/PVE/LXC/Setup/Base.pm                | 101 +++++++++++--------------------
 src/PVE/LXC/Setup/Redhat.pm              |   9 +--
 src/test/test-centos6-001/etc/hosts.exp  |   3 +
 src/test/test-debian-001/etc/hosts.exp   |   3 +
 src/test/test-debian-002/etc/hosts.exp   |   3 +
 src/test/test-debian-003/etc/hosts.exp   |   3 +
 src/test/test-debian-004/etc/hosts.exp   |   3 +
 src/test/test-debian-005/etc/hosts.exp   |   3 +
 src/test/test-debian-006/etc/hosts.exp   |   3 +
 src/test/test-debian-007/etc/hosts.exp   |   3 +
 src/test/test-debian-008/etc/hosts.exp   |   4 ++
 src/test/test-debian-009/etc/hosts.exp   |   3 +
 src/test/test-debian-010/etc/hosts.exp   |   3 +
 src/test/test-debian-013/etc/hosts.exp   |   3 +
 src/test/test-opensuse-001/etc/hosts.exp |   3 +
 15 files changed, 75 insertions(+), 75 deletions(-)

diff --git a/src/PVE/LXC/Setup/Base.pm b/src/PVE/LXC/Setup/Base.pm
index 502a085..dd82d4f 100644
--- a/src/PVE/LXC/Setup/Base.pm
+++ b/src/PVE/LXC/Setup/Base.pm
@@ -54,12 +54,10 @@ sub lookup_dns_conf {
 }
 
 sub update_etc_hosts {
-    my ($etc_hosts_data, $hostip, $oldname, $newname, $searchdomains) = @_;
+    my ($self, $hostip, $oldname, $newname, $searchdomains) = @_;
 
     my $done = 0;
 
-    my @lines;
-
     my $namepart = ($newname =~ s/\..*$//r);
 
     my $all_names = '';
@@ -73,67 +71,32 @@ sub update_etc_hosts {
 	$all_names .= ' ' if $all_names;
 	$all_names .= $newname;
     }
-    
-    foreach my $line (split(/\n/, $etc_hosts_data)) {
-	if ($line =~ m/^#/ || $line =~ m/^\s*$/) {
-	    push @lines, $line;
-	    next;
-	}
 
-	my ($ip, @names) = split(/\s+/, $line);
-	if (($ip eq '127.0.0.1') || ($ip eq '::1')) {
-	    push @lines, $line;
-	    next;
-	}
+    # Prepare section:
+    my $section = '';
+    my $hosts_fn = '/etc/hosts';
 
-	my $found = 0;
-	foreach my $name (@names) {
-	    if ($name eq $oldname || $name eq $newname) {
-		$found = 1;
-	    } else {
-		# fixme: record extra names?
-	    }
-	}
-	$found = 1 if defined($hostip) && ($ip eq $hostip);
-	
-	if ($found) {
-	    if (!$done) {
-		if (defined($hostip)) {
-		    push @lines, "$hostip $all_names";
-		} else {
-		    push @lines, "127.0.1.1 $namepart";
-		}
-		$done = 1;
-	    }
-	    next;
-	} else {
-	    push @lines, $line;
-	}
-    }
+    my $lo4 = "127.0.0.1 localhost.localnet localhost\n";
+    my $lo6 = "::1 localhost.localnet localhost\n";
+    if ($self->ct_file_exists($hosts_fn)) {
+	my $data = $self->ct_file_get_contents($hosts_fn);
+	# don't take localhost entries within our hosts sections into account
+	$data = remove_sections($data, 'HOSTS');
 
-    if (!$done) {
-	if (defined($hostip)) {
-	    push @lines, "$hostip $all_names";
-	} else {
-	    push @lines, "127.0.1.1 $namepart";
-	}	
+	# check for existing localhost entries
+	$section .= $lo4 if $data !~ /^\h*127\.0\.0\.1\h+/m;
+	$section .= $lo6 if $data !~ /^\h*::1\h+/m;
+    } else {
+	$section .= $lo4 . $lo6;
     }
 
-    my $found_localhost = 0;
-    foreach my $line (@lines) {
-	if ($line =~ m/^127.0.0.1\s/) {
-	    $found_localhost = 1;
-	    last;
-	}
+    if (defined($hostip)) {
+	$section .= "$hostip $all_names\n";
+    } else {
+	$section .= "127.0.1.1 $namepart\n";
     }
 
-    if (!$found_localhost) {
-	unshift @lines, "127.0.0.1 localhost.localnet localhost";
-    }
-    
-    $etc_hosts_data = join("\n", @lines) . "\n";
-    
-    return $etc_hosts_data;
+    $self->ct_modify_file($hosts_fn, 'HOSTS', $section);
 }
 
 sub template_fixup {
@@ -170,23 +133,14 @@ sub set_hostname {
     
     my $oldname = $self->ct_file_read_firstline($hostname_fn) || 'localhost';
 
-    my $hosts_fn = "/etc/hosts";
-    my $etc_hosts_data = '';
-    
-    if ($self->ct_file_exists($hosts_fn)) {
-	$etc_hosts_data =  $self->ct_file_get_contents($hosts_fn);
-    }
-
     my ($ipv4, $ipv6) = PVE::LXC::get_primary_ips($conf);
     my $hostip = $ipv4 || $ipv6;
 
     my ($searchdomains) = $self->lookup_dns_conf($conf);
 
-    $etc_hosts_data = update_etc_hosts($etc_hosts_data, $hostip, $oldname, 
-				       $hostname, $searchdomains);
+    $self->update_etc_hosts($hostip, $oldname, $hostname, $searchdomains);
     
     $self->ct_file_set_contents($hostname_fn, "$namepart\n");
-    $self->ct_file_set_contents($hosts_fn, $etc_hosts_data);
 }
 
 sub setup_network {
@@ -619,4 +573,17 @@ sub ct_modify_file {
     }
 }
 
+sub remove_sections {
+    my ($data, $header) = @_;
+
+    my $head = "# --- BEGIN PVE $header ---";
+    my $tail = "# --- END PVE $header ---";
+
+    # Remove the sections enclosed with the above headers and footers.
+    # from a line (^) starting with '\h*$head'
+    # to a line (the other ^) starting with '\h*$tail' up to including that
+    # line's end (.*?$).
+    return $data =~ s/^\h*\Q$head\E.*^\h*\Q$tail\E.*?$//rgms;
+}
+
 1;
diff --git a/src/PVE/LXC/Setup/Redhat.pm b/src/PVE/LXC/Setup/Redhat.pm
index 8bc9f16..ea4d9c0 100644
--- a/src/PVE/LXC/Setup/Redhat.pm
+++ b/src/PVE/LXC/Setup/Redhat.pm
@@ -138,18 +138,13 @@ sub set_hostname {
     }
 
     my $hosts_fn = "/etc/hosts";
-    my $etc_hosts_data = '';
-    if ($self->ct_file_exists($hosts_fn)) {
-	$etc_hosts_data =  $self->ct_file_get_contents($hosts_fn);
-    }
 
     my ($ipv4, $ipv6) = PVE::LXC::get_primary_ips($conf);
     my $hostip = $ipv4 || $ipv6;
 
     my ($searchdomains) = $self->lookup_dns_conf($conf);
 
-    $etc_hosts_data = PVE::LXC::Setup::Base::update_etc_hosts($etc_hosts_data, $hostip, $oldname,
-							    $hostname, $searchdomains);
+    $self->update_etc_hosts($hostip, $oldname, $hostname, $searchdomains);
 
     if ($self->ct_file_exists($hostname_fn)) {
 	$self->ct_file_set_contents($hostname_fn, "$hostname\n");
@@ -162,8 +157,6 @@ sub set_hostname {
 	}
 	$self->ct_file_set_contents($sysconfig_network, $data);
     }
-
-    $self->ct_file_set_contents($hosts_fn, $etc_hosts_data);
 }
 
 sub setup_network {
diff --git a/src/test/test-centos6-001/etc/hosts.exp b/src/test/test-centos6-001/etc/hosts.exp
index 0791e77..8ae3aff 100644
--- a/src/test/test-centos6-001/etc/hosts.exp
+++ b/src/test/test-centos6-001/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 test1.proxmox.com test1
+# --- END PVE HOSTS ---
diff --git a/src/test/test-debian-001/etc/hosts.exp b/src/test/test-debian-001/etc/hosts.exp
index 7f3910c..ee8bd50 100644
--- a/src/test/test-debian-001/etc/hosts.exp
+++ b/src/test/test-debian-001/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE HOSTS ---
diff --git a/src/test/test-debian-002/etc/hosts.exp b/src/test/test-debian-002/etc/hosts.exp
index 0791e77..8ae3aff 100644
--- a/src/test/test-debian-002/etc/hosts.exp
+++ b/src/test/test-debian-002/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 test1.proxmox.com test1
+# --- END PVE HOSTS ---
diff --git a/src/test/test-debian-003/etc/hosts.exp b/src/test/test-debian-003/etc/hosts.exp
index 9e3fb59..44c52fc 100644
--- a/src/test/test-debian-003/etc/hosts.exp
+++ b/src/test/test-debian-003/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test3
+# --- END PVE HOSTS ---
diff --git a/src/test/test-debian-004/etc/hosts.exp b/src/test/test-debian-004/etc/hosts.exp
index cd65bdf..299f2f9 100644
--- a/src/test/test-debian-004/etc/hosts.exp
+++ b/src/test/test-debian-004/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.2 test3.use-fqdn.com test3
+# --- END PVE HOSTS ---
diff --git a/src/test/test-debian-005/etc/hosts.exp b/src/test/test-debian-005/etc/hosts.exp
index 7f3910c..ee8bd50 100644
--- a/src/test/test-debian-005/etc/hosts.exp
+++ b/src/test/test-debian-005/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE HOSTS ---
diff --git a/src/test/test-debian-006/etc/hosts.exp b/src/test/test-debian-006/etc/hosts.exp
index 7f3910c..ee8bd50 100644
--- a/src/test/test-debian-006/etc/hosts.exp
+++ b/src/test/test-debian-006/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE HOSTS ---
diff --git a/src/test/test-debian-007/etc/hosts.exp b/src/test/test-debian-007/etc/hosts.exp
index 7f3910c..ee8bd50 100644
--- a/src/test/test-debian-007/etc/hosts.exp
+++ b/src/test/test-debian-007/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE HOSTS ---
diff --git a/src/test/test-debian-008/etc/hosts.exp b/src/test/test-debian-008/etc/hosts.exp
index 58c7bba..0226d7b 100644
--- a/src/test/test-debian-008/etc/hosts.exp
+++ b/src/test/test-debian-008/etc/hosts.exp
@@ -1,2 +1,6 @@
 127.0.0.1 localhost.localdomain localhost
+127.0.1.1 CT102
+# --- BEGIN PVE HOSTS ---
+::1 localhost.localnet localhost
 192.168.3.102 CT102.proxmox.com CT102
+# --- END PVE HOSTS ---
diff --git a/src/test/test-debian-009/etc/hosts.exp b/src/test/test-debian-009/etc/hosts.exp
index 41eed1f..9d358a3 100644
--- a/src/test/test-debian-009/etc/hosts.exp
+++ b/src/test/test-debian-009/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test9
+# --- END PVE HOSTS ---
diff --git a/src/test/test-debian-010/etc/hosts.exp b/src/test/test-debian-010/etc/hosts.exp
index 00a9546..e264557 100644
--- a/src/test/test-debian-010/etc/hosts.exp
+++ b/src/test/test-debian-010/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test10
+# --- END PVE HOSTS ---
diff --git a/src/test/test-debian-013/etc/hosts.exp b/src/test/test-debian-013/etc/hosts.exp
index bd9bcd7..5f1f324 100644
--- a/src/test/test-debian-013/etc/hosts.exp
+++ b/src/test/test-debian-013/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 localhost
+# --- END PVE HOSTS ---
diff --git a/src/test/test-opensuse-001/etc/hosts.exp b/src/test/test-opensuse-001/etc/hosts.exp
index c0113b8..b5d14ff 100644
--- a/src/test/test-opensuse-001/etc/hosts.exp
+++ b/src/test/test-opensuse-001/etc/hosts.exp
@@ -1,2 +1,5 @@
+# --- BEGIN PVE HOSTS ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 pvesuse1.proxmox.com pvesuse1
+# --- END PVE HOSTS ---
-- 
2.1.4





More information about the pve-devel mailing list