Config-HAProxy
view release on metacpan or search on metacpan
lib/Config/HAProxy.pm view on Meta::CPAN
$self->{_lint}{command} = $v;
}
if (defined($v = delete $_{path})) {
$self->{_lint}{path} = $v;
}
croak "unrecognized keywords" if keys %_;
} else {
croak "bad number of arguments";
}
}
if ($self->{_lint}{enable}) {
$self->{_lint}{command} ||= 'haproxy -c -f';
if ($self->{_lint}{path}) {
my ($prog, $args) = split /\s+/, $self->{_lint}{command}, 2;
if (!File::Spec->file_name_is_absolute($prog)) {
foreach my $dir (split /:/, $self->{_lint}{path}) {
my $name = File::Spec->catfile($dir, $prog);
if (-x $name) {
$prog = $name;
last;
}
}
if ($args) {
$prog .= ' '.$args;
}
$self->{_lint}{command} = $prog;
}
}
return $self->{_lint}{command};
}
}
sub save {
my $self = shift;
croak "bad number of arguments" if @_ % 2;
local %_ = @_;
my $dry_run = delete $_{dry_run};
my @wrargs = %_;
return unless $self->tree;# FIXME
return unless $self->tree->is_dirty;
my ($fh, $tempfile) = tempfile('haproxy.XXXXXX',
DIR => dirname($self->filename));
$self->write($fh, @wrargs);
close($fh);
if (my $cmd = $self->lint) {
my ($ok, $err, $full, $outbuf, $errbuf) =
run(command => "$cmd $tempfile");
unless ($ok) {
unlink $tempfile;
if ($errbuf && @$errbuf) {
croak "Syntax check failed: ".join("\n", @$errbuf)."\n";
}
croak $err;
}
}
return 1 if $dry_run;
my $sb = stat($self->filename);
$self->backup;
rename($tempfile, $self->filename)
or croak "can't rename $tempfile to ".$self->tempfile.": $!";
# This will succeed: we've created the file, so we're owning it.
chmod $sb->mode & 0777, $self->filename;
# This will fail unless we are root, let it be so.
chown $sb->uid, $sb->gid, $self->filename;
$self->tree->clear_dirty;
return 1;
}
sub backup_name {
my $self = shift;
$self->filename . '~'
}
sub backup {
my $self = shift;
my $backup = $self->backup_name;
if (-f $backup) {
unlink $backup
or croak "can't unlink $backup: $!"
}
rename $self->filename, $self->backup_name
or croak "can't rename :"
. $self->filename
. " to "
. $self->backup_name
. ": $!";
}
1;
__END__
=head1 NAME
Config::HAProxy - Parser for HAProxy configuration file
=head1 SYNOPSIS
use Config::HAProxy;
$cfg = new Config::HAProxy($filename);
$cfg->parse;
$name = $cfg->filename;
@frontends = $cfg->select(name => 'frontend');
$itr = $cfg->iterator(inorder => 1);
while (defined($node = $itr->next)) {
# do something with $node
}
$cfg->lint(enable => 1, command => 'haproxy -c -f',
path => '/sbin:/usr/sbin')
$cfg->save(%hash);
$cfg->write($file_or_handle, %hash);
$cfg->backup;
$name = $self->backup_name;
$cfg->reset;
$cfg->push($node);
$node = $cfg->pop;
$node = $cfg->tos;
$node = $cfg->tree;
=head1 DESCRIPTION
The B<Config::HAProxy> class is a parser that converts the B<HAProxy>
configuration file to a parse tree and provides methods for various
operations on this tree, such as: searching, modifying and saving it
to a file.
An object of this class contains a I<parse tree> representing the
configuration read from the file (or created from scratch). Nodes in the
tree can be of four distinct classes:
=over 4
=item Empty
Represents an empty line.
=item Comment
Represents a comment line.
=item Statement
Represents a simple statement.
=item Section
A container, representing a C<compound statement>, i.e. a statement that
contains multiple sub-statements. Compound statements are: B<global>,
B<defaults>, B<frontend>, B<backend>, and B<resolvers>. The list of
compound statements may be modified using the B<declare_section> and
B<undeclare_section> class methods (see below).
=back
In addition to these four classes, a special class B<Root> is provided, which
represents the topmost node in the parse tree (i.e. the parent of other nodes).
A set of attributes is associated with each node. Among these, the B<orig>
attribute contains the original line from the configuration file that triggered
creation of this node, and B<locus> contains the location of this line (or
lines, for sections) in the configuration file (as a B<Text::Locus>) object.
These two attributes are meaningful for all nodes. For statement nodes (simple
statements and sections) the B<kw> attribute contains the statement I<keyword>,
and the B<argv> attribute - its arguments. For example, the statement
server localhost 127.0.0.1:8080
is represented by a node of class B<Config::HAProxy::Node::Statement>, with
C<server> as B<kw> and list (C<localhost>, C<127.0.0.1:8080>) as B<argv>.
Additionally, section nodes provide methods for accessing their subtrees.
( run in 1.510 second using v1.01-cache-2.11-cpan-df04353d9ac )