| 1 | # | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | Rex::Commands::Upload - Upload a local file to a remote server | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | With this module you can upload a local file via sftp to a remote host. | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | task "upload", "remoteserver", sub { | ||||||||||||||||||||||||||||||||||||||||||
| 16 | upload "localfile", "/remote/file"; | ||||||||||||||||||||||||||||||||||||||||||
| 17 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | package Rex::Commands::Upload; | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 49 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 49 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 49 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 49 | require Rex::Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 49 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 49 | ▶ | use Rex::Config; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 49 | ▶ | use Rex::Commands::Fs; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 49 | ▶ | use Rex::Interface::Fs; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 49 | ▶ | use Rex::Helper::Path; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 49 | ▶ | use Rex::Commands::MD5; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 49 | ▶ | use Rex::Commands; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 49 | ▶ | use Rex::Hook; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 49 | ▶ | use vars qw(@EXPORT); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 49 | ▶ | use base qw(Rex::Exporter); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 49 | @EXPORT = qw(upload); | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =head2 upload($local, $remote) | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | Perform an upload. If $remote is a directory the file will be uploaded to that directory. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | task "upload", "remoteserver", sub { | ||||||||||||||||||||||||||||||||||||||||||
| 50 | upload "localfile", "/path"; | ||||||||||||||||||||||||||||||||||||||||||
| 51 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | This function supports the following L<hooks|Rex::Hook>: | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | =item before | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | This gets executed before anything is done. All original parameters are passed to it. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | The return value of this hook overwrites the original parameters of the function-call. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | =item before_change | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | This gets executed right before the new file is written. The local file name, and the remote file name are passed as parameters. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =item after_change | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | This gets executed right after the file was written. On top of the local file name, and the remote file name, any returned results are passed as parameters. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =item after | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | This gets executed right before the C<upload()> function returns. All original parameters, and any results returned are passed to it. | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | sub upload { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
upload: called CC=13 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | #### check and run before hook | ||||||||||||||||||||||||||||||||||||||||||
| 83 | eval { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | my @new_args = Rex::Hook::run_hook( upload => "before", @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | ▶ | if (@new_args) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | @_ = @new_args; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | die("Before-Hook failed. Canceling upload() action: $@"); | |||||||||||||||||||||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 92 | ############################## | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | my ( $local, $remote ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 3 | $local = resolv_path( $local, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | $remote = resolv_path($remote); | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | my $fs = Rex::Interface::Fs->create; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | # if remote not set, use name of local. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | # must be done before the next line. | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | ▶ | unless ($remote) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $remote = basename($local); | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | $local = Rex::Helper::Path::get_file_path( $local, caller() ); | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | # if there is a file called filename.environment then use this file | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # ex: | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # upload "files/hosts", "/etc/hosts"; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # rex -E live ... | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # will first look if files/hosts.live is available, if not it will | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # use files/hosts | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | my $old_local = $local; # for the upload location use the given name | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | ▶ | if ( Rex::Config->get_environment | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
"Rex::Config"->get_environment and -f "$local." . "Rex::Config"->get_environment
MC/DC0%0/2
"Rex::Config"->get_environment and -f "$local." . "Rex::Config"->get_environment
"Rex::Config"->get_environment
-f "$local." . "Rex::Config"->get_environment
Truth table33%1/3
A and B
A= "Rex::Config"->get_environmentB= -f "$local." . "Rex::Config"->get_environment
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | && -f "$local." . Rex::Config->get_environment ) | ||||||||||||||||||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $local = "$local." . Rex::Config->get_environment; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | ▶ | if ( !-f $local ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | Rex::Logger::info("File Not Found: $local"); | |||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | die("File $local not found."); | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 3 | ▶ | if ( is_dir($remote) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | $remote = $remote . '/' . basename($old_local); | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | Rex::get_current_connection()->{reporter} | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 3 | ->report_resource_start( type => "upload", name => $remote ); | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | # first get local md5 | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 3 | my $local_md5; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 3 | ▶ | LOCAL { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 3 | $local_md5 = md5($local); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 3 | ▶ | if ( !$local_md5 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | die("Error getting local md5 sum of $local"); | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | # than get remote md5 to test if we need to upload the file | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 3 | my $remote_md5 = ""; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 3 | eval { $remote_md5 = md5($remote); }; | |||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 3 | my $__ret; | |||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 3 | ▶ | if ( $local_md5 && $remote_md5 && $local_md5 eq $remote_md5 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$local_md5 and $remote_md5 and $local_md5 eq $remote_md5
$local_md5 and $remote_md5
MC/DC0%0/3
$local_md5 and $remote_md5 and $local_md5 eq $remote_md5
$local_md5
$remote_md5
$local_md5 eq $remote_md5
Truth table25%1/4
A and B and C
A= $local_md5B= $remote_md5C= $local_md5 eq $remote_md5
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | Rex::Logger::debug( | |||||||||||||||||||||||||||||||||||||||||
| 155 | "local md5 and remote md5 are the same: $local_md5 eq $remote_md5. Not uploading." | ||||||||||||||||||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | $__ret = { changed => 0, ret => 0 }; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 3 | Rex::Logger::debug("Uploading: $local to $remote"); | |||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | #### check and run before_change hook | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 3 | Rex::Hook::run_hook( upload => "before_change", $local, $remote ); | |||||||||||||||||||||||||||||||||||||||||
| 165 | ############################## | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | $__ret = $fs->upload( $local, $remote ); | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | #### check and run after_change hook | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 3 | Rex::Hook::run_hook( upload => "after_change", $local, $remote, $__ret ); | |||||||||||||||||||||||||||||||||||||||||
| 171 | ############################## | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 3 | $__ret = { changed => 1, ret => $__ret }; | |||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | #### check and run before hook | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 3 | Rex::Hook::run_hook( upload => "after", @_, $__ret ); | |||||||||||||||||||||||||||||||||||||||||
| 179 | ############################## | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 3 | ▶ | if ( $__ret->{changed} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | Rex::get_current_connection()->{reporter}->report( | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 3 | changed => 1, | |||||||||||||||||||||||||||||||||||||||||
| 184 | message => "File uploaded. old md5: $remote_md5 new md5: $local_md5" | ||||||||||||||||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | Rex::get_current_connection()->{reporter}->report( changed => 0, ); | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | Rex::get_current_connection()->{reporter} | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 3 | ->report_resource_end( type => "upload", name => $remote ); | |||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 3 | return $__ret; | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 49 | 1; | |||||||||||||||||||||||||||||||||||||||||