root/trunk/lib/Perlbal/Plugin/Include.pm

Revision 793, 2.2 kB (checked in by dormando, 14 months ago)

Remove version check in bundled plugin.. was confusing PAUSE.

Line 
1=head1 NAME
2
3Perlbal::Plugin::Include - Allows multiple, nesting configuration files
4
5=head1 DESCRIPTION
6
7This module adds an INCLUDE command to the Perlbal management console
8and allows the globbed inclusion of configuration files.
9
10=head1 SYNOPSIS
11
12This module provides a Perlbal plugin which can be loaded and used as
13follows:
14
15    LOAD include
16    INCLUDE = /etc/perlbal/my.conf
17
18You may also specify multiple configuration files a la File::Glob:
19
20    INCLUDE = /foo/bar.conf /foo/quux/*.conf
21
22=head1 BUGS AND LIMITATIONS
23
24This module relies entirely on Perlbal::load_config for loading, so if
25you have trouble with INCLUDE, be sure you can load the same
26configuration without error using "perlbal -c" first.
27
28Also note that Perlbal::load_config versions 1.60 and below do not use
29a local filehandle while reading the configuration file, so this
30module overrides that routine on load to allow nested calls.
31
32=head1 COPYRIGHT AND LICENSE
33
34Copyright 2008 Eamon Daly <eamon@eamondaly.com>
35
36This module is part of the Perlbal distribution, and as such can be
37distributed under the same licence terms as the rest of Perlbal.
38
39=cut
40
41package Perlbal::Plugin::Include;
42
43use strict;
44use warnings;
45no  warnings qw(deprecated);
46
47# called when we are loaded
48sub load {
49    my $class = shift;
50
51    Perlbal::register_global_hook('manage_command.include', sub {
52        my $mc = shift->parse(qr/^include\s+=\s+(.+)\s*$/,
53                              "usage: INCLUDE = <config files>");
54
55        my ($glob) = $mc->args;
56
57        for (glob($glob)) {
58            Perlbal::load_config($_, sub { print STDOUT "$_[0]\n"; });
59        }
60
61        return $mc->ok;
62    });
63
64    return 1;
65}
66
67# called for a global unload
68sub unload {
69    # unregister our global hooks
70    Perlbal::unregister_global_hook('manage_command.include');
71
72    return 1;
73}
74
75# In older versions of Perlbal, load_config uses a typeglob, throwing
76# warnings when re-entering. This uses a locally-scoped filehandle.
77sub load_config_local {
78    my ($file, $writer) = @_;
79    open(my $fh, $file) or die "Error opening config file ($file): $!\n";
80    my $ctx = Perlbal::CommandContext->new;
81    $ctx->verbose(0);
82    while (my $line = <$fh>) {
83        $line =~ s/\$(\w+)/$ENV{$1}/g;
84        return 0 unless Perlbal::run_manage_command($line, $writer, $ctx);
85    }
86    close($fh);
87    return 1;
88}
89
901;
Note: See TracBrowser for help on using the browser.