/usr/share/perl5/Rex/Commands/Upload.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 | #
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
=head1 NAME
Rex::Commands::Upload - Upload a local file to a remote server
=head1 DESCRIPTION
With this module you can upload a local file via sftp to a remote host.
=head1 SYNOPSIS
task "upload", "remoteserver", sub {
upload "localfile", "/remote/file";
};
=head1 EXPORTED FUNCTIONS
=cut
package Rex::Commands::Upload;
use strict;
use warnings;
our $VERSION = '1.4.1'; # VERSION
require Rex::Exporter;
use File::Basename qw(basename);
use Rex::Config;
use Rex::Commands::Fs;
use Rex::Interface::Fs;
use Rex::Helper::Path;
use Rex::Commands::MD5;
use Rex::Commands;
use Rex::Hook;
use vars qw(@EXPORT);
use base qw(Rex::Exporter);
@EXPORT = qw(upload);
=head2 upload($local, $remote)
Perform an upload. If $remote is a directory the file will be uploaded to that directory.
task "upload", "remoteserver", sub {
upload "localfile", "/path";
};
This function supports the following hooks:
=over 8
=item before
This gets executed before everything is done. The return value of this hook overwrite the original parameters of the function-call.
=item before_change
This gets executed right before the new file is written.
=item after_change
This gets executed right after the file was written.
=item after
This gets executed right before the upload() function returns.
=back
=cut
sub upload {
#### check and run before hook
eval {
my @new_args = Rex::Hook::run_hook( upload => "before", @_ );
if (@new_args) {
@_ = @new_args;
}
1;
} or do {
die("Before-Hook failed. Canceling upload() action: $@");
};
##############################
my ( $local, $remote ) = @_;
$local = resolv_path( $local, 1 );
$remote = resolv_path($remote);
my $fs = Rex::Interface::Fs->create;
# if remote not set, use name of local.
# must be done before the next line.
unless ($remote) {
$remote = basename($local);
}
$local = Rex::Helper::Path::get_file_path( $local, caller() );
# if there is a file called filename.environment then use this file
# ex:
# upload "files/hosts", "/etc/hosts";
#
# rex -E live ...
# will first look if files/hosts.live is available, if not it will
# use files/hosts
my $old_local = $local; # for the upload location use the given name
if ( Rex::Config->get_environment
&& -f "$local." . Rex::Config->get_environment )
{
$local = "$local." . Rex::Config->get_environment;
}
if ( !-f $local ) {
Rex::Logger::info("File Not Found: $local");
die("File $local not found.");
}
if ( is_dir($remote) ) {
$remote = $remote . '/' . basename($old_local);
}
Rex::get_current_connection()->{reporter}
->report_resource_start( type => "upload", name => $remote );
# first get local md5
my $local_md5;
LOCAL {
$local_md5 = md5($local);
};
if ( !$local_md5 ) {
die("Error getting local md5 sum of $local");
}
# than get remote md5 to test if we need to upload the file
my $remote_md5 = "";
eval { $remote_md5 = md5($remote); };
my $__ret;
if ( $local_md5 && $remote_md5 && $local_md5 eq $remote_md5 ) {
Rex::Logger::debug(
"local md5 and remote md5 are the same: $local_md5 eq $remote_md5. Not uploading."
);
$__ret = { changed => 0, ret => 0 };
}
else {
Rex::Logger::debug("Uploading: $local to $remote");
#### check and run before_change hook
Rex::Hook::run_hook( upload => "before_change", $local, $remote );
##############################
$__ret = $fs->upload( $local, $remote );
#### check and run after_change hook
Rex::Hook::run_hook( upload => "after_change", $local, $remote, $__ret );
##############################
$__ret = { changed => 1, ret => $__ret };
}
#### check and run before hook
Rex::Hook::run_hook( upload => "after", @_, $__ret );
##############################
if ( $__ret->{changed} ) {
Rex::get_current_connection()->{reporter}->report(
changed => 1,
message => "File uploaded. old md5: $remote_md5 new md5: $local_md5"
);
}
else {
Rex::get_current_connection()->{reporter}->report( changed => 0, );
}
Rex::get_current_connection()->{reporter}
->report_resource_end( type => "upload", name => $remote );
return $__ret;
}
1;
|