diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Config/HAProxy.pm | 102 | ||||
-rw-r--r-- | lib/Config/HAProxy/Iterator.pm | 68 | ||||
-rw-r--r-- | lib/Config/HAProxy/Node.pm | 137 | ||||
-rw-r--r-- | lib/Config/HAProxy/Node/Comment.pm | 31 | ||||
-rw-r--r-- | lib/Config/HAProxy/Node/Empty.pm | 29 | ||||
-rw-r--r-- | lib/Config/HAProxy/Node/Root.pm | 50 | ||||
-rw-r--r-- | lib/Config/HAProxy/Node/Section.pm | 151 | ||||
-rw-r--r-- | lib/Config/HAProxy/Node/Statement.pm | 45 |
8 files changed, 607 insertions, 6 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 | |||
@@ -182,10 +182,10 @@ sub save { | |||
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 | } |
@@ -215,7 +215,7 @@ __END__ | |||
215 | 215 | ||
216 | =head1 NAME | 216 | =head1 NAME |
217 | 217 | ||
218 | Config::HAProxy - HAProxy configuration file | 218 | Config::HAProxy - Parser for HAProxy configuration file |
219 | 219 | ||
220 | =head1 SYNOPSIS | 220 | =head1 SYNOPSIS |
221 | 221 | ||
@@ -247,7 +247,58 @@ $node = $cfg->tree; | |||
247 | 247 | ||
248 | =head1 DESCRIPTION | 248 | =head1 DESCRIPTION |
249 | 249 | ||
250 | FIXME | 250 | The B<Config::HAProxy> class is a parser that converts the B<HAProxy> |
251 | configuration file to a parse tree and provides methods for various | ||
252 | operations on this tree, such as: searching, modifying and saving it | ||
253 | to a file. | ||
254 | |||
255 | An object of this class contains a I<parse tree> representing the | ||
256 | configuration read from the file (or created from scratch). Nodes in the | ||
257 | tree can be of four distinct classes: | ||
258 | |||
259 | =over 4 | ||
260 | |||
261 | =item Empty | ||
262 | |||
263 | Represents an empty line. | ||
264 | |||
265 | =item Comment | ||
266 | |||
267 | Represents a comment line. | ||
268 | |||
269 | =item Statement | ||
270 | |||
271 | Represents a simple statement. | ||
272 | |||
273 | =item Section | ||
274 | |||
275 | A container, representing a C<compound statement>, i.e. a statement that | ||
276 | contains multiple sub-statements. Compound statements are: B<global>, | ||
277 | B<defaults>, B<frontend>, and B<backend>. | ||
278 | |||
279 | =back | ||
280 | |||
281 | In addition to these four classes, a special class B<Root> is provided, which | ||
282 | represents the topmost node in the parse tree (i.e. the parent of other nodes). | ||
283 | |||
284 | A set of attributes is associated with each node. Among these, the B<orig> | ||
285 | attribute contains the original line from the configuration file that triggered | ||
286 | creation of this node, and B<locus> contains the location of this line (or | ||
287 | lines, for sections) in the configuration file (as a B<Text::Locus>) object. | ||
288 | |||
289 | These two attributes are meaningful for all nodes. For statement nodes (simple | ||
290 | statements and sections) the B<kw> attribute contains the statement I<keyword>, | ||
291 | and the B<argv> attribute - its arguments. For example, the statement | ||
292 | |||
293 | server localhost 127.0.0.1:8080 | ||
294 | |||
295 | is represented by a node of class B<Config::HAProxy::Node::Statement>, with | ||
296 | C<server> as B<kw> and list (C<localhost>, C<127.0.0.1:8080>) as B<argv>. | ||
297 | |||
298 | Additionally, section nodes provide methods for accessing their subtrees. | ||
299 | |||
300 | For a detailed description of the node class and its methods, please refer to | ||
301 | B<Config::HAProxy::Node>. | ||
251 | 302 | ||
252 | =head1 CONSTRUCTOR | 303 | =head1 CONSTRUCTOR |
253 | 304 | ||
@@ -257,14 +308,22 @@ Creates and returns a new object for manipulating the HAProxy configuration. | |||
257 | Optional B<$filename> specifies the name of the file to read configuration | 308 | Optional B<$filename> specifies the name of the file to read configuration |
258 | from. It defaults to F</etc/haproxy/haproxy.cfg>. | 309 | from. It defaults to F</etc/haproxy/haproxy.cfg>. |
259 | 310 | ||
260 | =head1 THE PARSE TREE | 311 | =head2 filename |
312 | |||
313 | $s = $cfg->filename; | ||
314 | |||
315 | Returns 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 | ||
266 | Reads and parses the configuration file. Croaks if the file does not exist. | 323 | Reads and parses the configuration file. Croaks if the file does not exist. |
324 | Returns B<$cfg>. | ||
267 | 325 | ||
326 | =head1 BUILDING THE PARSE TREE | ||
268 | 327 | ||
269 | =head2 reset | 328 | =head2 reset |
270 | 329 | ||
@@ -305,7 +364,7 @@ Returns the last node in the tree. | |||
305 | 364 | ||
306 | $cfg->save; | 365 | $cfg->save; |
307 | 366 | ||
308 | Saves the configuration file. | 367 | Saves the parse tree in the configuration file. |
309 | 368 | ||
310 | =head2 write | 369 | =head2 write |
311 | 370 | ||
@@ -327,3 +386,34 @@ Normally, comments retain their original indentation. However, if the | |||
327 | key B<reintent_comments> is present, and its value is evaluated as true, | 386 | key B<reintent_comments> is present, and its value is evaluated as true, |
328 | then comments are reindented following the rules described above. | 387 | then comments are reindented following the rules described above. |
329 | 388 | ||
389 | =head1 SEE ALSO | ||
390 | |||
391 | B<Config::HAProxy::Node>, | ||
392 | B<Config::HAProxy::Node::Section>, | ||
393 | B<Config::HAProxy::Node::Statement>, | ||
394 | B<Config::HAProxy::Iterator>. | ||
395 | |||
396 | =head1 AUTHOR | ||
397 | |||
398 | Sergey Poznyakoff, E<lt>gray@gnu.orgE<gt> | ||
399 | |||
400 | =head1 COPYRIGHT AND LICENSE | ||
401 | |||
402 | Copyright (C) 2018 by Sergey Poznyakoff | ||
403 | |||
404 | This library is free software; you can redistribute it and/or modify it | ||
405 | under the terms of the GNU General Public License as published by the | ||
406 | Free Software Foundation; either version 3 of the License, or (at your | ||
407 | option) any later version. | ||
408 | |||
409 | It is distributed in the hope that it will be useful, | ||
410 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
411 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
412 | GNU General Public License for more details. | ||
413 | |||
414 | You should have received a copy of the GNU General Public License along | ||
415 | with this library. If not, see <http://www.gnu.org/licenses/>. | ||
416 | |||
417 | =cut | ||
418 | |||
419 | |||
diff --git a/lib/Config/HAProxy/Iterator.pm b/lib/Config/HAProxy/Iterator.pm index 6b0208e..6d80dd9 100644 --- a/lib/Config/HAProxy/Iterator.pm +++ b/lib/Config/HAProxy/Iterator.pm | |||
@@ -63,6 +63,74 @@ sub next { | |||
63 | } | 63 | } |
64 | 64 | ||
65 | 1; | 65 | 1; |
66 | __END__ | ||
67 | |||
68 | =head1 NAME | ||
69 | |||
70 | Config::HAProxy::Iterator - Iterate over objects in the parse tree | ||
71 | |||
72 | =head1 SYNOPSIS | ||
73 | |||
74 | $cfg = Config::HAProxy->new->parse; | ||
75 | $itr = $cfg->iterator(inorder => 1); | ||
76 | while (defined(my $node = $itr->next)) { | ||
77 | # Do something with $node | ||
78 | } | ||
79 | |||
80 | =head1 DESCRIPTION | ||
81 | |||
82 | The iterator object provides a method for iterating over all nodes in the | ||
83 | HAProxy parse tree. The object is returned by the B<iterator> method of | ||
84 | B<Config::HAProxy> and B<Config::HAProxy::Node> objects. The method takes | ||
85 | as optional argument the keyword specifying the order in which the tree nodes | ||
86 | should be traversed. This keyword can be one of the following: | ||
87 | |||
88 | =over 4 | ||
89 | |||
90 | =item B<recursive =E<gt> 0> | ||
91 | |||
92 | No recursion. The traversal will not descend into section nodes. This is the | ||
93 | default. | ||
94 | |||
95 | =item B<inorder =E<gt> 1> | ||
96 | |||
97 | The nodes will be traversed in the inorder manner, i.e. the section node | ||
98 | will be visited first, and all its sub-nodes after it. | ||
99 | |||
100 | =item B<postorder =E<gt> 1> | ||
101 | |||
102 | The nodes will be traversed in the postorder manner, i.e. for each section | ||
103 | node, its sub-nodes will be visited first, and the node itself afterward. | ||
104 | |||
105 | =back | ||
106 | |||
107 | =head1 CONSTRUCTOR | ||
108 | |||