Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Seeking module appropriate for manipulating apache config files.

by hesco (Deacon)
on Jul 02, 2011 at 22:16 UTC ( [id://912503]=perlquestion: print w/replies, xml ) Need Help??

hesco has asked for the wisdom of the Perl Monks concerning the following question:

I am scripting the manipulation of apache virtual host configuration files and seek an appropriate tool for the task.

I've played with Apache::Admin::Config and Config::General both.

I like the former for its use of ordered lists preserving directive order, comments, empty lines and applying tidy rules to the format. Perhaps it is just me, but it leaves much to be desired in the way of clean and informative perldoc. I have a small concern that being 10 years old (apache 1.3 or earlier days), that it might be less relevant, though can't imagine what might have changes in the configuration files themselves. I have yet to figure out how to address and manipulate the depths of the data structure.

The latter I like because it provides an easy to navigate data structure and clear docs. But it fails on all of the benefits mentioned above offered by its competition. Also, I had thought it would provide a ->param() method like Config::Simple, but apparently it does not.

I have found next to nothing in the literature showing working examples and one option I have used failed altogether to garner a single hit here in perlmonks.org.

I would particularly appreciate any pointers to published examples of the successful use of either or both of the these modules; or pointers to others which might permit me to read a template config file, programmatically manipulate it and save it in a new location.

Any feedback would be appreciated.

-- Hugh

Here is what I'm seeing.

Using Apache::Admin::Config:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Test::Most tests => 5; use lib qw ( lib ); use_ok('My::NewModule::Apache'); my $apache_config_path = 't/conf/etc/apache2/sites-available'; my $virtual_host_name = 'aac.example.com.conf'; my $config_file = "$apache_config_path/$virtual_host_name"; is($config_file,'t/conf/etc/apache2/sites-available/aac.example.com.co +nf',"Our config file is: $config_file"); my $conf = new Apache::Admin::Config ( "$config_file", -indent => 2 ) or die $Apache::Admin::Config::ERROR; $conf->save('-reformat'); isa_ok($conf,'Apache::Admin::Config'); my @result = $conf->select( 'section' ); foreach my $result ( @result ){ isa_ok($result,'Apache::Admin::Config::Tree'); is($result->name,'VirtualHost','Found our Virtual Host container for + ' . $result->value); # print 'Name: ' . $result->name . ' : ' . $result->value . "\n"; # warn Dumper( $result ); foreach my $child ( $result->select( 'directive' ) ){ # isa_ok($child,'Apache::Admin::Config::Tree'); print 'Directive: ' . $child->name . ' : ' . $child->value . "\n"; } foreach my $section_child ( $result->select( 'section' ) ){ # isa_ok($section_child,'Apache::Admin::Config::Tree'); print 'Section: ' . $section_child->name . ' : ' . $section_child- +>value . "\n"; warn Dumper( $section_child->select('directive') ); # ->select('di +rective') ); foreach my $x ( @{$section_child->select('directive')} ){ print $x->name . "\n"; } } } # warn Dumper($conf->section('VirtualHost'));

and output like this:

$ perl -T t/11-aac.t 1..6 ok 1 - use My::NewModule::Apache; ok 2 - Our config file is: t/conf/etc/apache2/sites-available/cg.examp +le.com.conf # Looks like you planned 6 tests but ran 2.

Using Config::General:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Config::General; use Test::Most tests => 6; use lib qw ( lib ); use_ok('My::NewModule::Apache'); my $apache_config_path = 't/conf/etc/apache2/sites-available'; my $virtual_host_name = 'cg.example.com'; my $virtual_host_conf_name = $virtual_host_name . '.conf'; my $config_file = "$apache_config_path/$virtual_host_conf_name"; is($config_file,'t/conf/etc/apache2/sites-available/cg.example.com.con +f',"Our config file is: $config_file"); my $conf = Config::General->new( -ConfigFile => "$config_file", 'Apach +eCompatible' => 1 ); # can_ok($conf,'params'); my %config = $conf->getall; # warn Dumper( \%config ); my $new_virtual_host_name = 'www.our_new_site.com'; my $new_config_conf_file = "$apache_config_path/$new_virtual_host_name +"; my $new_virtual_host_conf_name = $new_virtual_host_name . '.conf'; foreach my $key (keys %{$config{'VirtualHost'}->{'*:80'}}){ if(ref $config{'VirtualHost'}->{'*:80'}->{$key} eq 'HASH'){ # skip this for the moment } else { $config{'VirtualHost'}->{'*:80'}->{$key} =~ s/$virtual_host_name/$ +new_virtual_host_name/; } } # warn Dumper( \%config ); my $new_conf = $conf->save_file( "$new_config_conf_file", \%config );

and output like this:

$ perl -T t/11-cg.t 1..6 ok 1 - use My::NewModule::Apache; ok 2 - Our config file is: t/conf/etc/apache2/sites-available/cg.examp +le.com.conf # Looks like you planned 6 tests but ran 2.
if( $lal && $lol ) { $life++; }
if( $insurance->rationing() ) { $people->die(); }

Replies are listed 'Best First'.
Re: Seeking module appropriate for manipulating apache config files.
by GrandFather (Saint) on Jul 02, 2011 at 23:31 UTC

    This looks like a good opportunity for some interested person to either take over maintenance of Apache::Admin::Config which fails its tests on every version of Perl since 5.8.8, or to write a new module and contribute it to CPAN.

    True laziness is hard work

      I suspect, if only due to your moniker around here, that you have likely been around the Perl community longer than I. For those of us unaware, what is the process for 'tak(ing) over maintenance' of a potentially abandoned module? If one wanted to do that, how would one proceed? I have only now attempted to contact this module's author and have no idea if it is abandoned or not. This page reports that the author has contributed as recently as three years ago (with something oddly called mysql-genocide-0.03).

      http://search.cpan.org/~rsoliv/

      if( $lal && $lol ) { $life++; }
      if( $insurance->rationing() ) { $people->die(); }

        There is a process for contributing to a neglected module discussed at https://pause.perl.org/pause/authenquery?ACTION=pause_04about#takeover. Given the three year passage of time since the last apparent CPAN activity of the author and the fact that there are outstanding bugs that are at least that old I suspect that the PAUSE admins would look kindly on a request to assist in the maintenance of this module.

        Yes, I also noticed that oddly named module, although my interest wasn't sufficiently peaked to actually investigate it. ;)

        True laziness is hard work
      <rs-pause@rhapsodyk.net>: host mail.rhapsodyk.net78.40.120.230 said: 550
          5.1.1 <rs-pause@rhapsodyk.net>: Recipient address rejected: rhapsodyk.net
          (in reply to RCPT TO command)
      
      if( $lal && $lol ) { $life++; }
      if( $insurance->rationing() ) { $people->die(); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://912503]
Approved by GrandFather
Front-paged by chrestomanci
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-20 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found