blib/lib/Rex/Augeas.pm untested

filter: Statementstmt 0.0% 0 / 158 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 245 CC 27 SCAR 62.6
CC
27
Cov
0%
augeas
756.0
BEGIN
2.0
BEGIN
2.0
CRAP 524.0
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4 # vim: set ts=3 sw=3 tw=0:
5 # vim: set expandtab:
6  
7 =head1 NAME
8  
9 Rex::Augeas - An augeas module for (R)?ex
10  
11 =head1 DESCRIPTION
12  
13 This is a simple module to manipulate configuration files with the help of augeas.
14  
15 =head1 SYNOPSIS
16  
17 my $k = augeas exists => "/files/etc/hosts/*/ipaddr", "127.0.0.1";
18
19 augeas insert => "/files/etc/hosts",
20 label => "01",
21 after => "/7",
22 ipaddr => "192.168.2.23",
23 canonical => "test";
24
25 augeas dump => "/files/etc/hosts";
26  
27  
28 =head1 EXPORTED FUNCTIONS
29  
30 =over 4
31  
32 =cut
33  
34 package Rex::Augeas;
35  
36 use strict;
37 use warnings;
38  
39 require Exporter;
40  
41 our $VERSION = "0.2.0";
42  
43 use base qw(Exporter);
44 use vars qw(@EXPORT);
45  
46 use Rex::Logger;
47 use Rex::Commands;
48 use Rex::Commands::Run;
49 use Rex::Commands::Fs;
50 use Rex::Commands::File;
51  
52 use Config::Augeas;
53 use IO::String;
54  
55 @EXPORT = qw(augeas);
56  
57 =item augeas($action, @options)
58  
59 It returns 1 on success and 0 on failure.
60  
61 Actions:
62  
63 =over 4
64  
65 =cut
66  
67 sub augeas {
68 my ($action, @options) = @_;
69 my $ret;
70  
71 Rex::Logger::debug("Creating Config::Augeas Object");
72 my $aug = Config::Augeas->new;
73  
74 my $is_ssh = Rex::is_ssh();
75  
76 =item modify
77  
78 This modifies the keys given in @options in $file.
79  
80 augeas modify =>
81 "/files/etc/hosts/7/ipaddr" => "127.0.0.2",
82 "/files/etc/hosts/7/canonical" => "test01";
83  
84 =cut
85  
86 if($action eq "modify") {
87 my $config_option = { @options };
88  
89 for my $key (keys %{$config_option}) {
90 my $aug_key = $key;
91 Rex::Logger::debug("modifying $aug_key -> " . $config_option->{$key});
92  
93 my $_r;
94 if($is_ssh) {
95 run 'echo "set ' . $aug_key . ' ' . $config_option->{$key} . '" | augtool -s';
96 if($? == 0) {
97 $_r = 1;
98 }
99 else {
100 $_r = 0;
101 }
102 }
103 else {
104 $_r = $aug->set($aug_key, $config_option->{$key});
105 }
106 Rex::Logger::debug("Augeas set status: $_r");
107 }
108  
109 $ret = $aug->save;
110 }
111  
112 =item remove
113  
114 Remove an entry.
115  
116 augeas remove => "/files/etc/hosts/2";
117  
118 =cut
119  
120 elsif($action eq "remove") {
121 for my $key (@options) {
122 my $aug_key = $key;
123 Rex::Logger::debug("deleting $aug_key");
124  
125 my $_r;
126 if($is_ssh) {
127 run "echo 'rm $aug_key' | augtool -s";
128 if($? == 0) {
129 $_r = 1;
130 }
131 else {
132 $_r = 0;
133 }
134 }
135 else {
136 $_r = $aug->remove($aug_key);
137 }
138 Rex::Logger::debug("Augeas delete status: $_r");
139 }
140  
141 $ret = $aug->save;
142 }
143  
144 =item insert
145  
146 Insert an item into the file. Here, the order of the options is important. If the order is wrong it won't save your changes.
147  
148 augeas insert => "/files/etc/hosts",
149 label => "01",
150 after => "/7",
151 ipaddr => "192.168.2.23",
152 alias => "test02";
153  
154 =cut
155  
156 elsif($action eq "insert") {
157 my $file = shift @options;
158 my $opts = { @options };
159 my $label = $opts->{"label"};
160 delete $opts->{"label"};
161  
162 if($is_ssh) {
163 my $position = (exists $opts->{"before"}?"before":"after");
164 unless(exists $opts->{$position}) {
165 Rex::Logger::info("Error inserting key. You have to specify before or after.");
166 return 0;
167 }
168
169 delete $opts->{$position};
170  
171 my $aug_commands = "ins $label $position " . $opts->{$position} . "\n";
172  
173 for(my $i=0; $i < @options; $i+=2) {
174 my $key = $options[$i];
175 my $val = $options[$i+1];
176 next if($key eq "after" or $key eq "before" or $key eq "label");
177  
178 my $_key = "$file/$label/$key";
179 Rex::Logger::debug("Setting $_key => $val");
180  
181 $aug_commands .= "set $_key $val\n";
182 }
183  
184 $aug_commands .= "save\n";
185  
186 my $tmp_file = "/tmp/" . get_random(12, 'a'..'z', 0..9) . '.tmp';
187 my $fh = file_write $tmp_file;
188 $fh->write($aug_commands);
189 $fh->close;
190  
191 run "cat $tmp_file | augtool";
192  
193 $ret = 0;
194 if($? == 0) {
195 $ret = 1;
196 }
197  
198 unlink "$tmp_file";
199 }
200 else {
201 if(exists $opts->{"before"}) {
202 $aug->insert($label, before => "$file" . $opts->{"before"});
203 delete $opts->{"before"};
204 }
205 elsif(exists $opts->{"after"}) {
206 my $t = $aug->insert($label, after => "$file" . $opts->{"after"});
207 delete $opts->{"after"};
208 }
209 else {
210 Rex::Logger::info("Error inserting key. You have to specify before or after.");
211 return 0;
212 }
213  
214 for(my $i=0; $i < @options; $i+=2) {
215 my $key = $options[$i];
216 my $val = $options[$i+1];
217  
218 next if($key eq "after" or $key eq "before" or $key eq "label");
219  
220 my $_key = "$file/$label/$key";
221 Rex::Logger::debug("Setting $_key => $val");
222  
223 $aug->set($_key, $val);
224 }
225  
226 $ret = $aug->save();
227 }
228 }
229  
230 =item dump
231  
232 Dump the contents of a file to STDOUT.
233  
234 augeas dump => "/files/etc/hosts";
235  
236 =cut
237  
238 elsif($action eq "dump") {
239 my $file = shift @options;
240 my $aug_key = $file;
241  
242 if($is_ssh) {
243 my @list = run "augtool print $aug_key";
244 print join("\n", @list) . "\n";
245 }
246 else {
247 $aug->print($aug_key);
248 }
249 $ret = 0;
250 }
251  
252 =item exists
253  
254 Check if an item exists.
255  
256 my $exists = augeas exists => "/files/etc/hosts/*/ipaddr" => "127.0.0.1";
257 if($exists) {
258 say "127.0.0.1 exists!";
259 }
260  
261 =cut
262  
263 elsif($action eq "exists") {
264 my $file = shift @options;
265  
266 my $aug_key = $file;
267 my $val = $options[0] || "";
268  
269 if($is_ssh) {
270 my @paths = grep { s/\s=[^=]+$// } run "echo 'match $aug_key' | augtool";
271  
272 if($val) {
273 for my $k (@paths) {
274 my @ret = grep { s/^[^=]+=\s// } run "echo 'get $k' | augtool";
275
276 if($ret[0] eq $val) {
277 return $k;
278 }
279 }
280 }
281 else {
282 return @paths;
283 }
284  
285 $ret = undef;
286 }
287 else {
288 my @paths = $aug->match($aug_key);
289  
290 if($val) {
291 for my $k (@paths) {
292 if($aug->get($k) eq $val) {
293 return $k;
294 }
295 }
296 }
297 else {
298 return @paths;
299 }
300  
301 $ret = undef;
302 }
303 }
304  
305 =item get
306  
307 Returns the value of the given item.
308  
309 my $val = augeas get => "/files/etc/hosts/1/ipaddr";
310  
311 =cut
312  
313 elsif($action eq "get") {
314 my $file = shift @options;
315  
316 if($is_ssh) {
317 my @lines = grep { s/^[^=]+=\s//; } run "echo 'get $file' | augtool";
318 return $lines[0];
319 }
320 else {
321 return $aug->get($file);
322 }
323 }
324  
325 else {
326 Rex::Logger::info("Unknown augeas action.");
327 }
328  
329 Rex::Logger::debug("Augeas Returned: $ret") if $ret;
330  
331 return $ret;
332 }
333  
334 =back
335  
336 =cut
337  
338  
339 1;
340