perl script inside apache configuration, svn example

For every svn project you have a <Location> section in your apache configuration. Whenever you add a svn project you have to manually add a section in that configuration file.
The configuration file gets very long and whenever you have to change something you have to change it for every project.

You can write a script that generates the big file, but when you add a repositorie you have to run the script and restart apache.

This can be solved by adding a “perl script” inside the apache configuration. First you have to install the mod_perl which allows you to use the module “mod_perl.c”.
Now you can write some perl scripts inside your apache configuration, these scripts will be executed whenever the configuration file is parsed (with a restart/reload).

This is how the mod_perl section in my configuration looks like:

<IfModule mod_perl.c>
<Perl>
#!/usr/bin/perl

sub loop_dir {

  my $base = shift;
  my $sub  = shift;
  if($sub) { $sub .= "/";}

  opendir(DIR, "$base$sub")
    or die "Unable to open SVN repository base '$base$sub'\n";

  foreach $project (readdir(DIR)) {
    next unless $project =~ /^[[:alnum:]_\-]+$/;
    next unless -d "$base$sub$project";

    if( -e "$base$sub$project/format"){

      print "\nHandling project: $sub$project";

      $Location{"/$sub$project"} = {
        DAV => 'svn',
        SVNPath => "$base$sub$project",

        AuthType => 'Basic',
        AuthName => "'Cronos CVSVN server'",

        AuthLDAPAuthoritative => 'on',
        AuthLDAPURL => ' ldap://localhost/ou=users,dc=cronos,dc=be?uid',
        AuthLDAPGroupAttribute => 'memberUid',
        AuthLDAPGroupAttributeIsDN => 'off',

        Require => "group cn=$project,ou=groups,dc=cronos,dc=be",
      }
    } else {
      &loop_dir("$base", "$sub$project");
    }
  }
  closedir(DIR);
}

&loop_dir ('/var/lib/svn/');

</Perl>
</IfModule>

This part of the configuration file will create a <Location> section for every svn-project inside /var/lib/svn it will also looks inside the subdirectories (if the directory is not a project).

Leave a Reply