mathieu: server/perl-kolab/lib/Kolab Conf.pm,1.12,1.13

cvs at kolab.org cvs at kolab.org
Sun Dec 13 16:19:25 CET 2009


Author: mathieu

Update of /kolabrepository/server/perl-kolab/lib/Kolab
In directory doto:/tmp/cvs-serv22798/perl-kolab/lib/Kolab

Modified Files:
	Conf.pm 
Log Message:
perl-kolab:
    new @@@print@@@ macro available for: getLDAPReplicas(), getLDAPAccess(),
    getCyrusGroups(), getPostfixMap(map). This removed the use of
   %special_templates, %haschanged, Kolab::Conf::reload().

kolabd:
 
    kolab/issue1740 (fully generate slapd.access, slapd.replicas, transport,
                    virtual and imapd.groups by templates) using the new
                    @@@print@@@ macro (perl-kolab) and RUNONCHANGE.



Index: Conf.pm
===================================================================
RCS file: /kolabrepository/server/perl-kolab/lib/Kolab/Conf.pm,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- Conf.pm	10 Dec 2009 17:43:37 -0000	1.12
+++ Conf.pm	13 Dec 2009 15:19:23 -0000	1.13
@@ -42,10 +42,8 @@
 
 our %EXPORT_TAGS = (
     'all' => [ qw(
-        &buildCyrusGroups
-        &buildLDAPReplicas
         &rebuildTemplates
-	&checkPermissions
+        &checkPermissions
     ) ]
     );
 
@@ -59,7 +57,6 @@
 my %ownership = ();
 my %permissions = ();
 my %templatehaschanged = ();
