Index: /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Buildnet.pm
===================================================================
--- /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Buildnet.pm	(revision 2058)
+++ /trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Buildnet.pm	(revision 2059)
@@ -74,5 +74,4 @@
     }
 
-    my %headers;
     foreach my $zone ($self->_base->search_objects('netzone', 'exported=*')) {
         my $ozone = $self->_base->get_object('netzone', $zone)
@@ -83,13 +82,10 @@
             next;
         };
-        my $header = $self->_pre_zone($ozone) or next;
-        $headers{$zone} = $header;
-    }
-    $self->_base->commit;
-
-    foreach (keys %headers) {
-        $self->gen_zone($_, $headers{$_}) or return;
-    }
-    $self->_base->rollback;
+        $self->gen_zone($zone) or do {
+            $self->_base->rollback;
+            return;
+        };
+        $self->_base->commit;
+    }
 
     if (my $cmd = $self->_la->val('_network_', 'post')) {
@@ -220,8 +216,8 @@
 
 
-    if ($ozone->get_attributes('type') !~ /^(dhcp|radius)$/ && $ozone->get_attributes('templateD')) {
+    if ($ozone->get_attributes('templateD')) {
         my $template = $self->_template_file($ozone);
         my $output = $self->_output_file($ozone);
-        my @tstat = stat($template);
+        my @tstat = stat($template) or return;
         my @ostat = stat($output);
         if (($ostat[9] || 0) <= ($tstat[9] || 0)) {
@@ -250,8 +246,11 @@
 
     my $textzone = $self->_comment_zone($ozone);
-    if ($ozone->get_attributes('type') =~ /^(dns|reverse)$/) {
-        my $tzone = $self->_read_template($ozone) or return;
+    my $tzone = $self->_read_template($ozone);
+    if (defined($tzone)) {
         $textzone .= $tzone;
-    }
+    } else {
+        return;
+    }
+
     $self->_set_last_build($ozone);
 
@@ -265,8 +264,6 @@
 =cut
 
-# TODO what is $header
-
 sub gen_zone {
-    my ($self, $zone, $header) = @_;
+    my ($self, $zone) = @_;
 
     my $ozone = $self->_base->get_object('netzone', $zone)
@@ -276,5 +273,5 @@
         $ozone->get_attributes('type'));
 
-    $header ||= $self->_pre_zone($ozone) or return;
+    my $header = $self->_pre_zone($ozone);
 
     my $type = $ozone->get_attributes('type');
@@ -287,11 +284,38 @@
         undef;
 
-    if ($res) {
-
-        if (my $cmd = $self->_la->val('_network_', 'post_file',
-                $self->_la->val('_network_', 'post_zone'))) {
-            exec_command(
-                $cmd,
-                {
+    if (!defined($res)) {
+    }
+
+    my $textzone =
+        $header .
+        "\n" .
+        $self->_comment($ozone, "Comming from database:\n") .
+        $res .
+        $self->_comment($ozone, "End of data from database\n");
+
+
+    if ($type =~ /^(dns|reverse)$/) {
+        if (!$self->_checkzone_output($ozone, $textzone)) {
+            la_log(LA_ERR, "Output of DNS zone %s not ok, not updating this zone",
+                $ozone->id);
+            return;
+        }
+    }
+
+    if (open(my $handle, '>', $self->_output_file($ozone))) {
+        print $handle $textzone;
+        close($handle);
+        la_log(LA_INFO, "zone %s written into %s", $ozone->id,
+            $self->_output_file($ozone));
+    } else {
+       la_log(LA_ERR, "Can't open output file for zone %s", $ozone->id);
+       return;
+    }
+
+    if (my $cmd = $self->_la->val('_network_', 'post_file',
+            $self->_la->val('_network_', 'post_zone'))) {
+        exec_command(
+            $cmd,
+            {
                 TEMPLATE_DIR => $self->_la->val('_network_', 'template_dir', ''),
                 OUTPUT_DIR => $self->_la->val('_network_', 'output_dir', ''),
@@ -300,22 +324,19 @@
                 OUTPUT_FILE => $ozone->get_attributes('outputD'),
                 HOOK_TYPE => 'POSTFILE',
-                },
-            );
-        }
-
-        $self->_bnet_state->newval($ozone->id, 'dbrev',
-            $ozone->get_attributes('rev'));
-        la_log LA_DEBUG, "Zone rev build point is %d for %s",
-            $ozone->get_attributes('rev'),
-            $ozone->id;
-        $self->_bnet_state->SetParameterComment(
-            $ozone->id, 'dbrev',
-            scalar(localtime));
-        $self->_write_state_file;
-
-    } else {
-        $self->_base->rollback;
-    }
-    $res
+            },
+        );
+    }
+
+    $self->_bnet_state->newval($ozone->id, 'dbrev',
+        $ozone->get_attributes('rev'));
+    la_log LA_DEBUG, "Zone rev build point is %d for %s",
+    $ozone->get_attributes('rev'),
+    $ozone->id;
+    $self->_bnet_state->SetParameterComment(
+        $ozone->id, 'dbrev',
+        scalar(localtime));
+    $self->_write_state_file;
+
+    1;
 }
 
@@ -349,4 +370,16 @@
     }
     $res
+}
+
+sub _comment {
+    my ($self, $ozone, $message, @args) = @_;
+    my $com_prefix =
+        $ozone->get_attributes('type') =~ /^(dhcp|puppet|radius)$/ ? '#' : ';';
+
+    if ($message) {
+        return(sprintf("$com_prefix $message", @args));
+    } else {
+        $com_prefix
+    }
 }
 
@@ -398,10 +431,12 @@
 }
 
