diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Config/HAProxy.pm | 140 | ||||
-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 | 52 | ||||
-rw-r--r-- | lib/Config/HAProxy/Node/Section.pm | 153 | ||||
-rw-r--r-- | lib/Config/HAProxy/Node/Statement.pm | 45 |
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 | ||
193 | sub backup_name { | 193 | sub backup_name { |
194 | my $self = shift; | 194 | my $self = shift; |
@@ -212,63 +212,122 @@ sub backup { | |||
212 | 212 | ||
213 | 1; | 213 | 1; |
214 | __END__ | 214 | __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 | ||
222 | use 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); |
231 | while (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 | ||
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 | ||
254 | $cfg = new Config::HAProxy($filename); | 305 | $cfg = new Config::HAProxy($filename); |
255 | 306 | ||
256 | Creates and returns a new object for manipulating the HAProxy configuration. | 307 | 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 | ||
268 | 326 | =head1 BUILDING THE PARSE TREE | |
327 | |||
269 | =head2 reset | 328 | =head2 reset |
270 | 329 | ||
271 | $cfg->reset; | 330 | $cfg->reset; |
272 | 331 | ||
273 | Clears the parse tree. | 332 | Clears 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 | ||
308 | Saves the configuration file. | 367 | Saves 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 | ||
314 | Writes configuration to the named file or file handle. If B<$file> is the | 373 | Writes 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 | |||
324 | the resulting output, preserving their original offsets. | 383 | the resulting output, preserving their original offsets. |
325 | 384 | ||
326 | Normally, comments retain their original indentation. However, if the | 385 | 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>, | ||