-my %haschanged = ();
 my %commentchar = ();
 
 sub fixup {
@@ -92,7 +89,6 @@
     $cc = "#" if (!defined $cc);
 
     # Different warnings during bootstrapping and regular configuration
-# $Kolab::config{"bootstrap_config"} = "true";
     if ((defined $Kolab::config{"bootstrap_config"}) &&
         ($Kolab::config{"bootstrap_config"} eq "true")) {
 
@@ -127,14 +123,6 @@
     my $oldcfg = $cfg . '.old';
     my $templatedir = $Kolab::config{"templatedir"};
 
-    my %special_templates = (
-	    "$templatedir/transport.template"      => 1,
-	    "$templatedir/virtual.template"        => 1,
-	    "$templatedir/imapd.group.template"    => 1,
-	    "$templatedir/slapd.access.template"   => 1,
-	    "$templatedir/slapd.replicas.template" => 1
-	);
-
     my $oldmask = umask 077;
     #creating the config file is changing it
     if (! -f $cfg) { 
@@ -145,7 +133,6 @@
     # To avoid warnings, the backup files must be owned by root
     chown(0, 0, $oldcfg);
     umask $oldmask;
-    #chmod(0600, $oldcfg) if ($oldcfg =~ /openldap/);
 
     Kolab::log('T', "Creating new configuration file `$cfg' from template `$tmpl'", KOLAB_DEBUG );
 
@@ -202,6 +189,7 @@
             }
             s/\@{3}if (\S+?)\@{3}\n?//;
         } elsif (/\@{3}else\@{3}/) {
+            # @@@else@@@
             if( $keep == 0 ) {
             # Now keep
             $keep = 1;
@@ -214,15 +202,28 @@
                 s/\@{3}else\@{3}\n?//;
 
         } elsif (/\@{3}endif\@{3}/) {
+            # @@@endif@@@
             ($skip > 0) && $skip--;
             s/\@{3}endif\@{3}\n?//;
 
         } elsif (/\@{3}warning\@{3}/) {
-
+            # @@@warning@@@
             printWarning($config, $tmpl, $cchr);
 
+        } elsif (/\@{3}print\s+([^\s()]+?)\s*\(([^,]+)?\)\@{3}/) {
+            # @@@print func([arg])@@@
+            my $val;
+            if ($1 eq 'getLDAPReplicas') {$val = getLDAPReplicas();}
+            elsif ($1 eq 'getLDAPAccess') {$val = getLDAPAccess();}
+            elsif ($1 eq 'getCyrusGroups') {$val = getCyrusGroups();}
+            elsif ($1 eq 'getPostfixMap') {$val = getPostfixMap($2);}
+            else {Kolab::log('T', "Unknown printable value `$1'", KOLAB_WARN);}
+            s/\@{3}print\s+(\S+?)\@{3}//;
+            ($skip == 0) && print $config $val;
         } else {
             while (/\@{3}([^\s\@]+?)(\|(.+?)\((.*)\))?\@{3}/) {
+                # @@@attr@@@
+                # @@@attr|function(args)@@@
                 my $attr = $1;
                 my $fct  = $3;
                 my $args = $4;
@@ -278,13 +279,10 @@
     move($config->filename, $cfg) || Kolab::log('T', "Error moving configfile to $cfg, error: $!", KOLAB_ERROR );
     fixup( $cfg, $owner, $perm );
 
-    if (-f $oldcfg && !defined $special_templates{$tmpl} ) {
+    if (-f $oldcfg) {
         my $rc = `diff -q $cfg $oldcfg`;
         chomp($rc);
         if ($rc) {
-            if ($cfg =~ /openldap/) {
-                $haschanged{'slapd'} = 1;
-            }
             $templatehaschanged{$tmpl} = 1;
 
             Kolab::log('T', "`$cfg' change detected: $rc", KOLAB_DEBUG );
@@ -294,23 +292,12 @@
     Kolab::log('T', "Finished creating configuration file `$cfg'", KOLAB_DEBUG );
 }
 
-sub buildPostfixMap
+sub getPostfixMap
 {
     my $map = shift;
+    my $ret = '';
     Kolab::log('T', "Building Postfix $map map", KOLAB_DEBUG);
 
-    my $templatedir = $Kolab::config{"templatedir"};
-
-    my $keytemplate = "$templatedir/$map.template";
-    my $cfg = $templates{$keytemplate};
-    my $oldcfg = $cfg . '.old';
-
-    my $cfgfile;
-    if (!($cfgfile = IO::File->new($cfg, 'a'))) {
-        Kolab::log('T', "Unable to create Postfix $map map: $!", KOLAB_ERROR);
-        exit(1);
-    }
-
     my $ldap = Kolab::LDAP::create(
         $Kolab::config{'ldap_ip'},
         $Kolab::config{'ldap_port'},
@@ -335,7 +322,7 @@
             foreach (@$routes) {
                 $_ = trim($_);
                 Kolab::log('T', "Adding entry `$_' to $map");
-                print $cfgfile $_ . "\n";
+                $ret .= $_ . "\n";
             }
         }
     } else {
@@ -343,45 +330,15 @@
     }
 
     Kolab::LDAP::destroy($ldap);
-    $cfgfile->close;
-
-    if (-f $oldcfg) {
-        my $rc = `diff -q $cfg $oldcfg`;
-        chomp($rc);
-        if ($rc) {
-            Kolab::log('T', "`$cfg' change detected: $rc", KOLAB_DEBUG);
-            $templatehaschanged{$keytemplate} = 1;
-        }
-    } else {
-        Kolab::log('T', "`$cfg' creation detected", KOLAB_DEBUG);
-        $templatehaschanged{$keytemplate} = 1;
-    }
-
     Kolab::log('T', "Finished building Postfix $map map", KOLAB_DEBUG);
+    return $ret;
 }
 
-sub buildCyrusGroups
+sub getCyrusGroups
 {
+    my $ret ='';
     Kolab::log('T', 'Building Cyrus groups', KOLAB_DEBUG);
 
-    my $templatedir = $Kolab::config{"templatedir"};
-
-    my $keytemplate = "$templatedir/imapd.group.template";
-    my $cfg = $templates{$keytemplate};
-    my $oldcfg = $cfg . '.old';
-    #delete $templates{$keytemplate};
-
-    #my $oldmask = umask 077;
-    #copy($cfg, $oldcfg);
-    #chown($Kolab::config{'kolab_uid'}, $Kolab::config{'kolab_gid'}, $oldcfg);
-    #umask $oldmask;
-
-    my $groupconf;
-    if (!($groupconf = IO::File->new($cfg, 'a'))) {
-        Kolab::log('T', "Unable to open configuration file `$cfg': $!", KOLAB_ERROR);
-        exit(1);
-    }
-
     my $ldap = Kolab::LDAP::create(
         $Kolab::config{'ldap_ip'},
         $Kolab::config{'ldap_port'},
@@ -419,7 +376,7 @@
 		}
 	    }
             $group =~ s/,$//;
-            print $groupconf $group . "\n";
+            $ret .= $group . "\n";
             Kolab::log('T', "Adding cyrus group `$group'");
             $count++;
         }
@@ -427,33 +384,17 @@
         Kolab::log('T', 'No Cyrus groups found');
     }
 
-    $groupconf->close;
     Kolab::LDAP::destroy($ldap);
 
-    fixup( $cfg, $ownership{$cfg}, $permissions{$cfg});
-
     Kolab::log('T', 'Finished building Cyrus groups', KOLAB_DEBUG );
+    return $ret;
 }
 
-sub buildLDAPAccess
+sub getLDAPAccess
 {
+    my $ret = '';
     Kolab::log('T', 'Building LDAP access file', KOLAB_DEBUG);
 
-    my $templatedir = $Kolab::config{"templatedir"};
-
-    my $keytemplate = "$templatedir/slapd.access.template";
-    if( ! -f $keytemplate ) {
-        Kolab::log('T', "No LDAP access file `$keytemplate', skipping", KOLAB_DEBUG);
-        return;
-    }
-    my $cfg = $templates{$keytemplate};
-    my $oldcfg = $cfg . '.old';
-
-    my $access;
-    if (!($access = IO::File->new($cfg, 'a'))) {
-        Kolab::log('T', "Unable to open configuration file `$cfg': $!", KOLAB_ERROR);
-        exit(1);
-    }
 
 my $global_acl = <<'EOS';
 # Domain specific access
@@ -497,68 +438,37 @@
     }
 
     ($str = $dom_acl1) =~ s/\@{3}base_dn\@{3}/$Kolab::config{'base_dn'}/g;
-    print $access $str;
+    $ret .= $str;
 
     foreach $domain (@domains) {
 	($str = $dom_acl2) =~ s/\@{3}domain\@{3}/$domain/g;
 	$str =~ s/\@{3}base_dn\@{3}/$Kolab::config{'base_dn'}/g;	
-	print $access $str;
+	$ret .= $str;
     }
 
     ($str = $dom_acl3) =~ s/\@{3}base_dn\@{3}/$Kolab::config{'base_dn'}/g;
-    print $access $str;
+    $ret .= $str;
 
     foreach $domain (@domains) {
 	($str = $global_acl) =~ s/\@{3}domain\@{3}/$domain/g;
 	$str =~ s/\@{3}base_dn\@{3}/$Kolab::config{'base_dn'}/g;	
-	print $access $str;
+	$ret .= $str;
 	Kolab::log('T', "Adding acl for domain '$str'");
     }
-
-    $access->close;
-
-    if (-f $oldcfg) {
-        my $rc = `diff -q $cfg $oldcfg`;
-        chomp($rc);
-        if ($rc) {
-	    Kolab::log('T', "`$cfg' change detected: $rc", KOLAB_DEBUG);
-	    $haschanged{'slapd'} = 1;
-        }
-    } else {
-        $haschanged{'slapd'} = 1;
-    }
-
-    fixup( $cfg, $ownership{$cfg}, $permissions{$cfg});
-
-    Kolab::log('T', 'Finished building LDAP access file', KOLAB_DEBUG );
+    return $ret;
 }
 
-sub buildLDAPReplicas
+sub getLDAPReplicas
 {
+    my $ret = '';
     Kolab::log('T', 'Building LDAP replicas', KOLAB_DEBUG);
 
-    my $templatedir = $Kolab::config{"templatedir"};
-
-    my $keytemplate = "$templatedir/slapd.replicas.template";
-    if( ! -f $keytemplate ) {
-        Kolab::log('T', "No LDAP replicas `$keytemplate', skipping", KOLAB_DEBUG);
-        return;
-    }
-    my $cfg = $templates{$keytemplate};
-    my $oldcfg = $cfg . '.old';
-
-    my $repl;
-    if (!($repl = IO::File->new($cfg, 'a'))) {
-        Kolab::log('T', "Unable to open configuration file `$cfg': $!", KOLAB_ERROR);
-        exit(1);
-    }
-
     # directory_mode syncrepl is supported from openldap-2.3.x and beyond
     if ($Kolab::config{'directory_mode'} eq "syncrepl") {
 
       if ( $Kolab::config{'is_master'} eq "false" ) {
         # Output a syncrepl statement for database synchronisation
-        print $repl "syncrepl rid=0 \n"
+        $ret .=   "syncrepl rid=0 \n"
                  ."         provider=".$Kolab::config{"ldap_master_uri"}."\n"
                  ."         type=refreshAndPersist\n"
                  ."         retry=\"60 10 300 +\"\n"
@@ -582,34 +492,20 @@
   	}
   	for my $h ( @kh ) {
   	    next if lc($h) eq lc($Kolab::config{'fqdnhostname'});
-  	    print $repl "replica uri=ldaps://$h\n"
+  	    $ret .= "replica uri=ldaps://$h\n"
   		."  binddn=\"".$Kolab::config{'bind_dn'}."\"\n"
   		."  bindmethod=simple credentials=".$Kolab::config{'bind_pw'}."\n\n";
   	}
       } else {
   	# Slave setup
   	# Output an update dn statement instead
-  	print $repl "updatedn ".$Kolab::config{'bind_dn'}."\n";
-  	print $repl "updateref ".$Kolab::config{'ldap_master_uri'}."\n";
+  	$ret .= "updatedn ".$Kolab::config{'bind_dn'}."\n";
+  	$ret .= "updateref ".$Kolab::config{'ldap_master_uri'}."\n";
       }
     }
 
-    $repl->close;
-
-    fixup( $cfg, $ownership{$cfg}, $permissions{$cfg});
-
-    if (-f $oldcfg) {
-        my $rc = `diff -q $cfg $oldcfg`;
-        chomp($rc);
-        if ($rc) {
-	    Kolab::log('T', "`$cfg' change detected: $rc", KOLAB_DEBUG);
-	    $haschanged{'slapd'} = 1;
-        }
-    } else {
-        $haschanged{'slapd'} = 1;
-    }
-
     Kolab::log('T', 'Finished building LDAP replicas', KOLAB_DEBUG);
+    return $ret;
 }
 
 sub replaceMetaVar
