lib/Rex/Commands/Fs.pm

filter: Statementstmt 44.8% 178 / 397 Branchbran 23.6% 34 / 144 Conditioncond 21.2% 17 / 80 MC/DCmcdc 6.5% 3 / 46 Subroutinesub 62.5% 30 / 48 Podpod 100% 29 / 29 totaltotal 39.1% 291 / 744 CC 106 SCAR 49.4
CC
106
Cov
37%
mount
600.0
rename
210.0
mkdir
89.7
CRAP 139.5
« lib/Rex/Box/VBox.pm ↑ Summary lib/Rex/Interface/Exec/Sudo.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Commands::Fs - File system commands
8  
9 =head1 DESCRIPTION
10  
11 With this module you can do file system tasks like creating directories, deleting or moving files, and more.
12  
13 =head1 SYNOPSIS
14  
15 my @files = list_files "/etc";
16  
17 unlink("/tmp/file");
18  
19 rmdir("/tmp");
20 mkdir("/tmp");
21  
22 my %stat = stat("/etc/passwd");
23  
24 my $link = readlink("/path/to/a/link");
25 symlink("/source", "/dest");
26  
27 rename("oldname", "newname");
28  
29 chdir("/tmp");
30  
31 is_file("/etc/passwd");
32 is_dir("/etc");
33 is_writeable("/tmp");
34 is_writable("/tmp");
35  
36 chmod 755, "/tmp";
37 chown "user", "/tmp";
38 chgrp "group", "/tmp";
39  
40  
41 =head1 EXPORTED FUNCTIONS
42  
43 =cut
44  
45 package Rex::Commands::Fs;
46  
47 81 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 81 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 81 our $VERSION = '1.16.1'; # VERSION
51  
52 81 require Rex::Exporter;
53 81 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 81 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 81 ▶use Rex::Helper::File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 81 ▶use Rex::Helper::SSH2;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 81 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 81 ▶use Rex::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 81 ▶use Rex::Interface::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 81 ▶use Rex::Interface::Exec;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 81 ▶use Rex::Interface::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 81 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 81 ▶use Rex::Commands::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 81 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
66 81 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 81 @EXPORT = qw(list_files ls
69 unlink rm rmdir mkdir stat readlink symlink ln rename mv chdir cd cp
70 chown chgrp chmod
71 is_file is_dir is_readable is_writeable is_writable is_symlink
72 df du
73 mount umount
74 glob);
75  
76 81 ▶use vars qw(%file_handles);
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 =head2 Changing content
79  
80 These commands are supposed to change the contents of the file system.
81  
82 =head3 symlink($from, $to)
83  
84 This function will create a symbolic link from C<$from> to C<$to>.
85  
86 task "symlink", "server01", sub {
87 symlink("/var/www/versions/1.0.0", "/var/www/html");
88 };
89  
90 =cut
91  
92 14 ▶sub symlink {
Subroutine symlink: called CC=4 SCAR=13.9
Pod documented
93 14 my ( $from, $to ) = @_;
94 14 $from = resolv_path($from);
95 14 $to = resolv_path($to);
96  
97 Rex::get_current_connection()->{reporter}
98 14 ->report_resource_start( type => "symlink", name => $to );
99  
100 14 my $fs = Rex::Interface::Fs->create;
101 14 ▶ if ( $fs->is_symlink($to) && $fs->readlink($to) eq $from ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs->is_symlink($to) and $fs->readlink($to) eq $from) { } 11 3 14
Condition66%2/3
$fs->is_symlink($to) and $fs->readlink($to) eq $from
!ll&&!rl&&r
3011
MC/DC50%1/2
$fs->is_symlink($to) and $fs->readlink($to) eq $from
$fs->is_symlink($to) $fs->readlink($to) eq $from
Truth table66%2/3
A and B
A= $fs->is_symlink($to)B= $fs->readlink($to) eq $from
ABresult
0X0
100
111
102 11 Rex::get_current_connection()->{reporter}->report( changed => 0, );
103 }
104 else {
105 3 ▶ $fs->ln( $from, $to ) or die("Can't link $from -> $to");
Branch: 1 branch
BranchTrueFalseTotal
$fs->ln($from, $to) or die "Can't link $from -> $to" 3 0 3
106 Rex::get_current_connection()->{reporter}
107 3 ->report( changed => 1, message => "Symlink created: $from -> $to." );
108 }
109  
110 Rex::get_current_connection()->{reporter}
111 14 ->report_resource_end( type => "symlink", name => $to );
112  
113 14 return 1;
114 }
115  
116 =head3 ln($from, $to)
117  
118 C<ln> is an alias for C<symlink>
119  
120 =cut
121  
122 0 ▶sub ln {
Subroutine ln: not called CC=1 SCAR=6.9
Pod documented
123 0 &symlink(@_);
124 }
125  
126 =head3 unlink($file)
127  
128 This function will remove the given C<$file>.
129  
130 task "unlink", "server01", sub {
131 unlink("/tmp/testfile");
132 };
133  
134 =cut
135  
136 7 ▶sub unlink {
Subroutine unlink: called CC=7 SCAR=21.8
Pod documented
137 7 my @files = @_;
138  
139 7 my $f;
140 7 ▶ if ( ref $files[0] eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $files[0] eq 'ARRAY') { } 0 7 7
141 0 $f = $files[0];
142 }
143 else {
144 7 $f = \@files;
145 }
146  
147 7 ▶ if ( scalar @{$f} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$f;} == 1) { } 7 0 7
148 7 my $file = resolv_path $f->[0];
149 7 my $fs = Rex::Interface::Fs->create;
150  
151 Rex::get_current_connection()->{reporter}
152 7 ->report_resource_start( type => "unlink", name => $file );
153  
154 7 ▶ if ( $fs->is_file($file) || $fs->is_symlink($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs->is_file($file) or $fs->is_symlink($file)) { } 7 0 7
Condition33%1/3
$fs->is_file($file) or $fs->is_symlink($file)
l!l&&r!l&&!r
700
MC/DC0%0/2
$fs->is_file($file) or $fs->is_symlink($file)
$fs->is_file($file) $fs->is_symlink($file)
Truth table33%1/3
A or B
A= $fs->is_file($file)B= $fs->is_symlink($file)
ABresult
1X1
011
000
155 7 $fs->unlink($file);
156  
157 7 my $tmp_path = Rex::Config->get_tmp_dir;
158 7 ▶ if ( $file !~ m/^\Q$tmp_path\E[\/\\][a-z]+\.tmp$/ ) { # skip tmp rex files
Branch: 1 branch
BranchTrueFalseTotal
unless ($file =~ m(^\Q$tmp_path\E[/\\][a-z]+\.tmp$)u) 7 0 7
159 Rex::get_current_connection()->{reporter}
160 7 ->report( changed => 1, message => "File $file removed." );
161 }
162 }
163 else {
164 0 Rex::get_current_connection()->{reporter}->report( changed => 0, );
165 }
166  
167 Rex::get_current_connection()->{reporter}
168 7 ->report_resource_end( type => "unlink", name => $file );
169 }
170 else {
171 0 &unlink($_) for @{$f};
172 }
173  
174 }
175  
176 =head3 rm($file)
177  
178 This is an alias for C<unlink>.
179  
180 =cut
181  
182 0 ▶sub rm {
Subroutine rm: not called CC=1 SCAR=6.9
Pod documented
183 0 &unlink(@_);
184 }
185  
186 =head3 rmdir($dir)
187  
188 This function will remove the given directory.
189  
190 task "rmdir", "server01", sub {
191 rmdir("/tmp");
192 };
193  
194  
195 With Rex-0.45 and newer, please use the L<file|Rex::Commands::File#file> resource instead.
196  
197 task "prepare", sub {
198 file "/tmp",
199 ensure => "absent";
200 };
201  
202 =cut
203  
204 2 ▶sub rmdir {
Subroutine rmdir: called CC=7 SCAR=22.4
Pod documented
205 2 my @dirs = @_;
206  
207 2 my $d;
208 2 ▶ if ( ref $dirs[0] eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dirs[0] eq 'ARRAY') { } 0 2 2
209 0 $d = $dirs[0];
210 }
211 else {
212 2 $d = \@dirs;
213 }
214  
215 2 ▶ if ( scalar @{$d} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$d;} == 1) { } 2 0 2
216 2 my $dir = resolv_path $d->[0];
217 2 my $fs = Rex::Interface::Fs->create;
218  
219 Rex::get_current_connection()->{reporter}
220 2 ->report_resource_start( type => "rmdir", name => $dir );
221  
222 2 ▶ if ( !$fs->is_dir($dir) && $dir !~ m/[\*\[]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fs->is_dir($dir) || $dir =~ /[\*\[]/u) { } 0 2 2
Condition33%1/3
$fs->is_dir($dir) || $dir =~ /[\*\[]/u
l!l&&r!l&&!r
200
MC/DC0%0/2
$fs->is_dir($dir) || $dir =~ /[\*\[]/u
$fs->is_dir($dir) $dir =~ /[\*\[]/u
Truth table33%1/3
A || B
A= $fs->is_dir($dir)B= $dir =~ /[\*\[]/u
ABresult
1X1
011
000
223 0 Rex::get_current_connection()->{reporter}->report( changed => 0, );
224 }
225 else {
226 2 $fs->rmdir($dir);
227 2 ▶ if ( $fs->is_dir($dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs->is_dir($dir)) 0 2 2
228 0 die "Can't remove $dir.";
229 }
230  
231 Rex::get_current_connection()->{reporter}
232 2 ->report( changed => 1, message => "Directory $dir removed." );
233 }
234  
235 Rex::get_current_connection()->{reporter}
236 2 ->report_resource_end( type => "rmdir", name => $dir );
237 }
238 else {
239 0 &rmdir($_) for @{$d};
240 }
241 }
242  
243 =head3 mkdir($newdir)
244  
245 This function will create a new directory.
246  
247 The following options are supported:
248  
249 =over 4
250  
251 =item * owner
252  
253 =item * group
254  
255 =item * mode
256  
257 =item * on_change
258  
259 =back
260  
261 With Rex-0.45 and newer, please use the L<file|Rex::Commands::File#file> resource instead.
262  
263 task "prepare", sub {
264 file "/tmp",
265 ensure => "directory",
266 owner => "root",
267 group => "root",
268 mode => 1777;
269 };
270  
271 Direct usage:
272  
273 task "mkdir", "server01", sub {
274 mkdir "/tmp";
275  
276 mkdir "/tmp",
277 owner => "root",
278 group => "root",
279 mode => 1777;
280 };
281  
282 =cut
283  
284 5 ▶sub mkdir {
Subroutine mkdir: called CC=28 SCAR=45.0
Pod documented
285 5 Rex::Logger::debug("Creating directory $_[0]");
286 5 my $dir = shift;
287 5 $dir = resolv_path($dir);
288  
289 5 my $options = {@_};
290  
291 5 ▶ $options->{on_change} //= sub { };
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition66%2/3
$options->{"on_change"} //= sub { }
l!l&&r!l&&!r
230
MC/DC0%0/2
$options->{"on_change"} //= sub { }
$options->{"on_change"} sub { }
Truth table66%2/3
A //= B
A= $options->{"on_change"}B= sub { }
ABresult
1X1
011
000
292  
293 Rex::get_current_connection()->{reporter}
294 5 ->report_resource_start( type => "mkdir", name => $dir );
295  
296 5 my $fs = Rex::Interface::Fs->create;
297  
298 5 my $not_created = 0;
299 5 my %old_stat;
300 5 my $changed = 0;
301  
302 5 ▶ if ( $fs->is_dir($dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs->is_dir($dir)) 0 5 5
303 0 $not_created = 1;
304 0 %old_stat = &stat($dir);
305 }
306  
307 5 ▶ my $mode = $options->{"mode"} || 755;
Condition50%1/2
$options->{'mode'} || 755
l!l
05
MC/DC0%0/1
$options->{'mode'} || 755
$options->{'mode'}
Truth table50%1/2
A
A= $options->{'mode'}
Aresult
11
00
308 5 ▶ my $owner = $options->{"owner"} || "";
Condition50%1/2
$options->{'owner'} || ''
l!l
05
MC/DC0%0/1
$options->{'owner'} || ''
$options->{'owner'}
Truth table50%1/2
A
A= $options->{'owner'}
Aresult
11
00
309 5 ▶ my $group = $options->{"group"} || "";
Condition50%1/2
$options->{'group'} || ''
l!l
05
MC/DC0%0/1
$options->{'group'} || ''
$options->{'group'}
Truth table50%1/2
A
A= $options->{'group'}
Aresult
11
00
310 5 ▶ my $not_recursive = $options->{"not_recursive"} || 0;
Condition50%1/2
$options->{'not_recursive'} || 0
l!l
05
MC/DC0%0/1
$options->{'not_recursive'} || 0
$options->{'not_recursive'}
Truth table50%1/2
A
A= $options->{'not_recursive'}
Aresult
11
00
311  
312 5 ▶ if ($not_recursive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_recursive) { } 0 5 5
313 0 ▶ if ( !$fs->mkdir($dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fs->mkdir($dir)) 0 0 0
314 0 Rex::Logger::debug("Can't create directory $dir");
315 0 die("Can't create directory $dir");
316 }
317  
318 0 ▶ &chown( $owner, $dir ) if $owner;
Branch: 1 branch
BranchTrueFalseTotal
if $owner 0 0 0
319 0 ▶ &chgrp( $group, $dir ) if $group;
Branch: 1 branch
BranchTrueFalseTotal
if $group 0 0 0
320 0 ▶ &chmod( $mode, $dir ) if $mode;
Branch: 1 branch
BranchTrueFalseTotal
if $mode 0 0 0
321 }
322 else {
323 5 ▶ if ( !Rex::Helper::File::Spec->file_name_is_absolute($dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Rex::Helper::File::Spec"->file_name_is_absolute($dir)) 2 3 5
324 2 $dir = Rex::Helper::File::Spec->rel2abs($dir);
325 }
326  
327 5 my @directories = __splitdir($dir);
328 5 my $path_so_far = shift @directories;
329  
330 5 for my $part (@directories) {
331 18 $path_so_far = Rex::Helper::File::Spec->join( $path_so_far, $part );
332  
333 18 ▶ if ( !is_dir($path_so_far) && !is_file($path_so_far) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_dir($path_so_far) or is_file($path_so_far)) 5 13 18
Condition66%2/3
is_dir($path_so_far) or is_file($path_so_far)
l!l&&r!l&&!r
1305
MC/DC50%1/2
is_dir($path_so_far) or is_file($path_so_far)
is_dir($path_so_far) is_file($path_so_far)
Truth table66%2/3
A or B
A= is_dir($path_so_far)B= is_file($path_so_far)
ABresult
1X1
011
000
334 5 ▶ if ( !$fs->mkdir($path_so_far) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fs->mkdir($path_so_far)) 0 5 5
335 0 Rex::Logger::debug("Can't create directory $dir");
336 0 die("Can't create directory $dir");
337 }
338  
339 5 ▶ &chown( $owner, $path_so_far ) if $owner;
Branch: 1 branch
BranchTrueFalseTotal
if $owner 0 5 5
340 5 ▶ &chgrp( $group, $path_so_far ) if $group;
Branch: 1 branch
BranchTrueFalseTotal
if $group 0 5 5
341 5 ▶ &chmod( $mode, $path_so_far ) if $mode;
Branch: 1 branch
BranchTrueFalseTotal
if $mode 5 0 5
342 }
343 }
344 }
345  
346 5 my %new_stat = &stat($dir);
347  
348 5 ▶ if ( !$not_created ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($not_created) 5 0 5
349 Rex::get_current_connection()->{reporter}
350 5 ->report( changed => 1, message => "Directory created." );
351 5 $changed = 1;
352 }
353  
354 5 ▶ if ( %old_stat && $old_stat{uid} != $new_stat{uid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%old_stat and $old_stat{"uid"} != $new_stat{"uid"}) 0 5 5
Condition33%1/3
%old_stat and $old_stat{"uid"} != $new_stat{"uid"}
!ll&&!rl&&r
500
MC/DC0%0/2
%old_stat and $old_stat{"uid"} != $new_stat{"uid"}
%old_stat $old_stat{"uid"} != $new_stat{"uid"}
Truth table33%1/3
A and B
A= %old_statB= $old_stat{"uid"} != $new_stat{"uid"}
ABresult
0X0
100
111
355 Rex::get_current_connection()->{reporter}
356 0 ->report( changed => 1, message => "Owner updated." );
357 0 $changed = 1;
358 }
359  
360 5 ▶ if ( %old_stat && $old_stat{gid} != $new_stat{gid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%old_stat and $old_stat{"gid"} != $new_stat{"gid"}) 0 5 5
Condition33%1/3
%old_stat and $old_stat{"gid"} != $new_stat{"gid"}
!ll&&!rl&&r
500
MC/DC0%0/2
%old_stat and $old_stat{"gid"} != $new_stat{"gid"}
%old_stat $old_stat{"gid"} != $new_stat{"gid"}
Truth table33%1/3
A and B
A= %old_statB= $old_stat{"gid"} != $new_stat{"gid"}
ABresult
0X0
100
111
361 Rex::get_current_connection()->{reporter}
362 0 ->report( changed => 1, message => "Group updated." );
363 0 $changed = 1;
364 }
365  
366 5 ▶ if ( %old_stat && $old_stat{mode} ne $new_stat{mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%old_stat and $old_stat{"mode"} ne $new_stat{"mode"}) 0 5 5
Condition33%1/3
%old_stat and $old_stat{"mode"} ne $new_stat{"mode"}
!ll&&!rl&&r
500
MC/DC0%0/2
%old_stat and $old_stat{"mode"} ne $new_stat{"mode"}
%old_stat $old_stat{"mode"} ne $new_stat{"mode"}
Truth table33%1/3
A and B
A= %old_statB= $old_stat{"mode"} ne $new_stat{"mode"}
ABresult
0X0
100
111
367 Rex::get_current_connection()->{reporter}
368 0 ->report( changed => 1, message => "Mode updated." );
369 0 $changed = 1;
370 }
371  
372 5 ▶ if ( $changed == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed == 0) { } 0 5 5
373 0 Rex::get_current_connection()->{reporter}->report( changed => 0, );
374 }
375 else {
376 5 $options->{on_change}->($dir);
377 }
378  
379 Rex::get_current_connection()->{reporter}
380 5 ->report_resource_end( type => "mkdir", name => $dir );
381  
382 5 return 1;
383 }
384  
385 6 ▶sub __splitdir {
Subroutine __splitdir: called CC=1 SCAR=0.0
386 6 return Rex::Helper::File::Spec->splitdir(shift);
387 }
388  
389 =head3 chown($owner, $path)
390  
391 Change the owner of a file or a directory.
392  
393 chown "www-data", "/var/www/html";
394  
395 chown "www-data", "/var/www/html",
396 recursive => 1;
397  
398  
399 This command will not be reported.
400  
401 If you want to use reports, please use the L<file|Rex::Commands::File#file> resource instead.
402  
403 =cut
404  
405 0 ▶sub chown {
Subroutine chown: not called CC=2 SCAR=17.9
Pod documented
406 0 my ( $user, $file, @opts ) = @_;
407  
408 0 $file = resolv_path($file);
409 0 my $fs = Rex::Interface::Fs->create;
410 0 ▶ $fs->chown( $user, $file, @opts ) or die("Can't chown $file");
Branch: 1 branch
BranchTrueFalseTotal
$fs->chown($user, $file, @opts) or die "Can't chown $file" 0 0 0
411 }
412  
413 =head3 chgrp($group, $path)
414  
415 Change the group of a file or a directory.
416  
417 chgrp "nogroup", "/var/www/html";
418  
419 chgrp "nogroup", "/var/www/html",
420 recursive => 1;
421  
422  
423 This command will not be reported.
424  
425 If you want to use reports, please use the L<file|Rex::Commands::File#file> resource instead.
426  
427 =cut
428  
429 0 ▶sub chgrp {
Subroutine chgrp: not called CC=2 SCAR=17.9
Pod documented
430 0 my ( $group, $file, @opts ) = @_;
431 0 $file = resolv_path($file);
432  
433 0 my $fs = Rex::Interface::Fs->create;
434 0 ▶ $fs->chgrp( $group, $file, @opts ) or die("Can't chgrp $file");
Branch: 1 branch
BranchTrueFalseTotal
$fs->chgrp($group, $file, @opts) or die "Can't chgrp $file" 0 0 0
435 }
436  
437 =head3 chmod($mode, $path)
438  
439 Change the permissions of a file or a directory.
440  
441 chmod 755, "/var/www/html";
442  
443 chmod 755, "/var/www/html",
444 recursive => 1;
445  
446  
447 This command will not be reported.
448  
449 If you want to use reports, please use the L<file|Rex::Commands::File#file> resource instead.
450  
451 =cut
452  
453 5 ▶sub chmod {
Subroutine chmod: called CC=2 SCAR=7.0
Pod documented
454 5 my ( $mode, $file, @opts ) = @_;
455 5 $file = resolv_path($file);
456  
457 5 my $fs = Rex::Interface::Fs->create;
458 5 ▶ $fs->chmod( $mode, $file, @opts ) or die("Can't chmod $file");
Branch: 1 branch
BranchTrueFalseTotal
$fs->chmod($mode, $file, @opts) or die "Can't chmod $file" 5 0 5
459 }
460  
461 =head3 rename($old, $new)
462  
463 This function will rename C<$old> to C<$new>. Will return 1 on success and 0 on failure.
464  
465 task "rename", "server01", sub {
466 rename("/tmp/old", "/tmp/new");
467 };
468  
469 =cut
470  
471 0 ▶sub rename {
Subroutine rename: not called CC=14 SCAR=53.5
Pod documented
472 0 my ( $old, $new ) = @_;
473 0 $old = resolv_path($old);
474 0 $new = resolv_path($new);
475  
476 Rex::get_current_connection()->{reporter}
477 0 ->report_resource_start( type => "rename", name => "$old -> $new" );
478  
479 0 my $fs = Rex::Interface::Fs->create;
480  
481 0 my $old_present = 0;
482 0 ▶ if ( $fs->is_file($old) || $fs->is_dir($old) || $fs->is_symlink($old) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs->is_file($old) or $fs->is_dir($old) or $fs->is_symlink($old)) 0 0 0
Condition0%0/6
$fs->is_file($old) or $fs->is_dir($old) or $fs->is_symlink($old)
l!l&&r!l&&!r
000
$fs->is_file($old) or $fs->is_dir($old)
l!l&&r!l&&!r
000
MC/DC0%0/3
$fs->is_file($old) or $fs->is_dir($old) or $fs->is_symlink($old)
$fs->is_file($old) $fs->is_dir($old) $fs->is_symlink($old)
Truth table0%0/4
A or B or C
A= $fs->is_file($old)B= $fs->is_dir($old)C= $fs->is_symlink($old)
ABCresult
1XX1
01X1
0011
0000
483 0 $old_present = 1;
484 }
485  
486 0 Rex::Logger::debug("Renaming $old to $new");
487  
488 0 ▶ if ( !$fs->rename( $old, $new ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fs->rename($old, $new)) 0 0 0
489 0 Rex::Logger::info("Rename failed ($old -> $new)");
490 0 die("Rename failed $old -> $new");
491 }
492  
493 0 my $new_present = 0;
494 0 ▶ if ( $fs->is_file($new) || $fs->is_dir($new) || $fs->is_symlink($new) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs->is_file($new) or $fs->is_dir($new) or $fs->is_symlink($new)) 0 0 0
Condition0%0/6
$fs->is_file($new) or $fs->is_dir($new) or $fs->is_symlink($new)
l!l&&r!l&&!r
000
$fs->is_file($new) or $fs->is_dir($new)
l!l&&r!l&&!r
000
MC/DC0%0/3
$fs->is_file($new) or $fs->is_dir($new) or $fs->is_symlink($new)
$fs->is_file($new) $fs->is_dir($new) $fs->is_symlink($new)
Truth table0%0/4
A or B or C
A= $fs->is_file($new)B= $fs->is_dir($new)C= $fs->is_symlink($new)
ABCresult
1XX1
01X1
0011
0000
495 0 $new_present = 1;
496 }
497  
498 0 my $old_absent = 0;
499 0 ▶ if ( !( $fs->is_file($old) || $fs->is_dir($old) || $fs->is_symlink($old) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fs->is_file($old) or $fs->is_dir($old) or $fs->is_symlink($old)) 0 0 0
Condition0%0/6
$fs->is_file($old) or $fs->is_dir($old) or $fs->is_symlink($old)
l!l&&r!l&&!r
000
$fs->is_file($old) or $fs->is_dir($old)
l!l&&r!l&&!r
000
MC/DC0%0/3
$fs->is_file($old) or $fs->is_dir($old) or $fs->is_symlink($old)
$fs->is_file($old) $fs->is_dir($old) $fs->is_symlink($old)
Truth table0%0/4
A or B or C
A= $fs->is_file($old)B= $fs->is_dir($old)C= $fs->is_symlink($old)
ABCresult
1XX1
01X1
0011
0000
500 0 $old_absent = 1;
501 }
502  
503 0 ▶ if ( $old_present == 1 && $new_present == 1 && $old_absent == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_present == 1 and $new_present == 1 and $old_absent == 1) { } 0 0 0
Condition0%0/6
$old_present == 1 and $new_present == 1 and $old_absent == 1
!ll&&!rl&&r
000
$old_present == 1 and $new_present == 1
!ll&&!rl&&r
000
MC/DC0%0/3
$old_present == 1 and $new_present == 1 and $old_absent == 1
$old_present == 1 $new_present == 1 $old_absent == 1
Truth table0%0/4
A and B and C
A= $old_present == 1B= $new_present == 1C= $old_absent == 1
ABCresult
0XX0
10X0
1100
1111
504 0 Rex::get_current_connection()->{reporter}->report( changed => 1 );
505 }
506 else {
507 0 Rex::get_current_connection()->{reporter}->report( changed => 0 );
508 }
509  
510 Rex::get_current_connection()->{reporter}
511 0 ->report_resource_end( type => "rename", name => "$old -> $new" );
512 }
513  
514 =head3 mv($old, $new)
515  
516 C<mv> is an alias for C<rename>.
517  
518 =cut
519  
520 0 ▶sub mv {
Subroutine mv: not called CC=1 SCAR=6.9
Pod documented
521 0 return &rename(@_);
522 }
523  
524 =head3 cp($source, $destination)
525  
526 C<cp> will copy C<$source> to C<$destination> recursively.
527  
528 task "cp", "server01", sub {
529 cp("/var/www", "/var/www.old");
530 };
531  
532 =cut
533  
534 0 ▶sub cp {
Subroutine cp: not called CC=8 SCAR=42.8
Pod documented
535 0 my ( $source, $dest ) = @_;
536  
537 0 $source = resolv_path($source);
538 0 $dest = resolv_path($dest);
539  
540 Rex::get_current_connection()->{reporter}
541 0 ->report_resource_start( type => "cp", name => "$source -> $dest" );
542  
543 0 my $fs = Rex::Interface::Fs->create;
544  
545 0 my $new_present = 0;
546 0 ▶ if ( $fs->is_file($source) && $fs->is_dir($dest) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs->is_file($source) and $fs->is_dir($dest)) 0 0 0
Condition0%0/3
$fs->is_file($source) and $fs->is_dir($dest)
!ll&&!rl&&r
000
MC/DC0%0/2
$fs->is_file($source) and $fs->is_dir($dest)
$fs->is_file($source) $fs->is_dir($dest)
Truth table0%0/3
A and B
A= $fs->is_file($source)B= $fs->is_dir($dest)
ABresult
0X0
100
111
547 0 $dest = "$dest/" . basename $source;
548 }
549  
550 0 ▶ if ( $fs->is_file($dest) || $fs->is_dir($dest) || $fs->is_symlink($dest) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs->is_file($dest) or $fs->is_dir($dest) or $fs->is_symlink($dest)) 0 0 0
Condition0%0/6
$fs->is_file($dest) or $fs->is_dir($dest) or $fs->is_symlink($dest)
l!l&&r!l&&!r
000
$fs->is_file($dest) or $fs->is_dir($dest)
l!l&&r!l&&!r
000
MC/DC0%0/3
$fs->is_file($dest) or $fs->is_dir($dest) or $fs->is_symlink($dest)
$fs->is_file($dest) $fs->is_dir($dest) $fs->is_symlink($dest)
Truth table0%0/4
A or B or C
A= $fs->is_file($dest)B= $fs->is_dir($dest)C= $fs->is_symlink($dest)
ABCresult
1XX1
01X1
0011
0000
551 0 $new_present = 1;
552 }
553  
554 0 ▶ if ( !$fs->cp( $source, $dest ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fs->cp($source, $dest)) 0 0 0
555 0 die("Copy failed from $source to $dest");
556 }
557  
558 0 ▶ if ( $new_present == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_present == 0) { } 0 0 0
559 0 Rex::get_current_connection()->{reporter}->report( changed => 1, );
560 }
561 else {
562 0 Rex::get_current_connection()->{reporter}->report( changed => 0, );
563 }
564  
565 Rex::get_current_connection()->{reporter}
566 0 ->report_resource_end( type => "cp", name => "$source -> $dest" );
567 }
568  
569 =head2 Not changing content
570  
571 These commands should not change the contents of the file system.
572  
573 =head3 list_files("/path");
574  
575 This function lists all entries (files, directories, ...) in a given directory and returns them as an array.
576  
577 task "ls-etc", "server01", sub {
578 my @tmp_files = grep { /\.tmp$/ } list_files("/etc");
579 };
580  
581 This command will not be reported.
582  
583 =cut
584  
585 2 ▶sub list_files {
Subroutine list_files: called CC=1 SCAR=0.0
Pod documented
586 2 my $path = shift;
587 2 $path = resolv_path($path);
588  
589 2 my $fs = Rex::Interface::Fs->create;
590 2 my @ret = $fs->ls($path);
591  
592 2 return @ret;
593 }
594  
595 =head3 ls($path)
596  
597 Just an alias for C<list_files>.
598  
599 =cut
600  
601 0 ▶sub ls {
Subroutine ls: not called CC=1 SCAR=6.9
Pod documented
602 0 return list_files(@_);
603 }
604  
605 =head3 stat($file)
606  
607 This function will return a hash with the following information about a file or directory:
608  
609 =over 4
610  
611 =item mode
612  
613 =item size
614  
615 =item uid
616  
617 =item gid
618  
619 =item atime
620  
621 =item mtime
622  
623 =back
624  
625 task "stat", "server01", sub {
626 my %file_stat = stat("/etc/passwd");
627 };
628  
629  
630 This command will not be reported.
631  
632 =cut
633  
634 7 ▶sub stat {
Subroutine stat: called CC=4 SCAR=14.2
Pod documented
635 7 my ($file) = @_;
636 7 $file = resolv_path($file);
637 7 my %ret;
638  
639 7 Rex::Logger::debug("Getting fs stat from $file");
640  
641 7 my $fs = Rex::Interface::Fs->create;
642  
643 # may return undef, so capture into a list first.
644 7 my @stat = $fs->stat($file);
645 7 ▶ die("Can't stat $file") if ( !defined $stat[0] && scalar @stat == 1 );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $stat[0] and scalar @stat == 1 1 6 7
Condition66%2/3
not defined $stat[0] and scalar @stat == 1
!ll&&!rl&&r
601
MC/DC50%1/2
not defined $stat[0] and scalar @stat == 1
not defined $stat[0] scalar @stat == 1
Truth table66%2/3
not(A) and B
A= not defined $stat[0]B= scalar @stat == 1
ABresult
0X0
100
111
646  
647 6 ▶ if ( scalar @stat % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @stat % 2) 0 6 6
648 0 Rex::Logger::debug( 'stat output: ' . join ', ', @stat );
649 0 die('stat returned odd number of elements');
650 }
651  
652 6 %ret = @stat;
653  
654 6 return %ret;
655 }
656  
657 =head3 is_file($path)
658  
659 This function tests if C<$path> is a file. Returns 1 if true, 0 if false.
660  
661 task "isfile", "server01", sub {
662 if( is_file("/etc/passwd") ) {
663 say "it is a file.";
664 }
665 else {
666 say "hm, this is not a file.";
667 }
668 };
669  
670 This command will not be reported.
671  
672 =cut
673  
674 678 ▶sub is_file {
Subroutine is_file: called CC=1 SCAR=0.0
Pod documented
675 678 my ($file) = @_;
676 678 $file = resolv_path($file);
677  
678 678 my $fs = Rex::Interface::Fs->create;
679 678 return $fs->is_file($file);
680 }
681  
682 =head3 is_dir($path)
683  
684 This function tests if C<$path> is a directory. Returns 1 if true, 0 if false.
685  
686 task "isdir", "server01", sub {
687 if( is_dir("/etc") ) {
688 say "it is a directory.";
689 }
690 else {
691 say "hm, this is not a directory.";
692 }
693 };
694  
695 This command will not be reported.
696  
697 =cut
698  
699 284 ▶sub is_dir {
Subroutine is_dir: called CC=1 SCAR=0.0
Pod documented
700 284 my ($path) = @_;
701 284 $path = resolv_path($path);
702  
703 284 my $fs = Rex::Interface::Fs->create;
704 284 return $fs->is_dir($path);
705  
706 }
707  
708 =head3 is_symlink($path)
709  
710 This function tests if C<$path> is a symbolic link. Returns 1 if true, 0 if false.
711  
712 task "issym", "server01", sub {
713 if( is_symlink("/etc/foo.txt") ) {
714 say "it is a symlink.";
715 }
716 else {
717 say "hm, this is not a symlink.";
718 }
719 };
720  
721 This command will not be reported.
722  
723 =cut
724  
725 20 ▶sub is_symlink {
Subroutine is_symlink: called CC=1 SCAR=0.0
Pod documented
726 20 my ($path) = @_;
727 20 $path = resolv_path($path);
728  
729 20 my $fs = Rex::Interface::Fs->create;
730 20 return $fs->is_symlink($path);
731 }
732  
733 =head3 is_readable($path)
734  
735 This function tests if C<$path> is readable. It returns 1 if true, 0 if false.
736  
737 task "readable", "server01", sub {
738 if( is_readable("/etc/passwd") ) {
739 say "passwd is readable";
740 }
741 else {
742 say "not readable.";
743 }
744 };
745  
746 This command will not be reported.
747  
748 =cut
749  
750 2 ▶sub is_readable {
Subroutine is_readable: called CC=1 SCAR=0.0
Pod documented
751 2 my ($file) = @_;
752 2 $file = resolv_path($file);
753 2 Rex::Logger::debug("Checking if $file is readable");
754  
755 2 my $fs = Rex::Interface::Fs->create;
756 2 return $fs->is_readable($file);
757 }
758  
759 =head3 is_writable($path)
760  
761 This function tests if C<$path> is writable. It returns 1 if true, 0 if false.
762  
763 task "writable", "server01", sub {
764 if( is_writable("/etc/passwd") ) {
765 say "passwd is writable";
766 }
767 else {
768 say "not writable.";
769 }
770 };
771  
772 This command will not be reported.
773  
774 =cut
775  
776 0 ▶sub is_writable {
Subroutine is_writable: not called CC=1 SCAR=6.9
Pod documented
777 0 my ($file) = @_;
778 0 $file = resolv_path($file);
779 0 Rex::Logger::debug("Checking if $file is writable");
780  
781 0 my $fs = Rex::Interface::Fs->create;
782 0 return $fs->is_writable($file);
783 }
784  
785 =head3 is_writeable($file)
786  
787 This is only an alias for C<is_writable>.
788  
789 =cut
790  
791 0 ▶sub is_writeable {
Subroutine is_writeable: not called CC=1 SCAR=6.9
Pod documented
792 0 is_writable(@_);
793 }
794  
795 =head3 readlink($link)
796  
797 If C<$link> is a symbolic link, returns the path it resolves to, and C<die()>s otherwise.
798  
799 task "islink", "server01", sub {
800 my $link;
801 eval {
802 $link = readlink("/tmp/testlink");
803 };
804  
805 say "this is a link" if($link);
806 };
807  
808 This command will not be reported.
809  
810 =cut
811  
812 0 ▶sub readlink {
Subroutine readlink: not called CC=2 SCAR=17.9
Pod documented
813 0 my ($file) = @_;
814 0 $file = resolv_path($file);
815 0 Rex::Logger::debug("Reading link of $file");
816  
817 0 my $fs = Rex::Interface::Fs->create;
818 0 my $link = $fs->readlink($file);
819  
820 0 ▶ unless ($link) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($link) 0 0 0
821 0 Rex::Logger::debug("readlink: $file is not a link.");
822 0 die("readlink: $file is not a link.");
823 }
824  
825 0 return $link;
826 }
827  
828 =head3 chdir($newdir)
829  
830 This function will change the working directory to C<$newdir>. This function currently works only locally.
831  
832 task "chdir", "server01", sub {
833 chdir("/tmp");
834 };
835  
836 This command will not be reported.
837  
838 =cut
839  
840 0 ▶sub chdir {
Subroutine chdir: not called CC=1 SCAR=6.9
Pod documented
841 0 Rex::Logger::debug("chdir behaviour will be changed in the future.");
842 0 CORE::chdir( $_[0] );
843 }
844  
845 =head3 cd($newdir)
846  
847 This is an alias of C<chdir>.
848  
849 =cut
850  
851 0 ▶sub cd {
Subroutine cd: not called CC=1 SCAR=6.9
Pod documented
852 0 &chdir( $_[0] );
853 }
854  
855 =head3 df([$device])
856  
857 This function returns a hash reference which reflects the output of C<df>.
858  
859 task "df", "server01", sub {
860 my $df = df();
861 my $df_on_sda1 = df("/dev/sda1");
862 };
863  
864 This command will not be reported.
865  
866 =cut
867  
868 0 ▶sub df {
Subroutine df: not called CC=4 SCAR=30.0
Pod documented
869 0 my ($dev) = @_;
870  
871 0 my $ret = {};
872  
873 0 ▶ $dev ||= "";
Condition0%0/2
$dev ||= ""
l!l
00
MC/DC0%0/1
$dev ||= ""
$dev
Truth table0%0/2
A
A= $dev
Aresult
11
00
874  
875 0 my $exec = Rex::Interface::Exec->create;
876 0 my ( $out, $err ) = $exec->exec("df $dev 2>/dev/null");
877  
878 0 my @lines = split( /\r?\n/, $out );
879  
880 0 $ret = _parse_df(@lines);
881  
882 0 ▶ if ($dev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dev) 0 0 0
883 0 ▶ if ( keys %$ret == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$ret == 1) 0 0 0
884 0 ($dev) = keys %$ret;
885 }
886 0 return $ret->{$dev};
887 }
888  
889 0 return $ret;
890 }
891  
892 2 ▶sub _parse_df {
Subroutine _parse_df: called CC=4 SCAR=13.9
893 2 my @lines = @_;
894 2 chomp @lines;
895  
896 2 my $ret = {};
897  
898 2 shift @lines;
899 2 my $current_fs = "";
900  
901 2 for my $line (@lines) {
902 7 my ( $fs, $size, $used, $free, $use_per, $mounted_on ) =
903 split( /\s+/, $line, 6 );
904 7 ▶ $current_fs = $fs if $fs;
Branch: 1 branch
BranchTrueFalseTotal
if $fs 6 1 7
905  
906 7 ▶ if ( !$size ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($size) 1 6 7
907 1 next;
908 }
909  
910 6 $ret->{$current_fs} = {
911 size => $size,
912 used => $used,
913 free => $free,
914 used_perc => $use_per,
915 mounted_on => $mounted_on
916 };
917 }
918  
919 2 return $ret;
920 }
921  
922 =head3 du($path)
923  
924 Returns the disk usage of C<$path>.
925  
926 task "du", "server01", sub {
927 say "size of /var/www: " . du("/var/www");
928 };
929  
930 This command will not be reported.
931  
932 =cut
933  
934 0 ▶sub du {
Subroutine du: not called CC=1 SCAR=6.9
Pod documented
935 0 my ($path) = @_;
936 0 $path = resolv_path($path);
937  
938 0 my $exec = Rex::Interface::Exec->create;
939 0 my @lines = $exec->exec("du -s $path");
940 0 my ($du) = ( $lines[0] =~ m/^(\d+)/ );
941  
942 0 return $du;
943 }
944  
945 =head3 mount($device, $mount_point, @options)
946  
947 Mount devices.
948  
949 task "mount", "server01", sub {
950 mount "/dev/sda5", "/tmp";
951 mount "/dev/sda6", "/mnt/sda6",
952 ensure => "present",
953 type => "ext3",
954 options => [qw/noatime async/],
955 on_change => sub { say "device mounted"; };
956 #
957 # mount persistent with entry in /etc/fstab
958  
959 mount "/dev/sda6", "/mnt/sda6",
960 ensure => "persistent",
961 type => "ext3",
962 options => [qw/noatime async/],
963 on_change => sub { say "device mounted"; };
964  
965 # to umount a device
966 mount "/dev/sda6", "/mnt/sda6",
967 ensure => "absent";
968  
969 };
970  
971 In order to be more aligned with C<mount> terminology, the previously used C<fs> option has been deprecated in favor of the C<type> option. The C<fs> option is still supported and works as previously, but Rex prints a warning if it is being used. There's also a warning if both C<fs> and C<type> options are specified, and in this case C<type> will be used.
972  
973 =cut
974  
975 0 ▶sub mount {
Subroutine mount: not called CC=24 SCAR=64.0
Pod documented
976 0 my ( $device, $mount_point, @options ) = @_;
977 0 my $option = {@options};
978  
979 0 ▶ if ( defined $option->{fs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option->{"fs"}) 0 0 0
980 0 Rex::Logger::info(
981 'The `fs` option of the mount command has been deprecated in favor of the `type` option. Please update your task.',
982 'warn'
983 );
984  
985 0 ▶ if ( !defined $option->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $option->{'type'}) { } 0 0 0
986 0 $option->{type} = $option->{fs};
987 }
988 else {
989 0 Rex::Logger::info(
990 'Both `fs` and `type` options have been specified for mount command. Preferring `type`.',
991 'warn'
992 );
993 }
994 }
995  
996 0 delete $option->{fs};
997  
998 Rex::get_current_connection()->{reporter}
999 0 ->report_resource_start( type => "mount", name => "$mount_point" );
1000  
1001 0 ▶ $option->{ensure} ||= "present"; # default
Condition0%0/2
$option->{"ensure"} ||= "present"
l!l
00
MC/DC0%0/1
$option->{"ensure"} ||= "present"
$option->{"ensure"}
Truth table0%0/2
A
A= $option->{"ensure"}
Aresult
11
00
1002  
1003 0 ▶ if ( $option->{ensure} eq "absent" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{'ensure'} eq 'absent') { } 0 0 0
1004 &umount(
1005 $mount_point,
1006 device => $device,
1007 on_change =>
1008 ( exists $option->{on_change} ? $option->{on_change} : undef )
1009 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
exists $option->{'on_change'} ? : 0 0 0
1010 }
1011 else {
1012 0 ▶ if ( $option->{ensure} eq "persistent" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{"ensure"} eq "persistent") 0 0 0
1013 0 $option->{persistent} = 1;
1014 }
1015  
1016 0 my $changed = 0;
1017 0 my $exec = Rex::Interface::Exec->create;
1018  
1019 0 my ( $m_out, $m_err ) = $exec->exec("mount");
1020 0 my @mounted = split( /\r?\n/, $m_out );
1021 0 my ($already_mounted) = grep { m/$device on $mount_point/ } @mounted;
1022 0 ▶ if ($already_mounted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($already_mounted) 0 0 0
1023 0 Rex::Logger::debug("Device ($device) already mounted on $mount_point.");
1024 0 $changed = 0;
1025 }
1026  
1027 my $cmd = sprintf(
1028 "mount %s %s %s %s",
1029 $option->{type} ? "-t " . $option->{type} : "", # file system
1030 $option->{"options"}
1031 0 ▶ ? " -o " . join( ",", @{ $option->{"options"} } )
Branch: 2 branches
BranchTrueFalseTotal
$option->{'type'} ? : 0 0 0
$option->{'options'} ? : 0 0 0
1032 : "",
1033 $device,
1034 $mount_point
1035 );
1036  
1037 0 ▶ unless ($already_mounted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($already_mounted) 0 0 0
1038 0 $exec->exec($cmd);
1039 0 ▶ if ( $? != 0 ) { die("Mount failed of $mount_point"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1040 0 $changed = 1;
1041 Rex::get_current_connection()->{reporter}->report(
1042 0 changed => 1,
1043 message => "Device $device mounted on $mount_point."
1044 );
1045 }
1046  
1047 0 ▶ if ( exists $option->{persistent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"persistent"}) 0 0 0
1048 0 ▶ if ( !exists $option->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $option->{"type"}) 0 0 0
1049  
1050 # no fs given, so get it from mount output
1051 0 my ( $out, $err ) = $exec->exec("mount");
1052 0 my @output = split( /\r?\n/, $out );
1053 0 my ($line) = grep { /^$device/ } @output;
1054 0 my ( $_d, $_o, $_p, $_t, $fs_type ) = split( /\s+/, $line );
1055 0 $option->{type} = $fs_type;
1056  
1057 0 my ($_options) = ( $line =~ m/\((.+?)\)/ );
1058 0 $option->{options} = $_options;
1059 }
1060  
1061 0 my $fh = Rex::Interface::File->create;
1062  
1063 0 my $old_md5 = md5("/etc/fstab");
1064  
1065 0 ▶ if ( !$fh->open( "<", "/etc/fstab" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open("<", "/etc/fstab")) 0 0 0
1066 0 Rex::Logger::debug("Can't open /etc/fstab for reading.");
1067 0 die("Can't open /etc/fstab for reading.");
1068 }
1069  
1070 0 my $f = Rex::FS::File->new( fh => $fh );
1071 0 my @content = $f->read_all;
1072 0 $f->close;
1073  
1074 0 my @new_content = grep { !/^$device\s/ } @content;
1075  
1076 0 ▶ $option->{options} ||= "defaults";
Condition0%0/2
$option->{"options"} ||= "defaults"
l!l
00
MC/DC0%0/1
$option->{"options"} ||= "defaults"
$option->{"options"}
Truth table0%0/2
A
A= $option->{"options"}
Aresult
11
00
1077  
1078 0 ▶ if ( ref( $option->{options} ) eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $option->{'options'} eq 'ARRAY') { } 0 0 0
1079 0 my $mountops = join( ",", @{ $option->{"options"} } );
1080 0 ▶ if ( $option->{label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{'label'}) { } 0 0 0
1081 push( @new_content,
1082 "LABEL="
1083 . $option->{label}
1084 0 . "\t$mount_point\t$option->{type}\t$mountops\t0 0\n" );
1085 }
1086 else {
1087 0 push( @new_content,
1088 "$device\t$mount_point\t$option->{type}\t$mountops\t0 0\n" );
1089 }
1090 }
1091 else {
1092 0 ▶ if ( $option->{label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{'label'}) { } 0 0 0
1093 push( @new_content,
1094 "LABEL="
1095 . $option->{label}
1096 0 . "\t$mount_point\t$option->{type}\t$option->{options}\t0 0\n" );
1097 }
1098 else {
1099 0 push( @new_content,
1100 "$device\t$mount_point\t$option->{type}\t$option->{options}\t0 0\n"
1101 );
1102 }
1103 }
1104  
1105 0 $fh = Rex::Interface::File->create;
1106  
1107 0 ▶ if ( !$fh->open( ">", "/etc/fstab" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open(">", "/etc/fstab")) 0 0 0
1108 0 Rex::Logger::debug("Can't open /etc/fstab for writing.");
1109 0 die("Can't open /etc/fstab for writing.");
1110 }
1111  
1112 0 $f = Rex::FS::File->new( fh => $fh );
1113 0 $f->write( join( "\n", @new_content ) );
1114 0 $f->close;
1115  
1116 0 my $new_md5 = md5("/etc/fstab");
1117  
1118 0 ▶ if ( $new_md5 ne $old_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_md5 ne $old_md5) 0 0 0
1119 Rex::get_current_connection()->{reporter}
1120 0 ->report( changed => 1, message => "File /etc/fstab updated." );
1121 0 $changed = 1;
1122 }
1123 }
1124  
1125 0 ▶ if ( $changed == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed == 1) 0 0 0
1126 0 ▶ if ( exists $option->{on_change} && ref $option->{on_change} eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"on_change"} and ref $option->{"on_change"} eq "CODE") 0 0 0
Condition0%0/3
exists $option->{"on_change"} and ref $option->{"on_change"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $option->{"on_change"} and ref $option->{"on_change"} eq "CODE"
exists $option->{"on_change"} ref $option->{"on_change"} eq "CODE"
Truth table0%0/3
A and B
A= exists $option->{"on_change"}B= ref $option->{"on_change"} eq "CODE"
ABresult
0X0
100
111
1127 0 $option->{on_change}->( $device, $mount_point );
1128 }
1129 }
1130 }
1131  
1132 Rex::get_current_connection()->{reporter}
1133 0 ->report_resource_end( type => "mount", name => "$mount_point" );
1134 }
1135  
1136 =head3 umount($mount_point)
1137  
1138 Unmount device.
1139  
1140 task "umount", "server01", sub {
1141 umount "/tmp";
1142 };
1143  
1144 =cut
1145  
1146 0 ▶sub umount {
Subroutine umount: not called CC=8 SCAR=42.8
Pod documented
1147 0 my ( $mount_point, %option ) = @_;
1148  
1149 0 my $device;
1150  
1151 0 ▶ if ( exists $option{device} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"device"}) 0 0 0
1152 0 $device = $option{device};
1153 }
1154  
1155 0 my $exec = Rex::Interface::Exec->create;
1156  
1157 Rex::get_current_connection()->{reporter}
1158 0 ->report_resource_start( type => "umount", name => "$mount_point" );
1159  
1160 0 my $changed = 0;
1161 0 my ( $m_out, $m_err ) = $exec->exec("mount");
1162 0 my @mounted = split( /\r?\n/, $m_out );
1163 0 my $already_mounted;
1164  
1165 0 ▶ if ($device) {
Branch: 1 branch
BranchTrueFalseTotal
if ($device) { } 0 0 0
1166 0 ($already_mounted) = grep { m/$device on $mount_point/ } @mounted;
1167 }
1168 else {
1169 0 ($already_mounted) = grep { m/on $mount_point/ } @mounted;
1170 }
1171  
1172 0 ▶ if ($already_mounted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($already_mounted) 0 0 0
1173 0 $exec->exec("umount $mount_point");
1174 0 ▶ if ( $? != 0 ) { die("Umount failed of $mount_point"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1175 0 $changed = 1;
1176 }
1177  
1178 0 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 0 0 0
1179 0 ▶ if ( exists $option{on_change} && ref $option{on_change} eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"on_change"} and ref $option{"on_change"} eq "CODE") 0 0 0
Condition0%0/3
exists $option{"on_change"} and ref $option{"on_change"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $option{"on_change"} and ref $option{"on_change"} eq "CODE"
exists $option{"on_change"} ref $option{"on_change"} eq "CODE"
Truth table0%0/3
A and B
A= exists $option{"on_change"}B= ref $option{"on_change"} eq "CODE"
ABresult
0X0
100
111
1180 0 $option{on_change}->( $mount_point, %option );
1181 }
1182 Rex::get_current_connection()->{reporter}
1183 0 ->report( changed => 1, message => "Unmounted $mount_point." );
1184 }
1185  
1186 Rex::get_current_connection()->{reporter}
1187 0 ->report_resource_end( type => "umount", name => "$mount_point" );
1188 }
1189  
1190 =head3 glob($glob)
1191  
1192 Returns the list of filename expansions for C<$glob> as L<Perl's built-in glob|https://perldoc.perl.org/functions/glob.html> would do.
1193  
1194 task "glob", "server1", sub {
1195 my @files_with_p = grep { is_file($_) } glob("/etc/p*");
1196 };
1197  
1198 This command will not be reported.
1199  
1200 =cut
1201  
1202 0 ▶sub glob {
Subroutine glob: not called CC=1 SCAR=6.9
Pod documented
1203 0 my ($glob) = @_;
1204 0 $glob = resolv_path($glob);
1205  
1206 0 my $fs = Rex::Interface::Fs->create;
1207 0 return $fs->glob($glob);
1208 }
1209  
1210 81 1;
« lib/Rex/Box/VBox.pm ↑ Summary lib/Rex/Interface/Exec/Sudo.pm »