This file is indexed.

/usr/share/perl5/Rex/Commands/Rsync.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
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

=head1 NAME

Rex::Commands::Rsync - Simple Rsync Frontend

=head1 DESCRIPTION

With this module you can sync 2 directories via the I<rsync> command.

Version <= 1.0: All these functions will not be reported.

All these functions are not idempotent.

=head1 DEPENDENCIES

=over 4

=item Expect

The I<Expect> Perl module is required to be installed on the machine
executing the rsync task.

=item rsync

The I<rsync> command has to be installed on both machines involved in
the execution of the rsync task.

=back

=head1 SYNOPSIS

 use Rex::Commands::Rsync;

 sync "dir1", "dir2";

=head1 EXPORTED FUNCTIONS

=cut

package Rex::Commands::Rsync;

use strict;
use warnings;

our $VERSION = '1.4.1'; # VERSION

BEGIN {
  use Rex::Require;
  Expect->use;
  $Expect::Log_Stdout = 0;
}

require Rex::Exporter;

use base qw(Rex::Exporter);
use vars qw(@EXPORT);

@EXPORT = qw(sync);

=head2 sync($source, $dest, $opts)

This function executes rsync to sync $source and $dest.

If you want to use sudo, you need to disable I<requiretty> option for this user. You can do this with the following snippet in your sudoers configuration.

 Defaults:username !requiretty

=over 4

=item UPLOAD - Will upload all from the local directory I<html> to the remote directory I</var/www/html>.

 task "sync", "server01", sub {
   sync "html/*", "/var/www/html", {
    exclude => "*.sw*",
    parameters => '--backup --delete',
   };
 };

 task "sync", "server01", sub {
   sync "html/*", "/var/www/html", {
    exclude => ["*.sw*", "*.tmp"],
    parameters => '--backup --delete',
   };
 };

=item DOWNLOAD - Will download all from the remote directory I</var/www/html> to the local directory I<html>.

 task "sync", "server01", sub {
   sync "/var/www/html/*", "html/", {
    download => 1,
    parameters => '--backup',
   };
 };

=back

=cut