@@ -715,16 +611,9 @@
     my $tpl;
     foreach $tpl (keys %templates) {
         $cfg = $templates{$tpl};
-        #print STDOUT "Rebuilding $tpl => $cfg\n";
         build($tpl, $cfg, $ownership{$cfg}, $permissions{$cfg}, $commentchar{$cfg});
      }
 
-    buildPostfixMap('transport');
-    buildPostfixMap('virtual');
-    buildLDAPAccess;
-    buildLDAPReplicas;
-    buildCyrusGroups;
-
     Kolab::log('T', 'Finished regenerating configuration files', KOLAB_DEBUG );
 
     my %cmds = ();
@@ -770,7 +659,6 @@
     my $out;
     foreach my $tpl (@templ) {
       $cfg = $templates{$tpl};
-      # print STDOUT "Rebuilding $tpl => $cfg\n";
       build($tpl, $cfg, $ownership{$cfg}, $permissions{$cfg}, $commentchar{$cfg});
     }
 }
@@ -809,18 +697,6 @@
 	}
     }
     return $ok;
-}
-
-sub reload
-{
-    if ($haschanged{'slapd'}) {
-        &Kolab::log('K', 'Restarting OpenLDAP...');
-        system("$Kolab::config{'KOLABRC'} rc openldap restart &");
-    }
-
-    %Kolab::Conf::haschanged = ();
-
-    &Kolab::log('K', 'Reload finished');
 }
 
 1;





More information about the commits mailing list