+
+# return undef on fatal error, depending zone type
 sub _read_template {
     my ($self, $ozone) = @_;
 
     my $revision = $self->get_zone_rev($ozone) or return;
-    my $textzone = '';
     if (open(my $handle, '<', $self->_template_file($ozone))) {
+        my $textzone = '';
         while (my $line = <$handle>) {
             $line =~ s/(\d+\s*;\s*)?\@REVISION@/$revision/;
@@ -409,15 +444,19 @@
         }
         close($handle);
+        return $textzone;
     } else {
-        la_log(LA_ERR, "Can't open template file for zone %s", $ozone->id);
-        return;
-    }
-    return $textzone;
+        if ($ozone->get_attributes('type') =~ /^(dns|reverse)$/) {
+            la_log(LA_ERR, "Can't open template file for zone %s: %s", $ozone->id, $!);
+            return;
+        } else {
+            return '';
+        }
+    }
 }
 
 sub _gen_dns_zone {
-    my ($self, $ozone, $textzone) = @_;
-
-    my $dbzone = "\n; Comming from database:\n";
+    my ($self, $ozone) = @_;
+
+    my $dbzone = '';
     if ($ozone->get_attributes('net')) {
         my $findhost = $self->_base->db->prepare_cached(q{
@@ -506,30 +545,12 @@
     }
 
-    $dbzone .= "; End of data from database\n";
-
-    if (!$self->_checkzone_output($ozone, $textzone . $dbzone)) {
-        la_log(LA_ERR, "Output of DNS zone %s not ok, not updating this zone",
-            $ozone->id);
-        return;
-    }
-
-    if (open(my $handle, '>', $self->_output_file($ozone))) {
-        print $handle $textzone;
-        print $handle $dbzone;
-        close($handle);
-        la_log(LA_INFO, "zone %s written into %s", $ozone->id,
-            $self->_output_file($ozone));
-    } else {
-       la_log(LA_ERR, "Can't open output file for zone %s", $ozone->id);
-       return;
-   }
-   1;
+    return $dbzone;
 }
 
 sub _gen_reverse_zone {
-    my ($self, $ozone, $textzone) = @_;
+    my ($self, $ozone) = @_;
 
     my $domain = $ozone->get_attributes('domain') || '';
-    my $dbzone = "\n; Comming from database:\n";
+    my $dbzone = '';
     if ($ozone->get_attributes('net')) {
         my $findhost = $self->_base->db->prepare_cached(q{
@@ -605,30 +626,12 @@
     }
 
-    $dbzone .= "; End of data from database\n";
-
-    if (!$self->_checkzone_output($ozone, $textzone . $dbzone)) {
-        la_log(LA_ERR, "Output of DNS zone %s not ok, not updating this zone",
-            $ozone->id);
-        return;
-    }
-
-    if (open(my $handle, '>', $self->_output_file($ozone))) {
-        print $handle $textzone;
-        print $handle $dbzone;
-        close($handle);
-        la_log(LA_INFO, "zone %s written into %s", $ozone->id,
-            $self->_output_file($ozone));
-    } else {
-       la_log(LA_ERR, "can't open output file %s (%s)",
-           $self->_output_file($ozone), $!);
-       return;
-   }
-   1;
+    return $dbzone;
 }
 
 sub _gen_dhcp_zone {
-    my ($self, $ozone, $output) = @_;
+    my ($self, $ozone) = @_;
 
     my $outzone = $ozone;
+    my $output = '';
 
     my @net;
@@ -771,22 +774,12 @@
     }
 
-    $output .= "# End of data from database\n";
-    if (open(my $handle, '>', $self->_output_file($outzone))) {
-        print $handle $output;
-        close($handle);
-        la_log(LA_INFO, "zone %s written into %s", $outzone->id,
-            $self->_output_file($outzone));
-    } else {
-        la_log(LA_ERR, "Can't open output file for dhcp zone %s (%s)",
-            $outzone->id, $!);
-        return;
-    }
-    1;
+    $output
 }
 
 sub _gen_radius_zone {
-    my ($self, $ozone, $output) = @_;
+    my ($self, $ozone) = @_;
 
     my $outzone = $ozone;
+    my $output = '';
 
     my @net;
@@ -906,20 +899,11 @@
     }
 
-    $output .= "# End of data from database\n";
-    if (open(my $handle, '>', $self->_output_file($outzone))) {
-        print $handle $output;
-        close($handle);
-        la_log(LA_INFO, "zone %s written into %s", $outzone->id,
-            $self->_output_file($outzone));
-    } else {
-        la_log(LA_ERR, "Can't open output file for dhcp zone %s (%s)",
-            $outzone->id, $!);
-        return;
-    }
-    1;
+    $output;
 }
 
 sub _gen_puppet_zone {
-    my ($self, $ozone, $output) = @_;
+    my ($self, $ozone) = @_;
+
+    my $output = '';
 
     if ($ozone->get_attributes('net')) {
@@ -970,17 +954,5 @@
     }
 
-    $output .= "# End of data from database\n";
-    my $file = $self->_output_file($ozone);
-    if (open(my $handle, '>', $file)) {
-        print $handle $output;
-        close($handle);
-        la_log(LA_INFO, "zone %s written into %s", $ozone->id,
-            $self->_output_file($ozone));
-    } else {
-        la_log(LA_ERR, "Can't open output file for puppet zone %s (%s)",
-            $ozone->id, $!);
-        return;
-    }
-    1;
+    $output
 }
 
