| 1 | package Helm::Task::put; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Moose; | ||
| 5 | use Net::OpenSSH; | ||
| 6 | |||
| 7 | extends 'Helm::Task'; | ||
| 8 | |||
| 9 | sub validate { | ||
| 10 | my $self = shift; | ||
| 11 | my $helm = $self->helm; | ||
| 12 | my $extra_options = $helm->extra_options; | ||
| 13 | |||
| 14 | # make sure we have local and remote options and that the local file exists and is readable | ||
| 15 | my $local = $extra_options->{local}; | ||
| 16 | $helm->die('Missing option: local', option => 'local') unless $local; | ||
| 17 | $helm->die('Missing option: remote', option => 'remote') unless $extra_options->{remote}; | ||
| 18 | $helm->die("Invalid option: local - File \"$local\" does not exist") unless -e $local; | ||
| 19 | $helm->die("Invalid option: local - File \"$local\" is not readable") unless -r $local; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub help { | ||
| 23 | my $self = shift; | ||
| 24 | return <<END; | ||
| 25 | Put a local file onto the remote server(s). Takes the following required | ||
| 26 | options: | ||
| 27 | |||
| 28 | --local | ||
| 29 | The name of the file on the local machine. | ||
| 30 | |||
| 31 | --remote | ||
| 32 | The full path that the file will occupy on the remote server(s). | ||
| 33 | END | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | sub execute { | ||
| 38 | my ($self, %args) = @_; | ||
| 39 | my $server = $args{server}; | ||
| 40 | my $ssh = $args{ssh}; | ||
| 41 | my $helm = $self->helm; | ||
| 42 | my $options = $helm->extra_options; | ||
| 43 | my $local = $options->{local}; | ||
| 44 | my $remote = $options->{remote}; | ||
| 45 | my $sudo = $helm->sudo; | ||
| 46 | |||
| 47 | # if we're using sudo then use a temp file to move the file over | ||
| 48 | my $dest = $sudo ? $self->unique_tmp_file : $remote; | ||
| 49 | |||
| 50 | # send our file over there | ||
| 51 | $helm->log->debug("Trying to scp local file ($local) to $server:$dest"); | ||
| 52 | $ssh->scp_put($local, $dest) | ||
| 53 | || $helm->die("Can't scp file ($local) to server $server: " . $ssh->error); | ||
| 54 | $helm->log->info("File $local copied to $server:$remote"); | ||
| 55 | |||
| 56 | if ($sudo) { | ||
| 57 | # make it owned by the sudo user | ||
| 58 | $helm->log->debug("Changing owner of file ($dest) to $sudo"); | ||
| 59 | $helm->run_remote_command( | ||
| 60 | command => "sudo chown $sudo $dest", | ||
| 61 | ssh => $ssh, | ||
| 62 | ssh_options => {tty => 1}, | ||
| 63 | no_sudo => 1, | ||
| 64 | ); | ||
| 65 | $helm->log->debug("Owner of file ($dest) changed to $sudo"); | ||
| 66 | |||
| 67 | # move the file over to the correct location | ||
| 68 | $helm->log->debug("Moving file from $dest to $remote"); | ||
| 69 | $helm->run_remote_command( | ||
| 70 | command => "sudo mv $dest $remote", | ||
| 71 | ssh => $ssh, | ||
| 72 | ssh_options => {tty => 1}, | ||
| 73 | no_sudo => 1, | ||
| 74 | ); | ||
| 75 | $helm->log->debug("Filed moved from $dest to $remote"); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | __PACKAGE__->meta->make_immutable; | ||
| 80 | |||
| 81 | 1; |