sub sync {
  my ( $source, $dest, $opt ) = @_;

  my $current_connection = Rex::get_current_connection();
  my $server             = $current_connection->{server};
  my $cmd;

  my $port       = 22;
  my $servername = $server->to_s;

  # there is a :1234 port in the server  string
  if ( $servername =~ m/:\d+$/ ) {
    my $_t;
    ( $servername, $port ) = split( /:/, $servername );
  }

  my $auth = $current_connection->{conn}->get_auth;

  if ( !exists $opt->{download} && $source !~ m/^\// ) {

    # relative path, calculate from module root
    $source = Rex::Helper::Path::get_file_path( $source, caller() );
  }

  Rex::Logger::debug("Syncing $source -> $dest with rsync.");
  if ($Rex::Logger::debug) {
    $Expect::Log_Stdout = 1;
  }

  my $params = "";
  if ( $opt && exists $opt->{'exclude'} ) {
    my $excludes = $opt->{'exclude'};
    $excludes = [$excludes] unless ref($excludes) eq "ARRAY";
    for my $exclude (@$excludes) {
      $params .= " --exclude=" . $exclude;
    }
  }

  if ( $opt && exists $opt->{parameters} ) {
    $params .= " " . $opt->{parameters};
  }

  my @rsync_cmd = ();

  if ( $opt && exists $opt->{'download'} && $opt->{'download'} == 1 ) {
    Rex::Logger::debug("Downloading $source -> $dest");
    push @rsync_cmd, "rsync -a -e '\%s' --verbose --stats $params ";
    push @rsync_cmd, "'" . $auth->{user} . "\@" . $server . ":" . $source . "'";
    push @rsync_cmd, "'$dest'";
  }
  else {
    Rex::Logger::debug("Uploading $source -> $dest");
    push @rsync_cmd, "rsync -a -e '\%s' --verbose --stats $params '$source' ";
    push @rsync_cmd, "'" . $auth->{user} . "\@$server:$dest" . "'";
  }

  if (Rex::is_sudo) {
    push @rsync_cmd, "--rsync-path='sudo rsync'";
  }

  $cmd = join( " ", @rsync_cmd );

  my $pass           = $auth->{password};
  my @expect_options = ();

  my $auth_type = $auth->{auth_type};
  if ( $auth_type eq "try" ) {
    if ( $server->get_private_key && -f $server->get_private_key ) {
      $auth_type = "key";
    }
    else {
      $auth_type = "pass";
    }
  }

  if ( $auth_type eq "pass" ) {
    $cmd = sprintf( $cmd,
      'ssh -o StrictHostKeyChecking=no -o PubkeyAuthentication=no ', $port );
    push(
      @expect_options,
      [
        qr{Are you sure you want to continue connecting},
        sub {
          Rex::Logger::debug("Accepting key..");
          my $fh = shift;
          $fh->send("yes\n");
          exp_continue;
        }
      ],
      [
        qr{password: ?$}i,
        sub {
          Rex::Logger::debug("Want Password");
          my $fh = shift;
          $fh->send( $pass . "\n" );
          exp_continue;
        }
      ],
      [
        qr{password for.*:$}i,
        sub {
          Rex::Logger::debug("Want Password");
          my $fh = shift;
          $fh->send( $pass . "\n" );
          exp_continue;
        }
      ],
      [
        qr{rsync error: error in rsync protocol},
        sub {
          Rex::Logger::debug("Error in rsync");
          die;
        }
      ],
      [
        qr{rsync error: remote command not found},
        sub {
          Rex::Logger::info("Remote rsync command not found");
          Rex::Logger::info(
            "Please install rsync, or use Rex::Commands::Sync sync_up/sync_down"
          );
          die;
        }
      ],

    );
  }
  else {
    if ( $auth_type eq "key" ) {
      $cmd = sprintf( $cmd,
        'ssh -i ' . $server->get_private_key . " -o StrictHostKeyChecking=no ",
        $port );
    }
    else {
      $cmd = sprintf( $cmd, 'ssh -o StrictHostKeyChecking=no ' );
    }
    push(
      @expect_options,
      [
        qr{Are you sure you want to continue connecting},
        sub {
          Rex::Logger::debug("Accepting key..");
          my $fh = shift;
          $fh->send("yes\n");
          exp_continue;
        }
      ],
      [
        qr{password: ?$}i,
        sub {
          Rex::Logger::debug("Want Password");
          my $fh = shift;
          $fh->send( $pass . "\n" );
          exp_continue;
        }
      ],
      [
        qr{Enter passphrase for key.*: $},
        sub {
          Rex::Logger::debug("Want Passphrase");
          my $fh = shift;
          $fh->send( $pass . "\n" );
          exp_continue;
        }
      ],
      [
        qr{rsync error: error in rsync protocol},
        sub {
          Rex::Logger::debug("Error in rsync");
          die;
        }
      ],
      [
        qr{rsync error: remote command not found},
        sub {
          Rex::Logger::info("Remote rsync command not found");
          Rex::Logger::info(
            "Please install rsync, or use Rex::Commands::Sync sync_up/sync_down"
          );
          die;
        }
      ],

    );
  }

  Rex::Logger::debug("cmd: $cmd");

  eval {
    my $exp = Expect->spawn($cmd) or die($!);

    eval {
      $exp->expect(
        Rex::Config->get_timeout,
        @expect_options,
        [
          qr{total size is [\d,]+\s+speedup is },
          sub {
            Rex::Logger::debug("Finished transfer very fast");
            die;
          }

        ]
      );

      $exp->expect(
        undef,
        [
          qr{total size is [\d,]+\s+speedup is },
          sub {
            Rex::Logger::debug("Finished transfer");
            exp_continue;
          }
        ],
        [
          qr{rsync error: error in rsync protocol},
          sub {
            Rex::Logger::debug("Error in rsync");
            die;
          }
        ],
      );

    };

    $exp->soft_close;
    $? = $exp->exitstatus;
  };

  if ($@) {
    Rex::Logger::info($@);
  }

}

1;