Backend/Augeas.pm untested

filter: Statementstmt 0.0% 0 / 250 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 78 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 477 CC 38 SCAR 46.2
CC
38
Cov
0%
read
272.0
list_element_cb
72.0
hash_element_cb
42.0
CRAP 101.1
1 # Copyright (c) 2008-2011 Dominique Dumont.
2 #
3 # This file is part of Config-Model-Backend-Augeas.
4 #
5 # Config-Model is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser Public License as
7 # published by the Free Software Foundation; either version 2.1 of
8 # the License, or (at your option) any later version.
9 #
10 # Config-Model is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser Public License
16 # along with Config-Model; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  
19 package Config::Model::Backend::Augeas;
20 use Mouse;
21 use Carp;
22 use Log::Log4perl qw(get_logger :levels);
23  
24 extends 'Config::Model::Backend::Any';
25 with "Config::Model::Role::FileHandler";
26  
27 use Config::Model::Exception;
28  
29 use File::Path;
30 use Log::Log4perl qw(get_logger :levels);
31  
32 my $has_augeas = 1;
33 eval { require Config::Augeas; };
34 $has_augeas = 0 if $@;
35  
36 our $VERSION = '0.126';
37  
38 my $logger = get_logger('Backend::Augeas');
39  
40 =head1 NAME
41  
42 Config::Model::Backend::Augeas - Read and write config data through Augeas
43  
44 =head1 SYNOPSIS
45  
46 # model specification with augeas backend
47 {
48 config_class_name => 'OpenSsh::Sshd',
49  
50 read_config => {
51 backend => 'augeas' ,
52 file => '/etc/ssh/sshd_config',
53 # declare "seq" Augeas elements
54 sequential_lens => [/AcceptEnv AllowGroups [etc]/],
55 },
56  
57 # config is written back using read_config specifications
58  
59 element => ...
60 }
61  
62 =head1 DESCRIPTION
63  
64 This class provides a way to load or store configuration data through
65 L<Config::Augeas>. This way, the structure and comments of the
66 original configuration file are preserved.
67  
68 To use Augeas as a backend, you must specify the following
69 C<read_config> parameters:
70  
71 =over
72  
73 =item backend
74  
75 Use C<augeas> (or C<Augeas>)in this case.
76  
77 =item save
78  
79 Either C<backup> or C<newfile>. See L<Config::Augeas/Constructor> for
80 details.
81  
82 =item file
83  
84 Name of the configuration file.
85  
86 =item sequential_lens
87  
88 This one is tricky. Set to one when new Augeas list or hash node must
89 be created for each new list or hash element. See L</Sequential lens>
90 for details.
91  
92 =back
93  
94 For instance:
95  
96 read_config => {
97 backend => 'augeas' ,
98 save => 'backup',
99 file => '/etc/ssh/sshd_config',
100 # declare "seq" Augeas elements
101 sequential_lens => [/AcceptEnv AllowGroups/],
102 },
103  
104 =head2 Sequential lens
105  
106 Some configuration files feature data that must be written as list or
107 as hash. Depending on the syntax, Augeas list or hash lenses can be
108 written so that new "container" nodes are required for each new element.
109  
110 For instance, C<HostKey> lines can be repeated several times in
111 C<sshd_config>. Since Augeas must keep track of these several lines,
112 Augeas tree will be written like:
113  
114 /files/etc/ssh/sshd_config/HostKey[1]
115 /files/etc/ssh/sshd_config/HostKey[2]
116 /files/etc/ssh/sshd_config/HostKey[3]
117  
118 and not:
119  
120 /files/etc/ssh/sshd_config/HostKey/1
121 /files/etc/ssh/sshd_config/HostKey/2
122 /files/etc/ssh/sshd_config/HostKey/3
123  
124 The C<HostKey> node is created several times. A new hostkey must be
125 added with the following syntax:
126  
127 /files/etc/ssh/sshd_config/HostKey[4]
128  
129 and not:
130  
131 /files/etc/ssh/sshd_config/HostKey/4
132  
133 So the C<HostKey> lens is sequential.
134  
135 The situation is more complex when syntax allow repeated values on
136 several lines. Like:
137  
138 AcceptEnv LC_PAPER LC_NAME LC_ADDRESS
139 AcceptEnv LC_IDENTIFICATION LC_ALL
140  
141 Augeas will have this tree:
142  
143 /files/etc/ssh/sshd_config/AcceptEnv[1]/1
144 /files/etc/ssh/sshd_config/AcceptEnv[1]/2
145 /files/etc/ssh/sshd_config/AcceptEnv[1]/3
146 /files/etc/ssh/sshd_config/AcceptEnv[2]/4
147 /files/etc/ssh/sshd_config/AcceptEnv[2]/5
148  
149 Note that the first index between square brackets keeps track of how
150 the C<AcceptEnv> items are grouped, but the I<real> list index is
151 after the slash.
152  
153 Augeas does not require new elements to create C<AcceptEnv[3]>. A new
154 element can be added as :
155  
156 /files/etc/ssh/sshd_config/AcceptEnv[2]/6
157  
158 So this lens is not sequential.
159  
160 The same kind of trouble occurs with hash elements. Some hashes tree
161 are like:
162  
163 /files/etc/foo/my_hash/my_key1
164 /files/etc/foo/my_hash/my_key2
165  
166 Others are like:
167  
168 /files/etc/foo/my_hash[1]/my_key1
169 /files/etc/foo/my_hash[2]/my_key2
170  
171 Note that a list-like index is used with the hash key.
172  
173 This also depends on the syntax of the configuration file. For
174 instance, C<Subsystem> in C<sshd_config> can be :
175  
176 Subsystem sftp /usr/lib/openssh/sftp-server
177 Subsystem fooftp /usr/lib/openssh/fooftp-server
178 Subsystem barftp /usr/lib/openssh/barftp-server
179  
180  
181 This (unvalid) sshd configuration is represented by:
182  
183 /files/etc/ssh/sshd_config/Subsystem[1]/sftp
184 /files/etc/ssh/sshd_config/Subsystem[2]/fooftp
185 /files/etc/ssh/sshd_config/Subsystem[3]/barftp
186  
187 Any new Subsystem must be added with:
188  
189 /files/etc/ssh/sshd_config/Subsystem[4]/bazftp
190  
191 In this case, the hash is also sequential.
192  
193 For these examples, the augeas backend declaration must feature:
194  
195 sequential_lens => [qw/HostKey Subsystem/],
196  
197 =head2 Augeas backend limitation
198  
199 The structure and element names of the Config::Model tree must match
200 the structure defined in Augeas lenses. I.e. the order of the element
201 declared in Config::Model must match the order required by Augeas
202 lenses.
203  
204 Sometimes, the structure of a file loaded by Augeas starts directly
205 with a list of items. For instance C</etc/hosts> structure starts with
206 a list of lines that specify hosts and IP addresses. The C<set_in>
207 parameter specifies an element name in Config::Model root class that
208 will hold the configuration data retrieved by Augeas.
209  
210 =cut
211  
212 # for tests only
213 sub _augeas_object { return shift->{augeas_obj}; }
214  
215 sub read {
216 my $self = shift;
217 my %args = @_; # contains root and config_dir
218 return 0 unless $has_augeas;
219  
220 $self->{augeas_obj} ||= Config::Augeas->new(
221 root => $args{root},
222 save => $args{save} );
223  
224 if ( defined $args{config_file} ) {
225 warn "Augeas::read : deprecated config_file parameter, use file instead\n";
226 $args{file} ||= delete $args{config_file};
227 }
228  
229 foreach my $param (qw/config_dir file/) {
230 if ( not defined $args{$param} ) {
231 Config::Model::Exception::Model->throw(
232 error => "read_augeas error: model " . "does not specify '$param' for Augeas ",
233 object => $self->{node} );
234 }
235 }
236  
237 my $cdir = $self->get_tuned_config_dir(%args);
238 $logger->info( "Read config data through Augeas in directory '$cdir' " . "file $args{file}" );
239  
240 my $mainpath = '/files' . $args{config_dir} . '/'. $args{file};
241  
242 my @result = $self->augeas_deep_match($mainpath);
243 my @cm_path = @result;
244  
245 # cleanup resulting path to remove Augeas '/files', remove the
246 # file path and plug the remaining path where it is consistent in
247 # the model. I.e if the file "root" matches a list element (like
248 # for /etc/hosts), get this element name from "set_in" parameter
249 my $set_in = $args{set_in} || '';
250 foreach my $path (@cm_path) {
251 $path =~ s!$mainpath!!;
252 $path = "/$set_in/$path" if $set_in;
253 $path =~ s!/+!/!g;
254 }
255  
256 # Create a hash of sequential lenses
257 my %is_seq_lens = map { ( $_ => 1 ); } @{ $args{sequential_lens} || [] };
258  
259 my $augeas_obj = $self->{augeas_obj};
260  
261 # this may break as data will be written in the tree in an order
262 # decided by Augeas. This may break complex model with warping as
263 # the best writing order is indicated by the model stored in
264 # Config::Model and not by Augeas.
265 while (@result) {
266 my $aug_p = shift @result;
267 my $cm_p = shift @cm_path; # Config::Model path
268 my $v = $augeas_obj->get($aug_p);
269 next unless defined $v;
270  
271 $logger->debug("read-augeas $aug_p, will set C::M path '$cm_p' with '$v'");
272  
273 $cm_p =~ s!^/!!;
274  
275 # With some list, we can get
276 # /files/etc/ssh/sshd_config/AcceptEnv[1]/1/ = LC_PAPER
277 # /files/etc/ssh/sshd_config/AcceptEnv[1]/2/ = LC_NAME
278 # /files/etc/ssh/sshd_config/AcceptEnv[2]/3/ = LC_ADDRESS
279 # /files/etc/ssh/sshd_config/AcceptEnv[2]/4/ = LC_TELEPHONE
280  
281 # Depending on the syntax, list can be in the form:
282 # /files/etc/ssh/sshd_config/AcceptEnv[2]/3/ non-seq ditch idx
283 # /files/etc/hosts/4/ non-seq
284 # /files/etc/ssh/sshd_config/HostKey[2] is-seq keep idx
285  
286 # Likewise, hashes can be in the form
287 # /files/etc/ssh/sshd_config/Subsystem[2]/foo/ is-seq ditch idx
288 # /files/etc/ssh/sshd_config/Bar/foo/ non-seq
289  
290 my @cm_steps = split m!/+!, $cm_p;
291 my $obj = $self->{node};
292 $obj = $obj->fetch_element( shift @cm_steps ) if $set_in;
293  
294 while ( my $step = shift @cm_steps ) {
295 my ( $label, $idx ) = ( $step =~ /(\w+)(?:\[(\d+)\])?/ );
296 my $is_seq = $is_seq_lens{$label} || 0;
297 $logger->debug( "read-augeas: step label $label "
298 . ( defined $idx ? "idx $idx " : '' )
299 . "(is_seq $is_seq)" );
300  
301 # idx will be treated next iteration if needed
302 if ( $obj->get_type eq 'node'
303 and $obj->element_type($label) eq 'list'
304 and $is_seq ) {
305 $idx = 1 unless defined $idx;
306 $logger->debug("read-augeas: keep seq lens idx $idx");
307 unshift @cm_steps, $idx;
308 }
309  
310 if ( $label =~ /\[/ ) {
311 Config::Model::Exception::Model->throw(
312 error => "read_augeas error: can't use $label with "
313 . "Augeas index in Config::Model. $label should "
314 . "probably be listed as 'sequential_lens'",
315 object => $self->{node} );
316 }
317  
318 # augeas list begin at 1 not 0
319 my $type = $obj->get_type ;
320 $label -= 1 if $obj->get_type eq 'list';
321 if ( scalar @cm_steps > 0 ) {
322 $logger->debug("read-augeas: get $label");
323 $obj = $obj->get($label);
324 }
325 elsif ($type eq 'check_list') {
326 $logger->debug("read-augeas: set check_list $label $v");
327 $obj->check( $v );
328 }
329 elsif ($type eq 'leaf') {
330 $logger->debug("read-augeas: set leaf $label $v");
331 $obj->store( $v );
332 }
333 else {
334 # last step
335 $logger->debug("read-augeas: set $type $label $v");
336 $obj->set( $label , $v );
337 }
338  
339 if ( not defined $obj ) {
340 Config::Model::Exception::Model->throw(
341 error => "read_augeas error: '$cm_p' led to undef object. "
342 . "Check for errors in 'sequential_lens' specification",
343 object => $self->{node} );
344 }
345 }
346 }
347  
348 return 1;
349 }
350  
351 sub augeas_deep_match {
352 my ( $self, $mainpath ) = @_;
353  
354 # work around Augeas feature where '*' matches only one hierarchy
355 # level
356 # See https://www.redhat.com/archives/augeas-devel/2008-July/msg00016.html
357 my @worklist = ($mainpath);
358 my $logger = get_logger('Data::Read');
359 $logger->debug("read-augeas on @worklist");
360  
361 my $augeas_obj = $self->{augeas_obj};
362 my @result;
363 while (@worklist) {
364 my $p = pop @worklist;
365  
366 # filter out comments
367 # see http://augeas.net/page/Path_expressions
368 my @newpath = $augeas_obj->match( $p . "/*[label() != '#comment']" );
369 $logger->debug("read-augeas $p/* matches paths: @newpath");
370 push @worklist, @newpath;
371 push @result, @newpath;
372 }
373  
374 return @result;
375 }
376  
377 # this is a bit of a hack. This function is called by Autoread to
378 # check whether the configuration file should be opened before calling
379 # write or not. If the config file is opened before augeas writes in
380 # it, all comments and structure is lost.
381 sub skip_open { 1; }
382  
383 sub write {
384 my $self = shift;
385 my %args = @_; # contains root and config_dir
386 return 0 unless $has_augeas;
387  
388 if ( defined $args{config_file} ) {
389 warn "Augeas::write : deprecated config_file parameter, use file instead\n";
390 $args{file} ||= delete $args{config_file};
391 }
392  
393 foreach my $param (qw/config_dir file/) {
394 if ( not defined $args{$param} ) {
395 Config::Model::Exception::Model->throw(
396 error => "write_augeas error: model " . "does not specify '$param' for Augeas ",
397 object => $self->{node} );
398 }
399 }
400  
401 my $cdir = $self->get_tuned_config_dir(%args);
402 get_logger("Data::Write")
403 ->info( "Write config data through Augeas in directory '$cdir' " . "file $args{file}" );
404  
405 my $set_in = $args{set_in} || '';
406 my $mainpath = '/files' . $args{config_dir} . '/' . $args{file};
407 my $augeas_obj = $self->{augeas_obj} ||= Config::Augeas->new(
408 root => $args{root},
409 save => $args{save} );
410  
411 my %to_set = $self->copy_in_augeas( $augeas_obj, $mainpath, $set_in, $args{sequential_lens} );
412  
413 $self->save($mainpath);
414 }
415  
416 sub save {
417 my $self = shift;
418 my $mainpath = shift;
419  
420 # can't use augeas print directly in logging system...
421 $self->{augeas_obj}->print() if get_logger('Data::Write')->is_debug;
422  
423 $self->{augeas_obj}->save
424 || die "Augeas save failed" . $self->{augeas_obj}->print("/augeas/$mainpath/*");
425 }
426  
427 sub copy_in_augeas {
428 my $self = shift;
429 my $augeas_obj = shift;
430 my $mainpath = shift;
431 my $set_in = shift;
432 my $seq_list = shift || [];
433 my %is_seq_lens = map { ( $_ => 1 ); } @$seq_list;
434  
435 # The callback are kludgy and may be improved when the
436 # following bugs are fixed:
437 # https://fedorahosted.org/augeas/ticket/23
438 # https://fedorahosted.org/augeas/ticket/24
439  
440 my @scan_args = (
441 fallback => 'all',
442 auto_vivify => 0,
443 list_element_cb => \&list_element_cb,
444 check_list_element_cb => \&std_cb,
445 hash_element_cb => \&hash_element_cb,
446 leaf_cb => \&std_cb,
447 node_content_cb => \&node_content_cb,
448 );
449  
450 # perform the scan
451 my $view_scanner = Config::Model::ObjTreeScanner->new(@scan_args);
452  
453 $view_scanner->scan_node( [ $mainpath, $augeas_obj, $set_in, \%is_seq_lens ], $self->{node} );
454 }
455  
456 sub std_cb {
457 my ( $scanner, $data_ref, $obj, $element, $index, $value_obj ) = @_;
458 my ( $p, $augeas_obj, $set_in, $is_seq_lens ) = @$data_ref;
459  
460 my $v = $value_obj->fetch();
461 if ( defined $v and $v ne '' ) {
462 get_logger("Data::Write")->info("copy_in_augeas: set $p = '$v'");
463 $augeas_obj->set( $p, $v );
464  
465 #$self->save($mainpath) if $::debug ;
466 }
467 else {
468 $augeas_obj->remove($p);
469 }
470 }
471  
472 sub list_element_cb {
473 my ( $scanner, $data_ref, $node, $element_name, @idx ) = @_;
474 my ( $p, $augeas_obj, $set_in, $is_seq_lens ) = @$data_ref;
475  
476 my $is_seq = $is_seq_lens->{$element_name} || 0;
477  
478 # the idea is to compare list indexes from Config::Model with the
479 # corresponding hash-like keys in Augeas tree
480  
481 # find Augeas nodes matching this path
482 my @matches = $augeas_obj->match( $p . "[label() != '#comment']" );
483  
484 # need to find 2nd levels of sub-nodes for non-seq list lenses
485 @matches = sort map { $augeas_obj->match( $_ . "/*[label() != '#comment']" ); } @matches
486 unless $is_seq;
487  
488 # Depending on the syntax, list can be in the form:
489 # /files/etc/ssh/sshd_config/AcceptEnv[2]/3/ non-seq use [last()]/idx/
490 # /files/etc/hosts/4/ non-seq use [last()]/idx/
491 # /files/etc/ssh/sshd_config/HostKey[2] is-seq use [idx]
492  
493 if ($is_seq) {
494  
495 # sequential lens need a list index to store list element.
496 # I.e foo[1]/1 foo[1]/2 foo[2]/3 is ok. foo/1 foo/2 will
497 # fail. But Augeas does return foo/1 if only one element is
498 # present in the tree :-/
499 my $replace = $element_name . '[1]';
500 foreach (@matches) {
501 s/$element_name(?!\[)/$replace/;
502 }
503 }
504  
505 my $logger = get_logger("Data::Write");
506 $logger->debug( "copy_in_augeas: List (@idx) path $p matches (seq $is_seq):\n\t"
507 . join( "\n\t", @matches ) );
508  
509 # store list indexes found in Augeas and their corresponding path
510 my %match = map {
511 my ($k) = m!/([\w\[\]\-]+)$!;
512  
513 # need to keep only index in %match key
514 $k =~ s/\w+\[(\d+)\]$/$1/ if $is_seq;
515 ( $k => $_ );
516 } @matches;
517  
518 # Handle indexes found in Config::Model, but not in Augeas
519 # tree. Create a new Augeas path for sequential list lenses. This
520 # path will be used by scan_list
521 if ($is_seq) {
522 my $count = $augeas_obj->count_match( $p . "[label() != '#comment']" );
523 foreach my $idx (@idx) {
524  
525 # augeas index starts at 1 not 0
526 my $i = $idx + 1;
527 next if defined $match{$i};
528  
529 $match{$i} = $p . '[' . ++$count . "]" . ( $is_seq ? '' : "/$i" );
530  
531 $logger->debug( "copy_in_augeas: New list path $match{$i} " . "for index $i" );
532 }
533 }
534  
535 # now scan the elements stored by Config::Model hash keys to
536 # store the hash values
537 foreach my $i (@idx) {
538  
539 # use Augeas path (given by match command) or the path
540 # created for unknown non-seq list elements
541 my $scan_path = delete $match{ $i + 1 } || $p . '/' . ( $i + 1 );
542 $logger->debug("copy_in_augeas: scan list called on $scan_path index $i");
543 $scanner->scan_list( [ $scan_path, $augeas_obj, $set_in, $is_seq_lens ],
544 $node, $element_name, $i );
545 }
546  
547 # cleanup indexes found in Augeas but not in Config::Model
548 foreach ( keys %match ) {
549 my $rm_path = $match{$_};
550  
551 $logger->debug("copy_in_augeas: List rm $_ ->$rm_path");
552 $augeas_obj->remove($rm_path) || die "remove $rm_path failed";
553  
554 # check if removing parent node in Augeas is needed
555 $rm_path =~ s!/([\w\[\]\-]+)$!!; # chomp last "step" of the path
556 if ( $augeas_obj->count_match( $rm_path . "[label() != '#comment']" ) == 1
557 and $set_in ne $element_name
558 and $rm_path =~ /$element_name$/ ) {
559 $logger->debug("copy_in_augeas: List rm parent node $_ ->$rm_path");
560 $augeas_obj->remove($rm_path) || die "remove $rm_path failed";
561 }
562 }
563 }
564  
565 # this callback is similar but not identical to the list callback.
566 sub hash_element_cb {
567 my ( $scanner, $data_ref, $node, $element_name, @keys ) = @_;
568 my ( $p, $augeas_obj, $set_in, $is_seq_lens ) = @$data_ref;
569 my $is_seq = $is_seq_lens->{$element_name} || 0;
570  
571 # the idea is to compare hash keys from Config::Model with the
572 # corresponding hash-like keys in Augeas tree
573  
574 # find Augeas nodes matching this path
575 my @matches = $augeas_obj->match( $p . "[label() != '#comment']" );
576  
577 # need to find 2nd levels of sub-nodes
578 @matches = sort map { $augeas_obj->match( $_ . "/*[label() != '#comment']" ); } @matches;
579  
580 # sequential lens need a list index to store element. I.e
581 # foo[1]/key1 foo[2]/key2 is ok. foo/key1 foo/key2 will fail But
582 # Augeas does return foo/key1 if only one element is present in
583 # the tree :-/
584 if ($is_seq) {
585 my $replace = $element_name . '[1]';
586 foreach (@matches) {
587 s/$element_name(?!\[)/$replace/;
588 }
589 }
590  
591 my $logger = get_logger('Data::Write');
592 $logger->debug(
593 "copy_in_augeas: Hash path $p matches (seq $is_seq):\n\t" . join( "\n\t", @matches ) );
594  
595 # store indexes found in Augeas and their corresponding path
596 my %match = map {
597 my ($k) = m!/([\w\[\]\-]+)$!;
598  
599 # need to keep only index in %match key
600 $k =~ s/\w+\[(\d+)\]$/$1/ if $is_seq;
601 ( $k => $_ );
602 } @matches;
603  
604 # Handle keys found in Config::Model, but not in Augeas
605 # tree. Create a new Augeas path for sequential hash lenses. This
606 # path will be used by scan_list. This insertion cannot be done if
607 # no elements are already present in Augeas tree.
608 if ( $is_seq and @matches ) {
609 my $count = $augeas_obj->count_match( $p . "[label() != '#comment']" );
610 foreach (@keys) {
611 next if defined $match{$_};
612  
613 my $ip = $p . '[last()]';
614 $logger->debug("inserting $element_name after $ip\n");
615 $augeas_obj->insert( $element_name, after => $ip )
616 || die "augeas insert $element_name after $ip failed";
617  
618 my $np = $match{$_} = $p . '[' . ++$count . "]/$_";
619 $logger->debug("copy_in_augeas: New hash path $np for key $_");
620 }
621 }
622  
623 # now scan the elements stored by Config::Model hash keys to
624 # store the hash values
625 foreach (@keys) {
626  
627 # use Augeas path (given by match command) or a new path for
628 # new elements
629 my $scan_path = delete $match{$_} || $p . "/$_";
630 $scanner->scan_hash( [ $scan_path, $augeas_obj, $set_in, $is_seq_lens ],
631 $node, $element_name, $_ );
632 }
633  
634 # cleanup keys found in Augeas but not in Config::Model
635 foreach ( keys %match ) {
636 my $rm_path = $match{$_};
637 $logger->debug("copy_in_augeas: Hash rm $_ ->$rm_path");
638 $augeas_obj->remove($rm_path) || die "remove $rm_path failed";
639  
640 # check if removing parent node in Augeas is needed
641 $rm_path =~ s!/([\w\[\]\-]+)$!!;
642 if ( $augeas_obj->count_match( $rm_path . "[label() != '#comment']" ) == 1
643 and $set_in ne $element_name
644 and $is_seq ) {
645 $logger->debug("copy_in_augeas: Hash rm parent $_ ->$rm_path");
646 $augeas_obj->remove($rm_path) || die "remove $rm_path failed";
647 }
648 }
649 }
650  
651 sub node_content_cb {
652 my ( $scanner, $data_ref, $node, @element ) = @_;
653 my ( $p, $augeas_obj, $set_in, $is_seq_lens ) = @$data_ref;
654  
655 my $logger = get_logger('Data::Write');
656  
657 # See set_in parameter (who said kludge ?)
658 if ( scalar @element == 1 and $element[0] eq $set_in ) {
659  
660 # Augeas tree is stored below element[0]
661 $logger->debug("copy_in_augeas: Augeas tree set in node path $p");
662 $scanner->scan_element( [ $p, $augeas_obj, $set_in, $is_seq_lens ], $node, $element[0] );
663 }
664 else {
665 my @matches = $augeas_obj->match( $p . "/*[label() != '#comment']" );
666  
667 # cleanup indexes are we don't handle them now with element
668 # (later in lists and hashes)
669 foreach (@matches) {
670 s/\[\d+\]+$//;
671 }
672 $logger->debug( "copy_in_augeas: Node path $p matches:\n\t" . join( "\n\t", @matches ), );
673  
674 # store elements found in Augeas and their corresponding path
675 my %match = map {
676 my ($elt) = m!/([\w\-]+)$!;
677 ( $elt => $_ )
678 } @matches;
679  
680 # Handle element found in Config::Model, but not in Augeas
681 # tree. Create a new Augeas path for new elements respecting
682 # the order of the elements declared in Config::Model. This
683 # path will be used by scan_element. This insertion cannot
684 # be done if no elements are already present in Augeas tree.
685 if (@matches) {
686 my $previous_match = '';
687 foreach (@element) {
688 if ( defined $match{$_} ) {
689 $previous_match = $_;
690 }
691 elsif ( $node->fetch_element($_)->dump_as_data ) {
692  
693 # insert in Augeas only if the element contains
694 # something interesting
695 my ( $direction, $ip ) =
696 $previous_match
697 ? ( after => $p . '/' . $previous_match . '[last()]' )
698 : ( before => $matches[0] );
699  
700 $logger->debug("inserting $_ $direction $ip");
701 $augeas_obj->insert( $_, $direction => $ip )
702 || die "augeas insert $_ $direction $ip failed";
703  
704 my $np = $match{$_} = "$p/$_";
705 $logger->debug("copy_in_augeas: New hash path $np for element $_");
706 }
707 }
708 }
709  
710 # now scan the elements stored by Config::Model elements to
711 # store the children nodes
712 foreach (@element) {
713  
714 # use Augeas path (given by match command) or the path
715 # created for new elements
716 my $scan_path = delete $match{$_} || $p . '/' . $_;
717 $logger->debug("copy_in_augeas: Node scan $scan_path for element $_");
718 $scanner->scan_element( [ $scan_path, $augeas_obj, $set_in, $is_seq_lens ], $node, $_ );
719 }
720  
721 # cleanup keys found in Augeas but not in Config::Model
722 foreach ( keys %match ) {
723 my $rm_path = $match{$_};
724 $logger->debug("copy_in_augeas: Node rm $_ ->$rm_path");
725 $augeas_obj->remove($rm_path) || die "remove $rm_path failed";
726 }
727 }
728 }
729  
730 1;
731  
732 =head1 Log and trace
733  
734 This module use L<Log::Log4perl> to log debug and info trace with
735 C<Data::Read> and C<Data::Write> categories.
736  
737 =head1 CAVEATS
738  
739 =over
740  
741 =item *
742  
743 Augeas C<#comment> nodes are ignored
744  
745 =back
746  
747 =head1 SEE ALSO
748  
749 =over
750  
751 =item *
752  
753 http://augeas.net/ : Augeas project page
754  
755 =item *
756  
757 config-model wiki: L<http://github.com/dod38fr/config-model/wiki>
758  
759 =item *
760  
761 Blogs about this project: L<https://ddumont.wordpress.com/category/perl/configmodel/>
762  
763 =item *
764  
765 Augeas mailing list: http://augeas.net/developers.html
766  
767 =back
768  
769 =head1 AUTHOR
770  
771 Dominique Dumont, E<lt>ddumont at cpan dot org@<gt>
772  
773 =head1 COPYRIGHT
774  
775 Copyright (C) 2008-2010 by Dominique Dumont
776  
777 =head1 LICENSE
778  
779 This library is free software; you can redistribute it and/or modify
780 it under the LGPL terms.
781  
782 =cut