lib/Rex/Commands/Sync.pm

filter: Statementstmt 25.6% 49 / 191 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 60.0% 15 / 25 Podpod 0.0% 0 / 2 totaltotal 18.4% 64 / 347 CC 53 SCAR 53.6
CC
53
Cov
17%
sync_up
600.0
sync_down
110.0
_get_remote_files
56.0
CRAP 213.2
« lib/Rex/Hardware/VirtInfo.pm ↑ Summary lib/Rex/Virtualization/LibVirt/import.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Commands::Sync - Sync directories
8  
9 =head1 DESCRIPTION
10  
11 This module can sync directories between your Rex system and your servers without the need of rsync.
12  
13 =head1 SYNOPSIS
14  
15 use Rex::Commands::Sync;
16  
17 task "prepare", "mysystem01", sub {
18 # upload directory recursively to remote system.
19 sync_up "/local/directory", "/remote/directory";
20  
21 sync_up "/local/directory", "/remote/directory", {
22 # setting custom file permissions for every file
23 files => {
24 owner => "foo",
25 group => "bar",
26 mode => 600,
27 },
28 # setting custom directory permissions for every directory
29 directories => {
30 owner => "foo",
31 group => "bar",
32 mode => 700,
33 },
34 exclude => [ '*.tmp' ],
35 parse_templates => TRUE|FALSE,
36 on_change => sub {
37 my (@files_changed) = @_;
38 },
39 };
40  
41 # download a directory recursively from the remote system to the local machine
42 sync_down "/remote/directory", "/local/directory";
43 };
44  
45 =cut
46  
47 package Rex::Commands::Sync;
48  
49 32 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 32 our $VERSION = '1.16.1'; # VERSION
53  
54 32 require Rex::Exporter;
55 32 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 32 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 32 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 32 ▶use Rex::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 32 ▶use Rex::Commands::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 32 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 32 ▶use Rex::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 32 ▶use Rex::Commands::Download;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 32 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 32 ▶use Rex::Helper::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 32 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 32 ▶use Text::Glob 'glob_to_regex', 'match_glob';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 32 ▶use File::Basename 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 32 @EXPORT = qw(sync_up sync_down);
71 32 $Text::Glob::strict_wildcard_slash = 0;
72  
73 0 ▶sub sync_up {
Subroutine sync_up: not called CC=24 SCAR=64.0
Pod undocumented
74 0 my ( $source, $dest, @option ) = @_;
75  
76 0 my $options = {};
77  
78 0 ▶ if ( ref( $option[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $option[0]) { } 0 0 0
79 0 $options = $option[0];
80 }
81 else {
82 0 $options = {@option};
83 }
84  
85 # default is, parsing templates (*.tpl) files
86 0 $options->{parse_templates} = TRUE;
87  
88 0 $source = resolv_path($source);
89 0 $dest = resolv_path($dest);
90  
91 #
92 # 0. normalize local path
93 #
94 0 $source = get_file_path( $source, caller );
95  
96 #
97 # first, get all files on source side
98 #
99 0 my @local_files = _get_local_files($source);
100  
101 #print Dumper(\@local_files);
102  
103 #
104 # second, get all files from destination side
105 #
106  
107 0 my @remote_files = _get_remote_files($dest);
108  
109 #print Dumper(\@remote_files);
110  
111 #
112 # third, get the difference
113 #
114  
115 0 my @diff = _diff_files( \@local_files, \@remote_files );
116  
117 #print Dumper(\@diff);
118  
119 #
120 # fourth, build excludes list
121 #
122  
123 0 ▶ my $excludes = $options->{exclude} ||= [];
Condition0%0/2
$options->{"exclude"} ||= []
l!l
00
MC/DC0%0/1
$options->{"exclude"} ||= []
$options->{"exclude"}
Truth table0%0/2
A
A= $options->{"exclude"}
Aresult
11
00
124 0 ▶ $excludes = [$excludes] unless ref($excludes) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $excludes eq "ARRAY" 0 0 0
125  
126 0 my @excluded_files = @{$excludes};
127  
128 #
129 # fifth, upload the different files
130 #
131  
132 0 ▶ my $check_exclude_file = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
133 0 my ( $file, $cmp ) = @_;
134 0 ▶ if ( $cmp =~ m/\// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp =~ m[/]u) 0 0 0
135  
136 # this is a directory exclude
137 0 ▶ if ( match_glob( $cmp, $file ) || match_glob( $cmp, substr( $file, 1 ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (match_glob($cmp, $file) or match_glob($cmp, substr($file, 1))) 0 0 0
Condition0%0/3
match_glob($cmp, $file) or match_glob($cmp, substr($file, 1))
l!l&&r!l&&!r
000
MC/DC0%0/2
match_glob($cmp, $file) or match_glob($cmp, substr($file, 1))
match_glob($cmp, $file) match_glob($cmp, substr($file, 1))
Truth table0%0/3
A or B
A= match_glob($cmp, $file)B= match_glob($cmp, substr($file, 1))
ABresult
1X1
011
000
138 {
139 0 return 1;
140 }
141  
142 0 return 0;
143 }
144  
145 0 ▶ if ( match_glob( $cmp, basename($file) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (match_glob($cmp, basename($file))) 0 0 0
146 0 return 1;
147 }
148  
149 0 return 0;
150 0 };
151  
152 0 my @uploaded_files;
153 0 for my $file (@diff) {
154 next
155 if (
156 scalar(
157 0 ▶ grep { $check_exclude_file->( $file->{name}, $_ ) } @excluded_files
Branch: 1 branch
BranchTrueFalseTotal
if scalar grep({&$check_exclude_file($file->{"name"}, $_);} @excluded_files) > 0 0 0 0
158 ) > 0
159 );
160  
161 0 my ($dir) = ( $file->{path} =~ m/(.*)\/[^\/]+$/ );
162 0 my ($remote_dir) = ( $file->{name} =~ m/\/(.*)\/[^\/]+$/ );
163  
164 0 my ( %dir_stat, %file_stat );
165 0 ▶ LOCAL {
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 %dir_stat = stat($dir);
167 0 %file_stat = stat( $file->{path} );
168 0 };
169  
170 # check for overwrites
171 0 my %file_perm = ( mode => $file_stat{mode} );
172 0 ▶ if ( exists $options->{files} && exists $options->{files}->{mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"files"} and exists $options->{"files"}{"mode"}) 0 0 0
Condition0%0/3
exists $options->{"files"} and exists $options->{"files"}{"mode"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $options->{"files"} and exists $options->{"files"}{"mode"}
exists $options->{"files"} exists $options->{"files"}{"mode"}
Truth table0%0/3
A and B
A= exists $options->{"files"}B= exists $options->{"files"}{"mode"}
ABresult
0X0
100
111
173 0 $file_perm{mode} = $options->{files}->{mode};
174 }
175  
176 0 ▶ if ( exists $options->{files} && exists $options->{files}->{owner} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"files"} and exists $options->{"files"}{"owner"}) 0 0 0
Condition0%0/3
exists $options->{"files"} and exists $options->{"files"}{"owner"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $options->{"files"} and exists $options->{"files"}{"owner"}
exists $options->{"files"} exists $options->{"files"}{"owner"}
Truth table0%0/3
A and B
A= exists $options->{"files"}B= exists $options->{"files"}{"owner"}
ABresult
0X0
100
111
177 0 $file_perm{owner} = $options->{files}->{owner};
178 }
179  
180 0 ▶ if ( exists $options->{files} && exists $options->{files}->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"files"} and exists $options->{"files"}{"group"}) 0 0 0
Condition0%0/3
exists $options->{"files"} and exists $options->{"files"}{"group"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $options->{"files"} and exists $options->{"files"}{"group"}
exists $options->{"files"} exists $options->{"files"}{"group"}
Truth table0%0/3
A and B
A= exists $options->{"files"}B= exists $options->{"files"}{"group"}
ABresult
0X0
100
111
181 0 $file_perm{group} = $options->{files}->{group};
182 }
183  
184 0 my %dir_perm = ( mode => $dir_stat{mode} );
185 0 ▶ if ( exists $options->{directories}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"directories"} and exists $options->{"directories"}{"mode"}) 0 0 0
Condition0%0/3
exists $options->{"directories"} and exists $options->{"directories"}{"mode"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $options->{"directories"} and exists $options->{"directories"}{"mode"}
exists $options->{"directories"} exists $options->{"directories"}{"mode"}
Truth table0%0/3
A and B
A= exists $options->{"directories"}B= exists $options->{"directories"}{"mode"}
ABresult
0X0
100
111
186 && exists $options->{directories}->{mode} )
187 {
188 0 $dir_perm{mode} = $options->{directories}->{mode};
189 }
190  
191 0 ▶ if ( exists $options->{directories}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"directories"} and exists $options->{"directories"}{"owner"}) 0 0 0
Condition0%0/3
exists $options->{"directories"} and exists $options->{"directories"}{"owner"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $options->{"directories"} and exists $options->{"directories"}{"owner"}
exists $options->{"directories"} exists $options->{"directories"}{"owner"}
Truth table0%0/3
A and B
A= exists $options->{"directories"}B= exists $options->{"directories"}{"owner"}
ABresult
0X0
100
111
192 && exists $options->{directories}->{owner} )
193 {
194 0 $dir_perm{owner} = $options->{directories}->{owner};
195 }
196  
197 0 ▶ if ( exists $options->{directories}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"directories"} and exists $options->{"directories"}{"group"}) 0 0 0
Condition0%0/3
exists $options->{"directories"} and exists $options->{"directories"}{"group"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $options->{"directories"} and exists $options->{"directories"}{"group"}
exists $options->{"directories"} exists $options->{"directories"}{"group"}
Truth table0%0/3
A and B
A= exists $options->{"directories"}B= exists $options->{"directories"}{"group"}
ABresult
0X0
100
111
198 && exists $options->{directories}->{group} )
199 {
200 0 $dir_perm{group} = $options->{directories}->{group};
201 }
202 ## /check for overwrites
203  
204 0 ▶ if ($remote_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remote_dir) 0 0 0
205 0 mkdir "$dest/$remote_dir", %dir_perm;
206 }
207  
208 Rex::Logger::debug(
209 0 "(sync_up) Uploading $file->{path} to $dest/$file->{name}");
210 0 ▶ if ( $file->{path} =~ m/\.tpl$/ && $options->{parse_templates} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{'path'} =~ /\.tpl$/u and $options->{'parse_templates'}) { } 0 0 0
Condition0%0/3
$file->{"path"} =~ /\.tpl$/u and $options->{"parse_templates"}
!ll&&!rl&&r
000
MC/DC0%0/2
$file->{"path"} =~ /\.tpl$/u and $options->{"parse_templates"}
$file->{"path"} =~ /\.tpl$/u $options->{"parse_templates"}
Truth table0%0/3
A and B
A= $file->{"path"} =~ /\.tpl$/uB= $options->{"parse_templates"}
ABresult
0X0
100
111
211 0 my $file_name = $file->{name};
212 0 $file_name =~ s/\.tpl$//;
213  
214 file "$dest/" . $file_name,
215 0 content => template( $file->{path} ),
216 %file_perm;
217  
218 0 push @uploaded_files, "$dest/$file_name";
219 }
220 else {
221 file "$dest/" . $file->{name},
222 source => $file->{path},
223 0 %file_perm;
224  
225 0 push @uploaded_files, "$dest/" . $file->{name};
226 }
227 }
228  
229 0 ▶ if ( exists $options->{on_change}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"on_change"} and ref $options->{"on_change"} eq "CODE" and scalar @uploaded_files > 0) 0 0 0
Condition0%0/6
exists $options->{"on_change"} and ref $options->{"on_change"} eq "CODE" and scalar @uploaded_files > 0
!ll&&!rl&&r
000
exists $options->{"on_change"} and ref $options->{"on_change"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/3
exists $options->{"on_change"} and ref $options->{"on_change"} eq "CODE" and scalar @uploaded_files > 0
exists $options->{"on_change"} ref $options->{"on_change"} eq "CODE" scalar @uploaded_files > 0
Truth table0%0/4
A and B and C
A= exists $options->{"on_change"}B= ref $options->{"on_change"} eq "CODE"C= scalar @uploaded_files > 0
ABCresult
0XX0
10X0
1100
1111
230 && ref $options->{on_change} eq "CODE"
231 && scalar(@uploaded_files) > 0 )
232 {
233 0 Rex::Logger::debug("Calling on_change hook of sync_up");
234 0 $options->{on_change}->( map { $dest . $_->{name} } @diff );
235 }
236  
237 }
238  
239 0 ▶sub sync_down {
Subroutine sync_down: not called CC=10 SCAR=47.0
Pod undocumented
240 0 my ( $source, $dest, @option ) = @_;
241  
242 0 my $options = {};
243  
244 0 ▶ if ( ref( $option[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $option[0]) { } 0 0 0
245 0 $options = $option[0];
246 }
247 else {
248 0 $options = {@option};
249 }
250  
251 0 $source = resolv_path($source);
252 0 $dest = resolv_path($dest);
253  
254 #
255 # first, get all files on dest side
256 #
257 0 my @local_files = _get_local_files($dest);
258  
259 #print Dumper(\@local_files);
260  
261 #
262 # second, get all files from source side
263 #
264  
265 0 my @remote_files = _get_remote_files($source);
266  
267 #print Dumper(\@remote_files);
268  
269 #
270 # third, get the difference
271 #
272  
273 0 my @diff = _diff_files( \@remote_files, \@local_files );
274  
275 #print Dumper(\@diff);
276  
277 #
278 # fourth, build excludes list
279 #
280  
281 0 ▶ my $excludes = $options->{exclude} ||= [];
Condition0%0/2
$options->{"exclude"} ||= []
l!l
00
MC/DC0%0/1
$options->{"exclude"} ||= []
$options->{"exclude"}
Truth table0%0/2
A
A= $options->{"exclude"}
Aresult
11
00
282 0 ▶ $excludes = [$excludes] unless ref($excludes) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $excludes eq "ARRAY" 0 0 0
283  
284 0 my @excluded_files = map { glob_to_regex($_); } @{$excludes};
285  
286 #
287 # fifth, download the different files
288 #
289  
290 0 for my $file (@diff) {
291 0 ▶ next if grep { basename( $file->{name} ) =~ $_ } @excluded_files;
Branch: 1 branch
BranchTrueFalseTotal
if grep {basename($file->{"name"}) =~ /$_/u;} @excluded_files 0 0 0
292  
293 0 my ($dir) = ( $file->{path} =~ m/(.*)\/[^\/]+$/ );
294 0 my ($remote_dir) = ( $file->{name} =~ m/\/(.*)\/[^\/]+$/ );
295  
296 0 my ( %dir_stat, %file_stat );
297 0 %dir_stat = stat($dir);
298 0 %file_stat = stat( $file->{path} );
299  
300 0 ▶ LOCAL {
Subroutine __ANON__: not called CC=2 SCAR=17.9
301 0 ▶ if ($remote_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remote_dir) 0 0 0
302 0 mkdir "$dest/$remote_dir", mode => $dir_stat{mode};
303 }
304 0 };
305  
306 0 Rex::Logger::debug(
307 "(sync_down) Downloading $file->{path} to $dest/$file->{name}");
308 0 download( $file->{path}, "$dest/$file->{name}" );
309  
310 0 ▶ LOCAL {
Subroutine __ANON__: not called CC=1 SCAR=6.9
311 0 chmod $file_stat{mode}, "$dest/$file->{name}";
312 0 };
313 }
314  
315 0 ▶ if ( exists $options->{on_change}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"on_change"} and ref $options->{"on_change"} eq "CODE" and scalar @diff > 0) 0 0 0
Condition0%0/6
exists $options->{"on_change"} and ref $options->{"on_change"} eq "CODE" and scalar @diff > 0
!ll&&!rl&&r
000
exists $options->{"on_change"} and ref $options->{"on_change"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/3
exists $options->{"on_change"} and ref $options->{"on_change"} eq "CODE" and scalar @diff > 0
exists $options->{"on_change"} ref $options->{"on_change"} eq "CODE" scalar @diff > 0
Truth table0%0/4
A and B and C
A= exists $options->{"on_change"}B= ref $options->{"on_change"} eq "CODE"C= scalar @diff > 0
ABCresult
0XX0
10X0
1100
1111
316 && ref $options->{on_change} eq "CODE"
317 && scalar(@diff) > 0 )
318 {
319 0 Rex::Logger::debug("Calling on_change hook of sync_down");
320 0 ▶ if ( substr( $dest, -1 ) eq "/" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($dest, -1) eq "/") 0 0 0
321 0 $dest = substr( $dest, 0, -1 );
322 }
323 0 $options->{on_change}->( map { $dest . $_->{name} } @diff );
324 }
325  
326 }
327  
328 0 ▶sub _get_local_files {
Subroutine _get_local_files: not called CC=2 SCAR=17.9
329 0 my ($source) = @_;
330  
331 0 ▶ if ( !-d $source ) { die("$source : no such directory."); }
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $source) 0 0 0
332  
333 0 my @dirs = ($source);
334 0 my @local_files;
335 0 ▶ LOCAL {
Subroutine __ANON__: not called CC=6 SCAR=37.4
336 0 for my $dir (@dirs) {
337 0 for my $entry ( list_files($dir) ) {
338 0 ▶ next if ( $entry eq "." );
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "." 0 0 0
339 0 ▶ next if ( $entry eq ".." );
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq ".." 0 0 0
340 0 ▶ if ( is_dir("$dir/$entry") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_dir("$dir/$entry")) 0 0 0
341 0 push( @dirs, "$dir/$entry" );
342 0 next;
343 }
344  
345 0 my $name = "$dir/$entry";
346 0 $name =~ s/^\Q$source\E//;
347 0 push(
348 @local_files,
349 {
350 name => $name,
351 path => "$dir/$entry",
352 md5 => md5("$dir/$entry"),
353 }
354 );
355  
356 }
357 }
358 0 };
359  
360 0 return @local_files;
361 }
362  
363 0 ▶sub _get_remote_files {
Subroutine _get_remote_files: not called CC=7 SCAR=40.3
364 0 my ($dest) = @_;
365  
366 0 ▶ if ( !is_dir($dest) ) { die("$dest : no such directory."); }
Branch: 1 branch
BranchTrueFalseTotal
unless (is_dir($dest)) 0 0 0
367  
368 0 my @remote_dirs = ($dest);
369 0 my @remote_files;
370  
371 0 for my $dir (@remote_dirs) {
372 0 for my $entry ( list_files($dir) ) {
373 0 ▶ next if ( $entry eq "." );
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "." 0 0 0
374 0 ▶ next if ( $entry eq ".." );
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq ".." 0 0 0
375 0 ▶ if ( is_dir("$dir/$entry") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_dir("$dir/$entry")) 0 0 0
376 0 push( @remote_dirs, "$dir/$entry" );
377 0 next;
378 }
379  
380 0 my $name = "$dir/$entry";
381 0 $name =~ s/^\Q$dest\E//;
382 0 push(
383 @remote_files,
384 {
385 name => $name,
386 path => "$dir/$entry",
387 md5 => md5("$dir/$entry"),
388 }
389 );
390 }
391 }
392  
393 0 return @remote_files;
394 }
395  
396 0 ▶sub _diff_files {
Subroutine _diff_files: not called CC=4 SCAR=30.0
397 0 my ( $files1, $files2 ) = @_;
398 0 my @diff;
399  
400 0 for my $file1 ( @{$files1} ) {
401 my @data = grep {
402 ( $_->{name} eq $file1->{name} )
403 && ( $_->{md5} eq $file1->{md5} )
404 0 ▶ } @{$files2};
Condition0%0/3
$_->{'name'} eq $file1->{'name'} && $_->{'md5'} eq $file1->{'md5'}
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'name'} eq $file1->{'name'} && $_->{'md5'} eq $file1->{'md5'}
$_->{'name'} eq $file1->{'name'} $_->{'md5'} eq $file1->{'md5'}
Truth table0%0/3
A && B
A= $_->{'name'} eq $file1->{'name'}B= $_->{'md5'} eq $file1->{'md5'}
ABresult
0X0
100
111
405 0 ▶ if ( scalar @data == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @data == 0) 0 0 0
406 0 push( @diff, $file1 );
407 }
408 }
409  
410 0 return @diff;
411 }
412  
413 32 1;
« lib/Rex/Hardware/VirtInfo.pm ↑ Summary lib/Rex/Virtualization/LibVirt/import.pm »