]> git.proxmox.com Git - pve-container.git/commitdiff
change update_etc_hosts to use ct_modify_file
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 1 Mar 2016 09:50:33 +0000 (10:50 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 7 Mar 2016 08:42:21 +0000 (09:42 +0100)
15 files changed:
src/PVE/LXC/Setup/Base.pm
src/PVE/LXC/Setup/Redhat.pm
src/test/test-centos6-001/etc/hosts.exp
src/test/test-debian-001/etc/hosts.exp
src/test/test-debian-002/etc/hosts.exp
src/test/test-debian-003/etc/hosts.exp
src/test/test-debian-004/etc/hosts.exp
src/test/test-debian-005/etc/hosts.exp
src/test/test-debian-006/etc/hosts.exp
src/test/test-debian-007/etc/hosts.exp
src/test/test-debian-008/etc/hosts.exp
src/test/test-debian-009/etc/hosts.exp
src/test/test-debian-010/etc/hosts.exp
src/test/test-debian-013/etc/hosts.exp
src/test/test-opensuse-001/etc/hosts.exp

index 7e03ebc2fa6ebf621660b3cb6bf594b1a4e4b793..98f8f737cd2dc42fe466c946d9860213a005670a 100644 (file)
@@ -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_pve_sections($data);
 
-    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, $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_pve_sections {
+    my ($data) = @_;
+
+    my $head = "# --- BEGIN PVE ---";
+    my $tail = "# --- END PVE ---";
+
+    # 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;
index d04d810411aca7714add3c8c176a11c819ff1f9c..231941139e4287643082dff27581669d966e1fae 100644 (file)
@@ -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 {
index 0791e77aaaded3742e8061af7d8dd6bd211c18d7..01ac197e411febf029629edbceb8284d6cb33396 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 test1.proxmox.com test1
+# --- END PVE ---
index 7f3910cd11535e977150dc506fcc9e6b876e4dd1..c0dd46e0b6914789f0108e0a105b1812b6be96bc 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE ---
index 0791e77aaaded3742e8061af7d8dd6bd211c18d7..01ac197e411febf029629edbceb8284d6cb33396 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 test1.proxmox.com test1
+# --- END PVE ---
index 9e3fb59de81de0a88f1b2e978a267a3862827836..3f85d3f32075ed1a2ba040ffa54f622c35fa5fe2 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test3
+# --- END PVE ---
index cd65bdf0302600da1ecdfe4ca02747e11aca02a3..a7298186513e8d3c4fa64b4776ce58bd251ad1af 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.2 test3.use-fqdn.com test3
+# --- END PVE ---
index 7f3910cd11535e977150dc506fcc9e6b876e4dd1..c0dd46e0b6914789f0108e0a105b1812b6be96bc 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE ---
index 7f3910cd11535e977150dc506fcc9e6b876e4dd1..c0dd46e0b6914789f0108e0a105b1812b6be96bc 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE ---
index 7f3910cd11535e977150dc506fcc9e6b876e4dd1..c0dd46e0b6914789f0108e0a105b1812b6be96bc 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
+# --- END PVE ---
index 58c7bba828f0547c1aad28c55e1fb628102cadcd..7f540b412ab86b4b0b1b058cf673bec90b0a48cc 100644 (file)
@@ -1,2 +1,6 @@
 127.0.0.1 localhost.localdomain localhost
+127.0.1.1 CT102
+# --- BEGIN PVE ---
+::1 localhost.localnet localhost
 192.168.3.102 CT102.proxmox.com CT102
+# --- END PVE ---
index 41eed1fd19d70c31c657e7589c071be150e54ca2..ac7ee16f52f545b81e9217c7a215f282445e525d 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test9
+# --- END PVE ---
index 00a9546a4f1c5eeba0ad574b14cfdc913b91887f..e540d5a1cc7bcc64857c74b0572a547f88c56fb3 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test10
+# --- END PVE ---
index bd9bcd759c5f6375f916e977bdc1a1f4c795ad0a..1a8f0571c3ba31e48dc363692d8c2e784f392e2d 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 localhost
+# --- END PVE ---
index c0113b8a9b448f9a4d5df9e3be9f9ec97bd85f99..8299da3c044d0eddadf65f0e462d68e85e96e9f9 100644 (file)
@@ -1,2 +1,5 @@
+# --- BEGIN PVE ---
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 pvesuse1.proxmox.com pvesuse1
+# --- END PVE ---