Rex

 view release on metacpan or  search on metacpan

lib/Rex/Commands/Rsync.pm  view on Meta::CPAN

  }

  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 = ();

  my $exec   = Rex::Interface::Exec->create;
  my $quoter = Net::OpenSSH::ShellQuoter->quoter( $exec->shell->name );

  if ( $opt && exists $opt->{'download'} && $opt->{'download'} == 1 ) {
    $dest = resolv_path($dest);
    Rex::Logger::debug("Downloading $source -> $dest");
    push @rsync_cmd, "rsync -rl --verbose --stats $params ";

    if ( !$local_connection ) {
      push @rsync_cmd, "-e '\%s'";
      $source = $auth->{user} . "\@$servername:$source";
    }
  }
  else {
    $source = resolv_path($source);
    Rex::Logger::debug("Uploading $source -> $dest");

    push @rsync_cmd, "rsync -rl --verbose --stats $params";

    if ( !$local_connection ) {
      push @rsync_cmd, "-e '\%s'";
      $dest = $auth->{user} . "\@$servername:$dest";
    }
  }

  $source = $quoter->quote_glob($source);
  $dest   = $quoter->quote_glob($dest);

  push @rsync_cmd, $source;
  push @rsync_cmd, $dest;

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

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

  if ( !$local_connection ) {
    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 -p $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 -p $port" );
      }
      else {
        $cmd = sprintf( $cmd, 'ssh -o StrictHostKeyChecking=no -p ' . "$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{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($!);

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.471 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )