lib/Rex/Commands/File.pm

filter: Statementstmt 78.8% 402 / 510 Branchbran 61.6% 132 / 214 Conditioncond 49.1% 83 / 169 MC/DCmcdc 31.5% 30 / 95 Subroutinesub 94.8% 37 / 39 Podpod 100% 13 / 13 totaltotal 67.0% 697 / 1040 CC 180 SCAR 48.3
CC
180
Cov
69%
extract
506.0
file
177.1
delete_lines_according_to
90.0
CRAP 124.9
« lib/Rex/Virtualization/LibVirt/guestinfo.pm ↑ Summary lib/Rex/Interface/Shell/Csh.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Commands::File - Transparent File Manipulation
8  
9 =head1 DESCRIPTION
10  
11 With this module you can manipulate files.
12  
13 =head1 SYNOPSIS
14  
15 task "read_passwd", "server01", sub {
16 my $fh = file_read "/etc/passwd";
17 for my $line ($fh->read_all) {
18 print $line;
19 }
20 $fh->close;
21 };
22  
23 task "read_passwd2", "server01", sub {
24 say cat "/etc/passwd";
25 };
26  
27  
28 task "write_passwd", "server01", sub {
29 my $fh = file_write "/etc/passwd";
30 $fh->write("root:*:0:0:root user:/root:/bin/sh\n");
31 $fh->close;
32 };
33  
34 delete_lines_matching "/var/log/auth.log", matching => "root";
35 delete_lines_matching "/var/log/auth.log", matching => qr{Failed};
36 delete_lines_matching "/var/log/auth.log",
37 matching => "root", qr{Failed}, "nobody";
38  
39 file "/path/on/the/remote/machine",
40 source => "/path/on/local/machine";
41  
42 file "/path/on/the/remote/machine",
43 content => "foo bar";
44  
45 file "/path/on/the/remote/machine",
46 source => "/path/on/local/machine",
47 owner => "root",
48 group => "root",
49 mode => 400,
50 on_change => sub { say shift, " was changed."; },
51 on_no_change => sub { say shift, " wasn't changed."; };
52  
53  
54 =head1 EXPORTED FUNCTIONS
55  
56 =cut
57  
58 package Rex::Commands::File;
59  
60 49 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 49 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 49 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 49 our $VERSION = '1.16.1'; # VERSION
65  
66 49 require Rex::Exporter;
67 49 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 49 ▶use Rex::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 49 ▶use Rex::FS::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 49 ▶use Rex::Commands::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 49 ▶use Rex::Commands::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 49 ▶use Rex::File::Parser::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 49 ▶use Rex::Helper::File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 49 ▶use Rex::Helper::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 49 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 49 ▶use Rex::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 49 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 49 ▶use Rex::Interface::Exec;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 49 ▶use Rex::Interface::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 49 ▶use Rex::Interface::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 49 require Rex::CMDB;
83  
84 49 ▶use File::Basename qw(dirname basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 49 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
87 49 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 49 @EXPORT = qw(file_write file_read file_append
90 cat sed
91 delete_lines_matching append_if_no_such_line delete_lines_according_to
92 file template append_or_amend_line
93 extract);
94  
95 49 ▶use vars qw(%file_handles);
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 =head2 template($file [, %params])
98  
99 Parse a template and return the content.
100  
101 By default, it uses L<Rex::Template>. If any of the L<template_ng|Rex#template_ng> or L<1.3|Rex#1.3> (or newer) feature flag is enabled, then L<Rex::Template::NG> is used instead of this module (recommended).
102  
103 For more advanced functionality, you may use your favorite template engine via the L<set_template_function|Rex::Config#set_template_function> configuration option.
104  
105 Template variables may be passed either as hash or a hash reference. The following calls are equivalent:
106  
107 template( $template, variable => value );
108  
109 template( $template, { variable => value } );
110  
111 =head3 List of exposed template variables
112  
113 The following template variables are passed to the underlying templating engine, in order of precedence from low to high (variables of the same name are overridden by the next level aka "last one wins"):
114  
115 =over 4
116  
117 =item task parameters
118  
119 All task parameters coming from the command line via C<S<rex taskname --param=value>>, or from calling a task as a function, like C<S<taskname( { param =E<gt> value } )>>.
120  
121 =item resource parameters
122  
123 All resource parameters as returned by C<Rex::Resource-E<gt>get_current_resource()-E<gt>get_all_parameters>, when called inside a resource.
124  
125 =item explicit template variables
126  
127 All manually specified, explicit template variables passed to C<template()>.
128  
129 =item system information
130  
131 The results from all available L<Rex::Hardware> modules as returned by C<Rex::Hardware-E<gt>get('All')>.
132  
133 Pass C<__no_sys_info__ =E<gt> TRUE> as a template variable to disable including system information:
134  
135 my $content = template( $template, __no_sys_info__ => TRUE );
136  
137 =back
138  
139 =head3 Embedded templates
140  
141 Use C<__DATA__> to embed templates at the end of the file. Prefix embedded template names with C<@>. If embedding multiple templates, mark their end with C<@end>.
142  
143 =head4 Single template
144  
145 my $content = template( '@hello', name => 'world' ); # Hello, world!
146 __DATA__
147 @hello
148 Hello, <%= $name -%>!
149  
150 =head4 Multiple templates
151  
152 Use C<@end> to separate multiple templates inside C<__DATA__>.
153  
154 my $content = template( '@hello', name => 'world' ); # Hello, world!
155 my $alternative = template( '@hi', name => 'world' ); # Hi, world!
156  
157 __DATA__
158 @hello
159 Hello, <%= $name -%>!
160 @end
161  
162 @hi
163 Hi, <%= $name -%>!
164 @end
165  
166 =head3 File templates
167  
168 my $content = template("/files/templates/vhosts.tpl",
169 name => "test.lan",
170 webmaster => 'webmaster@test.lan');
171  
172 The file name specified is subject to "path_map" processing as documented
173 under the file() function to resolve to a physical file name.
174  
175 In addition to the "path_map" processing, if the B<-E> command line switch
176 is used to specify an environment name, existence of a file ending with
177 '.<env>' is checked and has precedence over the file without one, if it
178 exists. E.g. if rex is started as:
179  
180 $ rex -E prod task1
181  
182 then in task1 defined as:
183  
184 task "task1", sub {
185 say template("files/etc/ntpd.conf");
186 };
187  
188 will print the content of 'files/etc/ntpd.conf.prod' if it exists.
189  
190 Note: the appended environment mechanism is always applied, after
191 the 'path_map' mechanism, if that is configured.
192  
193 =cut
194  
195 19 ▶sub template {
Subroutine template: called CC=24 SCAR=33.6
Pod documented
196 19 my ( $file, @params ) = @_;
197 19 my $param;
198  
199 19 ▶ if ( ref $params[0] eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params[0] eq 'HASH') { } 3 16 19
200 3 $param = $params[0];
201 }
202 else {
203 16 $param = {@params};
204 }
205  
206 19 ▶ if ( !exists $param->{server} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $param->{"server"}) 19 0 19
207 19 $param->{server} = Rex::Commands::connection()->server;
208 }
209  
210 19 my $content;
211 19 ▶ if ( ref $file && ref $file eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file and ref $file eq 'SCALAR') { } 16 3 19
Condition66%2/3
ref $file and ref $file eq "SCALAR"
!ll&&!rl&&r
3016
MC/DC50%1/2
ref $file and ref $file eq "SCALAR"
ref $file ref $file eq "SCALAR"
Truth table66%2/3
A and B
A= ref $fileB= ref $file eq "SCALAR"
ABresult
0X0
100
111
212 16 $content = ${$file};
213 }
214 else {
215 3 $file = resolv_path($file);
216  
217 3 ▶ unless ( $file =~ m/^\// || $file =~ m/^\@/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file =~ m[^/]u or $file =~ /^\@/u) 1 2 3
Condition66%2/3
$file =~ m[^/]u or $file =~ /^\@/u
l!l&&r!l&&!r
021
MC/DC50%1/2
$file =~ m[^/]u or $file =~ /^\@/u
$file =~ m[^/]u $file =~ /^\@/u
Truth table66%2/3
A or B
A= $file =~ m[^/]uB= $file =~ /^\@/u
ABresult
1X1
011
000
218  
219 # path is relative and no template
220 1 Rex::Logger::debug("Relativ path $file");
221  
222 1 $file = Rex::Helper::Path::get_file_path( $file, caller() );
223  
224 1 Rex::Logger::debug("New filename: $file");
225 }
226  
227 # if there is a file called filename.environment then use this file
228 # ex:
229 # $content = template("files/hosts.tpl");
230 #
231 # rex -E live ...
232 # will first look if files/hosts.tpl.live is available, if not it will
233 # use files/hosts.tpl
234 3 ▶ if ( -f "$file." . Rex::Config->get_environment ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$file." . "Rex::Config"->get_environment) 0 3 3
235 0 $file = "$file." . Rex::Config->get_environment;
236 }
237  
238 3 ▶ if ( -f $file ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $file) { } 1 2 3
elsif ($file =~ /^\@/u) { } 2 0 2
239 1 $content = eval { local ( @ARGV, $/ ) = ($file); <>; };
240 }
241 elsif ( $file =~ m/^\@/ ) {
242 2 my @caller = caller(0);
243  
244 2 my $file_path = Rex::get_module_path( $caller[0] );
245  
246 2 ▶ if ( !-f $file_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file_path) 2 0 2
247 2 my ($mod_name) = ( $caller[0] =~ m/^.*::(.*?)$/ );
248 2 ▶ if ( $mod_name && -f "$file_path/$mod_name.pm" ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($mod_name and -f "$file_path/$mod_name.pm") { } 0 2 2
elsif (-f "$file_path/__module__.pm") { } 0 2 2
elsif (-f "$file_path/Module.pm") { } 0 2 2
elsif (-f $caller[1]) { } 2 0 2
elsif ($caller[1] =~ m[^/loader/[^/]+/__Rexfile__.pm$]u) { } 0 0 0
Condition33%1/3
$mod_name and -f "$file_path/$mod_name.pm"
!ll&&!rl&&r
200
MC/DC0%0/2
$mod_name and -f "$file_path/$mod_name.pm"
$mod_name -f "$file_path/$mod_name.pm"
Truth table33%1/3
A and B
A= $mod_nameB= -f "$file_path/$mod_name.pm"
ABresult
0X0
100
111
249 0 $file_path = "$file_path/$mod_name.pm";
250 }
251 elsif ( -f "$file_path/__module__.pm" ) {
252 0 $file_path = "$file_path/__module__.pm";
253 }
254 elsif ( -f "$file_path/Module.pm" ) {
255 0 $file_path = "$file_path/Module.pm";
256 }
257 elsif ( -f $caller[1] ) {
258 2 $file_path = $caller[1];
259 }
260 elsif ( $caller[1] =~ m|^/loader/[^/]+/__Rexfile__.pm$| ) {
261 0 $file_path = $INC{"__Rexfile__.pm"};
262 }
263 }
264  
265 2 my $file_content = eval { local ( @ARGV, $/ ) = ($file_path); <>; };
266 2 my ($data) = ( $file_content =~ m/.*__DATA__(.*)/ms );
267 2 my $fp = Rex::File::Parser::Data->new( data => [ split( /\n/, $data ) ] );
268 2 my $snippet_to_read = substr( $file, 1 );
269 2 $content = $fp->read($snippet_to_read);
270 }
271 else {
272 0 die("$file not found");
273 }
274 }
275  
276 19 my %template_vars;
277 19 ▶ if ( !exists $param->{__no_sys_info__} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $param->{'__no_sys_info__'}) { } 13 6 19
278 13 %template_vars = _get_std_template_vars($param);
279 }
280 else {
281 6 delete $param->{__no_sys_info__};
282 6 %template_vars = %{$param};
283 }
284  
285 # configuration variables
286 19 my $config_values = Rex::Config->get_all;
287 19 for my $key ( keys %{$config_values} ) {
288 26 ▶ if ( !exists $template_vars{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $template_vars{$key}) 25 1 26
289 25 $template_vars{$key} = $config_values->{$key};
290 }
291 }
292  
293 19 ▶ if ( Rex::CMDB::cmdb_active() && Rex::Config->get_register_cmdb_template ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Rex::CMDB::cmdb_active() and "Rex::Config"->get_register_cmdb_template) 4 15 19
Condition66%2/3
Rex::CMDB::cmdb_active() and "Rex::Config"->get_register_cmdb_template
!ll&&!rl&&r
1504
MC/DC50%1/2
Rex::CMDB::cmdb_active() and "Rex::Config"->get_register_cmdb_template
Rex::CMDB::cmdb_active() "Rex::Config"->get_register_cmdb_template
Truth table66%2/3
A and B
A= Rex::CMDB::cmdb_active()B= "Rex::Config"->get_register_cmdb_template
ABresult
0X0
100
111
294 4 my $data = Rex::CMDB::cmdb();
295 4 for my $key ( keys %{ $data->{value} } ) {
296 28 ▶ if ( !exists $template_vars{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $template_vars{$key}) 26 2 28
297 26 $template_vars{$key} = $data->{value}->{$key};
298 }
299 }
300 }
301  
302 19 return Rex::Config->get_template_function()->( $content, \%template_vars );
303 }
304  
305 13 ▶sub _get_std_template_vars {
Subroutine _get_std_template_vars: called CC=3 SCAR=11.5
306 13 my ($param) = @_;
307  
308 13 ▶ my %merge1 = %{ $param || {} };
Condition50%1/2
$param || {}
l!l
130
MC/DC0%0/1
$param || {}
$param
Truth table50%1/2
A
A= $param
Aresult
11
00
309 13 my %merge2;
310  
311 13 ▶ if ( Rex::get_cache()->valid("system_information_info") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Rex::get_cache()->valid('system_information_info')) { } 0 13 13
312 0 %merge2 = %{ Rex::get_cache()->get("system_information_info") };
313 }
314 else {
315 13 %merge2 = Rex::Helper::System::info();
316 13 Rex::get_cache()->set( "system_information_info", \%merge2 );
317 }
318  
319 13 my %template_vars = ( %merge1, %merge2 );
320  
321 13 return %template_vars;
322 }
323  
324 =head2 file($file_name [, %options])
325  
326 This function is the successor of I<install file>. Please use this function to upload files to your server.
327  
328 task "prepare", "server1", "server2", sub {
329 file "/file/on/remote/machine",
330 source => "/file/on/local/machine";
331  
332 file "/etc/hosts",
333 content => template("templates/etc/hosts.tpl"),
334 owner => "user",
335 group => "group",
336 mode => 700,
337 on_change => sub { say "Something was changed." },
338 on_no_change => sub { say "Nothing has changed." };
339  
340 file "/etc/motd",
341 content => `fortune`;
342  
343 file "/etc/named.conf",
344 content => template("templates/etc/named.conf.tpl"),
345 no_overwrite => TRUE; # this file will not be overwritten if already exists.
346  
347 file "/etc/httpd/conf/httpd.conf",
348 source => "/files/etc/httpd/conf/httpd.conf",
349 on_change => sub { service httpd => "restart"; };
350  
351 file "/etc/named.d",
352 ensure => "directory", # this will create a directory
353 owner => "root",
354 group => "root";
355  
356 file "/etc/motd",
357 ensure => "absent"; # this will remove the file or directory
358  
359 };
360  
361 The first parameter is either a string or an array reference. In the latter case the
362 function is called for all strings in the array. Therefore, the following constructs
363 are equivalent:
364  
365 file '/tmp/test1', ensure => 'directory';
366 file '/tmp/test2', ensure => 'directory';
367  
368 file [ qw( /tmp/test1 /tmp/test2 ) ], ensure => 'directory'; # use array ref
369  
370 file [ glob('/tmp/test{1,2}') ], ensure => 'directory'; # explicit glob call for array contents
371  
372 Use the glob carefully as B<it can leak local filesystem information> (e.g. when using wildcards).
373  
374 The I<source> is subject to a path resolution algorithm. This algorithm
375 can be configured using the I<set> function to set the value of the
376 I<path_map> variable to a hash containing path prefixes as its keys.
377 The associated values are arrays listing the prefix replacements in order
378 of (decreasing) priority.
379  
380 set "path_map", {
381 "files/" => [ "files/{environment}/{hostname}/_root_/",
382 "files/{environment}/_root_/" ]
383 };
384  
385 With this configuration, the file "files/etc/ntpd.conf" will be probed for
386 in the following locations:
387  
388 - files/{environment}/{hostname}/_root_/etc/ntpd.conf
389 - files/{environment}/_root_/etc/ntpd.conf
390 - files/etc/ntpd.conf
391  
392 Furthermore, if a path prefix matches multiple prefix entries in 'path_map',
393 e.g. "files/etc/ntpd.conf" matching both "files/" and "files/etc/", the
394 longer matching prefix(es) have precedence over shorter ones. Note that
395 keys without a trailing slash (i.e. "files/etc") will be treated as having
396 a trailing slash when matching the prefix ("files/etc/").
397  
398 If no file is found using the above procedure and I<source> is relative,
399 it will search from the location of your I<Rexfile> or the I<.pm> file if
400 you use Perl packages.
401  
402 All the possible variables ('{environment}', '{hostname}', ...) are documented
403 in the CMDB YAML documentation.
404  
405 =head3 Hooks
406  
407 This function supports the following L<hooks|Rex::Hook>:
408  
409 =over 4
410  
411 =item before
412  
413 This gets executed before anything is done. All original parameters are passed to it, including the applied defaults (C<ensure =E<gt> 'present'>, resolved path for C<source>).
414  
415 The return value of this hook overwrites the original parameters of the function call.
416  
417 =item before_change
418  
419 This gets executed right before the new file is written. All original parameters are passed to it, including the applied defaults (C<ensure =E<gt> 'present'>, resolved path for C<source>).
420  
421 =item after_change
422  
423 This gets executed right after the file is written. All original parameters, including the applied defaults (C<ensure =E<gt> 'present'>, resolved path for C<source>), and any returned results are passed to it.
424  
425 =item after
426  
427 This gets executed right before the C<file()> function returns. All original parameters, including the applied defaults (C<ensure =E<gt> 'present'>, resolved path for C<source>), and any returned results are passed to it.
428  
429 =back
430  
431 =cut
432  
433 59 ▶sub file {
Subroutine file: called CC=73 SCAR=51.8
Pod documented
434 59 my ( $file, @options ) = @_;
435  
436 59 ▶ if ( ref $file eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq "ARRAY") 0 59 59
437 0 my @ret;
438  
439 # $file is an array, so iterate over these files
440 0 for my $f ( @{$file} ) {
441 0 push( @ret, file( $f, @options ) );
442 }
443  
444 0 return \@ret;
445 }
446  
447 59 my $option = {@options};
448  
449 59 $file = resolv_path($file);
450  
451 59 my ($is_directory);
452 59 ▶ if ( exists $option->{ensure} && $option->{ensure} eq "directory" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"ensure"} and $option->{"ensure"} eq "directory") 2 57 59
Condition100%3/3
exists $option->{"ensure"} and $option->{"ensure"} eq "directory"
!ll&&!rl&&r
4892
MC/DC100%2/2
exists $option->{"ensure"} and $option->{"ensure"} eq "directory"
exists $option->{"ensure"} $option->{"ensure"} eq "directory"
Truth table100%3/3
A and B
A= exists $option->{"ensure"}B= $option->{"ensure"} eq "directory"
ABresult
0X0
100
111
453 2 $is_directory = 1;
454 }
455  
456 59 ▶ if ( exists $option->{source} && !$is_directory ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"source"} and not $is_directory) 3 56 59
Condition66%2/3
exists $option->{"source"} and not $is_directory
!ll&&!rl&&r
5603
MC/DC50%1/2
exists $option->{"source"} and not $is_directory
exists $option->{"source"} not $is_directory
Truth table66%2/3
A and not(B)
A= exists $option->{"source"}B= not $is_directory
ABresult
0X0
100
111
457 3 $option->{source} = resolv_path( $option->{source} );
458 }
459  
460 # default: ensure = present
461 59 ▶ $option->{ensure} ||= "present";
Condition100%2/2
$option->{"ensure"} ||= "present"
l!l
1148
MC/DC100%1/1
$option->{"ensure"} ||= "present"
$option->{"ensure"}
Truth table100%2/2
A
A= $option->{"ensure"}
Aresult
11
00
462  
463 59 my $fs = Rex::Interface::Fs->create;
464  
465 59 ▶ if ( $option->{ensure} ne 'absent' && $fs->is_symlink($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{"ensure"} ne "absent" and $fs->is_symlink($file)) 4 55 59
Condition100%3/3
$option->{"ensure"} ne "absent" and $fs->is_symlink($file)
!ll&&!rl&&r
2534
MC/DC100%2/2
$option->{"ensure"} ne "absent" and $fs->is_symlink($file)
$option->{"ensure"} ne "absent" $fs->is_symlink($file)
Truth table100%3/3
A and B
A= $option->{"ensure"} ne "absent"B= $fs->is_symlink($file)
ABresult
0X0
100
111
466 4 my $original_file = $file;
467 4 $file = resolve_symlink($file);
468 4 Rex::Logger::info(
469 "$original_file is a symlink, operating on $file instead", 'warn' );
470 }
471  
472 #### check and run before hook
473 eval {
474 59 my @new_args = Rex::Hook::run_hook( file => "before", $file, %{$option} );
475 59 ▶ if (@new_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@new_args) 3 56 59
476 3 ( $file, @options ) = @new_args;
477 3 $option = {@options};
478 }
479 59 1;
480 59 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my(@new_args) = Rex::Hook::run_hook("file", "before", $file, %{$option;}); if (@new_args) { () = @new_args; $option = {@options}; } ; 1 } } or do { die "Before hook failed. Cancelling file() action: $@" } 59 0 59
481 0 die("Before hook failed. Cancelling file() action: $@");
482 };
483 ##############################
484  
485 Rex::get_current_connection()->{reporter}
486 59 ->report_resource_start( type => "file", name => $file );
487  
488 59 ▶ my $need_md5 = ( $option->{"on_change"} && !$is_directory ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$option->{'on_change'} && !$is_directory ? : 3 56 59
Condition100%3/3
$option->{"on_change"} and not $is_directory
!ll&&!rl&&r
5513
MC/DC100%2/2
$option->{"on_change"} and not $is_directory
$option->{"on_change"} not $is_directory
Truth table100%3/3
A and not(B)
A= $option->{"on_change"}B= not $is_directory
ABresult
0X0
100
111
489 59 ▶ my $on_change = $option->{"on_change"} || sub { };
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition66%2/3
$option->{'on_change'} || sub { }
l!l&&r!l&&!r
4550
MC/DC0%0/2
$option->{'on_change'} || sub { }
$option->{'on_change'} sub { }
Truth table66%2/3
A || B
A= $option->{'on_change'}B= sub { }
ABresult
1X1
011
000
490 59 ▶ my $on_no_change = $option->{"on_no_change"} || sub { };
Subroutine __ANON__: called CC=1 SCAR=1.2
Condition33%1/3
$option->{'on_no_change'} || sub { }
l!l&&r!l&&!r
0590
MC/DC0%0/2
$option->{'on_no_change'} || sub { }
$option->{'on_no_change'} sub { }
Truth table33%1/3
A || B
A= $option->{'on_no_change'}B= sub { }
ABresult
1X1
011
000
491  
492 59 my $__ret = { changed => 0 };
493  
494 59 my ( $new_md5, $old_md5 );
495  
496 59 ▶ if ( exists $option->{no_overwrite}
Branch: 2 branches
BranchTrueFalseTotal
if (exists $option->{'no_overwrite'} and $option->{'no_overwrite'} and $fs->is_file($file)) { } 0 59 59
elsif (exists $option->{'content'} || exists $option->{'source'} and not $is_directory) { } 42 34 76
Condition33%2/6
exists $option->{"no_overwrite"} and $option->{"no_overwrite"} and $fs->is_file($file)
!ll&&!rl&&r
5900
exists $option->{"no_overwrite"} and $option->{"no_overwrite"}
!ll&&!rl&&r
5900
MC/DC0%0/3
exists $option->{"no_overwrite"} and $option->{"no_overwrite"} and $fs->is_file($file)
exists $option->{"no_overwrite"} $option->{"no_overwrite"} $fs->is_file($file)
Truth table25%1/4
A and B and C
A= exists $option->{"no_overwrite"}B= $option->{"no_overwrite"}C= $fs->is_file($file)
ABCresult
0XX0
10X0
1100
1111
497 && $option->{no_overwrite}
498 && $fs->is_file($file) )
499 {
500 0 Rex::Logger::debug(
501 "File already exists and no_overwrite option given. Doing nothing.");
502 0 $__ret = { changed => 0 };
503  
504 Rex::get_current_connection()->{reporter}->report(
505 0 changed => 0,
506 message =>
507 "File already exists and no_overwrite option given. Doing nothing."
508 );
509 }
510  
511 ▶ elsif ( ( exists $option->{content} || exists $option->{source} )
Condition83%5/6
exists $option->{"content"} || exists $option->{"source"} and not $is_directory
!ll&&!rl&&r
17042
exists $option->{"content"} or exists $option->{"source"}
l!l&&r!l&&!r
39317
MC/DC66%2/3
exists $option->{"content"} || exists $option->{"source"} and not $is_directory
exists $option->{"content"} exists $option->{"source"} not $is_directory
Truth table60%3/5
A or B and not(C)
A= exists $option->{"content"}B= exists $option->{"source"}C= not $is_directory
ABCresult
00X0
1X00
0100
1X11
0111
512 && !$is_directory )
513 {
514  
515 # first upload file to tmp location, to get md5 sum.
516 # than we can decide if we need to replace the current (old) file.
517  
518 42 my $tmp_file_name = get_tmp_file_name($file);
519  
520 42 ▶ if ( exists $option->{content} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $option->{'content'}) { } 39 3 42
elsif (exists $option->{'source'}) { } 3 0 3
521 39 my $fh = file_write($tmp_file_name);
522 39 my @lines = split( qr{$/}, $option->{"content"} );
523 39 for my $line (@lines) {
524 240 $fh->write( $line . $/ );
525 }
526 39 $fh->close;
527 }
528 elsif ( exists $option->{source} ) {
529 $option->{source} =
530 3 Rex::Helper::Path::get_file_path( $option->{source}, caller );
531  
532 3 upload $option->{source}, $tmp_file_name;
533 }
534  
535 # now get md5 sums
536 42 eval { $old_md5 = md5($file); };
537 42 $new_md5 = md5($tmp_file_name);
538  
539 42 ▶ if ( $new_md5 && $old_md5 && $new_md5 eq $old_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_md5 and $old_md5 and $new_md5 eq $old_md5) { } 4 38 42
Condition83%5/6
$new_md5 and $old_md5 and $new_md5 eq $old_md5
!ll&&!rl&&r
12264
$new_md5 and $old_md5
!ll&&!rl&&r
01230
MC/DC66%2/3
$new_md5 and $old_md5 and $new_md5 eq $old_md5
$new_md5 $old_md5 $new_md5 eq $old_md5
Truth table75%3/4
A and B and C
A= $new_md5B= $old_md5C= $new_md5 eq $old_md5
ABCresult
0XX0
10X0
1100
1111
540 4 Rex::Logger::debug(
541 "No need to overwrite existing file. Old and new files are the same. $old_md5 eq $new_md5."
542 );
543  
544 # md5 sums are the same, delete tmp.
545 4 $fs->unlink($tmp_file_name);
546 4 $need_md5 = 0; # we don't need to execute on_change hook
547  
548 Rex::get_current_connection()->{reporter}->report(
549 4 changed => 0,
550 message =>
551 "No need to overwrite existing file. Old and new files are the same. $old_md5 eq $new_md5."
552 );
553 }
554 else {
555 38 ▶ $old_md5 ||= "";
Condition100%2/2
$old_md5 ||= ""
l!l
2612
MC/DC100%1/1
$old_md5 ||= ""
$old_md5
Truth table100%2/2
A
A= $old_md5
Aresult
11
00
556 38 Rex::Logger::debug(
557 "Need to use the new file. md5 sums are different. <<$old_md5>> = <<$new_md5>>"
558 );
559  
560 #### check and run before_change hook
561 38 Rex::Hook::run_hook( file => "before_change", $file, %{$option} );
562 ##############################
563  
564 38 ▶ if (Rex::is_sudo) {
Branch: 1 branch
BranchTrueFalseTotal
if (Rex::is_sudo()) 0 38 38
565 0 my $current_options =
566 Rex::get_current_connection_object()->get_current_sudo_options;
567 0 Rex::get_current_connection_object()->push_sudo_options( {} );
568  
569 0 ▶ if ( exists $current_options->{user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $current_options->{"user"}) 0 0 0
570 0 $fs->chown( "$current_options->{user}:", $tmp_file_name );
571 }
572 }
573  
574 38 $fs->rename( $tmp_file_name, $file );
575 38 ▶ Rex::get_current_connection_object()->pop_sudo_options()
Branch: 1 branch
BranchTrueFalseTotal
if Rex::is_sudo() 0 38 38
576 if (Rex::is_sudo);
577  
578 38 $__ret = { changed => 1 };
579  
580 Rex::get_current_connection()->{reporter}->report(
581 38 changed => 1,
582 message => "File updated. old md5: $old_md5, new md5: $new_md5"
583 );
584  
585 #### check and run after_change hook
586 38 Rex::Hook::run_hook( file => "after_change", $file, %{$option}, $__ret );
587 ##############################
588  
589 }
590  
591 }
592  
593 59 ▶ if ( exists $option->{"ensure"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"ensure"}) 59 0 59
594 59 ▶ if ( $option->{ensure} eq "present" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($option->{'ensure'} eq 'present') { } 55 4 59
elsif ($option->{'ensure'} eq 'absent') { } 2 2 4
elsif ($option->{'ensure'} eq 'directory') { } 2 0 2
595 55 ▶ if ( !$fs->is_file($file) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $fs->is_file($file)) { } 7 48 55
elsif (not $__ret->{'changed'}) { } 10 38 48
596  
597 #### check and run before_change hook
598 7 Rex::Hook::run_hook( file => "before_change", $file, %{$option} );
599 ##############################
600  
601 7 my $fh = file_write($file);
602 7 $fh->write("");
603 7 $fh->close;
604 7 $__ret = { changed => 1 };
605  
606 Rex::get_current_connection()->{reporter}->report(
607 7 changed => 1,
608 message => "file is now present, with no content",
609 );
610  
611 #### check and run after_change hook
612 Rex::Hook::run_hook(
613 file => "after_change",
614 7 $file, %{$option}, $__ret
615 );
616 ##############################
617  
618 }
619 elsif ( !$__ret->{changed} ) {
620 10 $__ret = { changed => 0 };
621 10 Rex::get_current_connection()->{reporter}->report( changed => 0, );
622 }
623 }
624 elsif ( $option->{ensure} eq "absent" ) {
625 2 $need_md5 = 0;
626  
627 #### check and run before_change hook
628 2 Rex::Hook::run_hook( file => "before_change", $file, %{$option} );
629 ##############################
630  
631 2 ▶ if ( $fs->is_file($file) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fs->is_file($file)) { } 2 0 2
elsif ($fs->is_dir($file)) { } 0 0 0
632 2 $fs->unlink($file);
633 2 $__ret = { changed => 1 };
634 Rex::get_current_connection()->{reporter}->report(
635 2 changed => 1,
636 message => "File removed."
637 );
638 }
639 elsif ( $fs->is_dir($file) ) {
640 0 $fs->rmdir($file);
641 0 $__ret = { changed => 1 };
642 Rex::get_current_connection()->{reporter}->report(
643 0 changed => 1,
644 message => "Directory removed.",
645 );
646 }
647 else {
648 0 $__ret = { changed => 0 };
649 0 Rex::get_current_connection()->{reporter}->report( changed => 0, );
650 }
651  
652 #### check and run after_change hook
653 2 Rex::Hook::run_hook( file => "after_change", $file, %{$option}, $__ret );
654 ##############################
655  
656 }
657 elsif ( $option->{ensure} eq "directory" ) {
658 2 Rex::Logger::debug("file() should be a directory");
659 2 my %dir_option;
660 2 ▶ if ( exists $option->{owner} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"owner"}) 0 2 2
661 0 $dir_option{owner} = $option->{owner};
662 }
663 2 ▶ if ( exists $option->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"group"}) 0 2 2
664 0 $dir_option{group} = $option->{group};
665 }
666 2 ▶ if ( exists $option->{mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"mode"}) 0 2 2
667 0 $dir_option{mode} = $option->{mode};
668 }
669  
670 2 Rex::Commands::Fs::mkdir( $file, %dir_option, on_change => $on_change );
671 }
672 }
673  
674 59 ▶ if ( !exists $option->{content}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $option->{"content"} || exists $option->{"source"} and $option->{"ensure"} ne "absent") 15 44 59
Condition100%6/6
not exists $option->{"content"} || exists $option->{"source"} and $option->{"ensure"} ne "absent"
!ll&&!rl&&r
42215
exists $option->{'content'} || exists $option->{'source'}
l!l&&r!l&&!r
39317
MC/DC100%3/3
not exists $option->{"content"} || exists $option->{"source"} and $option->{"ensure"} ne "absent"
exists $option->{'content'} exists $option->{'source'} $option->{"ensure"} ne "absent"
Truth table100%4/4
not(A || B) and C
A= exists $option->{'content'}B= exists $option->{'source'}C= $option->{"ensure"} ne "absent"
ABCresult
1XX0
01X0
0000
0011
675 && !exists $option->{source}
676 && $option->{ensure} ne "absent" )
677 {
678  
679 # no content and no source, so just verify that the file is present
680 15 ▶ if ( !$fs->is_file($file) && !$is_directory ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fs->is_file($file) or $is_directory) 0 15 15
Condition66%2/3
$fs->is_file($file) or $is_directory
l!l&&r!l&&!r
1320
MC/DC0%0/2
$fs->is_file($file) or $is_directory
$fs->is_file($file) $is_directory
Truth table66%2/3
A or B
A= $fs->is_file($file)B= $is_directory
ABresult
1X1
011
000
681  
682 #### check and run before_change hook
683 0 Rex::Hook::run_hook( file => "before_change", $file, %{$option} );
684 ##############################
685  
686 0 my $fh = file_write($file);
687 0 $fh->write("");
688 0 $fh->close;
689  
690 0 my $f_type = "file is now present, with no content";
691 0 ▶ if ( exists $option->{ensure} && $option->{ensure} eq "directory" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"ensure"} and $option->{"ensure"} eq "directory") 0 0 0
Condition0%0/3
exists $option->{"ensure"} and $option->{"ensure"} eq "directory"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $option->{"ensure"} and $option->{"ensure"} eq "directory"
exists $option->{"ensure"} $option->{"ensure"} eq "directory"
Truth table0%0/3
A and B
A= exists $option->{"ensure"}B= $option->{"ensure"} eq "directory"
ABresult
0X0
100
111
692 0 $f_type = "directory is now present";
693 }
694  
695 Rex::get_current_connection()->{reporter}->report(
696 0 changed => 1,
697 message => $f_type,
698 );
699  
700 #### check and run after_change hook
701 0 Rex::Hook::run_hook( file => "after_change", $file, %{$option}, $__ret );
702 ##############################
703  
704 }
705 }
706  
707 59 ▶ if ( $option->{ensure} ne "absent" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{"ensure"} ne "absent") 57 2 59
708  
709 57 ▶ if ($need_md5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_md5) 2 55 57
710 2 eval { $new_md5 = md5($file); };
711 }
712 57 my %stat_old = $fs->stat($file);
713  
714 57 ▶ if ( exists $option->{"mode"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"mode"}) 28 29 57
715 28 $fs->chmod( $option->{"mode"}, $file );
716 }
717  
718 57 ▶ if ( exists $option->{"group"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"group"}) 25 32 57
719 25 $fs->chgrp( $option->{"group"}, $file );
720 }
721  
722 57 ▶ if ( exists $option->{"owner"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"owner"}) 25 32 57
723 25 $fs->chown( $option->{"owner"}, $file );
724 }
725  
726 57 my %stat_new = $fs->stat($file);
727  
728 57 ▶ if ( %stat_old && %stat_new && $stat_old{mode} ne $stat_new{mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%stat_old and %stat_new and $stat_old{"mode"} ne $stat_new{"mode"}) 15 42 57
Condition50%3/6
%stat_old and %stat_new and $stat_old{"mode"} ne $stat_new{"mode"}
!ll&&!rl&&r
04215
%stat_old and %stat_new
!ll&&!rl&&r
0057
MC/DC33%1/3
%stat_old and %stat_new and $stat_old{"mode"} ne $stat_new{"mode"}
%stat_old %stat_new $stat_old{"mode"} ne $stat_new{"mode"}
Truth table50%2/4
A and B and C
A= %stat_oldB= %stat_newC= $stat_old{"mode"} ne $stat_new{"mode"}
ABCresult
0XX0
10X0
1100
1111
729 Rex::get_current_connection()->{reporter}->report(
730 15 changed => 1,
731 message =>
732 "File-System permissions changed from $stat_old{mode} to $stat_new{mode}.",
733 );
734 }
735  
736 57 ▶ if ( %stat_old && %stat_new && $stat_old{uid} ne $stat_new{uid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%stat_old and %stat_new and $stat_old{"uid"} ne $stat_new{"uid"}) 0 57 57
Condition33%2/6
%stat_old and %stat_new and $stat_old{"uid"} ne $stat_new{"uid"}
!ll&&!rl&&r
0570
%stat_old and %stat_new
!ll&&!rl&&r
0057
MC/DC0%0/3
%stat_old and %stat_new and $stat_old{"uid"} ne $stat_new{"uid"}
%stat_old %stat_new $stat_old{"uid"} ne $stat_new{"uid"}
Truth table25%1/4
A and B and C
A= %stat_oldB= %stat_newC= $stat_old{"uid"} ne $stat_new{"uid"}
ABCresult
0XX0
10X0
1100
1111
737 Rex::get_current_connection()->{reporter}->report(
738 0 changed => 1,
739 message => "Owner changed from $stat_old{uid} to $stat_new{uid}.",
740 );
741 }
742  
743 57 ▶ if ( %stat_old && %stat_new && $stat_old{gid} ne $stat_new{gid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%stat_old and %stat_new and $stat_old{"gid"} ne $stat_new{"gid"}) 0 57 57
Condition33%2/6
%stat_old and %stat_new and $stat_old{"gid"} ne $stat_new{"gid"}
!ll&&!rl&&r
0570
%stat_old and %stat_new
!ll&&!rl&&r
0057
MC/DC0%0/3
%stat_old and %stat_new and $stat_old{"gid"} ne $stat_new{"gid"}
%stat_old %stat_new $stat_old{"gid"} ne $stat_new{"gid"}
Truth table25%1/4
A and B and C
A= %stat_oldB= %stat_newC= $stat_old{"gid"} ne $stat_new{"gid"}
ABCresult
0XX0
10X0
1100
1111
744 Rex::get_current_connection()->{reporter}->report(
745 0 changed => 1,
746 message => "Group changed from $stat_old{gid} to $stat_new{gid}.",
747 );
748 }
749  
750 }
751  
752 59 my $on_change_done = 0;
753  
754 59 ▶ if ($need_md5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_md5) 2 57 59
755 2 ▶ unless ( $old_md5 && $new_md5 && $old_md5 eq $new_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_md5 and $new_md5 and $old_md5 eq $new_md5) 2 0 2
Condition33%2/6
$old_md5 and $new_md5 and $old_md5 eq $new_md5
!ll&&!rl&&r
200
$old_md5 and $new_md5
!ll&&!rl&&r
200
MC/DC0%0/3
$old_md5 and $new_md5 and $old_md5 eq $new_md5
$old_md5 $new_md5 $old_md5 eq $new_md5
Truth table25%1/4
A and B and C
A= $old_md5B= $new_md5C= $old_md5 eq $new_md5
ABCresult
0XX0
10X0
1100
1111
756 2 ▶ $old_md5 ||= "";
Condition50%1/2
$old_md5 ||= ""
l!l
02
MC/DC0%0/1
$old_md5 ||= ""
$old_md5
Truth table50%1/2
A
A= $old_md5
Aresult
11
00
757 2 ▶ $new_md5 ||= "";
Condition50%1/2
$new_md5 ||= ""
l!l
20
MC/DC0%0/1
$new_md5 ||= ""
$new_md5
Truth table50%1/2
A
A= $new_md5
Aresult
11
00
758  
759 2 Rex::Logger::debug("File $file has been changed... Running on_change");
760 2 Rex::Logger::debug("old: $old_md5");
761 2 Rex::Logger::debug("new: $new_md5");
762  
763 2 &$on_change($file);
764  
765 2 $on_change_done = 1;
766  
767 Rex::get_current_connection()->{reporter}->report(
768 2 changed => 1,
769 message => "Content changed.",
770 );
771  
772 2 $__ret = { changed => 1 };
773 }
774 }
775  
776 59 ▶ if ( $__ret->{changed} == 1 && $on_change_done == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($__ret->{'changed'} == 1 and $on_change_done == 0) { } 45 14 59
elsif ($__ret->{'changed'} == 0) { } 12 2 14
Condition100%3/3
$__ret->{"changed"} == 1 and $on_change_done == 0
!ll&&!rl&&r
12245
MC/DC100%2/2
$__ret->{"changed"} == 1 and $on_change_done == 0
$__ret->{"changed"} == 1 $on_change_done == 0
Truth table100%3/3
A and B
A= $__ret->{"changed"} == 1B= $on_change_done == 0
ABresult
0X0
100
111
777 45 &$on_change($file);
778 }
779 elsif ( $__ret->{changed} == 0 ) {
780 12 Rex::Logger::debug(
781 "File $file has not been changed... Running on_no_change");
782 12 &$on_no_change($file);
783 }
784  
785 #### check and run after hook
786 59 Rex::Hook::run_hook( file => "after", $file, %{$option}, $__ret );
787 ##############################
788  
789 Rex::get_current_connection()->{reporter}
790 59 ->report_resource_end( type => "file", name => $file );
791  
792 59 return $__ret->{changed};
793 }
794  
795 =head2 get_tmp_file_name($file_name)
796  
797 To avoid partial changes and to ensure atomic operations when managing files, Rex first creates a temporary file with the new content, then replaces the target file when necessary.
798  
799 This function returns the name of the temporary file used for a given target file, which may prove useful in hooks.
800  
801 =cut
802  
803 44 ▶sub get_tmp_file_name {
Subroutine get_tmp_file_name: called CC=2 SCAR=6.9
Pod documented
804 44 my $file = shift;
805  
806 44 my $dirname = dirname($file);
807 44 my $filename = ".rex.tmp." . basename($file);
808  
809 44 ▶ my $tmp_file_name =
Branch: 1 branch
BranchTrueFalseTotal
$dirname eq '.' ? : 5 39 44
810 $dirname eq '.'
811 ? $filename
812 : Rex::Helper::File::Spec->catfile( $dirname, $filename );
813  
814 44 return $tmp_file_name;
815 }
816  
817 =head2 file_write($file_name)
818  
819 This function opens a file for writing (it will truncate the file if it already exists). It returns a Rex::FS::File object on success.
820  
821 On failure it will die.
822  
823 my $fh;
824 eval {
825 $fh = file_write("/etc/groups");
826 };
827  
828 # catch an error
829 if($@) {
830 print "An error occurred. $@.\n";
831 }
832  
833 # work with the filehandle
834 $fh->write("...");
835 $fh->close;
836  
837 =cut
838  
839 48 ▶sub file_write {
Subroutine file_write: called CC=2 SCAR=7.5
Pod documented
840 48 my ($file) = @_;
841 48 $file = resolv_path($file);
842  
843 48 Rex::Logger::debug("Opening file: $file for writing.");
844  
845 48 my $fh = Rex::Interface::File->create;
846 48 ▶ if ( !$fh->open( ">", $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open(">", $file)) 0 48 48
847 0 Rex::Logger::debug("Can't open $file for writing.");
848 0 die("Can't open $file for writing.");
849 }
850  
851 48 return Rex::FS::File->new( fh => $fh );
852 }
853  
854 =head2 file_append($file_name)
855  
856 =cut
857  
858 1 ▶sub file_append {
Subroutine file_append: called CC=2 SCAR=7.5
Pod documented
859 1 my ($file) = @_;
860 1 $file = resolv_path($file);
861  
862 1 Rex::Logger::debug("Opening file: $file for appending.");
863  
864 1 my $fh = Rex::Interface::File->create;
865  
866 1 ▶ if ( !$fh->open( ">>", $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open(">>", $file)) 0 1 1
867 0 Rex::Logger::debug("Can't open $file for appending.");
868 0 die("Can't open $file for appending.");
869 }
870  
871 1 return Rex::FS::File->new( fh => $fh );
872 }
873  
874 =head2 file_read($file_name)
875  
876 This function opens a file for reading. It returns a Rex::FS::File object on success.
877  
878 On failure it will die.
879  
880 my $fh;
881 eval {
882 $fh = file_read("/etc/groups");
883 };
884  
885 # catch an error
886 if($@) {
887 print "An error occurred. $@.\n";
888 }
889  
890 # work with the filehandle
891 my $content = $fh->read_all;
892 $fh->close;
893  
894 =cut
895  
896 137 ▶sub file_read {
Subroutine file_read: called CC=2 SCAR=7.5
Pod documented
897 137 my ($file) = @_;
898 137 $file = resolv_path($file);
899  
900 137 Rex::Logger::debug("Opening file: $file for reading.");
901  
902 137 my $fh = Rex::Interface::File->create;
903  
904 137 ▶ if ( !$fh->open( "<", $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open("<", $file)) 0 137 137
905 0 Rex::Logger::debug("Can't open $file for reading.");
906 0 die("Can't open $file for reading.");
907 }
908  
909 137 return Rex::FS::File->new( fh => $fh );
910 }
911  
912 =head2 cat($file_name)
913  
914 This function returns the complete content of $file_name as a string.
915  
916 print cat "/etc/passwd";
917  
918 =cut
919  
920 86 ▶sub cat {
Subroutine cat: called CC=2 SCAR=7.1
Pod documented
921 86 my ($file) = @_;
922 86 $file = resolv_path($file);
923  
924 86 my $fh = file_read($file);
925 86 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 86 86
926 0 die("Can't open $file for reading");
927 }
928 86 my $content = $fh->read_all;
929 86 $fh->close;
930  
931 86 return $content;
932 }
933  
934 =head2 delete_lines_matching($file, $regexp)
935  
936 Delete lines that match $regexp in $file.
937  
938 task "clean-logs", sub {
939 delete_lines_matching "/var/log/auth.log" => "root";
940 };
941  
942 =cut
943  
944 1 ▶sub delete_lines_matching {
Subroutine delete_lines_matching: called CC=9 SCAR=23.4
Pod documented
945 1 my ( $file, @m ) = @_;
946 1 $file = resolv_path($file);
947  
948 Rex::get_current_connection()->{reporter}
949 1 ->report_resource_start( type => "delete_lines_matching", name => $file );
950  
951 1 for (@m) {
952 1 ▶ if ( ref($_) ne "Regexp" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ ne "Regexp") 1 0 1
953 1 $_ = qr{\Q$_\E};
954 }
955 }
956  
957 1 my $fs = Rex::Interface::Fs->create;
958  
959 1 my %stat = $fs->stat($file);
960  
961 1 ▶ if ( !$fs->is_file($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fs->is_file($file)) 0 1 1
962 0 Rex::Logger::info("File: $file not found.");
963 0 die("$file not found");
964 }
965  
966 1 ▶ if ( !$fs->is_writable($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fs->is_writable($file)) 0 1 1
967 0 Rex::Logger::info("File: $file not writable.");
968 0 die("$file not writable");
969 }
970  
971 1 my $nl = $/;
972 1 my @content = split( /$nl/, cat($file) );
973  
974 1 my $old_md5 = "";
975 1 eval { $old_md5 = md5($file); };
976  
977 1 my @new_content;
978  
979 OUT:
980 1 for my $line (@content) {
981 IN:
982 1 for my $match (@m) {
983 1 ▶ if ( $line =~ $match ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$match/u) 1 0 1
984 1 next OUT;
985 }
986 }
987  
988 0 push @new_content, $line;
989 }
990  
991 file $file,
992 content => join( $nl, @new_content ),
993 owner => $stat{uid},
994 group => $stat{gid},
995 1 mode => $stat{mode};
996  
997 1 my $new_md5 = "";
998 1 eval { $new_md5 = md5($file); };
999  
1000 1 ▶ if ( $new_md5 ne $old_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_md5 ne $old_md5) { } 1 0 1
1001 Rex::get_current_connection()->{reporter}->report(
1002 1 changed => 1,
1003 message => "Content changed.",
1004 );
1005 }
1006 else {
1007 0 Rex::get_current_connection()->{reporter}->report( changed => 0, );
1008 }
1009  
1010 Rex::get_current_connection()->{reporter}
1011 1 ->report_resource_end( type => "delete_lines_matching", name => $file );
1012 }
1013  
1014 =head2 delete_lines_according_to($search, $file [, @options])
1015  
1016 This is the successor of the delete_lines_matching() function. This function also allows the usage of on_change and on_no_change hooks.
1017  
1018 It will search for $search in $file and remove the found lines. If on_change hook is present it will execute this if the file was changed.
1019  
1020 task "cleanup", "server1", sub {
1021 delete_lines_according_to qr{^foo:}, "/etc/passwd",
1022 on_change => sub {
1023 say "removed user foo.";
1024 };
1025 };
1026  
1027 =cut
1028  
1029 0 ▶sub delete_lines_according_to {
Subroutine delete_lines_according_to: not called CC=9 SCAR=45.0
Pod documented
1030 0 my ( $search, $file, @options ) = @_;
1031 0 $file = resolv_path($file);
1032  
1033 0 my $option = {@options};
1034 0 ▶ my $on_change = $option->{on_change} || undef;
Condition0%0/2
$option->{'on_change'} || undef
l!l
00
MC/DC0%0/1
$option->{'on_change'} || undef
$option->{'on_change'}
Truth table0%0/2
A
A= $option->{'on_change'}
Aresult
11
00
1035 0 ▶ my $on_no_change = $option->{on_no_change} || undef;
Condition0%0/2
$option->{'on_no_change'} || undef
l!l
00
MC/DC0%0/1
$option->{'on_no_change'} || undef
$option->{'on_no_change'}
Truth table0%0/2
A
A= $option->{'on_no_change'}
Aresult
11
00
1036  
1037 0 my ( $old_md5, $new_md5 );
1038  
1039 0 ▶ if ($on_change) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_change) 0 0 0
1040 0 $old_md5 = md5($file);
1041 }
1042  
1043 0 delete_lines_matching( $file, $search );
1044  
1045 0 ▶ if ( $on_change || $on_no_change ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_change or $on_no_change) 0 0 0
Condition0%0/3
$on_change or $on_no_change
l!l&&r!l&&!r
000
MC/DC0%0/2
$on_change or $on_no_change
$on_change $on_no_change
Truth table0%0/3
A or B
A= $on_changeB= $on_no_change
ABresult
1X1
011
000
1046 0 $new_md5 = md5($file);
1047  
1048 0 ▶ if ( $old_md5 ne $new_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_md5 ne $new_md5) { } 0 0 0
1049 0 ▶ &$on_change($file) if $on_change;
Branch: 1 branch
BranchTrueFalseTotal
if $on_change 0 0 0
1050 }
1051 else {
1052 0 ▶ &$on_no_change($file) if $on_no_change;
Branch: 1 branch
BranchTrueFalseTotal
if $on_no_change 0 0 0
1053 }
1054 }
1055  
1056 }
1057  
1058 =head2 append_if_no_such_line($file, $new_line [, @regexp])
1059  
1060 Append $new_line to $file if none in @regexp is found. If no regexp is
1061 supplied, the line is appended unless there is already an identical line
1062 in $file.
1063  
1064 task "add-group", sub {
1065 append_if_no_such_line "/etc/groups", "mygroup:*:100:myuser1,myuser2", on_change => sub { service sshd => "restart"; };
1066 };
1067  
1068 Since 0.42 you can use named parameters as well
1069  
1070 task "add-group", sub {
1071 append_if_no_such_line "/etc/groups",
1072 line => "mygroup:*:100:myuser1,myuser2",
1073 regexp => qr{^mygroup},
1074 on_change => sub {
1075 say "file was changed, do something.";
1076 };
1077  
1078 append_if_no_such_line "/etc/groups",
1079 line => "mygroup:*:100:myuser1,myuser2",
1080 regexp => [qr{^mygroup:}, qr{^ourgroup:}]; # this is an OR
1081 };
1082  
1083 =cut
1084  
1085 14 ▶sub append_if_no_such_line {
Subroutine append_if_no_such_line: called CC=1 SCAR=0.0
Pod documented
1086 14 _append_or_update( 'append_if_no_such_line', @_ );
1087 }
1088  
1089 =head2 append_or_amend_line($file, $line [, @regexp])
1090  
1091 Similar to L<append_if_no_such_line>, but if the line in the regexp is
1092 found, it will be updated. Otherwise, it will be appended.
1093  
1094 task "update-group", sub {
1095 append_or_amend_line "/etc/groups",
1096 line => "mygroup:*:100:myuser3,myuser4",
1097 regexp => qr{^mygroup},
1098 on_change => sub {
1099 say "file was changed, do something.";
1100 },
1101 on_no_change => sub {
1102 say "file was not changed, do something.";
1103 };
1104 };
1105  
1106 =cut
1107  
1108 5 ▶sub append_or_amend_line {
Subroutine append_or_amend_line: called CC=1 SCAR=0.0
Pod documented
1109 5 _append_or_update( 'append_or_amend_line', @_ );
1110 }
1111  
1112 19 ▶sub _append_or_update {
Subroutine _append_or_update: called CC=36 SCAR=37.1
1113 19 my $action = shift;
1114 19 my $file = shift;
1115  
1116 19 $file = resolv_path($file);
1117 19 my ( $new_line, @m );
1118  
1119 # check if parameters are in key => value format
1120 19 my ( $option, $on_change, $on_no_change );
1121  
1122 Rex::get_current_connection()->{reporter}
1123 19 ->report_resource_start( type => $action, name => $file );
1124  
1125 eval {
1126 49 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1127 19 $option = {@_};
1128  
1129 # if there is no line parameter, it is the old parameter format
1130 # so go dieing
1131 19 ▶ if ( !exists $option->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $option->{"line"}) 4 15 19
1132 4 die;
1133 }
1134 15 $new_line = $option->{line};
1135 15 ▶ if ( exists $option->{regexp} && ref $option->{regexp} eq "Regexp" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $option->{'regexp'} and ref $option->{'regexp'} eq 'Regexp') { } 10 5 15
elsif (ref $option->{'regexp'} eq 'ARRAY') { } 2 3 5
Condition100%3/3
exists $option->{"regexp"} and ref $option->{"regexp"} eq "Regexp"
!ll&&!rl&&r
3210
MC/DC100%2/2
exists $option->{"regexp"} and ref $option->{"regexp"} eq "Regexp"
exists $option->{"regexp"} ref $option->{"regexp"} eq "Regexp"
Truth table100%3/3
A and B
A= exists $option->{"regexp"}B= ref $option->{"regexp"} eq "Regexp"
ABresult
0X0
100
111
1136 10 @m = ( $option->{regexp} );
1137 }
1138 elsif ( ref $option->{regexp} eq "ARRAY" ) {
1139 2 @m = @{ $option->{regexp} };
1140 }
1141 15 ▶ $on_change = $option->{on_change} || undef;
Condition50%1/2
$option->{'on_change'} || undef
l!l
015
MC/DC0%0/1
$option->{'on_change'} || undef
$option->{'on_change'}
Truth table50%1/2
A
A= $option->{'on_change'}
Aresult
11
00
1142 15 ▶ $on_no_change = $option->{on_no_change} || undef;
Condition50%1/2
$option->{'on_no_change'} || undef
l!l
015
MC/DC0%0/1
$option->{'on_no_change'} || undef
$option->{'on_no_change'}
Truth table50%1/2
A
A= $option->{'on_no_change'}
Aresult
11
00
1143 15 1;
1144 19 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { no warnings; $option = {@_}; unless (exists $option->{"line"}) { die; } ; $new_line = $option->{"line"}; if (exists $option->{"regexp"} and ref $option->{"regexp"} eq "Regexp") { @m = $option->{"regexp"}; } elsif (ref $option->{"regexp"} eq "ARRAY") { @m = @{$option->{"regexp"};}; } ; $on_change = $option->{"on_change"} || undef; $on_no_change = $option->{"on_no_change"} || undef; 1 } } or do { ($new_line, @m) = ; foreach my $option (["on_change", \$on_change], ["on_no_change", \$on_no_change]) { for (my $i = 0; $i < $#m; ++$i) { if ($m[$i] eq $option->[0] and ref $m[$i + 1] eq "CODE") { ${$$option[1];} = $m[$i + 1]; splice @m, $i, 2; last; } ; } ; } } 15 4 19
1145 4 ( $new_line, @m ) = @_;
1146  
1147 # check if something in @m (the regexpes) is named on_change or on_no_change
1148 4 for my $option ( [ on_change => \$on_change ],
1149 [ on_no_change => \$on_no_change ] )
1150 {
1151 8 ▶ for ( my $i = 0 ; $i < $#m ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#m) 8 3 11
1152 8 ▶ if ( $m[$i] eq $option->[0] && ref( $m[ $i + 1 ] ) eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m[$i] eq $option->[0] and ref $m[$i + 1] eq "CODE") 5 3 8
Condition66%2/3
$m[$i] eq $option->[0] and ref $m[$i + 1] eq "CODE"
!ll&&!rl&&r
305
MC/DC50%1/2
$m[$i] eq $option->[0] and ref $m[$i + 1] eq "CODE"
$m[$i] eq $option->[0] ref $m[$i + 1] eq "CODE"
Truth table66%2/3
A and B
A= $m[$i] eq $option->[0]B= ref $m[$i + 1] eq "CODE"
ABresult
0X0
100
111
1153 5 ${ $option->[1] } = $m[ $i + 1 ];
1154 5 splice( @m, $i, 2 );
1155 5 last;
1156 }
1157 }
1158 }
1159 };
1160  
1161 19 ▶ unless ( defined $new_line ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new_line) 0 19 19
1162 0 my ( undef, undef, undef, $subroutine ) = caller(1);
1163 0 $subroutine =~ s/^.*:://;
1164 0 die "Undefined new line while trying to run $subroutine on $file";
1165 }
1166  
1167 19 my $fs = Rex::Interface::Fs->create;
1168  
1169 19 my %stat = $fs->stat($file);
1170  
1171 19 my ( $old_md5, $ret );
1172 19 $old_md5 = md5($file);
1173  
1174 # slow but secure way
1175 19 my $content;
1176 eval {
1177 19 $content = [ split( /\n/, cat($file) ) ];
1178 19 1;
1179 19 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $content = [split(/\n/u, cat($file), 0)]; 1 } } or do { $ret = 1 } 19 0 19
1180 0 $ret = 1;
1181 };
1182  
1183 19 ▶ if ( !@m ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@m) 5 14 19
1184 5 push @m, qr{\Q$new_line\E};
1185 }
1186  
1187 19 my $found;
1188 19 for my $line ( 0 .. $#{$content} ) {
1189 129 for my $match (@m) {
1190 144 ▶ if ( ref($match) ne "Regexp" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $match ne "Regexp") 0 144 144
1191 0 $match = qr{$match};
1192 }
1193 144 ▶ if ( $content->[$line] =~ $match ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->[$line] =~ /$match/u) 9 135 144
1194 9 $found = 1;
1195 9 ▶ last if $action eq 'append_if_no_such_line';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "append_if_no_such_line" 6 3 9
1196 3 $content->[$line] = "$new_line";
1197 }
1198 }
1199 }
1200  
1201 19 my $new_md5;
1202 19 ▶ if ( $action eq 'append_if_no_such_line' && $found ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq 'append_if_no_such_line' and $found) { } 6 13 19
Condition100%3/3
$action eq "append_if_no_such_line" and $found
!ll&&!rl&&r
586
MC/DC100%2/2
$action eq "append_if_no_such_line" and $found
$action eq "append_if_no_such_line" $found
Truth table100%3/3
A and B
A= $action eq "append_if_no_such_line"B= $found
ABresult
0X0
100
111
1203 6 $new_md5 = $old_md5;
1204 }
1205 else {
1206 13 ▶ push @$content, "$new_line" unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 10 3 13
1207  
1208 file $file,
1209 content => join( "\n", @$content ),
1210 owner => $stat{uid},
1211 group => $stat{gid},
1212 13 mode => $stat{mode};
1213 13 $new_md5 = md5($file);
1214 }
1215  
1216 19 ▶ if ( $on_change || $on_no_change ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_change or $on_no_change) 3 16 19
Condition66%2/3
$on_change or $on_no_change
l!l&&r!l&&!r
3016
MC/DC50%1/2
$on_change or $on_no_change
$on_change $on_no_change
Truth table66%2/3
A or B
A= $on_changeB= $on_no_change
ABresult
1X1
011
000
1217 3 ▶ if ( $old_md5 && $new_md5 && $old_md5 ne $new_md5 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($old_md5 and $new_md5 and $old_md5 ne $new_md5) { } 1 2 3
elsif ($on_no_change) { } 2 0 2
Condition50%3/6
$old_md5 and $new_md5 and $old_md5 ne $new_md5
!ll&&!rl&&r
021
$old_md5 and $new_md5
!ll&&!rl&&r
003
MC/DC33%1/3
$old_md5 and $new_md5 and $old_md5 ne $new_md5
$old_md5 $new_md5 $old_md5 ne $new_md5
Truth table50%2/4
A and B and C
A= $old_md5B= $new_md5C= $old_md5 ne $new_md5
ABCresult
0XX0
10X0
1100
1111
1218 1 ▶ if ($on_change) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_change) 1 0 1
1219 1 ▶ $old_md5 ||= "";
Condition50%1/2
$old_md5 ||= ""
l!l
10
MC/DC0%0/1
$old_md5 ||= ""
$old_md5
Truth table50%1/2
A
A= $old_md5
Aresult
11
00
1220 1 ▶ $new_md5 ||= "";
Condition50%1/2
$new_md5 ||= ""
l!l
10
MC/DC0%0/1
$new_md5 ||= ""
$new_md5
Truth table50%1/2
A
A= $new_md5
Aresult
11
00
1221  
1222 1 Rex::Logger::debug("File $file has been changed... Running on_change");
1223 1 Rex::Logger::debug("old: $old_md5");
1224 1 Rex::Logger::debug("new: $new_md5");
1225 1 &$on_change($file);
1226 }
1227 }
1228 elsif ($on_no_change) {
1229 2 ▶ $new_md5 ||= "";
Condition50%1/2
$new_md5 ||= ""
l!l
20
MC/DC0%0/1
$new_md5 ||= ""
$new_md5
Truth table50%1/2
A
A= $new_md5
Aresult
11
00
1230  
1231 2 Rex::Logger::debug(
1232 "File $file has not been changed (md5 $new_md5)... Running on_no_change"
1233 );
1234 2 &$on_no_change($file);
1235 }
1236 }
1237  
1238 19 ▶ if ( $old_md5 && $new_md5 && $old_md5 ne $new_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_md5 and $new_md5 and $old_md5 ne $new_md5) { } 13 6 19
Condition50%3/6
$old_md5 and $new_md5 and $old_md5 ne $new_md5
!ll&&!rl&&r
0613
$old_md5 and $new_md5
!ll&&!rl&&r
0019
MC/DC33%1/3
$old_md5 and $new_md5 and $old_md5 ne $new_md5
$old_md5 $new_md5 $old_md5 ne $new_md5
Truth table50%2/4
A and B and C
A= $old_md5B= $new_md5C= $old_md5 ne $new_md5
ABCresult
0XX0
10X0
1100
1111
1239 Rex::get_current_connection()->{reporter}->report(
1240 13 changed => 1,
1241 message => "Content changed.",
1242 );
1243 }
1244 else {
1245 6 Rex::get_current_connection()->{reporter}->report( changed => 0, );
1246 }
1247  
1248 Rex::get_current_connection()->{reporter}
1249 19 ->report_resource_end( type => $action, name => $file );
1250 }
1251  
1252 =head2 extract($file [, %options])
1253  
1254 This function extracts a file. The target directory optionally specified with the `to` option will be created automatically.
1255  
1256 Supported formats are .box, .tar, .tar.gz, .tgz, .tar.Z, .tar.bz2, .tbz2, .zip, .gz, .bz2, .war, .jar.
1257  
1258 task prepare => sub {
1259 extract "/tmp/myfile.tar.gz",
1260 owner => "root",
1261 group => "root",
1262 to => "/etc";
1263  
1264 extract "/tmp/foo.tgz",
1265 type => "tgz",
1266 mode => "g+rwX";
1267 };
1268  
1269 Can use the type=> option if the file suffix has been changed. (types are tar, tgz, tbz, zip, gz, bz2)
1270  
1271 =cut
1272  
1273 0 ▶sub extract {
Subroutine extract: not called CC=22 SCAR=62.3
Pod documented
1274 0 my ( $file, %option ) = @_;
1275 0 $file = resolv_path($file);
1276  
1277 0 my $pre_cmd = "";
1278 0 my $to = ".";
1279 0 my $type = "";
1280  
1281 0 ▶ if ( $option{chdir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"chdir"}) 0 0 0
1282 0 $to = $option{chdir};
1283 }
1284  
1285 0 ▶ if ( $option{to} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"to"}) 0 0 0
1286 0 $to = $option{to};
1287 }
1288 0 $to = resolv_path($to);
1289  
1290 0 ▶ if ( $option{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"type"}) 0 0 0
1291 0 $type = $option{type};
1292 }
1293  
1294 0 Rex::Commands::Fs::mkdir($to);
1295 0 $pre_cmd = "cd $to; ";
1296  
1297 0 my $exec = Rex::Interface::Exec->create;
1298 0 my $cmd = "";
1299  
1300 0 ▶ if ( $type eq 'tgz'
Branch: 6 branches
BranchTrueFalseTotal
if ($type eq 'tgz' or $file =~ /\.tar\.gz$/u or $file =~ /\.tgz$/u or $file =~ /\.tar\.Z$/u) { } 0 0 0
elsif ($type eq 'tbz' or $file =~ /\.tar\.bz2/u or $file =~ /\.tbz2/u) { } 0 0 0
elsif ($type eq 'tar' or $file =~ /\.(tar|box)/u) { } 0 0 0
elsif ($type eq 'zip' or $file =~ /\.(zip|war|jar)$/u) { } 0 0 0
elsif ($type eq 'gz' or $file =~ /\.gz$/u) { } 0 0 0
elsif ($type eq 'bz2' or $file =~ /\.bz2$/u) { } 0 0 0
Condition0%0/9
$type eq "tgz" or $file =~ /\.tar\.gz$/u or $file =~ /\.tgz$/u or $file =~ /\.tar\.Z$/u
l!l&&r!l&&!r
000
$type eq "tgz" or $file =~ /\.tar\.gz$/u or $file =~ /\.tgz$/u
l!l&&r!l&&!r
000
$type eq "tgz" or $file =~ /\.tar\.gz$/u
l!l&&r!l&&!r
000
MC/DC0%0/4
$type eq "tgz" or $file =~ /\.tar\.gz$/u or $file =~ /\.tgz$/u or $file =~ /\.tar\.Z$/u
$type eq "tgz" $file =~ /\.tar\.gz$/u $file =~ /\.tgz$/u $file =~ /\.tar\.Z$/u
Truth table0%0/5
A or B or C or D
A= $type eq "tgz"B= $file =~ /\.tar\.gz$/uC= $file =~ /\.tgz$/uD= $file =~ /\.tar\.Z$/u
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1301 || $file =~ m/\.tar\.gz$/
1302 || $file =~ m/\.tgz$/
1303 || $file =~ m/\.tar\.Z$/ )
1304 {
1305 0 $cmd = "${pre_cmd}gunzip -c $file | tar -xf -";
1306 }
1307 ▶ elsif ( $type eq 'tbz' || $file =~ m/\.tar\.bz2/ || $file =~ m/\.tbz2/ ) {
Condition0%0/6
$type eq "tbz" or $file =~ /\.tar\.bz2/u or $file =~ /\.tbz2/u
l!l&&r!l&&!r
000
$type eq "tbz" or $file =~ /\.tar\.bz2/u
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "tbz" or $file =~ /\.tar\.bz2/u or $file =~ /\.tbz2/u
$type eq "tbz" $file =~ /\.tar\.bz2/u $file =~ /\.tbz2/u
Truth table0%0/4
A or B or C
A= $type eq "tbz"B= $file =~ /\.tar\.bz2/uC= $file =~ /\.tbz2/u
ABCresult
1XX1
01X1
0011
0000
1308 0 $cmd = "${pre_cmd}bunzip2 -c $file | tar -xf -";
1309 }
1310 ▶ elsif ( $type eq 'tar' || $file =~ m/\.(tar|box)/ ) {
Condition0%0/3
$type eq "tar" or $file =~ /\.(tar|box)/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "tar" or $file =~ /\.(tar|box)/u
$type eq "tar" $file =~ /\.(tar|box)/u
Truth table0%0/3
A or B
A= $type eq "tar"B= $file =~ /\.(tar|box)/u
ABresult
1X1
011
000
1311 0 $cmd = "${pre_cmd}tar -xf $file";
1312 }
1313 ▶ elsif ( $type eq 'zip' || $file =~ m/\.(zip|war|jar)$/ ) {
Condition0%0/3
$type eq "zip" or $file =~ /\.(zip|war|jar)$/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "zip" or $file =~ /\.(zip|war|jar)$/u
$type eq "zip" $file =~ /\.(zip|war|jar)$/u
Truth table0%0/3
A or B
A= $type eq "zip"B= $file =~ /\.(zip|war|jar)$/u
ABresult
1X1
011
000
1314 0 $cmd = "${pre_cmd}unzip -o $file";
1315 }
1316 ▶ elsif ( $type eq 'gz' || $file =~ m/\.gz$/ ) {
Condition0%0/3
$type eq "gz" or $file =~ /\.gz$/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "gz" or $file =~ /\.gz$/u
$type eq "gz" $file =~ /\.gz$/u
Truth table0%0/3
A or B
A= $type eq "gz"B= $file =~ /\.gz$/u
ABresult
1X1
011
000
1317 0 $cmd = "${pre_cmd}gunzip -f $file";
1318 }
1319 ▶ elsif ( $type eq 'bz2' || $file =~ m/\.bz2$/ ) {
Condition0%0/3
$type eq "bz2" or $file =~ /\.bz2$/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "bz2" or $file =~ /\.bz2$/u
$type eq "bz2" $file =~ /\.bz2$/u
Truth table0%0/3
A or B
A= $type eq "bz2"B= $file =~ /\.bz2$/u
ABresult
1X1
011
000
1320 0 $cmd = "${pre_cmd}bunzip2 -f $file";
1321 }
1322 else {
1323 0 Rex::Logger::info("File not supported.");
1324 0 die("File ($file) not supported.");
1325 }
1326  
1327 0 $exec->exec($cmd);
1328  
1329 0 my $fs = Rex::Interface::Fs->create;
1330 0 ▶ if ( $option{owner} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"owner"}) 0 0 0
1331 0 $fs->chown( $option{owner}, $to, recursive => 1 );
1332 }
1333  
1334 0 ▶ if ( $option{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"group"}) 0 0 0
1335 0 $fs->chgrp( $option{group}, $to, recursive => 1 );
1336 }
1337  
1338 0 ▶ if ( $option{mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"mode"}) 0 0 0
1339 0 $fs->chmod( $option{mode}, $to, recursive => 1 );
1340 }
1341  
1342 }
1343  
1344 =head2 sed($search, $replace, $file [, %options])
1345  
1346 Search some string in a file and replace it.
1347  
1348 task sar => sub {
1349 # this will work line by line
1350 sed qr{search}, "replace", "/var/log/auth.log";
1351  
1352 # to use it in a multiline way
1353 sed qr{search}, "replace", "/var/log/auth.log",
1354 multiline => TRUE;
1355 };
1356  
1357 Like similar file management commands, it also supports C<on_change> and C<on_no_change> hooks.
1358  
1359 =cut
1360  
1361 11 ▶sub sed {
Subroutine sed: called CC=6 SCAR=18.1
Pod documented
1362 11 my ( $search, $replace, $file, @option ) = @_;
1363 11 $file = resolv_path($file);
1364 11 my $options = {};
1365  
1366 Rex::get_current_connection()->{reporter}
1367 11 ->report_resource_start( type => "sed", name => $file );
1368  
1369 11 ▶ if ( ref( $option[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $option[0]) { } 0 11 11
1370 0 $options = $option[0];
1371 }
1372 else {
1373 11 $options = {@option};
1374 }
1375  
1376 11 ▶ my $on_change = $options->{"on_change"} || undef;
Condition50%1/2
$options->{'on_change'} || undef
l!l
011
MC/DC0%0/1
$options->{'on_change'} || undef
$options->{'on_change'}
Truth table50%1/2
A
A= $options->{'on_change'}
Aresult
11
00
1377 11 ▶ my $on_no_change = $options->{"on_no_change"} || undef;
Condition50%1/2
$options->{'on_no_change'} || undef
l!l
011
MC/DC0%0/1
$options->{'on_no_change'} || undef
$options->{'on_no_change'}
Truth table50%1/2
A
A= $options->{'on_no_change'}
Aresult
11
00
1378  
1379 11 my @content;
1380  
1381 11 ▶ if ( exists $options->{multiline} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{'multiline'}) { } 1 10 11
1382 1 $content[0] = cat($file);
1383 1 $content[0] =~ s/$search/$replace/gms;
1384 }
1385 else {
1386 10 @content = split( /\n/, cat($file) );
1387 10 for (@content) {
1388 109 s/$search/$replace/;
1389 }
1390 }
1391  
1392 11 my $fs = Rex::Interface::Fs->create;
1393 11 my %stat = $fs->stat($file);
1394  
1395 my $ret = file(
1396 $file,
1397 content => join( "\n", @content ),
1398 on_change => $on_change,
1399 on_no_change => $on_no_change,
1400 owner => $stat{uid},
1401 group => $stat{gid},
1402 mode => $stat{mode}
1403 11 );
1404  
1405 Rex::get_current_connection()->{reporter}
1406 11 ->report_resource_end( type => "sed", name => $file );
1407  
1408 11 return $ret;
1409 }
1410  
1411 49 1;
« lib/Rex/Virtualization/LibVirt/guestinfo.pm ↑ Summary lib/Rex/Interface/Shell/Csh.pm »