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