defer node setup
[scpubgit/Object-Remote.git] / lib / Object / Remote / Connector / SSH.pm
1 package Object::Remote::Connector::SSH;
2
3 use Object::Remote::FatNode;
4 use Object::Remote::ModuleSender;
5 use Object::Remote::Handle;
6 use IPC::Open2;
7 use Moo;
8
9 with 'Object::Remote::Role::Connector';
10
11 sub _open2_for {
12   my $self = shift;
13   my $pid = open2(my $ssh_stdout, my $ssh_stdin, 'ssh', $_[0], 'perl', '-')
14     or die "Failed to start ssh connection: $!";;
15   print $ssh_stdin $Object::Remote::FatNode::DATA, "__END__\n";
16   return ($ssh_stdin, $ssh_stdout, $pid);
17 }
18
19 around connect => sub {
20   my ($orig, $self) = (shift, shift);
21   my $conn = $self->$orig(@_);
22   Object::Remote::Handle->new(
23     connection => $conn,
24     class => 'Object::Remote::ModuleLoader',
25     args => { module_sender => Object::Remote::ModuleSender->new }
26   )->disarm_free;
27   return $conn;
28 };
29
30 sub _ssh_object_for {
31   my ($self, $on) = @_;
32   $self->ssh_masters->{$on} ||= Net::OpenSSH->new($on);
33 }
34
35 push @Object::Remote::Connection::Guess, sub { 
36   for ($_[0]) {
37     # 0-9 a-z _ - first char, those or . subsequent - hostnamish
38     if (defined and !ref and /^(?:.*?\@)?[\w\-][\w\-\.]*/) {
39       return __PACKAGE__->new->connect($_[0]);
40     }
41   }
42   return;
43 };
44
45 1;