| 1 | =head1 NAME |
|---|
| 2 | |
|---|
| 3 | Perlbal::Plugin::Include - Allows multiple, nesting configuration files |
|---|
| 4 | |
|---|
| 5 | =head1 DESCRIPTION |
|---|
| 6 | |
|---|
| 7 | This module adds an INCLUDE command to the Perlbal management console |
|---|
| 8 | and allows the globbed inclusion of configuration files. |
|---|
| 9 | |
|---|
| 10 | =head1 SYNOPSIS |
|---|
| 11 | |
|---|
| 12 | This module provides a Perlbal plugin which can be loaded and used as |
|---|
| 13 | follows: |
|---|
| 14 | |
|---|
| 15 | LOAD include |
|---|
| 16 | INCLUDE = /etc/perlbal/my.conf |
|---|
| 17 | |
|---|
| 18 | You 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 | |
|---|
| 24 | This module relies entirely on Perlbal::load_config for loading, so if |
|---|
| 25 | you have trouble with INCLUDE, be sure you can load the same |
|---|
| 26 | configuration without error using "perlbal -c" first. |
|---|
| 27 | |
|---|
| 28 | Also note that Perlbal::load_config versions 1.60 and below do not use |
|---|
| 29 | a local filehandle while reading the configuration file, so this |
|---|
| 30 | module overrides that routine on load to allow nested calls. |
|---|
| 31 | |
|---|
| 32 | =head1 COPYRIGHT AND LICENSE |
|---|
| 33 | |
|---|
| 34 | Copyright 2008 Eamon Daly <eamon@eamondaly.com> |
|---|
| 35 | |
|---|
| 36 | This module is part of the Perlbal distribution, and as such can be |
|---|
| 37 | distributed under the same licence terms as the rest of Perlbal. |
|---|
| 38 | |
|---|
| 39 | =cut |
|---|
| 40 | |
|---|
| 41 | package Perlbal::Plugin::Include; |
|---|
| 42 | |
|---|
| 43 | use strict; |
|---|
| 44 | use warnings; |
|---|
| 45 | no warnings qw(deprecated); |
|---|
| 46 | |
|---|
| 47 | # called when we are loaded |
|---|
| 48 | sub 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 |
|---|
| 68 | sub 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. |
|---|
| 77 | sub 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 | |
|---|
| 90 | 1; |
|---|