summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Config/HAProxy.pm140
-rw-r--r--lib/Config/HAProxy/Iterator.pm68
-rw-r--r--lib/Config/HAProxy/Node.pm137
-rw-r--r--lib/Config/HAProxy/Node/Comment.pm31
-rw-r--r--lib/Config/HAProxy/Node/Empty.pm29
-rw-r--r--lib/Config/HAProxy/Node/Root.pm52
-rw-r--r--lib/Config/HAProxy/Node/Section.pm153
-rw-r--r--lib/Config/HAProxy/Node/Statement.pm45
8 files changed, 628 insertions, 27 deletions
diff --git a/lib/Config/HAProxy.pm b/lib/Config/HAProxy.pm
index 7938bcb..f99bf33 100644
--- a/lib/Config/HAProxy.pm
+++ b/lib/Config/HAProxy.pm
@@ -179,16 +179,16 @@ sub save {
179 close($fh); 179 close($fh);
180 180
181 my $sb = stat($self->filename); 181 my $sb = stat($self->filename);
182 $self->backup; 182 $self->backup;
183 rename($tempfile, $self->filename) 183 rename($tempfile, $self->filename)
184 or croak "can't rename $tempfile to ".$self->tempfile.": $!"; 184 or croak "can't rename $tempfile to ".$self->tempfile.": $!";
185 # This will fail unless we are root, let it be so.
186 chown $sb->uid, $sb->gid, $self->filename;
187 # This will succeed: we've created the file, so we're owning it. 185 # This will succeed: we've created the file, so we're owning it.
188 chmod $sb->mode & 0777, $self->filename; 186 chmod $sb->mode & 0777, $self->filename;
187 # This will fail unless we are root, let it be so.
188 chown $sb->uid, $sb->gid, $self->filename;
189 189
190 $self->tree->clear_dirty 190 $self->tree->clear_dirty
191} 191}
192 192
193sub backup_name { 193sub backup_name {
194 my $self = shift; 194 my $self = shift;
@@ -212,63 +212,122 @@ sub backup {
212 212
2131; 2131;
214__END__ 214__END__
215 215
216=head1 NAME 216=head1 NAME
217 217
218Config::HAProxy - HAProxy configuration file 218Config::HAProxy - Parser for HAProxy configuration file
219 219
220=head1 SYNOPSIS 220=head1 SYNOPSIS
221 221
222use Config::HAProxy; 222 use Config::HAProxy;
223$cfg = new Config::HAProxy($filename); 223 $cfg = new Config::HAProxy($filename);
224$cfg->parse; 224 $cfg->parse;
225 225
226$name = $cfg->filename; 226 $name = $cfg->filename;
227 227
228@frontends = $cfg->select(name => 'frontend'); 228 @frontends = $cfg->select(name => 'frontend');
229 229
230$itr = $cfg->iterator(inorder => 1); 230 $itr = $cfg->iterator(inorder => 1);
231while (defined($node = $itr->next)) { 231 while (defined($node = $itr->next)) {
232 # do something with $node 232 # do something with $node
233} 233 }
234 234
235$cfg->save; 235 $cfg->save;
236 236
237$cfg->write($file_or_handle); 237 $cfg->write($file_or_handle);
238 238
239$cfg->backup; 239 $cfg->backup;
240$name = $self->backup_name; 240 $name = $self->backup_name;
241 241
242$cfg->reset; 242 $cfg->reset;
243$cfg->push($node); 243 $cfg->push($node);
244$node = $cfg->pop; 244 $node = $cfg->pop;
245$node = $cfg->tos; 245 $node = $cfg->tos;
246$node = $cfg->tree; 246 $node = $cfg->tree;
247 247
248=head1 DESCRIPTION 248=head1 DESCRIPTION
249 249
250FIXME 250The B<Config::HAProxy> class is a parser that converts the B<HAProxy>
251configuration file to a parse tree and provides methods for various
252operations on this tree, such as: searching, modifying and saving it
253to a file.
254
255An object of this class contains a I<parse tree> representing the
256configuration read from the file (or created from scratch). Nodes in the
257tree can be of four distinct classes:
258
259=over 4
260
261=item Empty
262
263Represents an empty line.
264
265=item Comment
266
267Represents a comment line.
268
269=item Statement
270
271Represents a simple statement.
272
273=item Section
274
275A container, representing a C<compound statement>, i.e. a statement that
276contains multiple sub-statements. Compound statements are: B<global>,
277B<defaults>, B<frontend>, and B<backend>.
278
279=back
280
281In addition to these four classes, a special class B<Root> is provided, which
282represents the topmost node in the parse tree (i.e. the parent of other nodes).
283
284A set of attributes is associated with each node. Among these, the B<orig>
285attribute contains the original line from the configuration file that triggered
286creation of this node, and B<locus> contains the location of this line (or
287lines, for sections) in the configuration file (as a B<Text::Locus>) object.
288
289These two attributes are meaningful for all nodes. For statement nodes (simple
290statements and sections) the B<kw> attribute contains the statement I<keyword>,
291and the B<argv> attribute - its arguments. For example, the statement
292
293 server localhost 127.0.0.1:8080
294
295is represented by a node of class B<Config::HAProxy::Node::Statement>, with
296C<server> as B<kw> and list (C<localhost>, C<127.0.0.1:8080>) as B<argv>.
297
298Additionally, section nodes provide methods for accessing their subtrees.
299
300For a detailed description of the node class and its methods, please refer to
301B<Config::HAProxy::Node>.
251 302
252=head1 CONSTRUCTOR 303=head1 CONSTRUCTOR
253 304
254 $cfg = new Config::HAProxy($filename); 305 $cfg = new Config::HAProxy($filename);
255 306
256Creates and returns a new object for manipulating the HAProxy configuration. 307Creates and returns a new object for manipulating the HAProxy configuration.
257Optional B<$filename> specifies the name of the file to read configuration 308Optional B<$filename> specifies the name of the file to read configuration
258from. It defaults to F</etc/haproxy/haproxy.cfg>. 309from. It defaults to F</etc/haproxy/haproxy.cfg>.
259 310
260=head1 THE PARSE TREE 311=head2 filename
312
313 $s = $cfg->filename;
314
315Returns the configuration file name given when creating the object.
316
317=head1 PARSING
261 318
262=head2 parse 319=head2 parse
263 320
264 $cfg->parse; 321 $cfg->parse;
265 322
266Reads and parses the configuration file. Croaks if the file does not exist. 323Reads and parses the configuration file. Croaks if the file does not exist.
324Returns B<$cfg>.
267 325
268 326=head1 BUILDING THE PARSE TREE
327
269=head2 reset 328=head2 reset
270 329
271 $cfg->reset; 330 $cfg->reset;
272 331
273Clears the parse tree. 332Clears the parse tree.
274 333
@@ -302,13 +361,13 @@ Returns the last node in the tree.
302=head1 SAVING 361=head1 SAVING
303 362
304=head2 save 363=head2 save
305 364
306 $cfg->save; 365 $cfg->save;
307 366
308Saves the configuration file. 367Saves the parse tree in the configuration file.
309 368
310=head2 write 369=head2 write
311 370
312 $cfg->write($file, %hash); 371 $cfg->write($file, %hash);
313 372
314Writes configuration to the named file or file handle. If B<$file> is the 373Writes configuration to the named file or file handle. If B<$file> is the
@@ -324,6 +383,37 @@ Arguments with B<$i> greater than or equal to B<@tabstop> are appended to
324the resulting output, preserving their original offsets. 383the resulting output, preserving their original offsets.
325 384
326Normally, comments retain their original indentation. However, if the 385Normally, comments retain their original indentation. However, if the
327key B<reintent_comments> is present, and its value is evaluated as true, 386key B<reintent_comments> is present, and its value is evaluated as true,
328then comments are reindented following the rules described above. 387then comments are reindented following the rules described above.
329 388
389=head1 SEE ALSO
390
391B<Config::HAProxy::Node>,
392B<Config::HAProxy::Node::Section>,
393B<Config::HAProxy::Node::Statement>,