| 1 | package Connector::Builtin::File::SCP; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use English; | ||
| 6 | use File::Spec; | ||
| 7 | use File::Temp qw(tempfile tempdir); | ||
| 8 | use Proc::SafeExec; | ||
| 9 | use Data::Dumper; | ||
| 10 | use Template; | ||
| 11 | |||
| 12 | use Moose; | ||
| 13 | extends 'Connector::Builtin'; | ||
| 14 | |||
| 15 | with 'Connector::Role::LocalPath'; | ||
| 16 | |||
| 17 | has noargs => ( | ||
| 18 | is => 'rw', | ||
| 19 | isa => 'Bool', | ||
| 20 | default => 0, | ||
| 21 | ); | ||
| 22 | |||
| 23 | has file => ( | ||
| 24 | is => 'rw', | ||
| 25 | isa => 'Str', | ||
| 26 | ); | ||
| 27 | |||
| 28 | has path => ( | ||
| 29 | is => 'rw', | ||
| 30 | isa => 'Str', | ||
| 31 | ); | ||
| 32 | |||
| 33 | has content => ( | ||
| 34 | is => 'rw', | ||
| 35 | isa => 'Str', | ||
| 36 | ); | ||
| 37 | |||
| 38 | has command => ( | ||
| 39 | is => 'rw', | ||
| 40 | isa => 'Str', | ||
| 41 | default => '/usr/bin/scp' | ||
| 42 | ); | ||
| 43 | |||
| 44 | has identity => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => 'Str', | ||
| 47 | default => '' | ||
| 48 | ); | ||
| 49 | |||
| 50 | has sshconfig => ( | ||
| 51 | is => 'rw', | ||
| 52 | isa => 'Str', | ||
| 53 | default => '' | ||
| 54 | ); | ||
| 55 | |||
| 56 | has port => ( | ||
| 57 | is => 'rw', | ||
| 58 | isa => 'Str', | ||
| 59 | default => '' | ||
| 60 | ); | ||
| 61 | |||
| 62 | has timeout => ( | ||
| 63 | is => 'rw', | ||
| 64 | isa => 'Int', | ||
| 65 | default => 30 | ||
| 66 | ); | ||
| 67 | |||
| 68 | has preserve => ( | ||
| 69 | is => 'rw', | ||
| 70 | isa => 'Bool', | ||
| 71 | default => 0, | ||
| 72 | ); | ||
| 73 | |||
| 74 | has _scp_option => ( | ||
| 75 | is => 'rw', | ||
| 76 | isa => 'ArrayRef', | ||
| 77 | lazy => 1, | ||
| 78 | builder => '_init_scp_option', | ||
| 79 | ); | ||
| 80 | |||
| 81 | has filemode => ( | ||
| 82 | is => 'rw', | ||
| 83 | isa => 'Str', | ||
| 84 | default => '' | ||
| 85 | ); | ||
| 86 | |||
| 87 | sub _build_config { | ||
| 88 | |||
| 89 | my $self = shift; | ||
| 90 | if (! -d $self->{LOCATION}) { | ||
| 91 | confess("Cannot open directory " . $self->{LOCATION} ); | ||
| 92 | } | ||
| 93 | |||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _init_scp_option { | ||
| 98 | |||
| 99 | my $self = shift; | ||
| 100 | |||
| 101 | my @options; | ||
| 102 | push @options, '-P'. $self->port() if ($self->port()); | ||
| 103 | push @options, '-F'. $self->sshconfig() if ($self->sshconfig()); | ||
| 104 | push @options, '-i'. $self->identity() if ($self->identity()); | ||
| 105 | push @options, '-p' if ($self->preserve()); | ||
| 106 | |||
| 107 | return \@options; | ||
| 108 | |||
| 109 | } | ||
| 110 | |||
| 111 | # return the content of the file | ||
| 112 | sub get { | ||
| 113 | |||
| 114 | my $self = shift; | ||
| 115 | my $path = shift; | ||
| 116 | |||
| 117 | my $source = $self->_sanitize_path( $path ); | ||
| 118 | |||
| 119 | # We need to double encode the backslash escape (for local and remote) | ||
| 120 | $source =~ s/\\/\\/g; | ||
| 121 | |||
| 122 | my $tmpdir = tempdir( CLEANUP => 1 ); | ||
| 123 | my ($fh, $target) = tempfile( DIR => $tmpdir ); | ||
| 124 | |||
| 125 | my $res = $self->_transfer($source, $target ); | ||
| 126 | |||
| 127 | # soemthing went wrong | ||
| 128 | if ($res) { | ||
| 129 | unlink $target if (-e $target); | ||
| 130 | return $self->_node_not_exists(); | ||
| 131 | } | ||
| 132 | |||
| 133 | # read the content from temporary file | ||
| 134 | my $content = do { | ||
| 135 | local $INPUT_RECORD_SEPARATOR; | ||
| 136 | open my $fh, '<', $target; | ||
| 137 | <$fh>; | ||
| 138 | }; | ||
| 139 | |||
| 140 | unlink $target; | ||
| 141 | |||
| 142 | return $content; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub get_meta { | ||
| 146 | my $self = shift; | ||
| 147 | |||
| 148 | # If we have no path, we tell the caller that we are a connector | ||
| 149 | # but if noargs is set, we behave like a scalar... | ||
| 150 | my @path = $self->_build_path_with_prefix( shift ); | ||
| 151 | if (scalar @path == 0 && !$self->noargs()) { | ||
| 152 | return { TYPE => "connector" }; | ||
| 153 | } | ||
| 154 | |||
| 155 | return {TYPE => "scalar" }; | ||
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | sub exists { | ||
| 160 | |||
| 161 | my $self = shift; | ||
| 162 | |||
| 163 | # No path = connector root which always exists | ||
| 164 | my @path = $self->_build_path_with_prefix( shift ); | ||
| 165 | if (scalar @path == 0) { | ||
| 166 | return 1; | ||
| 167 | } | ||
| 168 | |||
| 169 | return 1; | ||
| 170 | |||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | # return the content of the file | ||
| 175 | sub set { | ||
| 176 | |||
| 177 | my $self = shift; | ||
| 178 | my $file = shift; | ||
| 179 | my $data = shift; | ||
| 180 | |||
| 181 | my $content; | ||
| 182 | if ($self->content()) { | ||
| 183 | $self->log()->debug('Process template for content ' . $self->content()); | ||
| 184 | my $template = Template->new({}); | ||
| 185 | |||
| 186 | $data = { DATA => $data } if (ref $data eq ''); | ||
| 187 | |||
| 188 | $template->process( \$self->content(), $data, \$content) || die "Error processing content template."; | ||
| 189 | } else { | ||
| 190 | if (ref $data ne '') { | ||
| 191 | die "You need to define a content template if data is not a scalar"; | ||
| 192 | } | ||
| 193 | $content = $data; | ||
| 194 | } | ||
| 195 | |||
| 196 | |||
| 197 | my $tmpdir = tempdir( CLEANUP => 1 ); | ||
| 198 | my ($fh, $source) = tempfile( DIR => $tmpdir ); | ||
| 199 | |||
| 200 | open FILE, ">$source" || die "Unable to open file for writing"; | ||
| 201 | print FILE $content; | ||
| 202 | close FILE; | ||
| 203 | |||
| 204 | if ($self->filemode()) { | ||
| 205 | my $mode = $self->filemode(); | ||
| 206 | $mode = oct($mode) if $mode =~ /^0/; | ||
| 207 | chmod $mode, $source; | ||
| 208 | } | ||
| 209 | |||
| 210 | my $target = $self->_sanitize_path( $file, $data ); | ||
| 211 | |||
| 212 | my $res = $self->_transfer( $source, $target ); | ||
| 213 | if ($res) { | ||
| 214 | die sprintf("Unable to transfer data (EC %01d)", $res); | ||
| 215 | } | ||
| 216 | |||
| 217 | return 1; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub _transfer { | ||
| 221 | |||
| 222 | my $self = shift; | ||
| 223 | my $source = shift; | ||
| 224 | my $target = shift; | ||
| 225 | |||
| 226 | my %filehandles; | ||
| 227 | my $stdout = File::Temp->new(); | ||
| 228 | $filehandles{stdout} = \*$stdout; | ||
| 229 | |||
| 230 | my $stderr = File::Temp->new(); | ||
| 231 | $filehandles{stderr} = \*$stderr; | ||
| 232 | |||
| 233 | # compose the system command to execute | ||
| 234 | my @cmd = @{$self->_scp_option()}; | ||
| 235 | |||
| 236 | unshift @cmd, $self->command(); | ||
| 237 | |||
| 238 | push @cmd, $source; | ||
| 239 | push @cmd, $target; | ||
| 240 | |||
| 241 | $self->log()->debug("scp command: " . join(" ",@cmd)); | ||
| 242 | |||
| 243 | local $SIG{'CHLD'} = 'DEFAULT'; | ||
| 244 | my $command = Proc::SafeExec->new({ | ||
| 245 | exec => \@cmd, | ||
| 246 | no_autowait => 1, | ||
| 247 | %filehandles, | ||
| 248 | }); | ||
| 249 | |||
| 250 | eval{ | ||
| 251 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
| 252 | alarm $self->timeout(); | ||
| 253 | $command->wait(); | ||
| 254 | }; | ||
| 255 | |||
| 256 | alarm 0; | ||
| 257 | |||
| 258 | if ($EVAL_ERROR) { | ||
| 259 | $self->log()->debug($EVAL_ERROR); | ||
| 260 | $self->log()->error("SCP tranfer timed out"); | ||
| 261 | return 2; | ||
| 262 | } | ||
| 263 | |||
| 264 | if ($command->exit_status() != 0) { | ||
| 265 | $self->log()->error("SCP tranfer failed, exit status was " . $command->exit_status()); | ||
| 266 | return 1; | ||
| 267 | } | ||
| 268 | |||
| 269 | return 0; | ||
| 270 | |||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | sub _sanitize_path { | ||
| 275 | |||
| 276 | my $self = shift; | ||
| 277 | my $inargs = shift; | ||
| 278 | my $data = shift; | ||
| 279 | |||
| 280 | my $host = $self->{LOCATION}; | ||
| 281 | |||
| 282 | if ($self->noargs()) { | ||
| 283 | $self->log()->debug('Skip filename rendering, noargs options is set'); | ||
| 284 | return $host; | ||
| 285 | } | ||
| 286 | |||
| 287 | if (!$self->path() && !$self->file()) { | ||
| 288 | $self->log()->error('Neither target pattern nor noargs set'); | ||
| 289 | die "You must set either file or path or use the noargs option."; | ||
| 290 | } | ||
| 291 | |||
| 292 | my @args = $self->_build_path_with_prefix( $inargs ); | ||
| 293 | my $file = $self->_render_local_path( \@args, $data ); | ||
| 294 | |||
| 295 | my $filename; | ||
| 296 | # check if the LOCATION already has a path spec | ||
| 297 | if ($host !~ /:/) { | ||
| 298 | # if the file name has a leading slash, just concat with : | ||
| 299 | if ($file =~ /^\//) { | ||
| 300 | $filename = $host.':'.$file; | ||
| 301 | # otherwise add ~/ for users home | ||
| 302 | } else { | ||
| 303 | $filename = $host.':~/'.$file; | ||
| 304 | } | ||
| 305 | |||
| 306 | } else { | ||
| 307 | # if a path spec is given, check if it has a trailing slash | ||
| 308 | if ($host !~ /\/$/) { | ||
| 309 | $host .= '/'; | ||
| 310 | } | ||
| 311 | $filename = $host.$file; | ||
| 312 | } | ||
| 313 | |||
| 314 | $self->log()->debug('Filename evaluated to ' . $filename); | ||
| 315 | |||
| 316 | $filename =~ s/ /\\ /g; | ||
| 317 | |||
| 318 | return $filename; | ||
| 319 | } | ||
| 320 | |||
| 321 | 1; | ||
| 322 | __END__ |